Add color_mode to demo light (#49694)

* Update demo light with color mode support

* Add rgbw and rgbww color properties

* Update demo light

* Tweak

* Remove unneeded _clear_colors
This commit is contained in:
Erik Montnemery 2021-04-29 10:45:17 +02:00 committed by GitHub
parent 25d257b631
commit bf57c77d5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 51 deletions

View file

@ -1,4 +1,6 @@
"""Demo light platform that implements lights."""
from __future__ import annotations
import random
from homeassistant.components.light import (
@ -6,12 +8,13 @@ from homeassistant.components.light import (
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_HS_COLOR,
ATTR_WHITE_VALUE,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
ATTR_RGBW_COLOR,
ATTR_RGBWW_COLOR,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_HS,
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
SUPPORT_EFFECT,
SUPPORT_WHITE_VALUE,
LightEntity,
)
@ -23,9 +26,7 @@ LIGHT_EFFECT_LIST = ["rainbow", "none"]
LIGHT_TEMPS = [240, 380]
SUPPORT_DEMO = (
SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_COLOR | SUPPORT_WHITE_VALUE
)
SUPPORT_DEMO = {COLOR_MODE_HS, COLOR_MODE_COLOR_TEMP}
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
@ -33,27 +34,43 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(
[
DemoLight(
unique_id="light_1",
name="Bed Light",
state=False,
available=True,
effect_list=LIGHT_EFFECT_LIST,
effect=LIGHT_EFFECT_LIST[0],
name="Bed Light",
state=False,
unique_id="light_1",
),
DemoLight(
unique_id="light_2",
name="Ceiling Lights",
state=True,
available=True,
ct=LIGHT_TEMPS[1],
name="Ceiling Lights",
state=True,
unique_id="light_2",
),
DemoLight(
unique_id="light_3",
name="Kitchen Lights",
state=True,
available=True,
hs_color=LIGHT_COLORS[1],
ct=LIGHT_TEMPS[0],
name="Kitchen Lights",
state=True,
unique_id="light_3",
),
DemoLight(
available=True,
ct=LIGHT_TEMPS[1],
name="Office RGBW Lights",
rgbw_color=(255, 0, 0, 255),
state=True,
supported_color_modes={COLOR_MODE_RGBW},
unique_id="light_4",
),
DemoLight(
available=True,
name="Living Room RGBWW Lights",
rgbww_color=(255, 0, 0, 255, 0),
state=True,
supported_color_modes={COLOR_MODE_RGBWW},
unique_id="light_5",
),
]
)
@ -73,26 +90,39 @@ class DemoLight(LightEntity):
name,
state,
available=False,
hs_color=None,
ct=None,
brightness=180,
white=200,
ct=None,
effect_list=None,
effect=None,
hs_color=None,
rgbw_color=None,
rgbww_color=None,
supported_color_modes=None,
):
"""Initialize the light."""
self._unique_id = unique_id
self._name = name
self._state = state
self._hs_color = hs_color
self._ct = ct or random.choice(LIGHT_TEMPS)
self._brightness = brightness
self._white = white
self._features = SUPPORT_DEMO
self._effect_list = effect_list
self._effect = effect
self._available = True
self._color_mode = "ct" if ct is not None and hs_color is None else "hs"
self._brightness = brightness
self._ct = ct or random.choice(LIGHT_TEMPS)
self._effect = effect
self._effect_list = effect_list
self._features = 0
self._hs_color = hs_color
self._name = name
self._rgbw_color = rgbw_color
self._rgbww_color = rgbww_color
self._state = state
self._unique_id = unique_id
if hs_color:
self._color_mode = COLOR_MODE_HS
elif rgbw_color:
self._color_mode = COLOR_MODE_RGBW
elif rgbww_color:
self._color_mode = COLOR_MODE_RGBWW
else:
self._color_mode = COLOR_MODE_COLOR_TEMP
if not supported_color_modes:
supported_color_modes = SUPPORT_DEMO
self._color_modes = supported_color_modes
if self._effect_list is not None:
self._features |= SUPPORT_EFFECT
@ -134,24 +164,30 @@ class DemoLight(LightEntity):
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def color_mode(self) -> str | None:
"""Return the color mode of the light."""
return self._color_mode
@property
def hs_color(self) -> tuple:
"""Return the hs color value."""
if self._color_mode == "hs":
return self._hs_color
return None
return self._hs_color
@property
def rgbw_color(self) -> tuple:
"""Return the rgbw color value."""
return self._rgbw_color
@property
def rgbww_color(self) -> tuple:
"""Return the rgbww color value."""
return self._rgbww_color
@property
def color_temp(self) -> int:
"""Return the CT color temperature."""
if self._color_mode == "ct":
return self._ct
return None
@property
def white_value(self) -> int:
"""Return the white value of this light between 0..255."""
return self._white
return self._ct
@property
def effect_list(self) -> list:
@ -173,24 +209,34 @@ class DemoLight(LightEntity):
"""Flag supported features."""
return self._features
@property
def supported_color_modes(self) -> set | None:
"""Flag supported color modes."""
return self._color_modes
async def async_turn_on(self, **kwargs) -> None:
"""Turn the light on."""
self._state = True
if ATTR_RGBW_COLOR in kwargs:
self._color_mode = COLOR_MODE_RGBW
self._rgbw_color = kwargs[ATTR_RGBW_COLOR]
if ATTR_RGBWW_COLOR in kwargs:
self._color_mode = COLOR_MODE_RGBWW
self._rgbww_color = kwargs[ATTR_RGBWW_COLOR]
if ATTR_HS_COLOR in kwargs:
self._color_mode = "hs"
self._color_mode = COLOR_MODE_HS
self._hs_color = kwargs[ATTR_HS_COLOR]
if ATTR_COLOR_TEMP in kwargs:
self._color_mode = "ct"
self._color_mode = COLOR_MODE_COLOR_TEMP
self._ct = kwargs[ATTR_COLOR_TEMP]
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
if ATTR_WHITE_VALUE in kwargs:
self._white = kwargs[ATTR_WHITE_VALUE]
if ATTR_EFFECT in kwargs:
self._effect = kwargs[ATTR_EFFECT]

View file

@ -11,7 +11,6 @@ from homeassistant.components.light import (
ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS,
ATTR_RGB_COLOR,
ATTR_WHITE_VALUE,
ATTR_XY_COLOR,
DOMAIN as LIGHT_DOMAIN,
SERVICE_TURN_OFF,
@ -54,13 +53,11 @@ async def test_state_attributes(hass):
{
ATTR_ENTITY_ID: ENTITY_LIGHT,
ATTR_RGB_COLOR: (251, 253, 255),
ATTR_WHITE_VALUE: 254,
},
blocking=True,
)
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(ATTR_WHITE_VALUE) == 254
assert state.attributes.get(ATTR_RGB_COLOR) == (250, 252, 255)
assert state.attributes.get(ATTR_XY_COLOR) == (0.319, 0.326)

View file

@ -91,6 +91,8 @@ ENTITY_IDS_BY_NUMBER = {
"22": "scene.light_on",
"23": "scene.light_off",
"24": "media_player.kitchen",
"25": "light.office_rgbw_lights",
"26": "light.living_room_rgbww_lights",
}
ENTITY_NUMBERS_BY_ID = {v: k for k, v in ENTITY_IDS_BY_NUMBER.items()}

View file

@ -380,4 +380,26 @@ DEMO_DEVICES = [
"type": "action.devices.types.SECURITYSYSTEM",
"willReportState": False,
},
{
"id": "light.living_room_rgbww_lights",
"name": {"name": "Living Room RGBWW Lights"},
"traits": [
"action.devices.traits.OnOff",
"action.devices.traits.Brightness",
"action.devices.traits.ColorSetting",
],
"type": "action.devices.types.LIGHT",
"willReportState": False,
},
{
"id": "light.office_rgbw_lights",
"name": {"name": "Office RGBW Lights"},
"traits": [
"action.devices.traits.OnOff",
"action.devices.traits.Brightness",
"action.devices.traits.ColorSetting",
],
"type": "action.devices.types.LIGHT",
"willReportState": False,
},
]