Migrate smartthings light to color_mode (#70968)

This commit is contained in:
Erik Montnemery 2022-08-30 20:45:52 +02:00 committed by GitHub
parent fe881230db
commit 8936c91f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 47 deletions

View File

@ -12,11 +12,10 @@ from homeassistant.components.light import (
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
SUPPORT_BRIGHTNESS, ColorMode,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
LightEntity, LightEntity,
LightEntityFeature, LightEntityFeature,
brightness_supported,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -74,26 +73,40 @@ def convert_scale(value, value_scale, target_scale, round_digits=4):
class SmartThingsLight(SmartThingsEntity, LightEntity): class SmartThingsLight(SmartThingsEntity, LightEntity):
"""Define a SmartThings Light.""" """Define a SmartThings Light."""
_attr_supported_color_modes: set[ColorMode]
def __init__(self, device): def __init__(self, device):
"""Initialize a SmartThingsLight.""" """Initialize a SmartThingsLight."""
super().__init__(device) super().__init__(device)
self._brightness = None self._brightness = None
self._color_temp = None self._color_temp = None
self._hs_color = None self._hs_color = None
self._supported_features = self._determine_features() self._attr_supported_color_modes = self._determine_color_modes()
self._attr_supported_features = self._determine_features()
def _determine_color_modes(self):
"""Get features supported by the device."""
color_modes = set()
# Color Temperature
if Capability.color_temperature in self._device.capabilities:
color_modes.add(ColorMode.COLOR_TEMP)
# Color
if Capability.color_control in self._device.capabilities:
color_modes.add(ColorMode.HS)
# Brightness
if not color_modes and Capability.switch_level in self._device.capabilities:
color_modes.add(ColorMode.BRIGHTNESS)
if not color_modes:
color_modes.add(ColorMode.ONOFF)
return color_modes
def _determine_features(self): def _determine_features(self):
"""Get features supported by the device.""" """Get features supported by the device."""
features = 0 features = 0
# Brightness and transition # Transition
if Capability.switch_level in self._device.capabilities: if Capability.switch_level in self._device.capabilities:
features |= SUPPORT_BRIGHTNESS | LightEntityFeature.TRANSITION features |= LightEntityFeature.TRANSITION
# Color Temperature
if Capability.color_temperature in self._device.capabilities:
features |= SUPPORT_COLOR_TEMP
# Color
if Capability.color_control in self._device.capabilities:
features |= SUPPORT_COLOR
return features return features
@ -101,17 +114,17 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
"""Turn the light on.""" """Turn the light on."""
tasks = [] tasks = []
# Color temperature # Color temperature
if self._supported_features & SUPPORT_COLOR_TEMP and ATTR_COLOR_TEMP in kwargs: if ATTR_COLOR_TEMP in kwargs:
tasks.append(self.async_set_color_temp(kwargs[ATTR_COLOR_TEMP])) tasks.append(self.async_set_color_temp(kwargs[ATTR_COLOR_TEMP]))
# Color # Color
if self._supported_features & SUPPORT_COLOR and ATTR_HS_COLOR in kwargs: if ATTR_HS_COLOR in kwargs:
tasks.append(self.async_set_color(kwargs[ATTR_HS_COLOR])) tasks.append(self.async_set_color(kwargs[ATTR_HS_COLOR]))
if tasks: if tasks:
# Set temp/color first # Set temp/color first
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
# Switch/brightness/transition # Switch/brightness/transition
if self._supported_features & SUPPORT_BRIGHTNESS and ATTR_BRIGHTNESS in kwargs: if ATTR_BRIGHTNESS in kwargs:
await self.async_set_level( await self.async_set_level(
kwargs[ATTR_BRIGHTNESS], kwargs.get(ATTR_TRANSITION, 0) kwargs[ATTR_BRIGHTNESS], kwargs.get(ATTR_TRANSITION, 0)
) )
@ -125,10 +138,7 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the light off.""" """Turn the light off."""
# Switch/transition # Switch/transition
if ( if ATTR_TRANSITION in kwargs:
self._supported_features & LightEntityFeature.TRANSITION
and ATTR_TRANSITION in kwargs
):
await self.async_set_level(0, int(kwargs[ATTR_TRANSITION])) await self.async_set_level(0, int(kwargs[ATTR_TRANSITION]))
else: else:
await self._device.switch_off(set_status=True) await self._device.switch_off(set_status=True)
@ -140,17 +150,17 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update entity attributes when the device status has changed.""" """Update entity attributes when the device status has changed."""
# Brightness and transition # Brightness and transition
if self._supported_features & SUPPORT_BRIGHTNESS: if brightness_supported(self._attr_supported_color_modes):
self._brightness = int( self._brightness = int(
convert_scale(self._device.status.level, 100, 255, 0) convert_scale(self._device.status.level, 100, 255, 0)
) )
# Color Temperature # Color Temperature
if self._supported_features & SUPPORT_COLOR_TEMP: if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._color_temp = color_util.color_temperature_kelvin_to_mired( self._color_temp = color_util.color_temperature_kelvin_to_mired(
self._device.status.color_temperature self._device.status.color_temperature
) )
# Color # Color
if self._supported_features & SUPPORT_COLOR: if ColorMode.HS in self._attr_supported_color_modes:
self._hs_color = ( self._hs_color = (
convert_scale(self._device.status.hue, 100, 360), convert_scale(self._device.status.hue, 100, 360),
self._device.status.saturation, self._device.status.saturation,
@ -179,6 +189,18 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
duration = int(transition) duration = int(transition)
await self._device.set_level(level, duration, set_status=True) await self._device.set_level(level, duration, set_status=True)
@property
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
if len(self._attr_supported_color_modes) == 1:
# The light supports only a single color mode
return list(self._attr_supported_color_modes)[0]
# The light supports hs + color temp, determine which one it is
if self._hs_color and self._hs_color[1]:
return ColorMode.HS
return ColorMode.COLOR_TEMP
@property @property
def brightness(self): def brightness(self):
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
@ -214,8 +236,3 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
# implemented within each device-type handler. This value is the # implemented within each device-type handler. This value is the
# highest kelvin found supported across 20+ handlers. # highest kelvin found supported across 20+ handlers.
return 111 # 9000K return 111 # 9000K
@property
def supported_features(self) -> int:
"""Flag supported features."""
return self._supported_features

View File

@ -11,11 +11,10 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_SUPPORTED_COLOR_MODES,
ATTR_TRANSITION, ATTR_TRANSITION,
DOMAIN as LIGHT_DOMAIN, DOMAIN as LIGHT_DOMAIN,
SUPPORT_BRIGHTNESS, ColorMode,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
LightEntityFeature, LightEntityFeature,
) )
from homeassistant.components.smartthings.const import DOMAIN, SIGNAL_SMARTTHINGS_UPDATE from homeassistant.components.smartthings.const import DOMAIN, SIGNAL_SMARTTHINGS_UPDATE
@ -66,7 +65,7 @@ def light_devices_fixture(device_factory):
Attribute.switch: "on", Attribute.switch: "on",
Attribute.level: 100, Attribute.level: 100,
Attribute.hue: 76.0, Attribute.hue: 76.0,
Attribute.saturation: 55.0, Attribute.saturation: 0.0,
Attribute.color_temperature: 4500, Attribute.color_temperature: 4500,
}, },
), ),
@ -80,33 +79,27 @@ async def test_entity_state(hass, light_devices):
# Dimmer 1 # Dimmer 1
state = hass.states.get("light.dimmer_1") state = hass.states.get("light.dimmer_1")
assert state.state == "on" assert state.state == "on"
assert ( assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.BRIGHTNESS]
state.attributes[ATTR_SUPPORTED_FEATURES] assert state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.TRANSITION
== SUPPORT_BRIGHTNESS | LightEntityFeature.TRANSITION
)
assert isinstance(state.attributes[ATTR_BRIGHTNESS], int) assert isinstance(state.attributes[ATTR_BRIGHTNESS], int)
assert state.attributes[ATTR_BRIGHTNESS] == 255 assert state.attributes[ATTR_BRIGHTNESS] == 255
# Color Dimmer 1 # Color Dimmer 1
state = hass.states.get("light.color_dimmer_1") state = hass.states.get("light.color_dimmer_1")
assert state.state == "off" assert state.state == "off"
assert ( assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.HS]
state.attributes[ATTR_SUPPORTED_FEATURES] assert state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.TRANSITION
== SUPPORT_BRIGHTNESS | LightEntityFeature.TRANSITION | SUPPORT_COLOR
)
# Color Dimmer 2 # Color Dimmer 2
state = hass.states.get("light.color_dimmer_2") state = hass.states.get("light.color_dimmer_2")
assert state.state == "on" assert state.state == "on"
assert ( assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [
state.attributes[ATTR_SUPPORTED_FEATURES] ColorMode.COLOR_TEMP,
== SUPPORT_BRIGHTNESS ColorMode.HS,
| LightEntityFeature.TRANSITION ]
| SUPPORT_COLOR assert state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.TRANSITION
| SUPPORT_COLOR_TEMP
)
assert state.attributes[ATTR_BRIGHTNESS] == 255 assert state.attributes[ATTR_BRIGHTNESS] == 255
assert state.attributes[ATTR_HS_COLOR] == (273.6, 55.0) assert ATTR_HS_COLOR not in state.attributes[ATTR_HS_COLOR]
assert isinstance(state.attributes[ATTR_COLOR_TEMP], int) assert isinstance(state.attributes[ATTR_COLOR_TEMP], int)
assert state.attributes[ATTR_COLOR_TEMP] == 222 assert state.attributes[ATTR_COLOR_TEMP] == 222