Migrate wemo light to ColorMode (#70857)

This commit is contained in:
epenet 2022-05-09 18:42:18 +02:00 committed by GitHub
parent e5870c65ee
commit 75ce66e8bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 25 deletions

View file

@ -11,9 +11,7 @@ from homeassistant.components.light import (
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
ATTR_TRANSITION,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
ColorMode,
LightEntity,
LightEntityFeature,
)
@ -29,13 +27,6 @@ from .const import DOMAIN as WEMO_DOMAIN
from .entity import WemoBinaryStateEntity, WemoEntity
from .wemo_device import DeviceCoordinator
SUPPORT_WEMO = (
SUPPORT_BRIGHTNESS
| SUPPORT_COLOR_TEMP
| SUPPORT_COLOR
| LightEntityFeature.TRANSITION
)
# The WEMO_ constants below come from pywemo itself
WEMO_OFF = 0
@ -94,6 +85,8 @@ def async_setup_bridge(
class WemoLight(WemoEntity, LightEntity):
"""Representation of a WeMo light."""
_attr_supported_features = LightEntityFeature.TRANSITION
def __init__(self, coordinator: DeviceCoordinator, light: bridge.Light) -> None:
"""Initialize the WeMo light."""
super().__init__(coordinator)
@ -133,27 +126,48 @@ class WemoLight(WemoEntity, LightEntity):
return cast(int, self.light.state.get("level", 255))
@property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hs color values of this light."""
if xy_color := self.light.state.get("color_xy"):
return color_util.color_xy_to_hs(*xy_color)
return None
def xy_color(self) -> tuple[float, float] | None:
"""Return the xy color value [float, float]."""
return self.light.state.get("color_xy") # type:ignore[no-any-return]
@property
def color_temp(self) -> int | None:
"""Return the color temperature of this light in mireds."""
return cast(Optional[int], self.light.state.get("temperature_mireds"))
@property
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
if (
"colorcontrol" in self.light.capabilities
and self.light.state.get("color_xy") is not None
):
return ColorMode.XY
if "colortemperature" in self.light.capabilities:
return ColorMode.COLOR_TEMP
if "levelcontrol" in self.light.capabilities:
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF
@property
def supported_color_modes(self) -> set[ColorMode]:
"""Flag supported color modes."""
modes: set[ColorMode] = set()
if "colorcontrol" in self.light.capabilities:
modes.add(ColorMode.XY)
if "colortemperature" in self.light.capabilities:
modes.add(ColorMode.COLOR_TEMP)
if "levelcontrol" in self.light.capabilities and not modes:
modes.add(ColorMode.BRIGHTNESS)
if not modes:
modes.add(ColorMode.ONOFF)
return modes
@property
def is_on(self) -> bool:
"""Return true if device is on."""
return cast(int, self.light.state.get("onoff")) != WEMO_OFF
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_WEMO
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
xy_color = None
@ -194,10 +208,8 @@ class WemoLight(WemoEntity, LightEntity):
class WemoDimmer(WemoBinaryStateEntity, LightEntity):
"""Representation of a WeMo dimmer."""
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
_attr_color_mode = ColorMode.BRIGHTNESS
@property
def brightness(self) -> int:

View file

@ -8,7 +8,13 @@ from homeassistant.components.homeassistant import (
DOMAIN as HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
)
from homeassistant.components.light import ATTR_COLOR_TEMP, DOMAIN as LIGHT_DOMAIN
from homeassistant.components.light import (
ATTR_COLOR_MODE,
ATTR_COLOR_TEMP,
ATTR_SUPPORTED_COLOR_MODES,
DOMAIN as LIGHT_DOMAIN,
ColorMode,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.setup import async_setup_component
@ -32,6 +38,7 @@ def pywemo_bridge_light_fixture(pywemo_device):
light.name = pywemo_device.name
light.bridge = pywemo_device
light.state = {"onoff": 0, "available": True}
light.capabilities = ["onoff", "levelcontrol", "colortemperature"]
pywemo_device.Lights = {pywemo_device.serialnumber: light}
return light
@ -102,6 +109,8 @@ async def test_light_update_entity(
)
state = hass.states.get(wemo_entity.entity_id)
assert state.attributes.get(ATTR_COLOR_TEMP) == 432
assert state.attributes.get(ATTR_SUPPORTED_COLOR_MODES) == [ColorMode.COLOR_TEMP]
assert state.attributes.get(ATTR_COLOR_MODE) == ColorMode.COLOR_TEMP
assert state.state == STATE_ON
# Off state.