ESPHome light color mode use capabilities (#55206)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
parent
a28593f133
commit
46159c3f18
4 changed files with 150 additions and 51 deletions
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from aioesphomeapi import APIVersion, LightColorMode, LightInfo, LightState
|
from aioesphomeapi import APIVersion, LightColorCapability, LightInfo, LightState
|
||||||
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
|
@ -34,12 +34,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import (
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
||||||
EsphomeEntity,
|
|
||||||
EsphomeEnumMapper,
|
|
||||||
esphome_state_property,
|
|
||||||
platform_async_setup_entry,
|
|
||||||
)
|
|
||||||
|
|
||||||
FLASH_LENGTHS = {FLASH_SHORT: 2, FLASH_LONG: 10}
|
FLASH_LENGTHS = {FLASH_SHORT: 2, FLASH_LONG: 10}
|
||||||
|
|
||||||
|
@ -59,20 +54,81 @@ async def async_setup_entry(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
_COLOR_MODES: EsphomeEnumMapper[LightColorMode, str] = EsphomeEnumMapper(
|
_COLOR_MODE_MAPPING = {
|
||||||
{
|
COLOR_MODE_ONOFF: [
|
||||||
LightColorMode.UNKNOWN: COLOR_MODE_UNKNOWN,
|
LightColorCapability.ON_OFF,
|
||||||
LightColorMode.ON_OFF: COLOR_MODE_ONOFF,
|
],
|
||||||
LightColorMode.BRIGHTNESS: COLOR_MODE_BRIGHTNESS,
|
COLOR_MODE_BRIGHTNESS: [
|
||||||
LightColorMode.WHITE: COLOR_MODE_WHITE,
|
LightColorCapability.ON_OFF | LightColorCapability.BRIGHTNESS,
|
||||||
LightColorMode.COLOR_TEMPERATURE: COLOR_MODE_COLOR_TEMP,
|
# for compatibility with older clients (2021.8.x)
|
||||||
LightColorMode.COLD_WARM_WHITE: COLOR_MODE_COLOR_TEMP,
|
LightColorCapability.BRIGHTNESS,
|
||||||
LightColorMode.RGB: COLOR_MODE_RGB,
|
],
|
||||||
LightColorMode.RGB_WHITE: COLOR_MODE_RGBW,
|
COLOR_MODE_COLOR_TEMP: [
|
||||||
LightColorMode.RGB_COLOR_TEMPERATURE: COLOR_MODE_RGBWW,
|
LightColorCapability.ON_OFF
|
||||||
LightColorMode.RGB_COLD_WARM_WHITE: COLOR_MODE_RGBWW,
|
| LightColorCapability.BRIGHTNESS
|
||||||
}
|
| LightColorCapability.COLOR_TEMPERATURE,
|
||||||
)
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.COLD_WARM_WHITE,
|
||||||
|
],
|
||||||
|
COLOR_MODE_RGB: [
|
||||||
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.RGB,
|
||||||
|
],
|
||||||
|
COLOR_MODE_RGBW: [
|
||||||
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.RGB
|
||||||
|
| LightColorCapability.WHITE,
|
||||||
|
],
|
||||||
|
COLOR_MODE_RGBWW: [
|
||||||
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.RGB
|
||||||
|
| LightColorCapability.WHITE
|
||||||
|
| LightColorCapability.COLOR_TEMPERATURE,
|
||||||
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.RGB
|
||||||
|
| LightColorCapability.COLD_WARM_WHITE,
|
||||||
|
],
|
||||||
|
COLOR_MODE_WHITE: [
|
||||||
|
LightColorCapability.ON_OFF
|
||||||
|
| LightColorCapability.BRIGHTNESS
|
||||||
|
| LightColorCapability.WHITE
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _color_mode_to_ha(mode: int) -> str:
|
||||||
|
"""Convert an esphome color mode to a HA color mode constant.
|
||||||
|
|
||||||
|
Choses the color mode that best matches the feature-set.
|
||||||
|
"""
|
||||||
|
candidates = []
|
||||||
|
for ha_mode, cap_lists in _COLOR_MODE_MAPPING.items():
|
||||||
|
for caps in cap_lists:
|
||||||
|
if caps == mode:
|
||||||
|
# exact match
|
||||||
|
return ha_mode
|
||||||
|
if (mode & caps) == caps:
|
||||||
|
# all requirements met
|
||||||
|
candidates.append((ha_mode, caps))
|
||||||
|
|
||||||
|
if not candidates:
|
||||||
|
return COLOR_MODE_UNKNOWN
|
||||||
|
|
||||||
|
# choose the color mode with the most bits set
|
||||||
|
candidates.sort(key=lambda key: bin(key[1]).count("1"))
|
||||||
|
return candidates[-1][0]
|
||||||
|
|
||||||
|
|
||||||
|
def _filter_color_modes(
|
||||||
|
supported: list[int], features: LightColorCapability
|
||||||
|
) -> list[int]:
|
||||||
|
"""Filter the given supported color modes, excluding all values that don't have the requested features."""
|
||||||
|
return [mode for mode in supported if mode & features]
|
||||||
|
|
||||||
|
|
||||||
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
||||||
|
@ -95,10 +151,17 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
data: dict[str, Any] = {"key": self._static_info.key, "state": True}
|
data: dict[str, Any] = {"key": self._static_info.key, "state": True}
|
||||||
|
# The list of color modes that would fit this service call
|
||||||
|
color_modes = self._native_supported_color_modes
|
||||||
|
try_keep_current_mode = True
|
||||||
|
|
||||||
# rgb/brightness input is in range 0-255, but esphome uses 0-1
|
# rgb/brightness input is in range 0-255, but esphome uses 0-1
|
||||||
|
|
||||||
if (brightness_ha := kwargs.get(ATTR_BRIGHTNESS)) is not None:
|
if (brightness_ha := kwargs.get(ATTR_BRIGHTNESS)) is not None:
|
||||||
data["brightness"] = brightness_ha / 255
|
data["brightness"] = brightness_ha / 255
|
||||||
|
color_modes = _filter_color_modes(
|
||||||
|
color_modes, LightColorCapability.BRIGHTNESS
|
||||||
|
)
|
||||||
|
|
||||||
if (rgb_ha := kwargs.get(ATTR_RGB_COLOR)) is not None:
|
if (rgb_ha := kwargs.get(ATTR_RGB_COLOR)) is not None:
|
||||||
rgb = tuple(x / 255 for x in rgb_ha)
|
rgb = tuple(x / 255 for x in rgb_ha)
|
||||||
|
@ -106,8 +169,8 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
# normalize rgb
|
# normalize rgb
|
||||||
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
||||||
data["color_brightness"] = color_bri
|
data["color_brightness"] = color_bri
|
||||||
if self._supports_color_mode:
|
color_modes = _filter_color_modes(color_modes, LightColorCapability.RGB)
|
||||||
data["color_mode"] = LightColorMode.RGB
|
try_keep_current_mode = False
|
||||||
|
|
||||||
if (rgbw_ha := kwargs.get(ATTR_RGBW_COLOR)) is not None:
|
if (rgbw_ha := kwargs.get(ATTR_RGBW_COLOR)) is not None:
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
@ -117,8 +180,10 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
||||||
data["white"] = w
|
data["white"] = w
|
||||||
data["color_brightness"] = color_bri
|
data["color_brightness"] = color_bri
|
||||||
if self._supports_color_mode:
|
color_modes = _filter_color_modes(
|
||||||
data["color_mode"] = LightColorMode.RGB_WHITE
|
color_modes, LightColorCapability.RGB | LightColorCapability.WHITE
|
||||||
|
)
|
||||||
|
try_keep_current_mode = False
|
||||||
|
|
||||||
if (rgbww_ha := kwargs.get(ATTR_RGBWW_COLOR)) is not None:
|
if (rgbww_ha := kwargs.get(ATTR_RGBWW_COLOR)) is not None:
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
@ -126,14 +191,14 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
color_bri = max(rgb)
|
color_bri = max(rgb)
|
||||||
# normalize rgb
|
# normalize rgb
|
||||||
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
data["rgb"] = tuple(x / (color_bri or 1) for x in rgb)
|
||||||
modes = self._native_supported_color_modes
|
color_modes = _filter_color_modes(color_modes, LightColorCapability.RGB)
|
||||||
if (
|
if _filter_color_modes(color_modes, LightColorCapability.COLD_WARM_WHITE):
|
||||||
self._supports_color_mode
|
# Device supports setting cwww values directly
|
||||||
and LightColorMode.RGB_COLD_WARM_WHITE in modes
|
|
||||||
):
|
|
||||||
data["cold_white"] = cw
|
data["cold_white"] = cw
|
||||||
data["warm_white"] = ww
|
data["warm_white"] = ww
|
||||||
target_mode = LightColorMode.RGB_COLD_WARM_WHITE
|
color_modes = _filter_color_modes(
|
||||||
|
color_modes, LightColorCapability.COLD_WARM_WHITE
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# need to convert cw+ww part to white+color_temp
|
# need to convert cw+ww part to white+color_temp
|
||||||
white = data["white"] = max(cw, ww)
|
white = data["white"] = max(cw, ww)
|
||||||
|
@ -142,11 +207,13 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
max_ct = self.max_mireds
|
max_ct = self.max_mireds
|
||||||
ct_ratio = ww / (cw + ww)
|
ct_ratio = ww / (cw + ww)
|
||||||
data["color_temperature"] = min_ct + ct_ratio * (max_ct - min_ct)
|
data["color_temperature"] = min_ct + ct_ratio * (max_ct - min_ct)
|
||||||
target_mode = LightColorMode.RGB_COLOR_TEMPERATURE
|
color_modes = _filter_color_modes(
|
||||||
|
color_modes,
|
||||||
|
LightColorCapability.COLOR_TEMPERATURE | LightColorCapability.WHITE,
|
||||||
|
)
|
||||||
|
try_keep_current_mode = False
|
||||||
|
|
||||||
data["color_brightness"] = color_bri
|
data["color_brightness"] = color_bri
|
||||||
if self._supports_color_mode:
|
|
||||||
data["color_mode"] = target_mode
|
|
||||||
|
|
||||||
if (flash := kwargs.get(ATTR_FLASH)) is not None:
|
if (flash := kwargs.get(ATTR_FLASH)) is not None:
|
||||||
data["flash_length"] = FLASH_LENGTHS[flash]
|
data["flash_length"] = FLASH_LENGTHS[flash]
|
||||||
|
@ -156,12 +223,15 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
|
|
||||||
if (color_temp := kwargs.get(ATTR_COLOR_TEMP)) is not None:
|
if (color_temp := kwargs.get(ATTR_COLOR_TEMP)) is not None:
|
||||||
data["color_temperature"] = color_temp
|
data["color_temperature"] = color_temp
|
||||||
if self._supports_color_mode:
|
if _filter_color_modes(color_modes, LightColorCapability.COLOR_TEMPERATURE):
|
||||||
supported_modes = self._native_supported_color_modes
|
color_modes = _filter_color_modes(
|
||||||
if LightColorMode.COLOR_TEMPERATURE in supported_modes:
|
color_modes, LightColorCapability.COLOR_TEMPERATURE
|
||||||
data["color_mode"] = LightColorMode.COLOR_TEMPERATURE
|
)
|
||||||
elif LightColorMode.COLD_WARM_WHITE in supported_modes:
|
else:
|
||||||
data["color_mode"] = LightColorMode.COLD_WARM_WHITE
|
color_modes = _filter_color_modes(
|
||||||
|
color_modes, LightColorCapability.COLD_WARM_WHITE
|
||||||
|
)
|
||||||
|
try_keep_current_mode = False
|
||||||
|
|
||||||
if (effect := kwargs.get(ATTR_EFFECT)) is not None:
|
if (effect := kwargs.get(ATTR_EFFECT)) is not None:
|
||||||
data["effect"] = effect
|
data["effect"] = effect
|
||||||
|
@ -171,7 +241,30 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
# HA only sends `white` in turn_on, and reads total brightness through brightness property
|
# HA only sends `white` in turn_on, and reads total brightness through brightness property
|
||||||
data["brightness"] = white_ha / 255
|
data["brightness"] = white_ha / 255
|
||||||
data["white"] = 1.0
|
data["white"] = 1.0
|
||||||
data["color_mode"] = LightColorMode.WHITE
|
color_modes = _filter_color_modes(
|
||||||
|
color_modes,
|
||||||
|
LightColorCapability.BRIGHTNESS | LightColorCapability.WHITE,
|
||||||
|
)
|
||||||
|
try_keep_current_mode = False
|
||||||
|
|
||||||
|
if self._supports_color_mode and color_modes:
|
||||||
|
# try the color mode with the least complexity (fewest capabilities set)
|
||||||
|
# popcount with bin() function because it appears to be the best way: https://stackoverflow.com/a/9831671
|
||||||
|
color_modes.sort(key=lambda mode: bin(mode).count("1"))
|
||||||
|
data["color_mode"] = color_modes[0]
|
||||||
|
if self._supports_color_mode and color_modes:
|
||||||
|
if (
|
||||||
|
try_keep_current_mode
|
||||||
|
and self._state is not None
|
||||||
|
and self._state.color_mode in color_modes
|
||||||
|
):
|
||||||
|
# if possible, stay with the color mode that is already set
|
||||||
|
data["color_mode"] = self._state.color_mode
|
||||||
|
else:
|
||||||
|
# otherwise try the color mode with the least complexity (fewest capabilities set)
|
||||||
|
# popcount with bin() function because it appears to be the best way: https://stackoverflow.com/a/9831671
|
||||||
|
color_modes.sort(key=lambda mode: bin(mode).count("1"))
|
||||||
|
data["color_mode"] = color_modes[0]
|
||||||
|
|
||||||
await self._client.light_command(**data)
|
await self._client.light_command(**data)
|
||||||
|
|
||||||
|
@ -198,7 +291,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
return None
|
return None
|
||||||
return next(iter(supported))
|
return next(iter(supported))
|
||||||
|
|
||||||
return _COLOR_MODES.from_esphome(self._state.color_mode)
|
return _color_mode_to_ha(self._state.color_mode)
|
||||||
|
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
def rgb_color(self) -> tuple[int, int, int] | None:
|
def rgb_color(self) -> tuple[int, int, int] | None:
|
||||||
|
@ -227,9 +320,8 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
def rgbww_color(self) -> tuple[int, int, int, int, int] | None:
|
def rgbww_color(self) -> tuple[int, int, int, int, int] | None:
|
||||||
"""Return the rgbww color value [int, int, int, int, int]."""
|
"""Return the rgbww color value [int, int, int, int, int]."""
|
||||||
rgb = cast("tuple[int, int, int]", self.rgb_color)
|
rgb = cast("tuple[int, int, int]", self.rgb_color)
|
||||||
if (
|
if not _filter_color_modes(
|
||||||
not self._supports_color_mode
|
self._native_supported_color_modes, LightColorCapability.COLD_WARM_WHITE
|
||||||
or self._state.color_mode != LightColorMode.RGB_COLD_WARM_WHITE
|
|
||||||
):
|
):
|
||||||
# Try to reverse white + color temp to cwww
|
# Try to reverse white + color temp to cwww
|
||||||
min_ct = self._static_info.min_mireds
|
min_ct = self._static_info.min_mireds
|
||||||
|
@ -262,7 +354,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
return self._state.effect
|
return self._state.effect
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _native_supported_color_modes(self) -> list[LightColorMode]:
|
def _native_supported_color_modes(self) -> list[int]:
|
||||||
return self._static_info.supported_color_modes_compat(self._api_version)
|
return self._static_info.supported_color_modes_compat(self._api_version)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -272,7 +364,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
|
|
||||||
# All color modes except UNKNOWN,ON_OFF support transition
|
# All color modes except UNKNOWN,ON_OFF support transition
|
||||||
modes = self._native_supported_color_modes
|
modes = self._native_supported_color_modes
|
||||||
if any(m not in (LightColorMode.UNKNOWN, LightColorMode.ON_OFF) for m in modes):
|
if any(m not in (0, LightColorCapability.ON_OFF) for m in modes):
|
||||||
flags |= SUPPORT_TRANSITION
|
flags |= SUPPORT_TRANSITION
|
||||||
if self._static_info.effects:
|
if self._static_info.effects:
|
||||||
flags |= SUPPORT_EFFECT
|
flags |= SUPPORT_EFFECT
|
||||||
|
@ -281,7 +373,14 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
|
||||||
@property
|
@property
|
||||||
def supported_color_modes(self) -> set[str] | None:
|
def supported_color_modes(self) -> set[str] | None:
|
||||||
"""Flag supported color modes."""
|
"""Flag supported color modes."""
|
||||||
return set(map(_COLOR_MODES.from_esphome, self._native_supported_color_modes))
|
supported = set(map(_color_mode_to_ha, self._native_supported_color_modes))
|
||||||
|
if COLOR_MODE_ONOFF in supported and len(supported) > 1:
|
||||||
|
supported.remove(COLOR_MODE_ONOFF)
|
||||||
|
if COLOR_MODE_BRIGHTNESS in supported and len(supported) > 1:
|
||||||
|
supported.remove(COLOR_MODE_BRIGHTNESS)
|
||||||
|
if COLOR_MODE_WHITE in supported and len(supported) == 1:
|
||||||
|
supported.remove(COLOR_MODE_WHITE)
|
||||||
|
return supported
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def effect_list(self) -> list[str]:
|
def effect_list(self) -> list[str]:
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"name": "ESPHome",
|
"name": "ESPHome",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/esphome",
|
"documentation": "https://www.home-assistant.io/integrations/esphome",
|
||||||
"requirements": ["aioesphomeapi==7.0.0"],
|
"requirements": ["aioesphomeapi==8.0.0"],
|
||||||
"zeroconf": ["_esphomelib._tcp.local."],
|
"zeroconf": ["_esphomelib._tcp.local."],
|
||||||
"codeowners": ["@OttoWinter", "@jesserockz"],
|
"codeowners": ["@OttoWinter", "@jesserockz"],
|
||||||
"after_dependencies": ["zeroconf", "tag"],
|
"after_dependencies": ["zeroconf", "tag"],
|
||||||
|
|
|
@ -164,7 +164,7 @@ aioeagle==1.1.0
|
||||||
aioemonitor==1.0.5
|
aioemonitor==1.0.5
|
||||||
|
|
||||||
# homeassistant.components.esphome
|
# homeassistant.components.esphome
|
||||||
aioesphomeapi==7.0.0
|
aioesphomeapi==8.0.0
|
||||||
|
|
||||||
# homeassistant.components.flo
|
# homeassistant.components.flo
|
||||||
aioflo==0.4.1
|
aioflo==0.4.1
|
||||||
|
|
|
@ -106,7 +106,7 @@ aioeagle==1.1.0
|
||||||
aioemonitor==1.0.5
|
aioemonitor==1.0.5
|
||||||
|
|
||||||
# homeassistant.components.esphome
|
# homeassistant.components.esphome
|
||||||
aioesphomeapi==7.0.0
|
aioesphomeapi==8.0.0
|
||||||
|
|
||||||
# homeassistant.components.flo
|
# homeassistant.components.flo
|
||||||
aioflo==0.4.1
|
aioflo==0.4.1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue