From 169264db66b97209c8b811d0d8fa70ae8f9cef7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Huryn?= Date: Thu, 14 Jul 2022 11:21:01 +0200 Subject: [PATCH] Fix Blebox light scenes (#75106) * Bug fix for light platform, when async_turn_on recieves multiple keys. * Changes according to @MartinHjelmare suggestion. * Moved effect set call in BleBoxLightEntity.async_turn_on method. * Added tests for effect in light platform. Added ValueError raise if effect not in effect list. * Removed duplicated line from test as @MartinHjelmare suggested. --- homeassistant/components/blebox/light.py | 15 +++++--- tests/components/blebox/test_light.py | 46 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/blebox/light.py b/homeassistant/components/blebox/light.py index 2bb6bc91762..a2ad51cfc9c 100644 --- a/homeassistant/components/blebox/light.py +++ b/homeassistant/components/blebox/light.py @@ -159,15 +159,20 @@ class BleBoxLightEntity(BleBoxEntity, LightEntity): else: value = feature.apply_brightness(value, brightness) + try: + await self._feature.async_on(value) + except ValueError as exc: + raise ValueError( + f"Turning on '{self.name}' failed: Bad value {value}" + ) from exc + if effect is not None: - effect_value = self.effect_list.index(effect) - await self._feature.async_api_command("effect", effect_value) - else: try: - await self._feature.async_on(value) + effect_value = self.effect_list.index(effect) + await self._feature.async_api_command("effect", effect_value) except ValueError as exc: raise ValueError( - f"Turning on '{self.name}' failed: Bad value {value}" + f"Turning on with effect '{self.name}' failed: {effect} not in effect list." ) from exc async def async_turn_off(self, **kwargs): diff --git a/tests/components/blebox/test_light.py b/tests/components/blebox/test_light.py index f61496714fb..7afb78e5b03 100644 --- a/tests/components/blebox/test_light.py +++ b/tests/components/blebox/test_light.py @@ -7,6 +7,7 @@ import pytest from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_EFFECT, ATTR_RGBW_COLOR, ATTR_SUPPORTED_COLOR_MODES, ColorMode, @@ -524,3 +525,48 @@ async def test_turn_on_failure(feature, hass, config, caplog): assert f"Turning on '{feature_mock.full_name}' failed: Bad value 123" in str( info.value ) + + +async def test_wlightbox_on_effect(wlightbox, hass, config): + """Test light on.""" + + feature_mock, entity_id = wlightbox + + def initial_update(): + feature_mock.is_on = False + + feature_mock.async_update = AsyncMock(side_effect=initial_update) + await async_setup_entity(hass, config, entity_id) + feature_mock.async_update = AsyncMock() + + state = hass.states.get(entity_id) + assert state.state == STATE_OFF + + def turn_on(value): + feature_mock.is_on = True + feature_mock.effect = "POLICE" + + feature_mock.async_on = AsyncMock(side_effect=turn_on) + + with pytest.raises(ValueError) as info: + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {"entity_id": entity_id, ATTR_EFFECT: "NOT IN LIST"}, + blocking=True, + ) + + assert ( + f"Turning on with effect '{feature_mock.full_name}' failed: NOT IN LIST not in effect list." + in str(info.value) + ) + + await hass.services.async_call( + "light", + SERVICE_TURN_ON, + {"entity_id": entity_id, ATTR_EFFECT: "POLICE"}, + blocking=True, + ) + + state = hass.states.get(entity_id) + assert state.attributes[ATTR_EFFECT] == "POLICE"