Use ColorMode enum in mqtt (#70579)
parent
472ffd3bc6
commit
38d86fc7b1
|
@ -19,22 +19,13 @@ from homeassistant.components.light import (
|
||||||
ATTR_WHITE,
|
ATTR_WHITE,
|
||||||
ATTR_WHITE_VALUE,
|
ATTR_WHITE_VALUE,
|
||||||
ATTR_XY_COLOR,
|
ATTR_XY_COLOR,
|
||||||
COLOR_MODE_BRIGHTNESS,
|
|
||||||
COLOR_MODE_COLOR_TEMP,
|
|
||||||
COLOR_MODE_HS,
|
|
||||||
COLOR_MODE_ONOFF,
|
|
||||||
COLOR_MODE_RGB,
|
|
||||||
COLOR_MODE_RGBW,
|
|
||||||
COLOR_MODE_RGBWW,
|
|
||||||
COLOR_MODE_UNKNOWN,
|
|
||||||
COLOR_MODE_WHITE,
|
|
||||||
COLOR_MODE_XY,
|
|
||||||
ENTITY_ID_FORMAT,
|
ENTITY_ID_FORMAT,
|
||||||
SUPPORT_BRIGHTNESS,
|
SUPPORT_BRIGHTNESS,
|
||||||
SUPPORT_COLOR,
|
SUPPORT_COLOR,
|
||||||
SUPPORT_COLOR_TEMP,
|
SUPPORT_COLOR_TEMP,
|
||||||
SUPPORT_EFFECT,
|
SUPPORT_EFFECT,
|
||||||
SUPPORT_WHITE_VALUE,
|
SUPPORT_WHITE_VALUE,
|
||||||
|
ColorMode,
|
||||||
LightEntity,
|
LightEntity,
|
||||||
valid_supported_color_modes,
|
valid_supported_color_modes,
|
||||||
)
|
)
|
||||||
|
@ -384,35 +375,35 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
self._optimistic_xy_color = optimistic or topic[CONF_XY_STATE_TOPIC] is None
|
self._optimistic_xy_color = optimistic or topic[CONF_XY_STATE_TOPIC] is None
|
||||||
supported_color_modes = set()
|
supported_color_modes = set()
|
||||||
if topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
if topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
|
supported_color_modes.add(ColorMode.COLOR_TEMP)
|
||||||
self._color_mode = COLOR_MODE_COLOR_TEMP
|
self._color_mode = ColorMode.COLOR_TEMP
|
||||||
if topic[CONF_HS_COMMAND_TOPIC] is not None:
|
if topic[CONF_HS_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_HS)
|
supported_color_modes.add(ColorMode.HS)
|
||||||
self._color_mode = COLOR_MODE_HS
|
self._color_mode = ColorMode.HS
|
||||||
if topic[CONF_RGB_COMMAND_TOPIC] is not None:
|
if topic[CONF_RGB_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_RGB)
|
supported_color_modes.add(ColorMode.RGB)
|
||||||
self._color_mode = COLOR_MODE_RGB
|
self._color_mode = ColorMode.RGB
|
||||||
if topic[CONF_RGBW_COMMAND_TOPIC] is not None:
|
if topic[CONF_RGBW_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_RGBW)
|
supported_color_modes.add(ColorMode.RGBW)
|
||||||
self._color_mode = COLOR_MODE_RGBW
|
self._color_mode = ColorMode.RGBW
|
||||||
if topic[CONF_RGBWW_COMMAND_TOPIC] is not None:
|
if topic[CONF_RGBWW_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_RGBWW)
|
supported_color_modes.add(ColorMode.RGBWW)
|
||||||
self._color_mode = COLOR_MODE_RGBWW
|
self._color_mode = ColorMode.RGBWW
|
||||||
if topic[CONF_WHITE_COMMAND_TOPIC] is not None:
|
if topic[CONF_WHITE_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_WHITE)
|
supported_color_modes.add(ColorMode.WHITE)
|
||||||
if topic[CONF_XY_COMMAND_TOPIC] is not None:
|
if topic[CONF_XY_COMMAND_TOPIC] is not None:
|
||||||
supported_color_modes.add(COLOR_MODE_XY)
|
supported_color_modes.add(ColorMode.XY)
|
||||||
self._color_mode = COLOR_MODE_XY
|
self._color_mode = ColorMode.XY
|
||||||
if len(supported_color_modes) > 1:
|
if len(supported_color_modes) > 1:
|
||||||
self._color_mode = COLOR_MODE_UNKNOWN
|
self._color_mode = ColorMode.UNKNOWN
|
||||||
|
|
||||||
if not supported_color_modes:
|
if not supported_color_modes:
|
||||||
if topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
|
if topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
|
||||||
self._color_mode = COLOR_MODE_BRIGHTNESS
|
self._color_mode = ColorMode.BRIGHTNESS
|
||||||
supported_color_modes.add(COLOR_MODE_BRIGHTNESS)
|
supported_color_modes.add(ColorMode.BRIGHTNESS)
|
||||||
else:
|
else:
|
||||||
self._color_mode = COLOR_MODE_ONOFF
|
self._color_mode = ColorMode.ONOFF
|
||||||
supported_color_modes.add(COLOR_MODE_ONOFF)
|
supported_color_modes.add(ColorMode.ONOFF)
|
||||||
|
|
||||||
# Validate the color_modes configuration
|
# Validate the color_modes configuration
|
||||||
self._supported_color_modes = valid_supported_color_modes(supported_color_modes)
|
self._supported_color_modes = valid_supported_color_modes(supported_color_modes)
|
||||||
|
@ -503,7 +494,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
def rgb_received(msg):
|
def rgb_received(msg):
|
||||||
"""Handle new MQTT messages for RGB."""
|
"""Handle new MQTT messages for RGB."""
|
||||||
rgb = _rgbx_received(
|
rgb = _rgbx_received(
|
||||||
msg, CONF_RGB_VALUE_TEMPLATE, COLOR_MODE_RGB, lambda *x: x
|
msg, CONF_RGB_VALUE_TEMPLATE, ColorMode.RGB, lambda *x: x
|
||||||
)
|
)
|
||||||
if not rgb:
|
if not rgb:
|
||||||
return
|
return
|
||||||
|
@ -522,7 +513,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
rgbw = _rgbx_received(
|
rgbw = _rgbx_received(
|
||||||
msg,
|
msg,
|
||||||
CONF_RGBW_VALUE_TEMPLATE,
|
CONF_RGBW_VALUE_TEMPLATE,
|
||||||
COLOR_MODE_RGBW,
|
ColorMode.RGBW,
|
||||||
color_util.color_rgbw_to_rgb,
|
color_util.color_rgbw_to_rgb,
|
||||||
)
|
)
|
||||||
if not rgbw:
|
if not rgbw:
|
||||||
|
@ -539,7 +530,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
rgbww = _rgbx_received(
|
rgbww = _rgbx_received(
|
||||||
msg,
|
msg,
|
||||||
CONF_RGBWW_VALUE_TEMPLATE,
|
CONF_RGBWW_VALUE_TEMPLATE,
|
||||||
COLOR_MODE_RGBWW,
|
ColorMode.RGBWW,
|
||||||
color_util.color_rgbww_to_rgb,
|
color_util.color_rgbww_to_rgb,
|
||||||
)
|
)
|
||||||
if not rgbww:
|
if not rgbww:
|
||||||
|
@ -577,7 +568,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._optimistic_color_mode:
|
if self._optimistic_color_mode:
|
||||||
self._color_mode = COLOR_MODE_COLOR_TEMP
|
self._color_mode = ColorMode.COLOR_TEMP
|
||||||
self._color_temp = int(payload)
|
self._color_temp = int(payload)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@ -610,7 +601,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
try:
|
try:
|
||||||
hs_color = tuple(float(val) for val in payload.split(",", 2))
|
hs_color = tuple(float(val) for val in payload.split(",", 2))
|
||||||
if self._optimistic_color_mode:
|
if self._optimistic_color_mode:
|
||||||
self._color_mode = COLOR_MODE_HS
|
self._color_mode = ColorMode.HS
|
||||||
self._hs_color = hs_color
|
self._hs_color = hs_color
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -647,7 +638,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
|
|
||||||
xy_color = tuple(float(val) for val in payload.split(","))
|
xy_color = tuple(float(val) for val in payload.split(","))
|
||||||
if self._optimistic_color_mode:
|
if self._optimistic_color_mode:
|
||||||
self._color_mode = COLOR_MODE_XY
|
self._color_mode = ColorMode.XY
|
||||||
if self._legacy_mode:
|
if self._legacy_mode:
|
||||||
self._hs_color = color_util.color_xy_to_hs(*xy_color)
|
self._hs_color = color_util.color_xy_to_hs(*xy_color)
|
||||||
else:
|
else:
|
||||||
|
@ -863,9 +854,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
"""Render RGBx payload."""
|
"""Render RGBx payload."""
|
||||||
if tpl := self._command_templates[template]:
|
if tpl := self._command_templates[template]:
|
||||||
keys = ["red", "green", "blue"]
|
keys = ["red", "green", "blue"]
|
||||||
if color_mode == COLOR_MODE_RGBW:
|
if color_mode == ColorMode.RGBW:
|
||||||
keys.append("white")
|
keys.append("white")
|
||||||
elif color_mode == COLOR_MODE_RGBWW:
|
elif color_mode == ColorMode.RGBWW:
|
||||||
keys.extend(["cold_white", "warm_white"])
|
keys.extend(["cold_white", "warm_white"])
|
||||||
rgb_color_str = tpl(variables=zip(keys, color))
|
rgb_color_str = tpl(variables=zip(keys, color))
|
||||||
else:
|
else:
|
||||||
|
@ -905,7 +896,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
):
|
):
|
||||||
# Legacy mode: Convert HS to RGB
|
# Legacy mode: Convert HS to RGB
|
||||||
rgb = scale_rgbx(color_util.color_hsv_to_RGB(*hs_color, 100))
|
rgb = scale_rgbx(color_util.color_hsv_to_RGB(*hs_color, 100))
|
||||||
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, COLOR_MODE_RGB)
|
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, ColorMode.RGB)
|
||||||
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
||||||
should_update |= set_optimistic(
|
should_update |= set_optimistic(
|
||||||
ATTR_HS_COLOR, hs_color, condition_attribute=ATTR_RGB_COLOR
|
ATTR_HS_COLOR, hs_color, condition_attribute=ATTR_RGB_COLOR
|
||||||
|
@ -913,7 +904,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
|
|
||||||
if hs_color and self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
if hs_color and self._topic[CONF_HS_COMMAND_TOPIC] is not None:
|
||||||
await publish(CONF_HS_COMMAND_TOPIC, f"{hs_color[0]},{hs_color[1]}")
|
await publish(CONF_HS_COMMAND_TOPIC, f"{hs_color[0]},{hs_color[1]}")
|
||||||
should_update |= set_optimistic(ATTR_HS_COLOR, hs_color, COLOR_MODE_HS)
|
should_update |= set_optimistic(ATTR_HS_COLOR, hs_color, ColorMode.HS)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
hs_color
|
hs_color
|
||||||
|
@ -933,9 +924,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
and not self._legacy_mode
|
and not self._legacy_mode
|
||||||
):
|
):
|
||||||
scaled = scale_rgbx(rgb)
|
scaled = scale_rgbx(rgb)
|
||||||
rgb_s = render_rgbx(scaled, CONF_RGB_COMMAND_TEMPLATE, COLOR_MODE_RGB)
|
rgb_s = render_rgbx(scaled, CONF_RGB_COMMAND_TEMPLATE, ColorMode.RGB)
|
||||||
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
||||||
should_update |= set_optimistic(ATTR_RGB_COLOR, rgb, COLOR_MODE_RGB)
|
should_update |= set_optimistic(ATTR_RGB_COLOR, rgb, ColorMode.RGB)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(rgbw := kwargs.get(ATTR_RGBW_COLOR))
|
(rgbw := kwargs.get(ATTR_RGBW_COLOR))
|
||||||
|
@ -943,9 +934,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
and not self._legacy_mode
|
and not self._legacy_mode
|
||||||
):
|
):
|
||||||
scaled = scale_rgbx(rgbw)
|
scaled = scale_rgbx(rgbw)
|
||||||
rgbw_s = render_rgbx(scaled, CONF_RGBW_COMMAND_TEMPLATE, COLOR_MODE_RGBW)
|
rgbw_s = render_rgbx(scaled, CONF_RGBW_COMMAND_TEMPLATE, ColorMode.RGBW)
|
||||||
await publish(CONF_RGBW_COMMAND_TOPIC, rgbw_s)
|
await publish(CONF_RGBW_COMMAND_TOPIC, rgbw_s)
|
||||||
should_update |= set_optimistic(ATTR_RGBW_COLOR, rgbw, COLOR_MODE_RGBW)
|
should_update |= set_optimistic(ATTR_RGBW_COLOR, rgbw, ColorMode.RGBW)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(rgbww := kwargs.get(ATTR_RGBWW_COLOR))
|
(rgbww := kwargs.get(ATTR_RGBWW_COLOR))
|
||||||
|
@ -953,9 +944,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
and not self._legacy_mode
|
and not self._legacy_mode
|
||||||
):
|
):
|
||||||
scaled = scale_rgbx(rgbww)
|
scaled = scale_rgbx(rgbww)
|
||||||
rgbww_s = render_rgbx(scaled, CONF_RGBWW_COMMAND_TEMPLATE, COLOR_MODE_RGBWW)
|
rgbww_s = render_rgbx(scaled, CONF_RGBWW_COMMAND_TEMPLATE, ColorMode.RGBWW)
|
||||||
await publish(CONF_RGBWW_COMMAND_TOPIC, rgbww_s)
|
await publish(CONF_RGBWW_COMMAND_TOPIC, rgbww_s)
|
||||||
should_update |= set_optimistic(ATTR_RGBWW_COLOR, rgbww, COLOR_MODE_RGBWW)
|
should_update |= set_optimistic(ATTR_RGBWW_COLOR, rgbww, ColorMode.RGBWW)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(xy_color := kwargs.get(ATTR_XY_COLOR))
|
(xy_color := kwargs.get(ATTR_XY_COLOR))
|
||||||
|
@ -963,7 +954,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
and not self._legacy_mode
|
and not self._legacy_mode
|
||||||
):
|
):
|
||||||
await publish(CONF_XY_COMMAND_TOPIC, f"{xy_color[0]},{xy_color[1]}")
|
await publish(CONF_XY_COMMAND_TOPIC, f"{xy_color[0]},{xy_color[1]}")
|
||||||
should_update |= set_optimistic(ATTR_XY_COLOR, xy_color, COLOR_MODE_XY)
|
should_update |= set_optimistic(ATTR_XY_COLOR, xy_color, ColorMode.XY)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
ATTR_BRIGHTNESS in kwargs
|
ATTR_BRIGHTNESS in kwargs
|
||||||
|
@ -990,7 +981,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
hs_color = self._hs_color if self._hs_color is not None else (0, 0)
|
hs_color = self._hs_color if self._hs_color is not None else (0, 0)
|
||||||
brightness = kwargs[ATTR_BRIGHTNESS]
|
brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
rgb = scale_rgbx(color_util.color_hsv_to_RGB(*hs_color, 100), brightness)
|
rgb = scale_rgbx(color_util.color_hsv_to_RGB(*hs_color, 100), brightness)
|
||||||
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, COLOR_MODE_RGB)
|
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, ColorMode.RGB)
|
||||||
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
||||||
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
||||||
elif (
|
elif (
|
||||||
|
@ -1001,7 +992,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
):
|
):
|
||||||
rgb_color = self._rgb_color if self._rgb_color is not None else (255,) * 3
|
rgb_color = self._rgb_color if self._rgb_color is not None else (255,) * 3
|
||||||
rgb = scale_rgbx(rgb_color, kwargs[ATTR_BRIGHTNESS])
|
rgb = scale_rgbx(rgb_color, kwargs[ATTR_BRIGHTNESS])
|
||||||
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, COLOR_MODE_RGB)
|
rgb_s = render_rgbx(rgb, CONF_RGB_COMMAND_TEMPLATE, ColorMode.RGB)
|
||||||
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
await publish(CONF_RGB_COMMAND_TOPIC, rgb_s)
|
||||||
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
||||||
elif (
|
elif (
|
||||||
|
@ -1014,7 +1005,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
self._rgbw_color if self._rgbw_color is not None else (255,) * 4
|
self._rgbw_color if self._rgbw_color is not None else (255,) * 4
|
||||||
)
|
)
|
||||||
rgbw = scale_rgbx(rgbw_color, kwargs[ATTR_BRIGHTNESS])
|
rgbw = scale_rgbx(rgbw_color, kwargs[ATTR_BRIGHTNESS])
|
||||||
rgbw_s = render_rgbx(rgbw, CONF_RGBW_COMMAND_TEMPLATE, COLOR_MODE_RGBW)
|
rgbw_s = render_rgbx(rgbw, CONF_RGBW_COMMAND_TEMPLATE, ColorMode.RGBW)
|
||||||
await publish(CONF_RGBW_COMMAND_TOPIC, rgbw_s)
|
await publish(CONF_RGBW_COMMAND_TOPIC, rgbw_s)
|
||||||
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
||||||
elif (
|
elif (
|
||||||
|
@ -1027,7 +1018,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
self._rgbww_color if self._rgbww_color is not None else (255,) * 5
|
self._rgbww_color if self._rgbww_color is not None else (255,) * 5
|
||||||
)
|
)
|
||||||
rgbww = scale_rgbx(rgbww_color, kwargs[ATTR_BRIGHTNESS])
|
rgbww = scale_rgbx(rgbww_color, kwargs[ATTR_BRIGHTNESS])
|
||||||
rgbww_s = render_rgbx(rgbww, CONF_RGBWW_COMMAND_TEMPLATE, COLOR_MODE_RGBWW)
|
rgbww_s = render_rgbx(rgbww, CONF_RGBWW_COMMAND_TEMPLATE, ColorMode.RGBWW)
|
||||||
await publish(CONF_RGBWW_COMMAND_TOPIC, rgbww_s)
|
await publish(CONF_RGBWW_COMMAND_TOPIC, rgbww_s)
|
||||||
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
should_update |= set_optimistic(ATTR_BRIGHTNESS, kwargs[ATTR_BRIGHTNESS])
|
||||||
if (
|
if (
|
||||||
|
@ -1040,7 +1031,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
|
|
||||||
await publish(CONF_COLOR_TEMP_COMMAND_TOPIC, color_temp)
|
await publish(CONF_COLOR_TEMP_COMMAND_TOPIC, color_temp)
|
||||||
should_update |= set_optimistic(
|
should_update |= set_optimistic(
|
||||||
ATTR_COLOR_TEMP, kwargs[ATTR_COLOR_TEMP], COLOR_MODE_COLOR_TEMP
|
ATTR_COLOR_TEMP, kwargs[ATTR_COLOR_TEMP], ColorMode.COLOR_TEMP
|
||||||
)
|
)
|
||||||
|
|
||||||
if ATTR_EFFECT in kwargs and self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
if ATTR_EFFECT in kwargs and self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
|
||||||
|
@ -1059,7 +1050,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
should_update |= set_optimistic(
|
should_update |= set_optimistic(
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
kwargs[ATTR_WHITE],
|
kwargs[ATTR_WHITE],
|
||||||
COLOR_MODE_WHITE,
|
ColorMode.WHITE,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -18,12 +18,6 @@ from homeassistant.components.light import (
|
||||||
ATTR_TRANSITION,
|
ATTR_TRANSITION,
|
||||||
ATTR_WHITE_VALUE,
|
ATTR_WHITE_VALUE,
|
||||||
ATTR_XY_COLOR,
|
ATTR_XY_COLOR,
|
||||||
COLOR_MODE_COLOR_TEMP,
|
|
||||||
COLOR_MODE_HS,
|
|
||||||
COLOR_MODE_RGB,
|
|
||||||
COLOR_MODE_RGBW,
|
|
||||||
COLOR_MODE_RGBWW,
|
|
||||||
COLOR_MODE_XY,
|
|
||||||
ENTITY_ID_FORMAT,
|
ENTITY_ID_FORMAT,
|
||||||
FLASH_LONG,
|
FLASH_LONG,
|
||||||
FLASH_SHORT,
|
FLASH_SHORT,
|
||||||
|
@ -35,6 +29,7 @@ from homeassistant.components.light import (
|
||||||
SUPPORT_TRANSITION,
|
SUPPORT_TRANSITION,
|
||||||
SUPPORT_WHITE_VALUE,
|
SUPPORT_WHITE_VALUE,
|
||||||
VALID_COLOR_MODES,
|
VALID_COLOR_MODES,
|
||||||
|
ColorMode,
|
||||||
LightEntity,
|
LightEntity,
|
||||||
legacy_supported_features,
|
legacy_supported_features,
|
||||||
valid_supported_color_modes,
|
valid_supported_color_modes,
|
||||||
|
@ -267,39 +262,39 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
||||||
_LOGGER.warning("Invalid color mode received")
|
_LOGGER.warning("Invalid color mode received")
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
if color_mode == COLOR_MODE_COLOR_TEMP:
|
if color_mode == ColorMode.COLOR_TEMP:
|
||||||
self._color_temp = int(values["color_temp"])
|
self._color_temp = int(values["color_temp"])
|
||||||
self._color_mode = COLOR_MODE_COLOR_TEMP
|
self._color_mode = ColorMode.COLOR_TEMP
|
||||||
elif color_mode == COLOR_MODE_HS:
|
elif color_mode == ColorMode.HS:
|
||||||
hue = float(values["color"]["h"])
|
hue = float(values["color"]["h"])
|
||||||
saturation = float(values["color"]["s"])
|
saturation = float(values["color"]["s"])
|
||||||
self._color_mode = COLOR_MODE_HS
|
self._color_mode = ColorMode.HS
|
||||||
self._hs = (hue, saturation)
|
self._hs = (hue, saturation)
|
||||||
elif color_mode == COLOR_MODE_RGB:
|
elif color_mode == ColorMode.RGB:
|
||||||
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
||||||
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
||||||
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
||||||
self._color_mode = COLOR_MODE_RGB
|
self._color_mode = ColorMode.RGB
|
||||||
self._rgb = (r, g, b)
|
self._rgb = (r, g, b)
|
||||||
elif color_mode == COLOR_MODE_RGBW:
|
elif color_mode == ColorMode.RGBW:
|
||||||
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
||||||
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
||||||
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
||||||
w = int(values["color"]["w"]) # pylint: disable=invalid-name
|
w = int(values["color"]["w"]) # pylint: disable=invalid-name
|
||||||
self._color_mode = COLOR_MODE_RGBW
|
self._color_mode = ColorMode.RGBW
|
||||||
self._rgbw = (r, g, b, w)
|
self._rgbw = (r, g, b, w)
|
||||||
elif color_mode == COLOR_MODE_RGBWW:
|
elif color_mode == ColorMode.RGBWW:
|
||||||
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
r = int(values["color"]["r"]) # pylint: disable=invalid-name
|
||||||
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
g = int(values["color"]["g"]) # pylint: disable=invalid-name
|
||||||
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
b = int(values["color"]["b"]) # pylint: disable=invalid-name
|
||||||
c = int(values["color"]["c"]) # pylint: disable=invalid-name
|
c = int(values["color"]["c"]) # pylint: disable=invalid-name
|
||||||
w = int(values["color"]["w"]) # pylint: disable=invalid-name
|
w = int(values["color"]["w"]) # pylint: disable=invalid-name
|
||||||
self._color_mode = COLOR_MODE_RGBWW
|
self._color_mode = ColorMode.RGBWW
|
||||||
self._rgbww = (r, g, b, c, w)
|
self._rgbww = (r, g, b, c, w)
|
||||||
elif color_mode == COLOR_MODE_XY:
|
elif color_mode == ColorMode.XY:
|
||||||
x = float(values["color"]["x"]) # pylint: disable=invalid-name
|
x = float(values["color"]["x"]) # pylint: disable=invalid-name
|
||||||
y = float(values["color"]["y"]) # pylint: disable=invalid-name
|
y = float(values["color"]["y"]) # pylint: disable=invalid-name
|
||||||
self._color_mode = COLOR_MODE_XY
|
self._color_mode = ColorMode.XY
|
||||||
self._xy = (x, y)
|
self._xy = (x, y)
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
_LOGGER.warning("Invalid or incomplete color value received")
|
_LOGGER.warning("Invalid or incomplete color value received")
|
||||||
|
@ -553,31 +548,31 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
||||||
self._hs = kwargs[ATTR_HS_COLOR]
|
self._hs = kwargs[ATTR_HS_COLOR]
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if ATTR_HS_COLOR in kwargs and self._supports_color_mode(COLOR_MODE_HS):
|
if ATTR_HS_COLOR in kwargs and self._supports_color_mode(ColorMode.HS):
|
||||||
hs_color = kwargs[ATTR_HS_COLOR]
|
hs_color = kwargs[ATTR_HS_COLOR]
|
||||||
message["color"] = {"h": hs_color[0], "s": hs_color[1]}
|
message["color"] = {"h": hs_color[0], "s": hs_color[1]}
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._color_mode = COLOR_MODE_HS
|
self._color_mode = ColorMode.HS
|
||||||
self._hs = hs_color
|
self._hs = hs_color
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if ATTR_RGB_COLOR in kwargs and self._supports_color_mode(COLOR_MODE_RGB):
|
if ATTR_RGB_COLOR in kwargs and self._supports_color_mode(ColorMode.RGB):
|
||||||
rgb = self._scale_rgbxx(kwargs[ATTR_RGB_COLOR], kwargs)
|
rgb = self._scale_rgbxx(kwargs[ATTR_RGB_COLOR], kwargs)
|
||||||
message["color"] = {"r": rgb[0], "g": rgb[1], "b": rgb[2]}
|
message["color"] = {"r": rgb[0], "g": rgb[1], "b": rgb[2]}
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._color_mode = COLOR_MODE_RGB
|
self._color_mode = ColorMode.RGB
|
||||||
self._rgb = rgb
|
self._rgb = rgb
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if ATTR_RGBW_COLOR in kwargs and self._supports_color_mode(COLOR_MODE_RGBW):
|
if ATTR_RGBW_COLOR in kwargs and self._supports_color_mode(ColorMode.RGBW):
|
||||||
rgb = self._scale_rgbxx(kwargs[ATTR_RGBW_COLOR], kwargs)
|
rgb = self._scale_rgbxx(kwargs[ATTR_RGBW_COLOR], kwargs)
|
||||||
message["color"] = {"r": rgb[0], "g": rgb[1], "b": rgb[2], "w": rgb[3]}
|
message["color"] = {"r": rgb[0], "g": rgb[1], "b": rgb[2], "w": rgb[3]}
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._color_mode = COLOR_MODE_RGBW
|
self._color_mode = ColorMode.RGBW
|
||||||
self._rgbw = rgb
|
self._rgbw = rgb
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if ATTR_RGBWW_COLOR in kwargs and self._supports_color_mode(COLOR_MODE_RGBWW):
|
if ATTR_RGBWW_COLOR in kwargs and self._supports_color_mode(ColorMode.RGBWW):
|
||||||
rgb = self._scale_rgbxx(kwargs[ATTR_RGBWW_COLOR], kwargs)
|
rgb = self._scale_rgbxx(kwargs[ATTR_RGBWW_COLOR], kwargs)
|
||||||
message["color"] = {
|
message["color"] = {
|
||||||
"r": rgb[0],
|
"r": rgb[0],
|
||||||
|
@ -587,15 +582,15 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
||||||
"w": rgb[4],
|
"w": rgb[4],
|
||||||
}
|
}
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._color_mode = COLOR_MODE_RGBWW
|
self._color_mode = ColorMode.RGBWW
|
||||||
self._rgbww = rgb
|
self._rgbww = rgb
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
if ATTR_XY_COLOR in kwargs and self._supports_color_mode(COLOR_MODE_XY):
|
if ATTR_XY_COLOR in kwargs and self._supports_color_mode(ColorMode.XY):
|
||||||
xy = kwargs[ATTR_XY_COLOR] # pylint: disable=invalid-name
|
xy = kwargs[ATTR_XY_COLOR] # pylint: disable=invalid-name
|
||||||
message["color"] = {"x": xy[0], "y": xy[1]}
|
message["color"] = {"x": xy[0], "y": xy[1]}
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
self._color_mode = COLOR_MODE_XY
|
self._color_mode = ColorMode.XY
|
||||||
self._xy = xy
|
self._xy = xy
|
||||||
should_update = True
|
should_update = True
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue