Add support for adjusting effect brightness in flux_led (#60247)

pull/60124/head
J. Nick Koston 2021-11-24 12:55:00 -06:00 committed by GitHub
parent 39f5eba97d
commit 86cd46a0dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 26 deletions

View File

@ -46,7 +46,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_PROTOCOL,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -88,6 +88,16 @@ _LOGGER = logging.getLogger(__name__)
SUPPORT_FLUX_LED: Final = SUPPORT_TRANSITION
MODE_ATTRS = {
ATTR_EFFECT,
ATTR_COLOR_TEMP,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
ATTR_WHITE,
}
# Constant color temp values for 2 flux_led special modes
# Warm-white and Cool-white modes
COLOR_TEMP_WARM_VS_COLD_WHITE_CUT_OFF: Final = 285
@ -303,12 +313,13 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
await self._device.async_turn_on()
if not kwargs:
return
if effect := kwargs.get(ATTR_EFFECT):
await self._async_set_effect(effect)
return
await self._async_set_colors(**kwargs)
async def _async_set_effect(self, effect: str) -> None:
if MODE_ATTRS.intersection(kwargs):
await self._async_set_mode(**kwargs)
return
await self._async_adjust_brightness(self._async_brightness(**kwargs))
async def _async_set_effect(self, effect: str, brightness: int) -> None:
"""Set an effect."""
# Random color effect
if effect == EFFECT_RANDOM:
@ -327,12 +338,14 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
self._custom_effect_transition,
)
return
effect_brightness = round(brightness / 255 * 100)
await self._device.async_set_effect(
effect, self._device.speed or DEFAULT_EFFECT_SPEED
effect, self._device.speed or DEFAULT_EFFECT_SPEED, effect_brightness
)
async def _async_set_colors(self, **kwargs: Any) -> None:
"""Set color (can be done before turning on)."""
@callback
def _async_brightness(self, **kwargs: Any) -> int:
"""Determine brightness from kwargs or current value."""
if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is None:
brightness = self.brightness
if not brightness:
@ -341,7 +354,15 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
# If the device was on and brightness was not
# set, it means it was masked by an effect
brightness = 255 if self.is_on else 1
return brightness
async def _async_set_mode(self, **kwargs: Any) -> None:
"""Set an effect or color mode."""
brightness = self._async_brightness(**kwargs)
# Handle switch to Effect Mode
if effect := kwargs.get(ATTR_EFFECT):
await self._async_set_effect(effect, brightness)
return
# Handle switch to CCT Color Mode
if ATTR_COLOR_TEMP in kwargs:
color_temp_mired = kwargs[ATTR_COLOR_TEMP]
@ -385,7 +406,12 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
await self._device.async_set_levels(w=kwargs[ATTR_WHITE])
return
# Handle brightness adjustment in CCT Color Mode
async def _async_adjust_brightness(self, brightness: int) -> None:
"""Adjust brightness."""
# Handle brightness adjustment in effect mode
if effect := self.effect:
await self._async_set_effect(effect, brightness)
return
if self.color_mode == COLOR_MODE_COLOR_TEMP:
await self._device.async_set_white_temp(self._device.color_temp, brightness)
return

View File

@ -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.28"],
"requirements": ["flux_led==0.24.33"],
"quality_scale": "platinum",
"codeowners": ["@icemanch"],
"iot_class": "local_push",

View File

@ -68,6 +68,7 @@ class FluxNumber(FluxEntity, CoordinatorEntity, NumberEntity):
async def async_set_value(self, value: float) -> None:
"""Set the flux speed value."""
current_effect = self._device.effect
current_brightness = self._device.brightness
new_speed = int(value)
if not current_effect:
raise HomeAssistantError(
@ -75,5 +76,8 @@ class FluxNumber(FluxEntity, CoordinatorEntity, NumberEntity):
)
if self._device.original_addressable and not self._device.is_on:
raise HomeAssistantError("Speed can only be adjusted when the light is on")
await self._device.async_set_effect(current_effect, new_speed)
effect_brightness = round(current_brightness / 255 * 100)
await self._device.async_set_effect(
current_effect, new_speed, effect_brightness
)
await self.coordinator.async_request_refresh()

