core/homeassistant/components/wemo/light.py

239 lines
7.7 KiB
Python
Raw Normal View History

"""Support for Belkin WeMo lights."""
2021-12-20 00:09:30 +00:00
from __future__ import annotations
import asyncio
2022-05-24 07:53:01 +00:00
from typing import Any, cast
2022-05-24 07:53:01 +00:00
from pywemo import Bridge, BridgeLight, Dimmer
2015-12-26 05:41:33 +00:00
from homeassistant.components.light import (
2019-07-31 19:25:30 +00:00
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
ATTR_TRANSITION,
ColorMode,
2020-04-26 16:49:41 +00:00
LightEntity,
LightEntityFeature,
2019-07-31 19:25:30 +00:00
)
2021-12-20 00:09:30 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
from homeassistant.helpers.dispatcher import async_dispatcher_connect
2021-10-28 21:58:33 +00:00
from homeassistant.helpers.entity import DeviceInfo
2021-12-20 00:09:30 +00:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
2015-12-26 05:41:33 +00:00
from .const import DOMAIN as WEMO_DOMAIN
from .entity import WemoBinaryStateEntity, WemoEntity
from .wemo_device import DeviceCoordinator
2015-12-26 05:41:33 +00:00
# The WEMO_ constants below come from pywemo itself
WEMO_OFF = 0
2015-12-26 05:41:33 +00:00
2021-12-20 00:09:30 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up WeMo lights."""
2015-12-26 05:41:33 +00:00
2021-12-20 00:09:30 +00:00
async def _discovered_wemo(coordinator: DeviceCoordinator) -> None:
"""Handle a discovered Wemo device."""
2022-05-24 07:53:01 +00:00
if isinstance(coordinator.wemo, Bridge):
async_setup_bridge(hass, config_entry, async_add_entities, coordinator)
else:
async_add_entities([WemoDimmer(coordinator)])
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.light", _discovered_wemo)
2015-12-26 05:41:33 +00:00
await asyncio.gather(
*(
_discovered_wemo(coordinator)
for coordinator in hass.data[WEMO_DOMAIN]["pending"].pop("light")
)
)
2015-12-26 05:41:33 +00:00
@callback
2021-12-20 00:09:30 +00:00
def async_setup_bridge(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
coordinator: DeviceCoordinator,
) -> None:
"""Set up a WeMo link."""
known_light_ids = set()
2015-12-26 05:41:33 +00:00
@callback
2021-12-20 00:09:30 +00:00
def async_update_lights() -> None:
"""Check to see if the bridge has any new lights."""
2015-12-26 05:41:33 +00:00
new_lights = []
2022-05-24 07:53:01 +00:00
bridge = cast(Bridge, coordinator.wemo)
for light_id, light in bridge.Lights.items():
if light_id not in known_light_ids:
known_light_ids.add(light_id)
new_lights.append(WemoLight(coordinator, light))
2015-12-26 05:41:33 +00:00
if new_lights:
async_add_entities(new_lights)
2015-12-26 05:41:33 +00:00
async_update_lights()
config_entry.async_on_unload(coordinator.async_add_listener(async_update_lights))
2015-12-26 05:41:33 +00:00
class WemoLight(WemoEntity, LightEntity):
2016-03-07 21:08:21 +00:00
"""Representation of a WeMo light."""
2015-12-26 05:41:33 +00:00
_attr_supported_features = LightEntityFeature.TRANSITION
2022-05-24 07:53:01 +00:00
def __init__(self, coordinator: DeviceCoordinator, light: BridgeLight) -> None:
"""Initialize the WeMo light."""
super().__init__(coordinator)
self.light = light
self._unique_id = self.light.uniqueID
self._model_name = type(self.light).__name__
@property
def name(self) -> str:
"""Return the name of the device if any."""
2022-05-24 07:53:01 +00:00
return self.light.name
@property
def available(self) -> bool:
"""Return true if the device is available."""
2022-05-24 07:53:01 +00:00
return super().available and self.light.state.get("available", False)
2015-12-26 05:41:33 +00:00
@property
2021-12-20 00:09:30 +00:00
def unique_id(self) -> str:
2016-03-07 21:08:21 +00:00
"""Return the ID of this light."""
2022-05-24 07:53:01 +00:00
return self.light.uniqueID
2015-12-26 05:41:33 +00:00
@property
2021-10-28 21:58:33 +00:00
def device_info(self) -> DeviceInfo:
"""Return the device info."""
2021-10-28 21:58:33 +00:00
return DeviceInfo(
connections={(CONNECTION_ZIGBEE, self._unique_id)},
identifiers={(WEMO_DOMAIN, self._unique_id)},
manufacturer="Belkin",
model=self._model_name,
name=self.name,
)
2015-12-26 05:41:33 +00:00
@property
2021-12-20 00:09:30 +00:00
def brightness(self) -> int:
2016-03-07 21:08:21 +00:00
"""Return the brightness of this light between 0..255."""
2022-05-24 07:53:01 +00:00
return self.light.state.get("level", 255)
@property
def xy_color(self) -> tuple[float, float] | None:
"""Return the xy color value [float, float]."""
2022-05-24 07:53:01 +00:00
return self.light.state.get("color_xy")
@property
2021-12-20 00:09:30 +00:00
def color_temp(self) -> int | None:
"""Return the color temperature of this light in mireds."""
2022-05-24 07:53:01 +00:00
return self.light.state.get("temperature_mireds")
2015-12-26 05:41:33 +00:00
@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
2015-12-26 05:41:33 +00:00
@property
2021-12-20 00:09:30 +00:00
def is_on(self) -> bool:
"""Return true if device is on."""
2022-05-24 07:53:01 +00:00
return self.light.state.get("onoff", WEMO_OFF) != WEMO_OFF
2015-12-26 05:41:33 +00:00
2021-12-20 00:09:30 +00:00
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
xy_color = None
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
color_temp = kwargs.get(ATTR_COLOR_TEMP)
hs_color = kwargs.get(ATTR_HS_COLOR)
transition_time = int(kwargs.get(ATTR_TRANSITION, 0))
if hs_color is not None:
xy_color = color_util.color_hs_to_xy(*hs_color)
turn_on_kwargs = {
"level": brightness,
"transition": transition_time,
"force_update": False,
}
with self._wemo_call_wrapper("turn on"):
if xy_color is not None:
self.light.set_color(xy_color, transition=transition_time)
if color_temp is not None:
self.light.set_temperature(
mireds=color_temp, transition=transition_time
)
self.light.turn_on(**turn_on_kwargs)
2015-12-26 05:41:33 +00:00
2021-12-20 00:09:30 +00:00
def turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
transition_time = int(kwargs.get(ATTR_TRANSITION, 0))
with self._wemo_call_wrapper("turn off"):
self.light.turn_off(transition=transition_time)
2015-12-26 05:41:33 +00:00
class WemoDimmer(WemoBinaryStateEntity, LightEntity):
"""Representation of a WeMo dimmer."""
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
_attr_color_mode = ColorMode.BRIGHTNESS
2022-05-24 07:53:01 +00:00
wemo: Dimmer
@property
2021-12-20 00:09:30 +00:00
def brightness(self) -> int:
"""Return the brightness of this light between 1 and 100."""
wemo_brightness: int = self.wemo.get_brightness()
return int((wemo_brightness * 255) / 100)
2021-12-20 00:09:30 +00:00
def turn_on(self, **kwargs: Any) -> None:
"""Turn the dimmer on."""
# Wemo dimmer switches use a range of [0, 100] to control
# brightness. Level 255 might mean to set it to previous value
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs[ATTR_BRIGHTNESS]
brightness = int((brightness / 255) * 100)
with self._wemo_call_wrapper("set brightness"):
2021-07-30 05:08:13 +00:00
self.wemo.set_brightness(brightness)
else:
with self._wemo_call_wrapper("turn on"):
2021-07-30 05:08:13 +00:00
self.wemo.on()
2021-12-20 00:09:30 +00:00
def turn_off(self, **kwargs: Any) -> None:
"""Turn the dimmer off."""
with self._wemo_call_wrapper("turn off"):
self.wemo.off()