2019-04-03 15:40:03 +00:00
|
|
|
"""Provides functionality to interact with lights."""
|
2021-02-12 17:54:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
import csv
|
2021-01-13 11:11:20 +00:00
|
|
|
import dataclasses
|
2017-01-05 23:16:12 +00:00
|
|
|
from datetime import timedelta
|
2013-12-11 08:07:30 +00:00
|
|
|
import logging
|
2014-03-26 07:08:50 +00:00
|
|
|
import os
|
2021-03-16 11:51:39 +00:00
|
|
|
from typing import Dict, List, Optional, Set, Tuple, cast
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_TOGGLE,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2020-10-23 14:28:21 +00:00
|
|
|
from homeassistant.core import callback
|
2018-01-21 06:35:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-11-16 09:22:07 +00:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
2019-12-03 00:23:12 +00:00
|
|
|
make_entity_service_schema,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-06-13 23:42:09 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2021-01-13 11:11:20 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2015-07-07 07:01:17 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "light"
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2020-10-23 14:28:21 +00:00
|
|
|
DATA_PROFILES = "light_profiles"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
# Bitfield of features supported by the light entity
|
2021-03-16 11:51:39 +00:00
|
|
|
SUPPORT_BRIGHTNESS = 1 # Deprecated, replaced by color modes
|
|
|
|
SUPPORT_COLOR_TEMP = 2 # Deprecated, replaced by color modes
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_EFFECT = 4
|
|
|
|
SUPPORT_FLASH = 8
|
2021-03-16 11:51:39 +00:00
|
|
|
SUPPORT_COLOR = 16 # Deprecated, replaced by color modes
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_TRANSITION = 32
|
2021-03-16 11:51:39 +00:00
|
|
|
SUPPORT_WHITE_VALUE = 128 # Deprecated, replaced by color modes
|
|
|
|
|
|
|
|
# Color mode of the light
|
|
|
|
ATTR_COLOR_MODE = "color_mode"
|
|
|
|
# List of color modes supported by the light
|
|
|
|
ATTR_SUPPORTED_COLOR_MODES = "supported_color_modes"
|
|
|
|
# Possible color modes
|
|
|
|
COLOR_MODE_UNKNOWN = "unknown" # Ambiguous color mode
|
|
|
|
COLOR_MODE_ONOFF = "onoff" # Must be the only supported mode
|
|
|
|
COLOR_MODE_BRIGHTNESS = "brightness" # Must be the only supported mode
|
|
|
|
COLOR_MODE_COLOR_TEMP = "color_temp"
|
|
|
|
COLOR_MODE_HS = "hs"
|
|
|
|
COLOR_MODE_XY = "xy"
|
|
|
|
COLOR_MODE_RGB = "rgb"
|
|
|
|
COLOR_MODE_RGBW = "rgbw"
|
|
|
|
COLOR_MODE_RGBWW = "rgbww"
|
|
|
|
|
|
|
|
VALID_COLOR_MODES = {
|
|
|
|
COLOR_MODE_ONOFF,
|
|
|
|
COLOR_MODE_BRIGHTNESS,
|
|
|
|
COLOR_MODE_COLOR_TEMP,
|
|
|
|
COLOR_MODE_HS,
|
|
|
|
COLOR_MODE_XY,
|
|
|
|
COLOR_MODE_RGB,
|
|
|
|
COLOR_MODE_RGBW,
|
|
|
|
COLOR_MODE_RGBWW,
|
|
|
|
}
|
|
|
|
COLOR_MODES_BRIGHTNESS = VALID_COLOR_MODES - {COLOR_MODE_ONOFF}
|
|
|
|
COLOR_MODES_COLOR = {COLOR_MODE_HS, COLOR_MODE_RGB, COLOR_MODE_XY}
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
# Float that represents transition time in seconds to make change.
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_TRANSITION = "transition"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Lists holding color values
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_RGB_COLOR = "rgb_color"
|
2021-03-16 11:51:39 +00:00
|
|
|
ATTR_RGBW_COLOR = "rgbw_color"
|
|
|
|
ATTR_RGBWW_COLOR = "rgbww_color"
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_XY_COLOR = "xy_color"
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_HS_COLOR = "hs_color"
|
2015-10-28 23:12:16 +00:00
|
|
|
ATTR_COLOR_TEMP = "color_temp"
|
2017-05-17 06:00:46 +00:00
|
|
|
ATTR_KELVIN = "kelvin"
|
2017-04-29 22:04:20 +00:00
|
|
|
ATTR_MIN_MIREDS = "min_mireds"
|
|
|
|
ATTR_MAX_MIREDS = "max_mireds"
|
2016-05-17 07:06:55 +00:00
|
|
|
ATTR_COLOR_NAME = "color_name"
|
2016-09-21 04:26:40 +00:00
|
|
|
ATTR_WHITE_VALUE = "white_value"
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
# Brightness of the light, 0..255 or percentage
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_BRIGHTNESS = "brightness"
|
2017-05-17 06:00:46 +00:00
|
|
|
ATTR_BRIGHTNESS_PCT = "brightness_pct"
|
2020-02-05 00:13:29 +00:00
|
|
|
ATTR_BRIGHTNESS_STEP = "brightness_step"
|
|
|
|
ATTR_BRIGHTNESS_STEP_PCT = "brightness_step_pct"
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# String representing a profile (built-in ones or external defined).
|
2014-03-26 07:08:50 +00:00
|
|
|
ATTR_PROFILE = "profile"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# If the light should flash, can be FLASH_SHORT or FLASH_LONG.
|
2014-12-09 07:02:38 +00:00
|
|
|
ATTR_FLASH = "flash"
|
|
|
|
FLASH_SHORT = "short"
|
|
|
|
FLASH_LONG = "long"
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
# List of possible effects
|
|
|
|
ATTR_EFFECT_LIST = "effect_list"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Apply an effect to the light, can be EFFECT_COLORLOOP.
|
2015-07-08 18:26:37 +00:00
|
|
|
ATTR_EFFECT = "effect"
|
|
|
|
EFFECT_COLORLOOP = "colorloop"
|
2015-12-25 03:10:27 +00:00
|
|
|
EFFECT_RANDOM = "random"
|
2015-11-01 15:04:23 +00:00
|
|
|
EFFECT_WHITE = "white"
|
2015-07-08 18:26:37 +00:00
|
|
|
|
2017-06-02 06:05:05 +00:00
|
|
|
COLOR_GROUP = "Color descriptors"
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
LIGHT_PROFILES_FILE = "light_profiles.csv"
|
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
# Service call validation schemas
|
2017-03-09 21:50:30 +00:00
|
|
|
VALID_TRANSITION = vol.All(vol.Coerce(float), vol.Clamp(min=0, max=6553))
|
2016-07-11 19:39:46 +00:00
|
|
|
VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255))
|
2017-05-17 06:00:46 +00:00
|
|
|
VALID_BRIGHTNESS_PCT = vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
2020-02-05 00:13:29 +00:00
|
|
|
VALID_BRIGHTNESS_STEP = vol.All(vol.Coerce(int), vol.Clamp(min=-255, max=255))
|
|
|
|
VALID_BRIGHTNESS_STEP_PCT = vol.All(vol.Coerce(float), vol.Clamp(min=-100, max=100))
|
2020-04-13 17:30:20 +00:00
|
|
|
VALID_FLASH = vol.In([FLASH_SHORT, FLASH_LONG])
|
2016-03-31 22:24:06 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
LIGHT_TURN_ON_SCHEMA = {
|
|
|
|
vol.Exclusive(ATTR_PROFILE, COLOR_GROUP): cv.string,
|
2019-09-03 07:50:24 +00:00
|
|
|
ATTR_TRANSITION: VALID_TRANSITION,
|
2020-02-05 00:13:29 +00:00
|
|
|
vol.Exclusive(ATTR_BRIGHTNESS, ATTR_BRIGHTNESS): VALID_BRIGHTNESS,
|
|
|
|
vol.Exclusive(ATTR_BRIGHTNESS_PCT, ATTR_BRIGHTNESS): VALID_BRIGHTNESS_PCT,
|
|
|
|
vol.Exclusive(ATTR_BRIGHTNESS_STEP, ATTR_BRIGHTNESS): VALID_BRIGHTNESS_STEP,
|
|
|
|
vol.Exclusive(ATTR_BRIGHTNESS_STEP_PCT, ATTR_BRIGHTNESS): VALID_BRIGHTNESS_STEP_PCT,
|
2019-12-03 00:23:12 +00:00
|
|
|
vol.Exclusive(ATTR_COLOR_NAME, COLOR_GROUP): cv.string,
|
|
|
|
vol.Exclusive(ATTR_RGB_COLOR, COLOR_GROUP): vol.All(
|
2021-03-16 11:51:39 +00:00
|
|
|
vol.ExactSequence((cv.byte,) * 3), vol.Coerce(tuple)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_RGBW_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence((cv.byte,) * 4), vol.Coerce(tuple)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_RGBWW_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence((cv.byte,) * 5), vol.Coerce(tuple)
|
2019-12-03 00:23:12 +00:00
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_XY_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence((cv.small_float, cv.small_float)), vol.Coerce(tuple)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_HS_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence(
|
|
|
|
(
|
|
|
|
vol.All(vol.Coerce(float), vol.Range(min=0, max=360)),
|
|
|
|
vol.All(vol.Coerce(float), vol.Range(min=0, max=100)),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
vol.Coerce(tuple),
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_COLOR_TEMP, COLOR_GROUP): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=1)
|
|
|
|
),
|
2020-10-11 20:04:49 +00:00
|
|
|
vol.Exclusive(ATTR_KELVIN, COLOR_GROUP): cv.positive_int,
|
2019-12-03 00:23:12 +00:00
|
|
|
ATTR_WHITE_VALUE: vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
|
2020-04-13 17:30:20 +00:00
|
|
|
ATTR_FLASH: VALID_FLASH,
|
2019-12-03 00:23:12 +00:00
|
|
|
ATTR_EFFECT: cv.string,
|
2019-09-03 07:50:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
|
2014-11-09 23:12:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2020-01-07 16:30:53 +00:00
|
|
|
def is_on(hass, entity_id):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return if the lights are on based on the statemachine."""
|
2014-04-24 07:40:45 +00:00
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
def preprocess_turn_on_alternatives(hass, params):
|
|
|
|
"""Process extra data for turn light on request.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
# Bail out, we process this later.
|
|
|
|
if ATTR_BRIGHTNESS_STEP in params or ATTR_BRIGHTNESS_STEP_PCT in params:
|
|
|
|
return
|
|
|
|
|
|
|
|
if ATTR_PROFILE in params:
|
|
|
|
hass.data[DATA_PROFILES].apply_profile(params.pop(ATTR_PROFILE), params)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
color_name = params.pop(ATTR_COLOR_NAME, None)
|
|
|
|
if color_name is not None:
|
2018-02-28 02:02:21 +00:00
|
|
|
try:
|
|
|
|
params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name)
|
|
|
|
except ValueError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Got unknown color %s, falling back to white", color_name)
|
2018-02-28 02:02:21 +00:00
|
|
|
params[ATTR_RGB_COLOR] = (255, 255, 255)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
kelvin = params.pop(ATTR_KELVIN, None)
|
|
|
|
if kelvin is not None:
|
|
|
|
mired = color_util.color_temperature_kelvin_to_mired(kelvin)
|
2017-05-18 02:20:59 +00:00
|
|
|
params[ATTR_COLOR_TEMP] = int(mired)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
|
|
|
|
if brightness_pct is not None:
|
2020-04-14 18:26:18 +00:00
|
|
|
params[ATTR_BRIGHTNESS] = round(255 * brightness_pct / 100)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2020-05-11 10:58:59 +00:00
|
|
|
|
|
|
|
def filter_turn_off_params(params):
|
|
|
|
"""Filter out params not used in turn off."""
|
|
|
|
return {k: v for k, v in params.items() if k in (ATTR_TRANSITION, ATTR_FLASH)}
|
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Expose light control via state machine and services."""
|
2018-04-09 14:09:08 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
2020-01-07 16:30:53 +00:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
profiles = hass.data[DATA_PROFILES] = Profiles(hass)
|
|
|
|
await profiles.async_initialize()
|
2015-08-03 15:42:28 +00:00
|
|
|
|
2020-02-05 00:13:29 +00:00
|
|
|
def preprocess_data(data):
|
|
|
|
"""Preprocess the service data."""
|
2020-04-13 13:33:04 +00:00
|
|
|
base = {
|
|
|
|
entity_field: data.pop(entity_field)
|
|
|
|
for entity_field in cv.ENTITY_SERVICE_FIELDS
|
|
|
|
if entity_field in data
|
|
|
|
}
|
2018-07-29 00:53:37 +00:00
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
preprocess_turn_on_alternatives(hass, data)
|
|
|
|
base["params"] = data
|
2020-02-05 00:13:29 +00:00
|
|
|
return base
|
|
|
|
|
|
|
|
async def async_handle_light_on_service(light, call):
|
|
|
|
"""Handle turning a light on.
|
|
|
|
|
|
|
|
If brightness is set to 0, this service will turn the light off.
|
|
|
|
"""
|
2021-03-11 10:46:32 +00:00
|
|
|
params = dict(call.data["params"])
|
2020-02-05 00:13:29 +00:00
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
# Only process params once we processed brightness step
|
|
|
|
if params and (
|
|
|
|
ATTR_BRIGHTNESS_STEP in params or ATTR_BRIGHTNESS_STEP_PCT in params
|
|
|
|
):
|
2020-02-05 00:13:29 +00:00
|
|
|
brightness = light.brightness if light.is_on else 0
|
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS_STEP in params:
|
|
|
|
brightness += params.pop(ATTR_BRIGHTNESS_STEP)
|
|
|
|
|
|
|
|
else:
|
2020-04-14 18:26:18 +00:00
|
|
|
brightness += round(params.pop(ATTR_BRIGHTNESS_STEP_PCT) / 100 * 255)
|
2020-02-05 00:13:29 +00:00
|
|
|
|
|
|
|
params[ATTR_BRIGHTNESS] = max(0, min(255, brightness))
|
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
preprocess_turn_on_alternatives(hass, params)
|
|
|
|
|
2021-01-23 05:20:53 +00:00
|
|
|
if ATTR_PROFILE not in params:
|
|
|
|
profiles.apply_default(light.entity_id, params)
|
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
supported_color_modes = light.supported_color_modes
|
|
|
|
# Backwards compatibility: if an RGBWW color is specified, convert to RGB + W
|
|
|
|
# for legacy lights
|
|
|
|
if ATTR_RGBW_COLOR in params:
|
|
|
|
legacy_supported_color_modes = (
|
|
|
|
light._light_internal_supported_color_modes # pylint: disable=protected-access
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
COLOR_MODE_RGBW in legacy_supported_color_modes
|
|
|
|
and not supported_color_modes
|
|
|
|
):
|
|
|
|
rgbw_color = params.pop(ATTR_RGBW_COLOR)
|
|
|
|
params[ATTR_RGB_COLOR] = rgbw_color[0:3]
|
|
|
|
params[ATTR_WHITE_VALUE] = rgbw_color[3]
|
|
|
|
|
|
|
|
# If a color is specified, convert to the color space supported by the light
|
|
|
|
# Backwards compatibility: Fall back to hs color if light.supported_color_modes
|
|
|
|
# is not implemented
|
|
|
|
if not supported_color_modes:
|
|
|
|
if (rgb_color := params.pop(ATTR_RGB_COLOR, None)) is not None:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
|
|
|
|
elif (xy_color := params.pop(ATTR_XY_COLOR, None)) is not None:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color)
|
|
|
|
elif ATTR_HS_COLOR in params and COLOR_MODE_HS not in supported_color_modes:
|
|
|
|
hs_color = params.pop(ATTR_HS_COLOR)
|
|
|
|
if COLOR_MODE_RGB in supported_color_modes:
|
|
|
|
params[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
|
|
|
|
elif COLOR_MODE_XY in supported_color_modes:
|
|
|
|
params[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)
|
|
|
|
elif ATTR_RGB_COLOR in params and COLOR_MODE_RGB not in supported_color_modes:
|
|
|
|
rgb_color = params.pop(ATTR_RGB_COLOR)
|
|
|
|
if COLOR_MODE_HS in supported_color_modes:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
|
|
|
|
elif COLOR_MODE_XY in supported_color_modes:
|
|
|
|
params[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color)
|
|
|
|
elif ATTR_XY_COLOR in params and COLOR_MODE_XY not in supported_color_modes:
|
|
|
|
xy_color = params.pop(ATTR_XY_COLOR)
|
|
|
|
if COLOR_MODE_HS in supported_color_modes:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color)
|
|
|
|
elif COLOR_MODE_RGB in supported_color_modes:
|
|
|
|
params[ATTR_RGB_COLOR] = color_util.color_xy_to_RGB(*xy_color)
|
|
|
|
|
|
|
|
# Remove deprecated white value if the light supports color mode
|
|
|
|
if supported_color_modes:
|
|
|
|
params.pop(ATTR_WHITE_VALUE, None)
|
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
# Zero brightness: Light will be turned off
|
|
|
|
if params.get(ATTR_BRIGHTNESS) == 0:
|
|
|
|
await light.async_turn_off(**filter_turn_off_params(params))
|
2020-02-05 00:13:29 +00:00
|
|
|
else:
|
|
|
|
await light.async_turn_on(**params)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2020-05-11 10:58:59 +00:00
|
|
|
async def async_handle_toggle_service(light, call):
|
2020-10-23 14:28:21 +00:00
|
|
|
"""Handle toggling a light."""
|
2020-05-11 10:58:59 +00:00
|
|
|
if light.is_on:
|
2021-01-09 00:10:47 +00:00
|
|
|
off_params = filter_turn_off_params(call.data["params"])
|
2020-05-11 10:58:59 +00:00
|
|
|
await light.async_turn_off(**off_params)
|
|
|
|
else:
|
|
|
|
await async_handle_light_on_service(light, call)
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Listen for light on and light off service calls.
|
2020-02-05 00:13:29 +00:00
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_TURN_ON,
|
2020-02-05 00:13:29 +00:00
|
|
|
vol.All(cv.make_entity_service_schema(LIGHT_TURN_ON_SCHEMA), preprocess_data),
|
2019-07-31 19:25:30 +00:00
|
|
|
async_handle_light_on_service,
|
|
|
|
)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
component.async_register_entity_service(
|
2019-12-03 00:23:12 +00:00
|
|
|
SERVICE_TURN_OFF,
|
2020-04-13 17:30:20 +00:00
|
|
|
{ATTR_TRANSITION: VALID_TRANSITION, ATTR_FLASH: VALID_FLASH},
|
2019-12-03 00:23:12 +00:00
|
|
|
"async_turn_off",
|
2018-08-16 12:28:59 +00:00
|
|
|
)
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
component.async_register_entity_service(
|
2020-05-11 10:58:59 +00:00
|
|
|
SERVICE_TOGGLE,
|
|
|
|
vol.All(cv.make_entity_service_schema(LIGHT_TURN_ON_SCHEMA), preprocess_data),
|
|
|
|
async_handle_toggle_service,
|
2018-08-16 12:28:59 +00:00
|
|
|
)
|
2016-01-16 15:45:05 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
return True
|
2015-06-13 23:42:09 +00:00
|
|
|
|
|
|
|
|
2018-04-09 14:09:08 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up a config entry."""
|
2018-04-09 14:09:08 +00:00
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
2018-04-12 12:28:54 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
def _coerce_none(value: str) -> None:
|
|
|
|
"""Coerce an empty string as None."""
|
|
|
|
|
|
|
|
if not isinstance(value, str):
|
|
|
|
raise vol.Invalid("Expected a string")
|
|
|
|
|
|
|
|
if value:
|
|
|
|
raise vol.Invalid("Not an empty string")
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Profile:
|
|
|
|
"""Representation of a profile."""
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
name: str
|
|
|
|
color_x: Optional[float] = dataclasses.field(repr=False)
|
|
|
|
color_y: Optional[float] = dataclasses.field(repr=False)
|
|
|
|
brightness: Optional[int]
|
|
|
|
transition: Optional[int] = None
|
|
|
|
hs_color: Optional[Tuple[float, float]] = dataclasses.field(init=False)
|
|
|
|
|
|
|
|
SCHEMA = vol.Schema( # pylint: disable=invalid-name
|
2020-10-23 14:28:21 +00:00
|
|
|
vol.Any(
|
|
|
|
vol.ExactSequence(
|
2021-01-13 11:11:20 +00:00
|
|
|
(
|
|
|
|
str,
|
|
|
|
vol.Any(cv.small_float, _coerce_none),
|
|
|
|
vol.Any(cv.small_float, _coerce_none),
|
|
|
|
vol.Any(cv.byte, _coerce_none),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
vol.ExactSequence(
|
|
|
|
(
|
|
|
|
str,
|
|
|
|
vol.Any(cv.small_float, _coerce_none),
|
|
|
|
vol.Any(cv.small_float, _coerce_none),
|
|
|
|
vol.Any(cv.byte, _coerce_none),
|
|
|
|
vol.Any(VALID_TRANSITION, _coerce_none),
|
|
|
|
)
|
2020-10-23 14:28:21 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
def __post_init__(self) -> None:
|
|
|
|
"""Convert xy to hs color."""
|
|
|
|
if None in (self.color_x, self.color_y):
|
|
|
|
self.hs_color = None
|
|
|
|
return
|
|
|
|
|
|
|
|
self.hs_color = color_util.color_xy_to_hs(
|
|
|
|
cast(float, self.color_x), cast(float, self.color_y)
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
2021-02-12 17:54:00 +00:00
|
|
|
def from_csv_row(cls, csv_row: List[str]) -> Profile:
|
2021-01-13 11:11:20 +00:00
|
|
|
"""Create profile from a CSV row tuple."""
|
|
|
|
return cls(*cls.SCHEMA(csv_row))
|
|
|
|
|
|
|
|
|
|
|
|
class Profiles:
|
|
|
|
"""Representation of available color profiles."""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistantType):
|
2020-10-23 14:28:21 +00:00
|
|
|
"""Initialize profiles."""
|
|
|
|
self.hass = hass
|
2021-01-13 11:11:20 +00:00
|
|
|
self.data: Dict[str, Profile] = {}
|
2020-10-23 14:28:21 +00:00
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
def _load_profile_data(self) -> Dict[str, Profile]:
|
2020-10-23 14:28:21 +00:00
|
|
|
"""Load built-in profiles and custom profiles."""
|
|
|
|
profile_paths = [
|
|
|
|
os.path.join(os.path.dirname(__file__), LIGHT_PROFILES_FILE),
|
|
|
|
self.hass.config.path(LIGHT_PROFILES_FILE),
|
|
|
|
]
|
|
|
|
profiles = {}
|
|
|
|
|
|
|
|
for profile_path in profile_paths:
|
|
|
|
if not os.path.isfile(profile_path):
|
|
|
|
continue
|
|
|
|
with open(profile_path) as inp:
|
|
|
|
reader = csv.reader(inp)
|
|
|
|
|
|
|
|
# Skip the header
|
|
|
|
next(reader, None)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for rec in reader:
|
2021-01-13 11:11:20 +00:00
|
|
|
profile = Profile.from_csv_row(rec)
|
|
|
|
profiles[profile.name] = profile
|
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
except vol.MultipleInvalid as ex:
|
|
|
|
_LOGGER.error(
|
2021-01-13 11:11:20 +00:00
|
|
|
"Error parsing light profile row '%s' from %s: %s",
|
|
|
|
rec,
|
|
|
|
profile_path,
|
|
|
|
ex,
|
2020-10-23 14:28:21 +00:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
return profiles
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
async def async_initialize(self) -> None:
|
2020-10-23 14:28:21 +00:00
|
|
|
"""Load and cache profiles."""
|
|
|
|
self.data = await self.hass.async_add_executor_job(self._load_profile_data)
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2020-10-23 14:28:21 +00:00
|
|
|
@callback
|
2021-01-13 11:11:20 +00:00
|
|
|
def apply_default(self, entity_id: str, params: Dict) -> None:
|
2018-07-24 18:29:59 +00:00
|
|
|
"""Return the default turn-on profile for the given light."""
|
2021-01-13 11:11:20 +00:00
|
|
|
for _entity_id in (entity_id, "group.all_lights"):
|
|
|
|
name = f"{_entity_id}.default"
|
|
|
|
if name in self.data:
|
|
|
|
self.apply_profile(name, params)
|
|
|
|
return
|
2020-10-23 14:28:21 +00:00
|
|
|
|
|
|
|
@callback
|
2021-01-13 11:11:20 +00:00
|
|
|
def apply_profile(self, name: str, params: Dict) -> None:
|
2020-10-23 14:28:21 +00:00
|
|
|
"""Apply a profile."""
|
|
|
|
profile = self.data.get(name)
|
|
|
|
|
|
|
|
if profile is None:
|
|
|
|
return
|
|
|
|
|
2021-01-13 11:11:20 +00:00
|
|
|
if profile.hs_color is not None:
|
|
|
|
params.setdefault(ATTR_HS_COLOR, profile.hs_color)
|
|
|
|
if profile.brightness is not None:
|
|
|
|
params.setdefault(ATTR_BRIGHTNESS, profile.brightness)
|
|
|
|
if profile.transition is not None:
|
|
|
|
params.setdefault(ATTR_TRANSITION, profile.transition)
|
2018-07-24 18:29:59 +00:00
|
|
|
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class LightEntity(ToggleEntity):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Representation of a light."""
|
2015-06-13 23:42:09 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def brightness(self) -> Optional[int]:
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2015-06-13 23:42:09 +00:00
|
|
|
return None
|
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
@property
|
|
|
|
def color_mode(self) -> Optional[str]:
|
|
|
|
"""Return the color mode of the light."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _light_internal_color_mode(self) -> str:
|
|
|
|
"""Return the color mode of the light with backwards compatibility."""
|
|
|
|
color_mode = self.color_mode
|
|
|
|
|
|
|
|
if color_mode is None:
|
|
|
|
# Backwards compatibility for color_mode added in 2021.4
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
|
|
|
supported = self._light_internal_supported_color_modes
|
|
|
|
|
|
|
|
if (
|
|
|
|
COLOR_MODE_RGBW in supported
|
|
|
|
and self.white_value is not None
|
|
|
|
and self.hs_color is not None
|
|
|
|
):
|
|
|
|
return COLOR_MODE_RGBW
|
|
|
|
if COLOR_MODE_HS in supported and self.hs_color is not None:
|
|
|
|
return COLOR_MODE_HS
|
|
|
|
if COLOR_MODE_COLOR_TEMP in supported and self.color_temp is not None:
|
|
|
|
return COLOR_MODE_COLOR_TEMP
|
|
|
|
if COLOR_MODE_BRIGHTNESS in supported and self.brightness is not None:
|
|
|
|
return COLOR_MODE_BRIGHTNESS
|
|
|
|
if COLOR_MODE_ONOFF in supported:
|
|
|
|
return COLOR_MODE_ONOFF
|
|
|
|
return COLOR_MODE_UNKNOWN
|
|
|
|
|
|
|
|
return color_mode
|
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def hs_color(self) -> Optional[Tuple[float, float]]:
|
2018-03-18 22:00:29 +00:00
|
|
|
"""Return the hue and saturation color value [float, float]."""
|
2015-11-07 09:25:33 +00:00
|
|
|
return None
|
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
@property
|
|
|
|
def xy_color(self) -> Optional[Tuple[float, float]]:
|
|
|
|
"""Return the xy color value [float, float]."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rgb_color(self) -> Optional[Tuple[int, int, int]]:
|
|
|
|
"""Return the rgb color value [int, int, int]."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rgbw_color(self) -> Optional[Tuple[int, int, int, int]]:
|
|
|
|
"""Return the rgbw color value [int, int, int, int]."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _light_internal_rgbw_color(self) -> Optional[Tuple[int, int, int, int]]:
|
|
|
|
"""Return the rgbw color value [int, int, int, int]."""
|
|
|
|
rgbw_color = self.rgbw_color
|
|
|
|
if (
|
|
|
|
rgbw_color is None
|
|
|
|
and self.hs_color is not None
|
|
|
|
and self.white_value is not None
|
|
|
|
):
|
|
|
|
# Backwards compatibility for rgbw_color added in 2021.4
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
|
|
|
r, g, b = color_util.color_hs_to_RGB( # pylint: disable=invalid-name
|
|
|
|
*self.hs_color
|
|
|
|
)
|
|
|
|
w = self.white_value # pylint: disable=invalid-name
|
|
|
|
rgbw_color = (r, g, b, w)
|
|
|
|
|
|
|
|
return rgbw_color
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rgbww_color(self) -> Optional[Tuple[int, int, int, int, int]]:
|
|
|
|
"""Return the rgbww color value [int, int, int, int, int]."""
|
|
|
|
return None
|
|
|
|
|
2015-10-27 22:34:49 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def color_temp(self) -> Optional[int]:
|
2016-03-20 02:44:20 +00:00
|
|
|
"""Return the CT color value in mireds."""
|
2015-10-27 22:34:49 +00:00
|
|
|
return None
|
|
|
|
|
2017-04-29 22:04:20 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def min_mireds(self) -> int:
|
2017-04-29 22:04:20 +00:00
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
# Default to the Philips Hue value that HA has always assumed
|
2018-04-02 07:45:38 +00:00
|
|
|
# https://developers.meethue.com/documentation/core-concepts
|
|
|
|
return 153
|
2017-04-29 22:04:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def max_mireds(self) -> int:
|
2017-04-29 22:04:20 +00:00
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
# Default to the Philips Hue value that HA has always assumed
|
2018-04-02 07:45:38 +00:00
|
|
|
# https://developers.meethue.com/documentation/core-concepts
|
2017-04-29 22:04:20 +00:00
|
|
|
return 500
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def white_value(self) -> Optional[int]:
|
2016-09-21 04:26:40 +00:00
|
|
|
"""Return the white value of this light between 0..255."""
|
|
|
|
return None
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def effect_list(self) -> Optional[List[str]]:
|
2016-11-28 01:15:28 +00:00
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def effect(self) -> Optional[str]:
|
2016-11-28 01:15:28 +00:00
|
|
|
"""Return the current effect."""
|
|
|
|
return None
|
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
@property
|
2019-12-02 19:15:50 +00:00
|
|
|
def capability_attributes(self):
|
|
|
|
"""Return capability attributes."""
|
2015-06-13 23:42:09 +00:00
|
|
|
data = {}
|
2018-07-18 10:18:22 +00:00
|
|
|
supported_features = self.supported_features
|
2015-06-13 23:42:09 +00:00
|
|
|
|
2018-07-18 10:18:22 +00:00
|
|
|
if supported_features & SUPPORT_COLOR_TEMP:
|
2018-03-08 22:39:10 +00:00
|
|
|
data[ATTR_MIN_MIREDS] = self.min_mireds
|
|
|
|
data[ATTR_MAX_MIREDS] = self.max_mireds
|
|
|
|
|
2019-02-19 05:01:26 +00:00
|
|
|
if supported_features & SUPPORT_EFFECT:
|
|
|
|
data[ATTR_EFFECT_LIST] = self.effect_list
|
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
data[ATTR_SUPPORTED_COLOR_MODES] = sorted(
|
|
|
|
list(self._light_internal_supported_color_modes)
|
|
|
|
)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def _light_internal_convert_color(self, color_mode: str) -> dict:
|
|
|
|
data: Dict[str, Tuple] = {}
|
|
|
|
if color_mode == COLOR_MODE_HS and self.hs_color:
|
|
|
|
hs_color = self.hs_color
|
|
|
|
data[ATTR_HS_COLOR] = (round(hs_color[0], 3), round(hs_color[1], 3))
|
|
|
|
data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
|
|
|
|
data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)
|
|
|
|
elif color_mode == COLOR_MODE_XY and self.xy_color:
|
|
|
|
xy_color = self.xy_color
|
|
|
|
data[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color)
|
|
|
|
data[ATTR_RGB_COLOR] = color_util.color_xy_to_RGB(*xy_color)
|
|
|
|
data[ATTR_XY_COLOR] = (round(xy_color[0], 6), round(xy_color[1], 6))
|
|
|
|
elif color_mode == COLOR_MODE_RGB and self.rgb_color:
|
|
|
|
rgb_color = self.rgb_color
|
|
|
|
data[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
|
|
|
|
data[ATTR_RGB_COLOR] = tuple(int(x) for x in rgb_color[0:3])
|
|
|
|
data[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color)
|
2019-12-02 19:15:50 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
if not self.is_on:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
supported_features = self.supported_features
|
2021-03-16 11:51:39 +00:00
|
|
|
color_mode = self._light_internal_color_mode
|
|
|
|
|
|
|
|
if color_mode not in self._light_internal_supported_color_modes:
|
|
|
|
# Increase severity to warning in 2021.6, reject in 2021.10
|
|
|
|
_LOGGER.debug(
|
|
|
|
"%s: set to unsupported color_mode: %s, supported_color_modes: %s",
|
|
|
|
self.entity_id,
|
|
|
|
color_mode,
|
|
|
|
self._light_internal_supported_color_modes,
|
|
|
|
)
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
data[ATTR_COLOR_MODE] = color_mode
|
|
|
|
|
|
|
|
if color_mode in COLOR_MODES_BRIGHTNESS:
|
|
|
|
data[ATTR_BRIGHTNESS] = self.brightness
|
|
|
|
elif supported_features & SUPPORT_BRIGHTNESS:
|
|
|
|
# Backwards compatibility for ambiguous / incomplete states
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
2019-12-02 19:15:50 +00:00
|
|
|
data[ATTR_BRIGHTNESS] = self.brightness
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
if color_mode == COLOR_MODE_COLOR_TEMP:
|
2019-12-02 19:15:50 +00:00
|
|
|
data[ATTR_COLOR_TEMP] = self.color_temp
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
if color_mode in COLOR_MODES_COLOR:
|
|
|
|
data.update(self._light_internal_convert_color(color_mode))
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
if color_mode == COLOR_MODE_RGBW:
|
|
|
|
data[ATTR_RGBW_COLOR] = self._light_internal_rgbw_color
|
|
|
|
|
|
|
|
if color_mode == COLOR_MODE_RGBWW:
|
|
|
|
data[ATTR_RGBWW_COLOR] = self.rgbww_color
|
|
|
|
|
|
|
|
if supported_features & SUPPORT_COLOR_TEMP and not self.supported_color_modes:
|
|
|
|
# Backwards compatibility
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
|
|
|
data[ATTR_COLOR_TEMP] = self.color_temp
|
|
|
|
|
|
|
|
if supported_features & SUPPORT_WHITE_VALUE and not self.supported_color_modes:
|
|
|
|
# Backwards compatibility
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
2019-12-02 19:15:50 +00:00
|
|
|
data[ATTR_WHITE_VALUE] = self.white_value
|
2021-03-16 11:51:39 +00:00
|
|
|
if self.hs_color is not None:
|
|
|
|
data.update(self._light_internal_convert_color(COLOR_MODE_HS))
|
2019-12-02 19:15:50 +00:00
|
|
|
|
|
|
|
if supported_features & SUPPORT_EFFECT:
|
|
|
|
data[ATTR_EFFECT] = self.effect
|
2015-11-07 09:25:33 +00:00
|
|
|
|
2018-07-18 10:18:22 +00:00
|
|
|
return {key: val for key, val in data.items() if val is not None}
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2021-03-16 11:51:39 +00:00
|
|
|
@property
|
|
|
|
def _light_internal_supported_color_modes(self) -> Set:
|
|
|
|
"""Calculate supported color modes with backwards compatibility."""
|
|
|
|
supported_color_modes = self.supported_color_modes
|
|
|
|
|
|
|
|
if supported_color_modes is None:
|
|
|
|
# Backwards compatibility for supported_color_modes added in 2021.4
|
|
|
|
# Add warning in 2021.6, remove in 2021.10
|
|
|
|
supported_features = self.supported_features
|
|
|
|
supported_color_modes = set()
|
|
|
|
|
|
|
|
if supported_features & SUPPORT_COLOR_TEMP:
|
|
|
|
supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
|
|
|
|
if supported_features & SUPPORT_COLOR:
|
|
|
|
supported_color_modes.add(COLOR_MODE_HS)
|
|
|
|
if supported_features & SUPPORT_WHITE_VALUE:
|
|
|
|
supported_color_modes.add(COLOR_MODE_RGBW)
|
|
|
|
if supported_features & SUPPORT_BRIGHTNESS and not supported_color_modes:
|
|
|
|
supported_color_modes = {COLOR_MODE_BRIGHTNESS}
|
|
|
|
|
|
|
|
if not supported_color_modes:
|
|
|
|
supported_color_modes = {COLOR_MODE_ONOFF}
|
|
|
|
|
|
|
|
return supported_color_modes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_color_modes(self) -> Optional[Set]:
|
|
|
|
"""Flag supported color modes."""
|
|
|
|
return None
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
2021-03-08 20:21:45 +00:00
|
|
|
def supported_features(self) -> int:
|
2016-08-16 06:07:07 +00:00
|
|
|
"""Flag supported features."""
|
|
|
|
return 0
|
2020-04-26 16:49:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Light(LightEntity):
|
|
|
|
"""Representation of a light (for backwards compatibility)."""
|
|
|
|
|
|
|
|
def __init_subclass__(cls, **kwargs):
|
|
|
|
"""Print deprecation warning."""
|
|
|
|
super().__init_subclass__(**kwargs)
|
|
|
|
_LOGGER.warning(
|
2020-08-27 11:56:20 +00:00
|
|
|
"Light is deprecated, modify %s to extend LightEntity",
|
|
|
|
cls.__name__,
|
2020-04-26 16:49:41 +00:00
|
|
|
)
|