Use ColorMode enum in esphome (#70490)

pull/70543/head
epenet 2022-04-23 21:13:40 +02:00 committed by GitHub
parent a0c7fca003
commit 31fbc088fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 22 deletions

View File

@ -15,16 +15,9 @@ from homeassistant.components.light import (
ATTR_RGBWW_COLOR,
ATTR_TRANSITION,
ATTR_WHITE,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF,
COLOR_MODE_RGB,
COLOR_MODE_RGBW,
COLOR_MODE_RGBWW,
COLOR_MODE_UNKNOWN,
COLOR_MODE_WHITE,
FLASH_LONG,
FLASH_SHORT,
ColorMode,
LightEntity,
LightEntityFeature,
)
@ -53,15 +46,15 @@ async def async_setup_entry(
_COLOR_MODE_MAPPING = {
COLOR_MODE_ONOFF: [
ColorMode.ONOFF: [
LightColorCapability.ON_OFF,
],
COLOR_MODE_BRIGHTNESS: [
ColorMode.BRIGHTNESS: [
LightColorCapability.ON_OFF | LightColorCapability.BRIGHTNESS,
# for compatibility with older clients (2021.8.x)
LightColorCapability.BRIGHTNESS,
],
COLOR_MODE_COLOR_TEMP: [
ColorMode.COLOR_TEMP: [
LightColorCapability.ON_OFF
| LightColorCapability.BRIGHTNESS
| LightColorCapability.COLOR_TEMPERATURE,
@ -69,18 +62,18 @@ _COLOR_MODE_MAPPING = {
| LightColorCapability.BRIGHTNESS
| LightColorCapability.COLD_WARM_WHITE,
],
COLOR_MODE_RGB: [
ColorMode.RGB: [
LightColorCapability.ON_OFF
| LightColorCapability.BRIGHTNESS
| LightColorCapability.RGB,
],
COLOR_MODE_RGBW: [
ColorMode.RGBW: [
LightColorCapability.ON_OFF
| LightColorCapability.BRIGHTNESS
| LightColorCapability.RGB
| LightColorCapability.WHITE,
],
COLOR_MODE_RGBWW: [
ColorMode.RGBWW: [
LightColorCapability.ON_OFF
| LightColorCapability.BRIGHTNESS
| LightColorCapability.RGB
@ -91,7 +84,7 @@ _COLOR_MODE_MAPPING = {
| LightColorCapability.RGB
| LightColorCapability.COLD_WARM_WHITE,
],
COLOR_MODE_WHITE: [
ColorMode.WHITE: [
LightColorCapability.ON_OFF
| LightColorCapability.BRIGHTNESS
| LightColorCapability.WHITE
@ -115,7 +108,7 @@ def _color_mode_to_ha(mode: int) -> str:
candidates.append((ha_mode, caps))
if not candidates:
return COLOR_MODE_UNKNOWN
return ColorMode.UNKNOWN
# choose the color mode with the most bits set
candidates.sort(key=lambda key: bin(key[1]).count("1"))
@ -366,12 +359,12 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
def supported_color_modes(self) -> set[str] | None:
"""Flag 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)
if ColorMode.ONOFF in supported and len(supported) > 1:
supported.remove(ColorMode.ONOFF)
if ColorMode.BRIGHTNESS in supported and len(supported) > 1:
supported.remove(ColorMode.BRIGHTNESS)
if ColorMode.WHITE in supported and len(supported) == 1:
supported.remove(ColorMode.WHITE)
return supported
@property