Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AppDaemon command. #49

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions appdaemontestframework/assert_that.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def turned_off(self):
def called_with(self, **kwargs):
pass

@abstractmethod
def set_to_option(self, **kwargs):
pass

def called(self):
self.called_with()

Expand Down Expand Up @@ -120,6 +124,23 @@ def called_with(self, **kwargs):
self.hass_functions['call_service'].assert_any_call(
service_full_name, **kwargs)

def set_to_option(self, newvalue, **service_specific_parameters):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FlorianKempenich I think I need some help here. I don't understand how it works. Never used lambdas and all this part seems to obscure to me.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree that part is a bit obscure. I tried to make it as readable as possible, but even for someone familiar w/ lambda it's not the most self-explanatory code . . .

The part with the lambda is for the turned on/off assertions. For turning on/off a light, there are 2 ways to do so, from the automation we can call self.turn_on(ENTITY) or self.call_service('turn_on', {'entity_id': ENTITY}). What the helper does when we call assert_that(ENTITY).was.turned_on(): it checks both (turn_on and call_service) and see if at least one was called. If none was called, it'll collect errors from both and merge them into 1. _capture_assert_failure_exception runs the lambda and capture any failure or None if there was no failure. Then we check in if service_not_called and turn_on_helper_not_called: if any of the 2 calls returned an error,, and if so we raise a merged error

raise EitherOrAssertionError(
                service_not_called, turn_on_helper_not_called)

In the case of set_option, if there is a way to call set_option via a service call, then we should be able to replicate a similar behavior indeed. I'll now look into why your test is failing :)

""" Assert that a given entity_id has been set to the choosen value """
entity_id = self.thing_to_check

service_not_called = _capture_assert_failure_exception(
lambda: self.hass_functions['call_service'].assert_any_call(
ServiceOnAnyDomain('select_option'),
**{'entity_id': entity_id, 'option': newvalue, **service_specific_parameters}))

set_option_helper_not_called = _capture_assert_failure_exception(
lambda: self.hass_functions['select_option'].assert_any_call(
entity_id, newvalue
**service_specific_parameters))

if service_not_called and set_option_helper_not_called:
raise EitherOrAssertionError(
service_not_called, set_option_helper_not_called)

class WasNotWrapper(Was):
def __init__(self, was_wrapper):
Expand Down Expand Up @@ -154,6 +175,16 @@ def called_with(self, **kwargs):
raise AssertionError(
"Service shoud NOT have been called with the given args: " + str(kwargs))

def set_to_option(self, newvalue, **service_specific_parameters):
""" Assert that a given entity_id has NOT the value newvalue """
val = _capture_assert_failure_exception(
lambda: self.was_wrapper.set_to_option(newvalue, **service_specific_parameters)
)
if not val:
raise AssertionError(
"Should NOT have the value: "
+ str(self.was_wrapper.thing_to_check))


class ListensToWrapper:
def __init__(self, automation_thing_to_check, hass_functions):
Expand Down
8 changes: 8 additions & 0 deletions test/test_assert_that.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

LIGHT = 'light.some_light'
SWITCH = 'switch.some_switch'
INPUT_SELECT = 'input_select.some_input_select'
TRANSITION_DURATION = 2


Expand Down Expand Up @@ -141,3 +142,10 @@ def test_with_kwargs(self, assert_that, automation):
assert_that(LIGHT).was_not.turned_off()
automation.turn_off_light_with_transition(via_helper=True)
assert_that(LIGHT).was.turned_off(transition=TRANSITION_DURATION)

class TestSelectOption:
class TestViaService:
def test_option_is_set(self, assert_that, automation):
assert_that(INPUT_SELECT).was_not.set_to_option('new_option')
automation.select_option('new_option')
assert_that(INPUT_SELECT).was.set_to_option('new_option')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one thing I noticed after answering your question above. Could you please add a test for the case when the option is selected via a service call instead of via the helper? :)