Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickw444 committed Mar 29, 2020
1 parent 00a0497 commit cdff13f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
19 changes: 11 additions & 8 deletions appdaemon_testing/hass_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def get_mock(self, meth: str) -> mock.Mock:

def inject_mocks(self) -> None:
"""
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock implementations.
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock
implementations.
"""
for meth_name, impl in self._mocks.items():
if getattr(hass.Hass, meth_name) is None:
Expand All @@ -73,11 +74,12 @@ def inject_mocks(self) -> None:
@contextlib.contextmanager
def setup(self):
"""
A context manager to indicate that execution is taking place during a "setup" phase.
A context manager to indicate that execution is taking place during a
"setup" phase.
This context manager can be used to configure/set up any existing states that might
be required to run the test. State changes during execution within this context manager
will cause `listen_state` handlers to not be called.
This context manager can be used to configure/set up any existing states
that might be required to run the test. State changes during execution within
this context manager will cause `listen_state` handlers to not be called.
Example:
Expand All @@ -102,15 +104,16 @@ def set_state(
"""
Update/set state of an entity.
State changes will cause listeners (via listen_state) to be called on their respective
state changes.
State changes will cause listeners (via listen_state) to be called on
their respective state changes.
Parameters:
entity: The entity to update
state: The state value to set
attribute_name: The attribute to set
previous: Forced previous value
trigger: Whether this change should trigger registered listeners (via listen_state)
trigger: Whether this change should trigger registered listeners
(via listen_state)
"""
if trigger is None:
# Avoid triggering state changes during state setup phase
Expand Down
3 changes: 2 additions & 1 deletion appdaemon_testing/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def automation_fixture(App: Type[T], args=None, initialize=True):
Parameters:
App: The AppDaemon application to create the fixture for
args: arguments that should be provided to the app when it is instantiated (`self.args`)
args: arguments that should be provided to the app when it is
instantiated (`self.args`)
initialize: Whether `app.initialize()` should be called.
"""

Expand Down
57 changes: 33 additions & 24 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ <h2 class="section-title" id="header-classes">Classes</h2>

def inject_mocks(self) -&gt; None:
&#34;&#34;&#34;
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock implementations.
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock
implementations.
&#34;&#34;&#34;
for meth_name, impl in self._mocks.items():
if getattr(hass.Hass, meth_name) is None:
Expand All @@ -203,11 +204,12 @@ <h2 class="section-title" id="header-classes">Classes</h2>
@contextlib.contextmanager
def setup(self):
&#34;&#34;&#34;
A context manager to indicate that execution is taking place during a &#34;setup&#34; phase.
A context manager to indicate that execution is taking place during a
&#34;setup&#34; phase.

This context manager can be used to configure/set up any existing states that might
be required to run the test. State changes during execution within this context manager
will cause `listen_state` handlers to not be called.
This context manager can be used to configure/set up any existing states
that might be required to run the test. State changes during execution within
this context manager will cause `listen_state` handlers to not be called.

Example:

Expand All @@ -232,15 +234,16 @@ <h2 class="section-title" id="header-classes">Classes</h2>
&#34;&#34;&#34;
Update/set state of an entity.

State changes will cause listeners (via listen_state) to be called on their respective
state changes.
State changes will cause listeners (via listen_state) to be called on
their respective state changes.

Parameters:
entity: The entity to update
state: The state value to set
attribute_name: The attribute to set
previous: Forced previous value
trigger: Whether this change should trigger registered listeners (via listen_state)
trigger: Whether this change should trigger registered listeners
(via listen_state)
&#34;&#34;&#34;
if trigger is None:
# Avoid triggering state changes during state setup phase
Expand Down Expand Up @@ -345,14 +348,16 @@ <h2 id="parameters">Parameters</h2>
<span>def <span class="ident">inject_mocks</span></span>(<span>self)</span>
</code></dt>
<dd>
<section class="desc"><p>Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock implementations.</p></section>
<section class="desc"><p>Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock
implementations.</p></section>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def inject_mocks(self) -&gt; None:
&#34;&#34;&#34;
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock implementations.
Monkey-patch the AppDaemon hassapi.Hass base-class methods with mock
implementations.
&#34;&#34;&#34;
for meth_name, impl in self._mocks.items():
if getattr(hass.Hass, meth_name) is None:
Expand All @@ -366,8 +371,8 @@ <h2 id="parameters">Parameters</h2>
</code></dt>
<dd>
<section class="desc"><p>Update/set state of an entity.</p>
<p>State changes will cause listeners (via listen_state) to be called on their respective
state changes.</p>
<p>State changes will cause listeners (via listen_state) to be called on
their respective state changes.</p>
<h2 id="parameters">Parameters</h2>
<dl>
<dt><strong><code>entity</code></strong></dt>
Expand All @@ -379,7 +384,8 @@ <h2 id="parameters">Parameters</h2>
<dt><strong><code>previous</code></strong></dt>
<dd>Forced previous value</dd>
<dt><strong><code>trigger</code></strong></dt>
<dd>Whether this change should trigger registered listeners (via listen_state)</dd>
<dd>Whether this change should trigger registered listeners
(via listen_state)</dd>
</dl></section>
<details class="source">
<summary>
Expand All @@ -391,15 +397,16 @@ <h2 id="parameters">Parameters</h2>
&#34;&#34;&#34;
Update/set state of an entity.

