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_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_click( sut: LightController, monkeypatch: MonkeyPatch, mocker: MockerFixture, attribute_input: str, direction_input: Literal["up", "down"], light_state: Literal["on", "off"], smooth_power_on: bool, expected_calls: int, ): value_attribute = 10 monkeypatch.setattr(sut, "get_entity_state", fake_fn(to_return=light_state, async_=True)) monkeypatch.setattr(sut, "get_value_attribute", fake_fn(to_return=value_attribute, async_=True)) monkeypatch.setattr(sut, "get_attribute", fake_fn(to_return=attribute_input, async_=True)) change_light_state_patch = mocker.patch.object(sut, "change_light_state") sut.smooth_power_on = smooth_power_on sut.feature_support._supported_features = 0 stepper = MinMaxStepper(1, 10, 10) sut.manual_steppers = {attribute_input: stepper} await sut.click(attribute_input, direction_input) assert change_light_state_patch.call_count == expected_calls
async def test_hold( sut: LightController, monkeypatch: MonkeyPatch, mocker: MockerFixture, attribute_input: str, direction_input: str, previous_direction: str, light_state: Literal["on", "off"], smooth_power_on: bool, expected_calls: int, expected_direction: str, ): value_attribute = 10 monkeypatch.setattr(sut, "get_entity_state", fake_fn(to_return=light_state, async_=True)) monkeypatch.setattr(sut, "get_value_attribute", fake_fn(to_return=value_attribute, async_=True)) monkeypatch.setattr(sut, "get_attribute", fake_fn(to_return=attribute_input, async_=True)) sut.smooth_power_on = smooth_power_on sut.feature_support._supported_features = 0 stepper = MinMaxStepper(1, 10, 10) stepper.previous_direction = previous_direction sut.automatic_steppers = {attribute_input: stepper} super_hold_patch = mocker.patch.object(ReleaseHoldController, "hold") await sut.hold(attribute_input, direction_input) assert super_hold_patch.call_count == expected_calls if expected_calls > 0: super_hold_patch.assert_called_with(attribute_input, expected_direction)
async def test_change_light_state( sut: LightController, mocker: MockerFixture, old: int, attribute: str, direction: Literal["up", "down"], stepper: MinMaxStepper, smooth_power_on_check: bool, stop_expected: bool, expected_value_attribute: int, ): called_service_patch = mocker.patch.object(sut, "call_service") sut.value_attribute = old sut.smooth_power_on_check = smooth_power_on_check sut.remove_transition_check = False sut.manual_steppers = {attribute: stepper} sut.automatic_steppers = {attribute: stepper} sut.feature_support._supported_features = 0 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_call_light_service( sut: LightController, mocker: MockerFixture, attributes_input: Dict[str, str], remove_transition_check: bool, attributes_expected: Dict[str, str], ): called_service_patch = mocker.patch.object(sut, "call_service") sut.transition = 300 sut.remove_transition_check = remove_transition_check await sut.call_light_service("test_service", **attributes_input) called_service_patch.assert_called_once_with("test_service", entity_id=ENTITY_NAME, **attributes_expected)
async def test_check_remove_transition( sut: LightController, add_transition: bool, add_transition_turn_toggle: bool, on_from_user: bool, transition_support: bool, expected_remove_transition_check: bool, ): sut.transition = 300 sut.add_transition = add_transition sut.add_transition_turn_toggle = add_transition_turn_toggle sut.feature_support._supported_features = (LightSupport.TRANSITION if transition_support else 0) output = await sut.check_remove_transition(on_from_user) assert output == expected_remove_transition_check
async def test_hold_loop(sut: LightController, mocker: MockerFixture, value_attribute: int): attribute = "test_attribute" direction = Stepper.UP sut.smooth_power_on_check = False sut.value_attribute = value_attribute change_light_state_patch = mocker.patch.object(sut, "change_light_state") stepper = MinMaxStepper(1, 10, 10) sut.automatic_steppers = {attribute: stepper} exceeded = await sut.hold_loop(attribute, direction) if value_attribute is None: assert exceeded else: change_light_state_patch.assert_called_once_with( sut.value_attribute, attribute, direction, stepper, "hold")
async def test_on_min(sut: LightController, mocker: MockerFixture): attribute = "test_attribute" min_ = 1 on_patch = mocker.patch.object(sut, "_on") stepper = MinMaxStepper(min_, 10, 10) sut.automatic_steppers = {attribute: stepper} await sut.on_min(attribute) on_patch.assert_called_once_with(**{attribute: min_})
async def test_on_full(sut: LightController, mocker: MockerFixture): attribute = "test_attribute" max_ = 10 on_patch = mocker.patch.object(sut, "on") stepper = MinMaxStepper(1, max_, 10) sut.automatic_steppers = {attribute: stepper} await sut.on_full(attribute, light_on=False) on_patch.assert_called_once_with(light_on=False, **{attribute: max_})
async def test_call_light_service( sut: LightController, mocker: MockerFixture, attributes_input: Dict[str, str], transition_support: bool, turned_toggle: bool, add_transition: bool, add_transition_turn_toggle: bool, attributes_expected: Dict[str, str], ): called_service_patch = mocker.patch.object(sut, "call_service") sut.transition = 300 sut.add_transition = add_transition sut.add_transition_turn_toggle = add_transition_turn_toggle sut.feature_support._supported_features = ( LightSupport.TRANSITION if transition_support else 0 ) await sut.call_light_service( "test_service", turned_toggle=turned_toggle, **attributes_input ) called_service_patch.assert_called_once_with( "test_service", entity_id=ENTITY_NAME, **attributes_expected )
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 test_get_value_attribute( sut: LightController, monkeypatch: MonkeyPatch, attribute_input: str, smooth_power_on_check: bool, light_state: str, expected_output: Union[int, float, str], error_expected: bool, ): sut.smooth_power_on = True sut.smooth_power_on_check = smooth_power_on_check async def fake_get_entity_state(entity, attribute=None): if entity == "light" and attribute is None: return light_state return expected_output monkeypatch.setattr(sut, "get_entity_state", fake_get_entity_state) with wrap_exetuction(error_expected=error_expected, exception=ValueError): output = await sut.get_value_attribute(attribute_input) if not error_expected: assert output == float(expected_output)
async def test_toggle_min( sut: LightController, mocker: MockerFixture, attribute: str, stepper: MinMaxStepper, expected_attribute_value: int, ): call_service_patch = mocker.patch.object(sut, "call_service") sut.automatic_steppers = {attribute: stepper} await sut.toggle_min(attribute) call_service_patch.assert_called_once_with( "light/toggle", **{"entity_id": ENTITY_NAME, attribute: expected_attribute_value} )
async def test_get_attribute( sut: LightController, attribute_input: str, color_mode: ColorMode, supported_features: int, expected_attribute: str, error_expected: bool, ): sut.feature_support._supported_features = supported_features sut.entity = LightEntity(name=ENTITY_NAME, color_mode=color_mode) with wrap_exetuction(error_expected=error_expected, exception=ValueError): output = await sut.get_attribute(attribute_input) if not error_expected: assert output == expected_attribute
async def test_init( sut_before_init: LightController, light_input: Union[str, Dict[str, str]], expected_name: str, expected_color_mode: str, error_expected: bool, ): sut_before_init.args["light"] = light_input # SUT with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.init() # Checks if not error_expected: assert sut_before_init.entity.name == expected_name assert sut_before_init.entity.color_mode == expected_color_mode
async def test_set_value( sut: LightController, mocker: MockerFixture, stepper_cls: Type[Union[MinMaxStepper, CircularStepper]], min_max: Tuple[int, int], fraction: float, expected_calls: int, expected_value: int, ): attribute = "test_attribute" on_patch = mocker.patch.object(sut, "_on") stepper = stepper_cls(min_max[0], min_max[1], 1) sut.automatic_steppers = {attribute: stepper} await sut.set_value(attribute, fraction) assert on_patch.call_count == expected_calls if expected_calls > 0: on_patch.assert_called_with(**{attribute: expected_value})
async def sut_before_init(mocker: MockerFixture) -> LightController: controller = LightController() # type: ignore controller.args = {} mocker.patch.object(Controller, "init") return controller
async def sut(mocker: MockerFixture) -> LightController: controller = LightController() # type: ignore mocker.patch.object(Controller, "init") controller.args = {"light": ENTITY_NAME} await controller.init() return controller
def fake_type_controller() -> LightController: c = LightController() # type: ignore c.args = {} return c