async def test_custom_controllers( monkeypatch: MonkeyPatch, mocker: MockerFixture, custom_cls: Type[TypeController], mapping: PredefinedActionsMapping, action_input: str, mock_function: str, expected_calls: int, ): sut = custom_cls() # type: ignore 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_fn(async_=True, to_return="0")) # SUT await sut.initialize() sut.action_delta = 0 await sut.handle_action(action_input) # Check assert mocked.call_count == expected_calls
async def test_sync( sut: LightController, monkeypatch: MonkeyPatch, mocker: MockerFixture, max_brightness: int, color_attribute: str, expected_attributes: Dict[str, Any], ): sut.max_brightness = max_brightness sut.add_transition_turn_toggle = True sut.feature_support._supported_features = LightSupport.TRANSITION async def fake_get_attribute(*args, **kwargs): if color_attribute == "error": raise ValueError() return color_attribute monkeypatch.setattr(sut, "get_attribute", fake_get_attribute) monkeypatch.setattr(sut, "get_entity_state", fake_fn(async_=True, to_return="on")) called_service_patch = mocker.patch.object(sut, "call_service") await sut.sync() called_service_patch.assert_called_once_with("light/turn_on", entity_id=ENTITY_NAME, **{ "transition": 0.3, **expected_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_fn(async_=True, to_return="0")) await sut.initialize() sut.action_delta = 0 await sut.handle_action(action_input) assert mocked.call_count == expected_calls
async def test_check_domain( sut: MyTypeController, monkeypatch: MonkeyPatch, entity: str, domains: List[str], entities: List[str], error_expected: bool, ): sut.domains = domains expected_error_message = "" if error_expected: if entities == []: expected_error_message = ( f"'{entity}' must be from one of the following domains " f"{domains} (e.g. {domains[0]}.bedroom)" ) else: expected_error_message = ( f"All entities from '{entity}' must be from one of the " f"following domains {domains} (e.g. {domains[0]}.bedroom)" ) monkeypatch.setattr(sut, "get_state", fake_fn(to_return=entities, async_=True)) with wrap_exetuction( error_expected=error_expected, exception=ValueError ) as err_info: await sut.check_domain(entity) if err_info is not None: assert str(err_info.value) == expected_error_message
async def test_change_light_state( sut: LightController, mocker: MockerFixture, monkeypatch: MonkeyPatch, old: int, attribute: str, direction: Literal["up", "down"], stepper: MinMaxStepper, light_state: str, smooth_power_on: bool, stop_expected: bool, expected_value_attribute: int, ): called_service_patch = mocker.patch.object(sut, "call_service") sut.smooth_power_on = smooth_power_on sut.value_attribute = old sut.manual_steppers = {attribute: stepper} sut.automatic_steppers = {attribute: stepper} sut.feature_support._supported_features = 0 monkeypatch.setattr( sut, "get_entity_state", fake_fn(to_return=light_state, async_=True) ) stop = await sut.change_light_state(old, attribute, direction, stepper, "hold") assert stop == stop_expected assert sut.value_attribute == expected_value_attribute called_service_patch.assert_called()
async def test_initialize(sut, monkeypatch): monkeypatch.setattr(Controller, "initialize", fake_fn(async_=True)) 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_fn(async_=True)) 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_fn(async_=True, to_return="0")) return c
async def test_hold( sut: FakeReleaseHoldController, monkeypatch: MonkeyPatch, mocker: MockerFixture, ): monkeypatch.setattr(sut, "hold_loop", fake_fn(to_return=True, async_=True)) hold_loop_patch = mocker.patch.object(sut, "hold_loop") await sut.hold() hold_loop_patch.assert_called_once()
async def test_call_action( sut: Controller, monkeypatch, mocker: MockerFixture, delay: int, handle: Optional[int], cancel_timer_called: bool, run_in_called: bool, action_timer_callback_called: bool, ): action_key = "test" sut.action_delay = {action_key: delay} action_delay_handles: Dict[ActionEvent, Optional[float]] = { action_key: handle } sut.action_delay_handles = action_delay_handles monkeypatch.setattr(sut, "cancel_timer", fake_fn(async_=True)) monkeypatch.setattr(sut, "run_in", fake_fn(async_=True)) monkeypatch.setattr(sut, "action_timer_callback", fake_fn(async_=True)) 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, extra=None) if action_timer_callback_called: action_timer_callback_patch.assert_called_once_with({ "action_key": action_key, "extra": None })
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_fn(async_=True, to_return="0")) await c.initialize() return c
async def test_volume_down(sut: MediaPlayerController, mocker: MockerFixture, monkeypatch: MonkeyPatch): monkeypatch.setattr(sut, "get_entity_state", fake_fn(async_=True, to_return=0.5)) sut.feature_support._supported_features = MediaPlayerSupport.VOLUME_SET called_service_patch = mocker.patch.object(sut, "call_service") await sut.volume_down() called_service_patch.assert_called_once_with("media_player/volume_set", entity_id=ENTITY_NAME, volume_level=0.4)
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_fn(async_=True, to_return=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()
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_fn(async_=True, to_return="0")) if error_expected: with pytest.raises(ValueError): 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_fn(async_=True)) monkeypatch.setattr(sut, "run_in", fake_fn(async_=True)) monkeypatch.setattr(sut, "action_timer_callback", fake_fn(async_=True)) 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})
def hass_mock(monkeypatch, mocker): """ Fixture for set up the tests, mocking appdaemon functions """ monkeypatch.setattr(hass.Hass, "__init__", fake_fn()) monkeypatch.setattr(hass.Hass, "listen_event", fake_fn()) monkeypatch.setattr(mqtt.Mqtt, "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_fn(async_=True)) monkeypatch.setattr(hass.Hass, "get_ad_version", fake_fn(to_return="4.0.0")) monkeypatch.setattr(hass.Hass, "run_in", fake_run_in) monkeypatch.setattr(hass.Hass, "cancel_timer", fake_cancel_timer)
async def test_hold( sut: FakeReleaseHoldController, monkeypatch: MonkeyPatch, mocker: MockerFixture, on_hold_input: bool, hold_release_toogle: bool, expected_calls: int, ): sut.on_hold = on_hold_input sut.hold_release_toggle = hold_release_toogle monkeypatch.setattr(sut, "hold_loop", fake_fn(to_return=True, async_=True)) hold_loop_patch = mocker.patch.object(sut, "hold_loop") await sut.hold() assert hold_loop_patch.call_count == expected_calls
async def test_on( sut: LightController, mocker: MockerFixture, light_on: bool, light_state: str, expected_turned_toggle: bool, ): mocker.patch.object( sut, "get_entity_state", fake_fn(async_=True, to_return=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_toggle( sut: CoverController, monkeypatch: MonkeyPatch, mocker: MockerFixture, cover_state: str, stop_expected: bool, ): called_service_patch = mocker.patch.object(sut, "call_service") open_patch = mocker.patch.object(sut, "open") monkeypatch.setattr(sut, "get_entity_state", fake_fn(async_=True, to_return=cover_state)) await sut.toggle(open_patch) if stop_expected: called_service_patch.assert_called_once_with("cover/stop_cover", entity_id=ENTITY_NAME) open_patch.assert_not_called() else: open_patch.assert_called_once()