Migrate xiaomi_miio light to color_mode (#70998)

This commit is contained in:
Erik Montnemery 2022-05-27 08:36:32 +02:00 committed by GitHub
parent cbd0c8976b
commit cc42a95100
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,9 +19,7 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
SUPPORT_BRIGHTNESS, ColorMode,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
LightEntity, LightEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -234,6 +232,9 @@ async def async_setup_entry(
class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity): class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
"""Representation of a Abstract Xiaomi Philips Light.""" """Representation of a Abstract Xiaomi Philips Light."""
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
def __init__(self, name, device, entry, unique_id): def __init__(self, name, device, entry, unique_id):
"""Initialize the light device.""" """Initialize the light device."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
@ -263,11 +264,6 @@ class XiaomiPhilipsAbstractLight(XiaomiMiioEntity, LightEntity):
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
return self._brightness return self._brightness
@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS
async def _try_command(self, mask_error, func, *args, **kwargs): async def _try_command(self, mask_error, func, *args, **kwargs):
"""Call a light command handling error messages.""" """Call a light command handling error messages."""
try: try:
@ -399,6 +395,9 @@ class XiaomiPhilipsGenericLight(XiaomiPhilipsAbstractLight):
class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight): class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
"""Representation of a Xiaomi Philips Bulb.""" """Representation of a Xiaomi Philips Bulb."""
_attr_color_mode = ColorMode.COLOR_TEMP
_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
def __init__(self, name, device, entry, unique_id): def __init__(self, name, device, entry, unique_id):
"""Initialize the light device.""" """Initialize the light device."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
@ -420,11 +419,6 @@ class XiaomiPhilipsBulb(XiaomiPhilipsGenericLight):
"""Return the warmest color_temp that this light supports.""" """Return the warmest color_temp that this light supports."""
return 333 return 333
@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn the light on.""" """Turn the light on."""
if ATTR_COLOR_TEMP in kwargs: if ATTR_COLOR_TEMP in kwargs:
@ -760,6 +754,8 @@ class XiaomiPhilipsEyecareLampAmbientLight(XiaomiPhilipsAbstractLight):
class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb): class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
"""Representation of a Xiaomi Philips Zhirui Bedside Lamp.""" """Representation of a Xiaomi Philips Zhirui Bedside Lamp."""
_attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
def __init__(self, name, device, entry, unique_id): def __init__(self, name, device, entry, unique_id):
"""Initialize the light device.""" """Initialize the light device."""
super().__init__(name, device, entry, unique_id) super().__init__(name, device, entry, unique_id)
@ -792,9 +788,11 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
return self._hs_color return self._hs_color
@property @property
def supported_features(self): def color_mode(self):
"""Return the supported features.""" """Return the color mode of the light."""
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR | SUPPORT_COLOR_TEMP if self.hs_color:
return ColorMode.HS
return ColorMode.COLOR_TEMP
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn the light on.""" """Turn the light on."""
@ -935,6 +933,9 @@ class XiaomiPhilipsMoonlightLamp(XiaomiPhilipsBulb):
class XiaomiGatewayLight(LightEntity): class XiaomiGatewayLight(LightEntity):
"""Representation of a gateway device's light.""" """Representation of a gateway device's light."""
_attr_color_mode = ColorMode.HS
_attr_supported_color_modes = {ColorMode.HS}
def __init__(self, gateway_device, gateway_name, gateway_device_id): def __init__(self, gateway_device, gateway_name, gateway_device_id):
"""Initialize the XiaomiGatewayLight.""" """Initialize the XiaomiGatewayLight."""
self._gateway = gateway_device self._gateway = gateway_device
@ -984,11 +985,6 @@ class XiaomiGatewayLight(LightEntity):
"""Return the hs color value.""" """Return the hs color value."""
return self._hs return self._hs
@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the light on.""" """Turn the light on."""
if ATTR_HS_COLOR in kwargs: if ATTR_HS_COLOR in kwargs:
@ -1036,6 +1032,9 @@ class XiaomiGatewayLight(LightEntity):
class XiaomiGatewayBulb(XiaomiGatewayDevice, LightEntity): class XiaomiGatewayBulb(XiaomiGatewayDevice, LightEntity):
"""Representation of Xiaomi Gateway Bulb.""" """Representation of Xiaomi Gateway Bulb."""
_attr_color_mode = ColorMode.COLOR_TEMP
_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
@property @property
def brightness(self): def brightness(self):
"""Return the brightness of the light.""" """Return the brightness of the light."""
@ -1061,11 +1060,6 @@ class XiaomiGatewayBulb(XiaomiGatewayDevice, LightEntity):
"""Return max cct.""" """Return max cct."""
return self._sub_device.status["cct_max"] return self._sub_device.status["cct_max"]
@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on.""" """Instruct the light to turn on."""
await self.hass.async_add_executor_job(self._sub_device.on) await self.hass.async_add_executor_job(self._sub_device.on)