Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions homeassistant/components/wemo/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
ATTR_TRANSITION,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
ColorMode,
LightEntity,
LightEntityFeature,
)
Expand All @@ -29,13 +27,6 @@
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

Expand Down Expand Up @@ -94,6 +85,8 @@ def async_update_lights() -> None:
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)
Expand Down Expand Up @@ -133,27 +126,48 @@ def brightness(self) -> int:
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
Expand Down Expand Up @@ -194,10 +208,8 @@ def turn_off(self, **kwargs: Any) -> None:
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:
Expand Down
11 changes: 10 additions & 1 deletion tests/components/wemo/test_light_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
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

Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down