Use random effect from flux_led library (#60312)

pull/60315/head
J. Nick Koston 2021-11-24 19:34:19 -06:00 committed by GitHub
parent 75057949d1
commit 5b199bcc6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 64 deletions

View File

@ -71,7 +71,7 @@ CONF_SPEED_PCT: Final = "speed_pct"
CONF_TRANSITION: Final = "transition"
EFFECT_SUPPORT_MODES = {COLOR_MODE_RGB, COLOR_MODE_RGBW, COLOR_MODE_RGBWW}
EFFECT_SPEED_SUPPORT_MODES: Final = {COLOR_MODE_RGB, COLOR_MODE_RGBW, COLOR_MODE_RGBWW}
CONF_CUSTOM_EFFECT_COLORS: Final = "custom_effect_colors"

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import ast
import logging
import random
from typing import Any, Final, cast
from flux_led.const import ATTR_ID, ATTR_IPADDR
@ -31,7 +30,6 @@ from homeassistant.components.light import (
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
COLOR_MODE_WHITE,
EFFECT_RANDOM,
PLATFORM_SCHEMA,
SUPPORT_EFFECT,
SUPPORT_TRANSITION,
@ -71,7 +69,6 @@ from .const import (
CONF_TRANSITION,
DEFAULT_EFFECT_SPEED,
DOMAIN,
EFFECT_SUPPORT_MODES,
FLUX_LED_DISCOVERY,
MODE_AUTO,
MODE_RGB,
@ -86,9 +83,6 @@ from .util import _flux_color_mode_to_hass, _hass_color_modes
_LOGGER = logging.getLogger(__name__)
SUPPORT_FLUX_LED: Final = SUPPORT_TRANSITION
MODE_ATTRS = {
ATTR_EFFECT,
ATTR_COLOR_TEMP,
@ -229,6 +223,8 @@ async def async_setup_entry(
class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
"""Representation of a Flux light."""
_attr_supported_features = SUPPORT_TRANSITION | SUPPORT_EFFECT
def __init__(
self,
coordinator: FluxLedUpdateCoordinator,
@ -240,17 +236,15 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
) -> None:
"""Initialize the light."""
super().__init__(coordinator, unique_id, name)
self._attr_supported_features = SUPPORT_FLUX_LED
self._attr_min_mireds = (
color_temperature_kelvin_to_mired(self._device.max_temp) + 1
) # for rounding
self._attr_max_mireds = color_temperature_kelvin_to_mired(self._device.min_temp)
self._attr_supported_color_modes = _hass_color_modes(self._device)
if self._attr_supported_color_modes.intersection(EFFECT_SUPPORT_MODES):
self._attr_supported_features |= SUPPORT_EFFECT
self._attr_effect_list = [*self._device.effect_list, EFFECT_RANDOM]
if custom_effect_colors:
self._attr_effect_list.append(EFFECT_CUSTOM)
custom_effects: list[str] = []
if custom_effect_colors:
custom_effects.append(EFFECT_CUSTOM)
self._attr_effect_list = [*self._device.effect_list, *custom_effects]
self._custom_effect_colors = custom_effect_colors
self._custom_effect_speed_pct = custom_effect_speed_pct
self._custom_effect_transition = custom_effect_transition
@ -321,14 +315,6 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
async def _async_set_effect(self, effect: str, brightness: int) -> None:
"""Set an effect."""
# Random color effect
if effect == EFFECT_RANDOM:
await self._device.async_set_levels(
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
return
# Custom effect
if effect == EFFECT_CUSTOM:
if self._custom_effect_colors:
@ -364,8 +350,7 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
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]
if color_temp_mired := kwargs.get(ATTR_COLOR_TEMP):
color_temp_kelvin = color_temperature_mired_to_kelvin(color_temp_mired)
if self.color_mode != COLOR_MODE_RGBWW:
await self._device.async_set_white_temp(color_temp_kelvin, brightness)
@ -381,29 +366,23 @@ class FluxLight(FluxOnOffEntity, CoordinatorEntity, LightEntity):
await self._device.async_set_levels(r=0, b=0, g=0, w=warm, w2=cold)
return
# Handle switch to RGB Color Mode
if ATTR_RGB_COLOR in kwargs:
await self._device.async_set_levels(
*kwargs[ATTR_RGB_COLOR], brightness=brightness
)
if rgb := kwargs.get(ATTR_RGB_COLOR):
await self._device.async_set_levels(*rgb, brightness=brightness)
return
# Handle switch to RGBW Color Mode
if ATTR_RGBW_COLOR in kwargs:
if rgbw := kwargs.get(ATTR_RGBW_COLOR):
if ATTR_BRIGHTNESS in kwargs:
rgbw = rgbw_brightness(kwargs[ATTR_RGBW_COLOR], brightness)
else:
rgbw = kwargs[ATTR_RGBW_COLOR]
rgbw = rgbw_brightness(rgbw, brightness)
await self._device.async_set_levels(*rgbw)
return
# Handle switch to RGBWW Color Mode
if ATTR_RGBWW_COLOR in kwargs:
if rgbcw := kwargs.get(ATTR_RGBWW_COLOR):
if ATTR_BRIGHTNESS in kwargs:
rgbcw = rgbcw_brightness(kwargs[ATTR_RGBWW_COLOR], brightness)
else:
rgbcw = kwargs[ATTR_RGBWW_COLOR]
await self._device.async_set_levels(*rgbcw_to_rgbwc(rgbcw))
return
if ATTR_WHITE in kwargs:
await self._device.async_set_levels(w=kwargs[ATTR_WHITE])
if (white := kwargs.get(ATTR_WHITE)) is not None:
await self._device.async_set_levels(w=white)
return
async def _async_adjust_brightness(self, brightness: int) -> None:

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

View File

@ -13,7 +13,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FluxLedUpdateCoordinator
from .const import DOMAIN, EFFECT_SUPPORT_MODES
from .const import DOMAIN, EFFECT_SPEED_SUPPORT_MODES
from .entity import FluxEntity
from .util import _hass_color_modes
@ -27,7 +27,7 @@ async def async_setup_entry(
coordinator: FluxLedUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
color_modes = _hass_color_modes(coordinator.device)
if not color_modes.intersection(EFFECT_SUPPORT_MODES):
if not color_modes.intersection(EFFECT_SPEED_SUPPORT_MODES):
return
async_add_entities(

View File

@ -658,7 +658,7 @@ fjaraskupan==1.0.2
flipr-api==1.4.1
# homeassistant.components.flux_led
flux_led==0.24.33
flux_led==0.24.34
# 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.33
flux_led==0.24.34
# homeassistant.components.homekit
fnvhash==0.1.0

View File

@ -40,7 +40,6 @@ 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 +201,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"]
assert attributes[ATTR_HS_COLOR] == (0, 100)
@ -272,8 +271,8 @@ async def test_rgb_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "random"},
blocking=True,
)
bulb.async_set_levels.assert_called_once()
bulb.async_set_levels.reset_mock()
bulb.async_set_effect.assert_called_once()
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -308,7 +307,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp", "rgb"]
assert attributes[ATTR_HS_COLOR] == (0, 100)
@ -350,8 +349,8 @@ async def test_rgb_cct_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "random"},
blocking=True,
)
bulb.async_set_levels.assert_called_once()
bulb.async_set_levels.reset_mock()
bulb.async_set_effect.assert_called_once()
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -427,7 +426,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgbw"]
assert attributes[ATTR_RGB_COLOR] == (255, 42, 42)
@ -494,8 +493,8 @@ async def test_rgbw_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "random"},
blocking=True,
)
bulb.async_set_levels.assert_called_once()
bulb.async_set_levels.reset_mock()
bulb.async_set_effect.assert_called_once()
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -529,7 +528,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb", "white"]
assert attributes[ATTR_RGB_COLOR] == (255, 0, 0)
@ -578,8 +577,8 @@ async def test_rgb_or_w_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "random"},
blocking=True,
)
bulb.async_set_levels.assert_called_once()
bulb.async_set_levels.reset_mock()
bulb.async_set_effect.assert_called_once()
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -640,7 +639,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["color_temp", "rgbww"]
assert attributes[ATTR_HS_COLOR] == (3.237, 94.51)
@ -732,8 +731,8 @@ async def test_rgbcw_light(hass: HomeAssistant) -> None:
{ATTR_ENTITY_ID: entity_id, ATTR_EFFECT: "random"},
blocking=True,
)
bulb.async_set_levels.assert_called_once()
bulb.async_set_levels.reset_mock()
bulb.async_set_effect.assert_called_once()
bulb.async_set_effect.reset_mock()
await hass.services.async_call(
LIGHT_DOMAIN,
@ -781,7 +780,7 @@ async def test_white_light(hass: HomeAssistant) -> None:
assert attributes[ATTR_BRIGHTNESS] == 128
assert attributes[ATTR_COLOR_MODE] == "brightness"
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["brightness"]
assert ATTR_EFFECT_LIST not in attributes # single channel does not support effects
assert ATTR_EFFECT_LIST in attributes # single channel now supports effects
await hass.services.async_call(
LIGHT_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
@ -835,7 +834,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] == [*bulb.effect_list, EFFECT_RANDOM, "custom"]
assert attributes[ATTR_EFFECT_LIST] == [*bulb.effect_list, "custom"]
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"]
assert attributes[ATTR_HS_COLOR] == (0, 100)
@ -917,7 +916,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"]
assert attributes[ATTR_HS_COLOR] == (0, 100)
@ -946,7 +945,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] == [*bulb.effect_list, EFFECT_RANDOM]
assert attributes[ATTR_EFFECT_LIST] == bulb.effect_list
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["rgb"]
assert attributes[ATTR_HS_COLOR] == (0, 100)
@ -1090,9 +1089,7 @@ async def test_addressable_light(hass: HomeAssistant) -> None:
assert state.state == STATE_ON
attributes = state.attributes
assert attributes[ATTR_COLOR_MODE] == "onoff"
assert (
ATTR_EFFECT_LIST not in attributes
) # no support for effects with addressable yet
assert ATTR_EFFECT_LIST in attributes
assert attributes[ATTR_SUPPORTED_COLOR_MODES] == ["onoff"]
await hass.services.async_call(