State changes will cause listeners (via listen_state) to be called on their respective
state changes.
State changes will cause listeners (via listen_state) to be called on
their respective state changes.

Parameters:
entity: The entity to update
state: The state value to set
attribute_name: The attribute to set
previous: Forced previous value
trigger: Whether this change should trigger registered listeners (via listen_state)
trigger: Whether this change should trigger registered listeners
(via listen_state)
&#34;&#34;&#34;
if trigger is None:
# Avoid triggering state changes during state setup phase
Expand Down Expand Up @@ -438,10 +445,11 @@ <h2 id="parameters">Parameters</h2>
<span>def <span class="ident">setup</span></span>(<span>self)</span>
</code></dt>
<dd>
<section class="desc"><p>A context manager to indicate that execution is taking place during a "setup" phase.</p>
<p>This context manager can be used to configure/set up any existing states that might
be required to run the test. State changes during execution within this context manager
will cause <code>listen_state</code> handlers to not be called.</p>
<section class="desc"><p>A context manager to indicate that execution is taking place during a
"setup" phase.</p>
<p>This context manager can be used to configure/set up any existing states
that might be required to run the test. State changes during execution within
this context manager will cause <code>listen_state</code> handlers to not be called.</p>
<p>Example:</p>
<pre><code class="py">def test_my_app(hass_driver, my_app: MyApp):
with hass_driver.setup():
Expand All @@ -459,11 +467,12 @@ <h2 id="parameters">Parameters</h2>
<pre><code class="python">@contextlib.contextmanager
def setup(self):
&#34;&#34;&#34;
A context manager to indicate that execution is taking place during a &#34;setup&#34; phase.
A context manager to indicate that execution is taking place during a
&#34;setup&#34; phase.

This context manager can be used to configure/set up any existing states that might
be required to run the test. State changes during execution within this context manager
will cause `listen_state` handlers to not be called.
This context manager can be used to configure/set up any existing states
that might be required to run the test. State changes during execution within
this context manager will cause `listen_state` handlers to not be called.

Example:

Expand Down
9 changes: 6 additions & 3 deletions docs/pytest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ <h1 class="title">Module <code>appdaemon_testing.pytest</code></h1>

Parameters:
App: The AppDaemon application to create the fixture for
args: arguments that should be provided to the app when it is instantiated (`self.args`)
args: arguments that should be provided to the app when it is
instantiated (`self.args`)
initialize: Whether `app.initialize()` should be called.
&#34;&#34;&#34;

Expand Down Expand Up @@ -96,7 +97,8 @@ <h2 id="parameters">Parameters</h2>
<dt><strong><code>App</code></strong></dt>
<dd>The AppDaemon application to create the fixture for</dd>
<dt><strong><code>args</code></strong></dt>
<dd>arguments that should be provided to the app when it is instantiated (<code>self.args</code>)</dd>
<dd>arguments that should be provided to the app when it is
instantiated (<code>self.args</code>)</dd>
<dt><strong><code>initialize</code></strong></dt>
<dd>Whether <code>app.initialize()</code> should be called.</dd>
</dl></section>
Expand All @@ -110,7 +112,8 @@ <h2 id="parameters">Parameters</h2>

Parameters:
App: The AppDaemon application to create the fixture for
args: arguments that should be provided to the app when it is instantiated (`self.args`)
args: arguments that should be provided to the app when it is
instantiated (`self.args`)
initialize: Whether `app.initialize()` should be called.
&#34;&#34;&#34;

Expand Down
3 changes: 2 additions & 1 deletion example/tests/test_living_room_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
def test_callbacks_are_registered(hass_driver, living_room_motion: LivingRoomMotion):
listen_state = hass_driver.get_mock("listen_state")
listen_state.assert_called_once_with(
living_room_motion.on_motion_detected, "binary_sensor.motion_detected")
living_room_motion.on_motion_detected, "binary_sensor.motion_detected"
)


def test_lights_are_turned_on_when_motion_detected(
Expand Down

0 comments on commit cdff13f

Please sign in to comment.