View File

@ -658,7 +658,7 @@ fjaraskupan==1.0.2
flipr-api==1.4.1
# homeassistant.components.flux_led
flux_led==0.24.28
flux_led==0.24.33
# homeassistant.components.homekit
fnvhash==0.1.0

View File

@ -399,7 +399,7 @@ fjaraskupan==1.0.2
flipr-api==1.4.1
# homeassistant.components.flux_led
flux_led==0.24.28
flux_led==0.24.33
# homeassistant.components.homekit
fnvhash==0.1.0

View File

@ -281,7 +281,7 @@ async def test_rgb_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 50)
bulb.async_set_effect.reset_mock()
@ -359,7 +359,7 @@ async def test_rgb_cct_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 50)
bulb.async_set_effect.reset_mock()
bulb.color_mode = FLUX_COLOR_MODE_CCT
bulb.getWhiteTemperature = Mock(return_value=(5000, 128))
@ -500,10 +500,10 @@ async def test_rgbw_light(hass: HomeAssistant) -> None:
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"},
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade", ATTR_BRIGHTNESS: 255},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 100)
bulb.async_set_effect.reset_mock()
@ -584,10 +584,10 @@ async def test_rgb_or_w_light(hass: HomeAssistant) -> None:
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"},
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade", ATTR_BRIGHTNESS: 255},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 100)
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
@ -741,7 +741,18 @@ async def test_rgbcw_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "purple_fade"},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 50)
bulb.async_set_effect.reset_mock()
bulb.effect = "purple_fade"
bulb.brightness = 128
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 255},
blocking=True,
)
bulb.async_set_effect.assert_called_with("purple_fade", 50, 100)
bulb.async_set_effect.reset_mock()

View File

@ -95,7 +95,7 @@ async def test_rgb_light_effect_speed(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: number_entity_id, ATTR_VALUE: 100},
blocking=True,
)
bulb.async_set_effect.assert_called_with("colorloop", 100)
bulb.async_set_effect.assert_called_with("colorloop", 100, 50)
bulb.async_set_effect.reset_mock()
await async_mock_effect_speed(hass, bulb, "red_fade", 50)
@ -105,7 +105,7 @@ async def test_rgb_light_effect_speed(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: number_entity_id, ATTR_VALUE: 50},
blocking=True,
)
bulb.async_set_effect.assert_called_with("red_fade", 50)
bulb.async_set_effect.assert_called_with("red_fade", 50, 50)
bulb.async_set_effect.reset_mock()
state = hass.states.get(number_entity_id)
@ -161,7 +161,7 @@ async def test_original_addressable_light_effect_speed(hass: HomeAssistant) -> N
{ATTR_ENTITY_ID: number_entity_id, ATTR_VALUE: 100},
blocking=True,
)
bulb.async_set_effect.assert_called_with("7 colors change gradually", 100)
bulb.async_set_effect.assert_called_with("7 colors change gradually", 100, 50)
bulb.async_set_effect.reset_mock()
await async_mock_effect_speed(hass, bulb, "7 colors run in olivary", 100)
@ -209,7 +209,7 @@ async def test_addressable_light_effect_speed(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: number_entity_id, ATTR_VALUE: 100},
blocking=True,
)
bulb.async_set_effect.assert_called_with("RBM 1", 100)
bulb.async_set_effect.assert_called_with("RBM 1", 100, 50)
bulb.async_set_effect.reset_mock()
await async_mock_device_turn_on(hass, bulb)
@ -219,7 +219,7 @@ async def test_addressable_light_effect_speed(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: number_entity_id, ATTR_VALUE: 100},
blocking=True,
)
bulb.async_set_effect.assert_called_with("RBM 1", 100)
bulb.async_set_effect.assert_called_with("RBM 1", 100, 50)
bulb.async_set_effect.reset_mock()
await async_mock_effect_speed(hass, bulb, "RBM 2", 100)