diff --git a/appdaemontestframework/assert_that.py b/appdaemontestframework/assert_that.py index 0d210fe..1c71b9f 100644 --- a/appdaemontestframework/assert_that.py +++ b/appdaemontestframework/assert_that.py @@ -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() @@ -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): @@ -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): diff --git a/appdaemontestframework/hass_mocks.py b/appdaemontestframework/hass_mocks.py index 0f11071..7aeacf1 100644 --- a/appdaemontestframework/hass_mocks.py +++ b/appdaemontestframework/hass_mocks.py @@ -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'), diff --git a/test/test_assert_that.py b/test/test_assert_that.py index ce23b19..e226235 100644 --- a/test/test_assert_that.py +++ b/test/test_assert_that.py @@ -22,6 +22,7 @@ LIGHT = 'light.some_light' SWITCH = 'switch.some_switch' +INPUT_SELECT = 'input_select.some_input_select' TRANSITION_DURATION = 2 @@ -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') \ No newline at end of file diff --git a/test/test_extra_hass_functions.py b/test/test_extra_hass_functions.py index 7f4ddde..2167f1a 100644 --- a/test/test_extra_hass_functions.py +++ b/test/test_extra_hass_functions.py @@ -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(): @@ -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")