async def test_on(sut, mocker, monkeypatch, light_on, light_state, expected_turned_toggle): monkeypatch.setattr(sut, "call_light_service", fake_async_function()) mocker.patch.object(sut, "get_entity_state", fake_async_function(light_state)) call_light_service_patch = mocker.patch.object(sut, "call_light_service") attributes = {"test": 0} await sut.on(light_on=light_on, **attributes) call_light_service_patch.assert_called_once_with( "light/turn_on", turned_toggle=expected_turned_toggle, **attributes)
async def test_custom_controllers( hass_mock, monkeypatch, mocker, custom_cls, mapping, action_input, mock_function, expected_calls, ): sut = custom_cls() sut.args = { "controller": "test_controller", "integration": "z2m", "light": "light.test_light", "media_player": "media_player.test_media_player", "switch": "switch.test_switch", "cover": "cover.test_cover", "mapping": mapping, } mocked = mocker.patch.object(sut, mock_function) monkeypatch.setattr(sut, "get_entity_state", fake_async_function("0")) await sut.initialize() sut.action_delta = 0 await sut.handle_action(action_input) assert mocked.call_count == expected_calls
async def test_initialize(sut, monkeypatch): monkeypatch.setattr(Controller, "initialize", fake_async_function()) monkeypatch.setattr(sut, "default_delay", lambda: 500) monkeypatch.setattr(sut, "sleep", lambda time: None) # SUT await sut.initialize() assert sut.delay == 500
async def test_toggle(sut, mocker, monkeypatch): monkeypatch.setattr(sut, "call_light_service", fake_async_function()) call_light_service_patch = mocker.patch.object(sut, "call_light_service") attributes = {"test": 0} await sut.toggle(**attributes) call_light_service_patch.assert_called_once_with("light/toggle", turned_toggle=True, **attributes)
def sut(hass_mock, monkeypatch): c = LightController() c.args = {} c.delay = 0 c.light = {"name": "light"} c.on_hold = False monkeypatch.setattr(c, "get_entity_state", fake_async_function("0")) return c
async def sut(monkeypatch, hass_mock, mocker): c = MediaPlayerController() c.args = {} c.delay = 0 c.media_player = "test" c.on_hold = False mocker.patch.object(ReleaseHoldController, "initialize") c.args["media_player"] = "media_player.test" monkeypatch.setattr(c, "get_entity_state", fake_async_function("0")) await c.initialize() return c
async def test_toggle(sut, monkeypatch, mocker, cover_state, stop_expected): called_service_patch = mocker.patch.object(sut, "call_service") open_patch = mocker.patch.object(sut, "open") monkeypatch.setattr(sut, "get_entity_state", fake_async_function(cover_state)) await sut.toggle(open_patch) if stop_expected: called_service_patch.assert_called_once_with("cover/stop_cover", entity_id=sut.cover) else: open_patch.assert_called_once()
def hass_mock(monkeypatch, mocker): """ Fixture for set up the tests, mocking appdaemon functions """ def fake_fn(*args, **kwargs): return None monkeypatch.setattr(hass.Hass, "__init__", fake_fn) monkeypatch.setattr(hass.Hass, "listen_event", fake_fn) monkeypatch.setattr(hass.Hass, "listen_state", fake_fn) monkeypatch.setattr(hass.Hass, "log", fake_fn) monkeypatch.setattr(hass.Hass, "call_service", fake_async_function())
async def test_initialize(sut, monkeypatch, open_position, close_position, error_expected): sut.args = { "cover": "cover.test2", "open_position": open_position, "close_position": close_position, } monkeypatch.setattr(sut, "get_entity_state", fake_async_function("0")) if error_expected: with pytest.raises(ValueError) as e: await sut.initialize() else: await sut.initialize() assert sut.cover == "cover.test2"
async def test_call_action( sut, monkeypatch, mocker, delay, handle, cancel_timer_called, run_in_called, action_timer_callback_called, ): action_key = "test" sut.actions_key_mapping = {"test": "test_action"} sut.action_delay = {action_key: delay} sut.action_delay_handles = {action_key: handle} monkeypatch.setattr(sut, "cancel_timer", fake_async_function()) monkeypatch.setattr(sut, "run_in", fake_async_function()) monkeypatch.setattr(sut, "action_timer_callback", fake_async_function()) cancel_timer_patch = mocker.patch.object(sut, "cancel_timer") run_in_patch = mocker.patch.object(sut, "run_in") action_timer_callback_patch = mocker.patch.object(sut, "action_timer_callback") # SUT await sut.call_action(action_key) # Checks if cancel_timer_called: cancel_timer_patch.assert_called_once_with(handle) if run_in_called: run_in_patch.assert_called_once_with(sut.action_timer_callback, delay, action_key=action_key) if action_timer_callback_called: action_timer_callback_patch.assert_called_once_with( {"action_key": action_key})