diff --git a/homeassistant/components/flux_led/light.py b/homeassistant/components/flux_led/light.py index f1fa4ed7dbb..15f9716b52a 100644 --- a/homeassistant/components/flux_led/light.py +++ b/homeassistant/components/flux_led/light.py @@ -38,7 +38,6 @@ from homeassistant.components.light import ( COLOR_MODE_RGBW, COLOR_MODE_RGBWW, COLOR_MODE_WHITE, - EFFECT_COLORLOOP, EFFECT_RANDOM, PLATFORM_SCHEMA, SUPPORT_EFFECT, @@ -110,55 +109,8 @@ EFFECT_SUPPORT_MODES = {COLOR_MODE_RGB, COLOR_MODE_RGBW, COLOR_MODE_RGBWW} # Warm-white and Cool-white modes COLOR_TEMP_WARM_VS_COLD_WHITE_CUT_OFF: Final = 285 -# List of supported effects which aren't already declared in LIGHT -EFFECT_RED_FADE: Final = "red_fade" -EFFECT_GREEN_FADE: Final = "green_fade" -EFFECT_BLUE_FADE: Final = "blue_fade" -EFFECT_YELLOW_FADE: Final = "yellow_fade" -EFFECT_CYAN_FADE: Final = "cyan_fade" -EFFECT_PURPLE_FADE: Final = "purple_fade" -EFFECT_WHITE_FADE: Final = "white_fade" -EFFECT_RED_GREEN_CROSS_FADE: Final = "rg_cross_fade" -EFFECT_RED_BLUE_CROSS_FADE: Final = "rb_cross_fade" -EFFECT_GREEN_BLUE_CROSS_FADE: Final = "gb_cross_fade" -EFFECT_COLORSTROBE: Final = "colorstrobe" -EFFECT_RED_STROBE: Final = "red_strobe" -EFFECT_GREEN_STROBE: Final = "green_strobe" -EFFECT_BLUE_STROBE: Final = "blue_strobe" -EFFECT_YELLOW_STROBE: Final = "yellow_strobe" -EFFECT_CYAN_STROBE: Final = "cyan_strobe" -EFFECT_PURPLE_STROBE: Final = "purple_strobe" -EFFECT_WHITE_STROBE: Final = "white_strobe" -EFFECT_COLORJUMP: Final = "colorjump" EFFECT_CUSTOM: Final = "custom" -EFFECT_MAP: Final = { - EFFECT_COLORLOOP: 0x25, - EFFECT_RED_FADE: 0x26, - EFFECT_GREEN_FADE: 0x27, - EFFECT_BLUE_FADE: 0x28, - EFFECT_YELLOW_FADE: 0x29, - EFFECT_CYAN_FADE: 0x2A, - EFFECT_PURPLE_FADE: 0x2B, - EFFECT_WHITE_FADE: 0x2C, - EFFECT_RED_GREEN_CROSS_FADE: 0x2D, - EFFECT_RED_BLUE_CROSS_FADE: 0x2E, - EFFECT_GREEN_BLUE_CROSS_FADE: 0x2F, - EFFECT_COLORSTROBE: 0x30, - EFFECT_RED_STROBE: 0x31, - EFFECT_GREEN_STROBE: 0x32, - EFFECT_BLUE_STROBE: 0x33, - EFFECT_YELLOW_STROBE: 0x34, - EFFECT_CYAN_STROBE: 0x35, - EFFECT_PURPLE_STROBE: 0x36, - EFFECT_WHITE_STROBE: 0x37, - EFFECT_COLORJUMP: 0x38, -} -EFFECT_ID_NAME: Final = {v: k for k, v in EFFECT_MAP.items()} -EFFECT_CUSTOM_CODE: Final = 0x60 - -FLUX_EFFECT_LIST: Final = sorted(EFFECT_MAP) + [EFFECT_RANDOM] - SERVICE_CUSTOM_EFFECT: Final = "set_custom_effect" CUSTOM_EFFECT_DICT: Final = { @@ -315,9 +267,9 @@ class FluxLight(FluxEntity, CoordinatorEntity, LightEntity): } if self._attr_supported_color_modes.intersection(EFFECT_SUPPORT_MODES): self._attr_supported_features |= SUPPORT_EFFECT - self._attr_effect_list = FLUX_EFFECT_LIST + self._attr_effect_list = [*self._device.effect_list, EFFECT_RANDOM] if custom_effect_colors: - self._attr_effect_list = [*FLUX_EFFECT_LIST, EFFECT_CUSTOM] + self._attr_effect_list.append(EFFECT_CUSTOM) self._custom_effect_colors = custom_effect_colors self._custom_effect_speed_pct = custom_effect_speed_pct self._custom_effect_transition = custom_effect_transition @@ -369,9 +321,10 @@ class FluxLight(FluxEntity, CoordinatorEntity, LightEntity): @property def effect(self) -> str | None: """Return the current effect.""" - if (current_mode := self._device.preset_pattern_num) == EFFECT_CUSTOM_CODE: - return EFFECT_CUSTOM - return EFFECT_ID_NAME.get(current_mode) + effect = self._device.effect + if effect is None: + return None + return cast(str, effect) async def _async_turn_on(self, **kwargs: Any) -> None: """Turn the specified or all lights on.""" @@ -444,13 +397,9 @@ class FluxLight(FluxEntity, CoordinatorEntity, LightEntity): self._custom_effect_transition, ) return - # Effect selection - if effect in EFFECT_MAP: - await self._device.async_set_preset_pattern( - EFFECT_MAP[effect], DEFAULT_EFFECT_SPEED - ) - return - raise ValueError(f"Unknown effect {effect}") + await self._device.async_set_effect(effect, DEFAULT_EFFECT_SPEED) + return + # Handle brightness adjustment in CCT Color Mode if self.color_mode == COLOR_MODE_COLOR_TEMP: await self._device.async_set_white_temp(self._device.color_temp, brightness) diff --git a/homeassistant/components/flux_led/manifest.json b/homeassistant/components/flux_led/manifest.json index 429465a0f3c..ef87f220a80 100644 --- a/homeassistant/components/flux_led/manifest.json +++ b/homeassistant/components/flux_led/manifest.json @@ -3,7 +3,7 @@ "name": "Flux LED/MagicHome", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/flux_led", - "requirements": ["flux_led==0.24.17"], + "requirements": ["flux_led==0.24.18"], "quality_scale": "platinum", "codeowners": ["@icemanch"], "iot_class": "local_push", diff --git a/requirements_all.txt b/requirements_all.txt index c20364ff914..f3082ca55cf 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -658,7 +658,7 @@ fjaraskupan==1.0.2 flipr-api==1.4.1 # homeassistant.components.flux_led -flux_led==0.24.17 +flux_led==0.24.18 # homeassistant.components.homekit fnvhash==0.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3b5ae4b654b..8d7adcf59f0 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -393,7 +393,7 @@ fjaraskupan==1.0.2 flipr-api==1.4.1 # homeassistant.components.flux_led -flux_led==0.24.17 +flux_led==0.24.18 # homeassistant.components.homekit fnvhash==0.1.0 diff --git a/tests/components/flux_led/__init__.py b/tests/components/flux_led/__init__.py index 8f484ac1989..08aefabe17d 100644 --- a/tests/components/flux_led/__init__.py +++ b/tests/components/flux_led/__init__.py @@ -46,8 +46,10 @@ def _mocked_bulb() -> AIOWifiLedBulb: bulb.device_type = DeviceType.Bulb bulb.async_setup = AsyncMock(side_effect=_save_setup_callback) + bulb.effect_list = ["some_effect"] bulb.async_set_custom_pattern = AsyncMock() bulb.async_set_preset_pattern = AsyncMock() + bulb.async_set_effect = AsyncMock() bulb.async_set_white_temp = AsyncMock() bulb.async_stop = AsyncMock() bulb.async_update = AsyncMock() @@ -68,6 +70,7 @@ def _mocked_bulb() -> AIOWifiLedBulb: bulb.getWhiteTemperature = MagicMock(return_value=(2700, 128)) bulb.brightness = 128 bulb.model_num = 0x35 + bulb.effect = None bulb.model = "Smart Bulb (0x35)" bulb.version_num = 8 bulb.rgbwcapable = True diff --git a/tests/components/flux_led/conftest.py b/tests/components/flux_led/conftest.py new file mode 100644 index 00000000000..abac297da2d --- /dev/null +++ b/tests/components/flux_led/conftest.py @@ -0,0 +1,11 @@ +"""Tests for the flux_led integration.""" + +import pytest + +from tests.common import mock_device_registry + + +@pytest.fixture(name="device_reg") +def device_reg_fixture(hass): + """Return an empty, loaded, registry.""" + return mock_device_registry(hass) diff --git a/tests/components/flux_led/test_light.py b/tests/components/flux_led/test_light.py index 6f0ad5aa253..8819852f6b2 100644 --- a/tests/components/flux_led/test_light.py +++ b/tests/components/flux_led/test_light.py @@ -27,7 +27,6 @@ from homeassistant.components.flux_led.const import ( MODE_AUTO, TRANSITION_JUMP, ) -from homeassistant.components.flux_led.light import EFFECT_CUSTOM_CODE, FLUX_EFFECT_LIST from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_MODE, @@ -41,6 +40,7 @@ from homeassistant.components.light import ( ATTR_SUPPORTED_COLOR_MODES, ATTR_WHITE, DOMAIN as LIGHT_DOMAIN, + EFFECT_RANDOM, ) from homeassistant.const import ( ATTR_ENTITY_ID, @@ -202,7 +202,7 @@ async def test_rgb_light(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"] assert attributes[ATTR_HS_COLOR] == (0, 100) @@ -253,16 +253,8 @@ async def test_rgb_light(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"}, blocking=True, ) - bulb.async_set_preset_pattern.assert_called_with(43, 50) - bulb.async_set_preset_pattern.reset_mock() - - with pytest.raises(ValueError): - await hass.services.async_call( - LIGHT_DOMAIN, - "turn_on", - {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "does not exist"}, - blocking=True, - ) + bulb.async_set_effect.assert_called_with("purple_fade", 50) + bulb.async_set_effect.reset_mock() async def test_rgb_cct_light(hass: HomeAssistant) -> None: @@ -288,7 +280,7 @@ async def test_rgb_cct_light(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp", "rgb"] assert attributes[ATTR_HS_COLOR] == (0, 100) @@ -339,8 +331,8 @@ async def test_rgb_cct_light(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"}, blocking=True, ) - bulb.async_set_preset_pattern.assert_called_with(43, 50) - bulb.async_set_preset_pattern.reset_mock() + bulb.async_set_effect.assert_called_with("purple_fade", 50) + bulb.async_set_effect.reset_mock() bulb.color_mode = FLUX_COLOR_MODE_CCT bulb.getWhiteTemperature = Mock(return_value=(5000, 128)) bulb.color_temp = 5000 @@ -407,7 +399,7 @@ async def test_rgbw_light(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgbw" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgbw"] assert attributes[ATTR_RGB_COLOR] == (255, 42, 42) @@ -483,8 +475,8 @@ async def test_rgbw_light(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"}, blocking=True, ) - bulb.async_set_preset_pattern.assert_called_with(43, 50) - bulb.async_set_preset_pattern.reset_mock() + bulb.async_set_effect.assert_called_with("purple_fade", 50) + bulb.async_set_effect.reset_mock() async def test_rgb_or_w_light(hass: HomeAssistant) -> None: @@ -509,7 +501,7 @@ async def test_rgb_or_w_light(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb", "white"] assert attributes[ATTR_RGB_COLOR] == (255, 0, 0) @@ -567,8 +559,8 @@ async def test_rgb_or_w_light(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"}, blocking=True, ) - bulb.async_set_preset_pattern.assert_called_with(43, 50) - bulb.async_set_preset_pattern.reset_mock() + bulb.async_set_effect.assert_called_with("purple_fade", 50) + bulb.async_set_effect.reset_mock() await hass.services.async_call( LIGHT_DOMAIN, @@ -620,7 +612,7 @@ async def test_rgbcw_light(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgbww" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp", "rgbww"] assert attributes[ATTR_HS_COLOR] == (3.237, 94.51) @@ -721,8 +713,8 @@ async def test_rgbcw_light(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"}, blocking=True, ) - bulb.async_set_preset_pattern.assert_called_with(43, 50) - bulb.async_set_preset_pattern.reset_mock() + bulb.async_set_effect.assert_called_with("purple_fade", 50) + bulb.async_set_effect.reset_mock() async def test_white_light(hass: HomeAssistant) -> None: @@ -804,7 +796,7 @@ async def test_rgb_light_custom_effects(hass: HomeAssistant) -> None: attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == [*FLUX_EFFECT_LIST, "custom"] + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM, "custom"] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"] assert attributes[ATTR_HS_COLOR] == (0, 100) @@ -822,11 +814,11 @@ async def test_rgb_light_custom_effects(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "custom"}, blocking=True, ) + bulb.effect = "custom" bulb.async_set_custom_pattern.assert_called_with( [[0, 0, 255], [255, 0, 0]], 88, "jump" ) bulb.async_set_custom_pattern.reset_mock() - bulb.preset_pattern_num = EFFECT_CUSTOM_CODE await async_mock_device_turn_on(hass, bulb) state = hass.states.get(entity_id) @@ -840,11 +832,11 @@ async def test_rgb_light_custom_effects(hass: HomeAssistant) -> None: {ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 55, ATTR_EFFECT: "custom"}, blocking=True, ) + bulb.effect = "custom" bulb.async_set_custom_pattern.assert_called_with( [[0, 0, 255], [255, 0, 0]], 88, "jump" ) bulb.async_set_custom_pattern.reset_mock() - bulb.preset_pattern_num = EFFECT_CUSTOM_CODE await async_mock_device_turn_on(hass, bulb) state = hass.states.get(entity_id) @@ -886,7 +878,7 @@ async def test_rgb_light_custom_effects_invalid_colors( attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == FLUX_EFFECT_LIST + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"] assert attributes[ATTR_HS_COLOR] == (0, 100) @@ -915,7 +907,7 @@ async def test_rgb_light_custom_effect_via_service( attributes = state.attributes assert attributes[ATTR_BRIGHTNESS] == 128 assert attributes[ATTR_COLOR_MODE] == "rgb" - assert attributes[ATTR_EFFECT_LIST] == [*FLUX_EFFECT_LIST] + assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, EFFECT_RANDOM] assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"] assert attributes[ATTR_HS_COLOR] == (0, 100)