core/homeassistant/components/vera/light.py

106 lines
3.2 KiB
Python
Raw Normal View History

"""Support for Vera lights."""
2021-03-18 13:43:52 +00:00
from __future__ import annotations
from typing import Any
import pyvera as veraApi
from homeassistant.components.light import (
2019-07-31 19:25:30 +00:00
ATTR_BRIGHTNESS,
ATTR_HS_COLOR,
DOMAIN as PLATFORM_DOMAIN,
2019-07-31 19:25:30 +00:00
ENTITY_ID_FORMAT,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
2020-04-26 16:49:41 +00:00
LightEntity,
2019-07-31 19:25:30 +00:00
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
2015-12-30 19:44:02 +00:00
from . import VeraDevice
from .common import ControllerData, get_controller_data
2015-03-08 14:58:11 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensor config entry."""
controller_data = get_controller_data(hass, entry)
async_add_entities(
2019-07-31 19:25:30 +00:00
[
VeraLight(device, controller_data)
for device in controller_data.devices.get(PLATFORM_DOMAIN)
],
True,
2019-07-31 19:25:30 +00:00
)
class VeraLight(VeraDevice[veraApi.VeraDimmer], LightEntity):
2016-03-07 21:08:21 +00:00
"""Representation of a Vera Light, including dimmable."""
def __init__(
self, vera_device: veraApi.VeraDimmer, controller_data: ControllerData
) -> None:
2016-03-07 21:08:21 +00:00
"""Initialize the light."""
2016-03-15 09:17:09 +00:00
self._state = False
self._color = None
self._brightness = None
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
@property
2021-03-18 13:43:52 +00:00
def brightness(self) -> int | None:
2016-03-07 21:08:21 +00:00
"""Return the brightness of the light."""
return self._brightness
@property
2021-03-18 13:43:52 +00:00
def hs_color(self) -> tuple[float, float] | None:
"""Return the color of the light."""
return self._color
@property
def supported_features(self) -> int:
"""Flag supported features."""
if self._color:
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
return SUPPORT_BRIGHTNESS
def turn_on(self, **kwargs: Any) -> None:
2016-03-07 21:08:21 +00:00
"""Turn the light on."""
if ATTR_HS_COLOR in kwargs and self._color:
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
self.vera_device.set_color(rgb)
elif ATTR_BRIGHTNESS in kwargs and self.vera_device.is_dimmable:
self.vera_device.set_brightness(kwargs[ATTR_BRIGHTNESS])
else:
self.vera_device.switch_on()
self._state = True
self.schedule_update_ha_state(True)
def turn_off(self, **kwargs: Any):
2016-03-07 21:08:21 +00:00
"""Turn the light off."""
self.vera_device.switch_off()
self._state = False
self.schedule_update_ha_state()
@property
def is_on(self) -> bool:
2016-03-07 21:08:21 +00:00
"""Return true if device is on."""
2016-03-15 09:17:09 +00:00
return self._state
def update(self) -> None:
"""Call to update state."""
2021-02-08 14:25:54 +00:00
super().update()
2016-03-15 09:17:09 +00:00
self._state = self.vera_device.is_switched_on()
if self.vera_device.is_dimmable:
# If it is dimmable, both functions exist. In case color
# is not supported, it will return None
self._brightness = self.vera_device.get_brightness()
rgb = self.vera_device.get_color()
self._color = color_util.color_RGB_to_hs(*rgb) if rgb else None