mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Implement color_mode support for mysensors (#52068)
This commit is contained in:
parent
3a0a8da648
commit
80cfd59939
@ -1,17 +1,17 @@
|
|||||||
"""Support for MySensors lights."""
|
"""Support for MySensors lights."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any, Tuple, cast
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_HS_COLOR,
|
ATTR_RGB_COLOR,
|
||||||
ATTR_WHITE_VALUE,
|
ATTR_RGBW_COLOR,
|
||||||
|
COLOR_MODE_BRIGHTNESS,
|
||||||
|
COLOR_MODE_RGB,
|
||||||
|
COLOR_MODE_RGBW,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SUPPORT_BRIGHTNESS,
|
|
||||||
SUPPORT_COLOR,
|
|
||||||
SUPPORT_WHITE_VALUE,
|
|
||||||
LightEntity,
|
LightEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -19,15 +19,12 @@ from homeassistant.const import STATE_OFF, STATE_ON
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.color as color_util
|
|
||||||
from homeassistant.util.color import rgb_hex_to_rgb_list
|
from homeassistant.util.color import rgb_hex_to_rgb_list
|
||||||
|
|
||||||
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType
|
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType
|
||||||
from .device import MySensorsDevice
|
from .device import MySensorsDevice
|
||||||
from .helpers import on_unload
|
from .helpers import on_unload
|
||||||
|
|
||||||
SUPPORT_MYSENSORS_RGBW = SUPPORT_COLOR | SUPPORT_WHITE_VALUE
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -69,24 +66,6 @@ class MySensorsLight(mysensors.device.MySensorsEntity, LightEntity):
|
|||||||
"""Initialize a MySensors Light."""
|
"""Initialize a MySensors Light."""
|
||||||
super().__init__(*args)
|
super().__init__(*args)
|
||||||
self._state: bool | None = None
|
self._state: bool | None = None
|
||||||
self._brightness: int | None = None
|
|
||||||
self._hs: tuple[int, int] | None = None
|
|
||||||
self._white: int | None = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def brightness(self) -> int | None:
|
|
||||||
"""Return the brightness of this light between 0..255."""
|
|
||||||
return self._brightness
|
|
||||||
|
|
||||||
@property
|
|
||||||
def hs_color(self) -> tuple[int, int] | None:
|
|
||||||
"""Return the hs color value [int, int]."""
|
|
||||||
return self._hs
|
|
||||||
|
|
||||||
@property
|
|
||||||
def white_value(self) -> int | None:
|
|
||||||
"""Return the white value of this light between 0..255."""
|
|
||||||
return self._white
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
@ -114,7 +93,7 @@ class MySensorsLight(mysensors.device.MySensorsEntity, LightEntity):
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
ATTR_BRIGHTNESS not in kwargs
|
ATTR_BRIGHTNESS not in kwargs
|
||||||
or kwargs[ATTR_BRIGHTNESS] == self._brightness
|
or kwargs[ATTR_BRIGHTNESS] == self._attr_brightness
|
||||||
or set_req.V_DIMMER not in self._values
|
or set_req.V_DIMMER not in self._values
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
@ -126,49 +105,9 @@ class MySensorsLight(mysensors.device.MySensorsEntity, LightEntity):
|
|||||||
|
|
||||||
if self.assumed_state:
|
if self.assumed_state:
|
||||||
# optimistically assume that light has changed state
|
# optimistically assume that light has changed state
|
||||||
self._brightness = brightness
|
self._attr_brightness = brightness
|
||||||
self._values[set_req.V_DIMMER] = percent
|
self._values[set_req.V_DIMMER] = percent
|
||||||
|
|
||||||
def _turn_on_rgb_and_w(self, hex_template: str, **kwargs: Any) -> None:
|
|
||||||
"""Turn on RGB or RGBW child device."""
|
|
||||||
assert self._hs
|
|
||||||
rgb = list(color_util.color_hs_to_RGB(*self._hs))
|
|
||||||
white = self._white
|
|
||||||
hex_color = self._values.get(self.value_type)
|
|
||||||
hs_color: tuple[float, float] | None = kwargs.get(ATTR_HS_COLOR)
|
|
||||||
new_rgb: tuple[int, int, int] | None
|
|
||||||
if hs_color is not None:
|
|
||||||
new_rgb = color_util.color_hs_to_RGB(*hs_color)
|
|
||||||
else:
|
|
||||||
new_rgb = None
|
|
||||||
new_white: int | None = kwargs.get(ATTR_WHITE_VALUE)
|
|
||||||
|
|
||||||
if new_rgb is None and new_white is None:
|
|
||||||
return
|
|
||||||
if new_rgb is not None:
|
|
||||||
rgb = list(new_rgb)
|
|
||||||
if hex_template == "%02x%02x%02x%02x":
|
|
||||||
if new_white is not None:
|
|
||||||
rgb.append(new_white)
|
|
||||||
elif white is not None:
|
|
||||||
rgb.append(white)
|
|
||||||
else:
|
|
||||||
rgb.append(0)
|
|
||||||
hex_color = hex_template % tuple(rgb)
|
|
||||||
if len(rgb) > 3:
|
|
||||||
white = rgb.pop()
|
|
||||||
self.gateway.set_child_value(
|
|
||||||
self.node_id, self.child_id, self.value_type, hex_color, ack=1
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.assumed_state:
|
|
||||||
# optimistically assume that light has changed state
|
|
||||||
# pylint: disable=no-value-for-parameter
|
|
||||||
# https://github.com/PyCQA/pylint/issues/4546
|
|
||||||
self._hs = color_util.color_RGB_to_hs(*rgb) # type: ignore[assignment]
|
|
||||||
self._white = white
|
|
||||||
self._values[self.value_type] = hex_color
|
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
value_type = self.gateway.const.SetReq.V_LIGHT
|
value_type = self.gateway.const.SetReq.V_LIGHT
|
||||||
@ -190,27 +129,16 @@ class MySensorsLight(mysensors.device.MySensorsEntity, LightEntity):
|
|||||||
"""Update the controller with values from dimmer child."""
|
"""Update the controller with values from dimmer child."""
|
||||||
value_type = self.gateway.const.SetReq.V_DIMMER
|
value_type = self.gateway.const.SetReq.V_DIMMER
|
||||||
if value_type in self._values:
|
if value_type in self._values:
|
||||||
self._brightness = round(255 * int(self._values[value_type]) / 100)
|
self._attr_brightness = round(255 * int(self._values[value_type]) / 100)
|
||||||
if self._brightness == 0:
|
if self._attr_brightness == 0:
|
||||||
self._state = False
|
self._state = False
|
||||||
|
|
||||||
@callback
|
|
||||||
def _async_update_rgb_or_w(self) -> None:
|
|
||||||
"""Update the controller with values from RGB or RGBW child."""
|
|
||||||
value = self._values[self.value_type]
|
|
||||||
color_list = rgb_hex_to_rgb_list(value)
|
|
||||||
if len(color_list) > 3:
|
|
||||||
self._white = color_list.pop()
|
|
||||||
self._hs = color_util.color_RGB_to_hs(*color_list) # type: ignore[assignment]
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsLightDimmer(MySensorsLight):
|
class MySensorsLightDimmer(MySensorsLight):
|
||||||
"""Dimmer child class to MySensorsLight."""
|
"""Dimmer child class to MySensorsLight."""
|
||||||
|
|
||||||
@property
|
_attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS}
|
||||||
def supported_features(self) -> int:
|
_attr_color_mode = COLOR_MODE_BRIGHTNESS
|
||||||
"""Flag supported features."""
|
|
||||||
return SUPPORT_BRIGHTNESS
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
@ -229,22 +157,33 @@ class MySensorsLightDimmer(MySensorsLight):
|
|||||||
class MySensorsLightRGB(MySensorsLight):
|
class MySensorsLightRGB(MySensorsLight):
|
||||||
"""RGB child class to MySensorsLight."""
|
"""RGB child class to MySensorsLight."""
|
||||||
|
|
||||||
@property
|
_attr_supported_color_modes = {COLOR_MODE_RGB}
|
||||||
def supported_features(self) -> int:
|
_attr_color_mode = COLOR_MODE_RGB
|
||||||
"""Flag supported features."""
|
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
if set_req.V_DIMMER in self._values:
|
|
||||||
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
|
||||||
return SUPPORT_COLOR
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
self._turn_on_light()
|
self._turn_on_light()
|
||||||
self._turn_on_dimmer(**kwargs)
|
self._turn_on_dimmer(**kwargs)
|
||||||
self._turn_on_rgb_and_w("%02x%02x%02x", **kwargs)
|
self._turn_on_rgb(**kwargs)
|
||||||
if self.assumed_state:
|
if self.assumed_state:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
def _turn_on_rgb(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn on RGB child device."""
|
||||||
|
hex_color = self._values.get(self.value_type)
|
||||||
|
new_rgb: tuple[int, int, int] | None = kwargs.get(ATTR_RGB_COLOR)
|
||||||
|
if new_rgb is None:
|
||||||
|
return
|
||||||
|
hex_color = "%02x%02x%02x" % new_rgb
|
||||||
|
self.gateway.set_child_value(
|
||||||
|
self.node_id, self.child_id, self.value_type, hex_color, ack=1
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.assumed_state:
|
||||||
|
# optimistically assume that light has changed state
|
||||||
|
self._attr_rgb_color = new_rgb
|
||||||
|
self._values[self.value_type] = hex_color
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
await super().async_update()
|
await super().async_update()
|
||||||
@ -252,22 +191,49 @@ class MySensorsLightRGB(MySensorsLight):
|
|||||||
self._async_update_dimmer()
|
self._async_update_dimmer()
|
||||||
self._async_update_rgb_or_w()
|
self._async_update_rgb_or_w()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_update_rgb_or_w(self) -> None:
|
||||||
|
"""Update the controller with values from RGB child."""
|
||||||
|
value = self._values[self.value_type]
|
||||||
|
self._attr_rgb_color = cast(
|
||||||
|
Tuple[int, int, int], tuple(rgb_hex_to_rgb_list(value))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MySensorsLightRGBW(MySensorsLightRGB):
|
class MySensorsLightRGBW(MySensorsLightRGB):
|
||||||
"""RGBW child class to MySensorsLightRGB."""
|
"""RGBW child class to MySensorsLightRGB."""
|
||||||
|
|
||||||
@property
|
_attr_supported_color_modes = {COLOR_MODE_RGBW}
|
||||||
def supported_features(self) -> int:
|
_attr_color_mode = COLOR_MODE_RGBW
|
||||||
"""Flag supported features."""
|
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
if set_req.V_DIMMER in self._values:
|
|
||||||
return SUPPORT_BRIGHTNESS | SUPPORT_MYSENSORS_RGBW
|
|
||||||
return SUPPORT_MYSENSORS_RGBW
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
self._turn_on_light()
|
self._turn_on_light()
|
||||||
self._turn_on_dimmer(**kwargs)
|
self._turn_on_dimmer(**kwargs)
|
||||||
self._turn_on_rgb_and_w("%02x%02x%02x%02x", **kwargs)
|
self._turn_on_rgbw(**kwargs)
|
||||||
if self.assumed_state:
|
if self.assumed_state:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
def _turn_on_rgbw(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn on RGBW child device."""
|
||||||
|
hex_color = self._values.get(self.value_type)
|
||||||
|
new_rgbw: tuple[int, int, int, int] | None = kwargs.get(ATTR_RGBW_COLOR)
|
||||||
|
if new_rgbw is None:
|
||||||
|
return
|
||||||
|
hex_color = "%02x%02x%02x%02x" % new_rgbw
|
||||||
|
self.gateway.set_child_value(
|
||||||
|
self.node_id, self.child_id, self.value_type, hex_color, ack=1
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.assumed_state:
|
||||||
|
# optimistically assume that light has changed state
|
||||||
|
self._attr_rgbw_color = new_rgbw
|
||||||
|
self._values[self.value_type] = hex_color
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_update_rgb_or_w(self) -> None:
|
||||||
|
"""Update the controller with values from RGBW child."""
|
||||||
|
value = self._values[self.value_type]
|
||||||
|
self._attr_rgbw_color = cast(
|
||||||
|
Tuple[int, int, int, int], tuple(rgb_hex_to_rgb_list(value))
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user