From 39601090bafd1afeb8b360ab42d71cd2a91b2910 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Wed, 2 Dec 2020 10:40:49 -0800 Subject: [PATCH] Automatically select "Solid" effect in Hyperion (#43799) --- homeassistant/components/hyperion/light.py | 9 +++-- tests/components/hyperion/test_light.py | 46 +++++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/hyperion/light.py b/homeassistant/components/hyperion/light.py index 90e362b3b16..b8e9040f7ce 100644 --- a/homeassistant/components/hyperion/light.py +++ b/homeassistant/components/hyperion/light.py @@ -342,7 +342,7 @@ class HyperionLight(LightEntity): @property def is_on(self) -> bool: """Return true if not black.""" - return bool(self._client.is_on()) + return bool(self._client.is_on()) and self._client.visible_priority is not None @property def icon(self) -> str: @@ -413,7 +413,10 @@ class HyperionLight(LightEntity): # == Get key parameters == brightness = kwargs.get(ATTR_BRIGHTNESS, self._brightness) - effect = kwargs.get(ATTR_EFFECT, self._effect) + if ATTR_EFFECT not in kwargs and ATTR_HS_COLOR in kwargs: + effect = KEY_EFFECT_SOLID + else: + effect = kwargs.get(ATTR_EFFECT, self._effect) rgb_color: Sequence[int] if ATTR_HS_COLOR in kwargs: rgb_color = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR]) @@ -549,7 +552,7 @@ class HyperionLight(LightEntity): rgb_color=visible_priority[const.KEY_VALUE][const.KEY_RGB], effect=KEY_EFFECT_SOLID, ) - self.async_write_ha_state() + self.async_write_ha_state() def _update_effect_list(self, _: Optional[Dict[str, Any]] = None) -> None: """Update Hyperion effects.""" diff --git a/tests/components/hyperion/test_light.py b/tests/components/hyperion/test_light.py index 36b3684f736..5366f6e14d1 100644 --- a/tests/components/hyperion/test_light.py +++ b/tests/components/hyperion/test_light.py @@ -502,6 +502,36 @@ async def test_light_async_turn_on(hass: HomeAssistantType) -> None: assert entity_state.attributes["icon"] == hyperion_light.ICON_EFFECT assert entity_state.attributes["effect"] == effect + # On (=), 100% (=), [0,0,255] (!) + # Ensure changing the color will move the effect to 'Solid' automatically. + hs_color = (240.0, 100.0) + client.async_send_set_color = AsyncMock(return_value=True) + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: TEST_ENTITY_ID_1, ATTR_HS_COLOR: hs_color}, + blocking=True, + ) + + assert client.async_send_set_color.call_args == call( + **{ + const.KEY_PRIORITY: TEST_PRIORITY, + const.KEY_COLOR: (0, 0, 255), + const.KEY_ORIGIN: hyperion_light.DEFAULT_ORIGIN, + } + ) + # Simulate a state callback from Hyperion. + client.visible_priority = { + const.KEY_COMPONENTID: const.KEY_COMPONENTID_COLOR, + const.KEY_VALUE: {const.KEY_RGB: (0, 0, 255)}, + } + _call_registered_callback(client, "priorities-update") + entity_state = hass.states.get(TEST_ENTITY_ID_1) + assert entity_state + assert entity_state.attributes["hs_color"] == hs_color + assert entity_state.attributes["icon"] == hyperion_light.ICON_LIGHTBULB + assert entity_state.attributes["effect"] == hyperion_light.KEY_EFFECT_SOLID + # No calls if disconnected. client.has_loaded_state = False _call_registered_callback(client, "client-update", {"loaded-state": False}) @@ -627,6 +657,14 @@ async def test_light_async_updates_from_hyperion_client( assert entity_state.attributes["icon"] == hyperion_light.ICON_LIGHTBULB assert entity_state.attributes["hs_color"] == (180.0, 100.0) + # Update priorities (None) + client.visible_priority = None + + _call_registered_callback(client, "priorities-update") + entity_state = hass.states.get(TEST_ENTITY_ID_1) + assert entity_state + assert entity_state.state == "off" + # Update effect list effects = [{const.KEY_NAME: "One"}, {const.KEY_NAME: "Two"}] client.effects = effects @@ -648,6 +686,10 @@ async def test_light_async_updates_from_hyperion_client( # Update connection status (e.g. re-connection) client.has_loaded_state = True + client.visible_priority = { + const.KEY_COMPONENTID: const.KEY_COMPONENTID_COLOR, + const.KEY_VALUE: {const.KEY_RGB: rgb}, + } _call_registered_callback(client, "client-update", {"loaded-state": True}) entity_state = hass.states.get(TEST_ENTITY_ID_1) assert entity_state @@ -691,7 +733,7 @@ async def test_unload_entry(hass: HomeAssistantType) -> None: assert client.async_client_disconnect.call_count == 2 -async def test_version_log_warning(caplog, hass: HomeAssistantType) -> None: +async def test_version_log_warning(caplog, hass: HomeAssistantType) -> None: # type: ignore[no-untyped-def] """Test warning on old version.""" client = create_mock_client() client.async_sysinfo_version = AsyncMock(return_value="2.0.0-alpha.7") @@ -700,7 +742,7 @@ async def test_version_log_warning(caplog, hass: HomeAssistantType) -> None: assert "Please consider upgrading" in caplog.text -async def test_version_no_log_warning(caplog, hass: HomeAssistantType) -> None: +async def test_version_no_log_warning(caplog, hass: HomeAssistantType) -> None: # type: ignore[no-untyped-def] """Test no warning on acceptable version.""" client = create_mock_client() client.async_sysinfo_version = AsyncMock(return_value="2.0.0-alpha.9")