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 all commits
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
32 changes: 32 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,24 @@ def called_with(self, **kwargs):
self.hass_functions['call_service'].assert_any_call(
service_full_name, **kwargs)

def set_to_option(self, option_selected, **service_specific_parameters):
""" Assert that a given entity_id has been set to the chosen 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': option_selected, **service_specific_parameters}))

set_option_helper_not_called = _capture_assert_failure_exception(
lambda: self.hass_functions['select_option'].assert_any_call(
entity_id,
option_selected,
**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 +176,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
1 change: 1 addition & 0 deletions appdaemontestframework/hass_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _hass_init_mock(self, _ad, name, *_args):
MockHandler(Hass, 'turn_on'),
MockHandler(Hass, 'turn_off'),
MockHandler(Hass, 'fire_event'),
MockHandler(Hass, 'select_option'),

### Custom callback functions
MockHandler(Hass, 'register_constraint'),
Expand Down
15 changes: 15 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,17 @@ 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 TestViaHelper:
def test_option_is_set(self, assert_that, automation):
assert_that(INPUT_SELECT).was_not.set_to_option('new_option')
automation.select_option(INPUT_SELECT, 'new_option')
assert_that(INPUT_SELECT).was.set_to_option('new_option')

class TestViaService:
def test_option_is_set(self, assert_that, automation):
assert_that(INPUT_SELECT).was_not.set_to_option('new_service_option')
automation.call_service("input_select/select_option", entity_id=INPUT_SELECT, option='new_service_option')
assert_that(INPUT_SELECT).was.set_to_option('new_service_option')
7 changes: 7 additions & 0 deletions test/test_extra_hass_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def call_notify(self):
def call_now_is_between(self):
self.now_is_between("sunset - 00:45:00", "sunrise + 00:45:00")

def call_select_option(self):
self.select_option("select_item", "value")


@automation_fixture(WithExtraHassFunctions)
def with_extra_hass_functions():
Expand All @@ -27,3 +30,7 @@ def test_now_is_between(given_that, with_extra_hass_functions, hass_mocks):
def test_notify(given_that, with_extra_hass_functions, hass_mocks):
with_extra_hass_functions.call_notify()
hass_mocks.hass_functions['notify'].assert_called_with(message="test", name="html5")

def test_select_option(given_that, with_extra_hass_functions, hass_mocks):
with_extra_hass_functions.call_select_option()
hass_mocks.hass_functions['select_option'].assert_called_with("select_item", "value")