2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Vera lights."""
|
2021-03-18 14:43:52 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 22:36:48 +01:00
|
|
|
from typing import Any
|
2020-09-16 14:23:50 -07:00
|
|
|
|
|
|
|
import pyvera as veraApi
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2016-09-12 15:52:22 +02:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ENTITY_ID_FORMAT,
|
2022-04-27 17:18:35 +02:00
|
|
|
ColorMode,
|
2020-04-26 18:49:41 +02:00
|
|
|
LightEntity,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2020-04-03 00:49:50 -07:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-20 07:46:26 +01:00
|
|
|
from homeassistant.const import Platform
|
2020-04-03 00:49:50 -07:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 22:36:48 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-03-18 18:00:29 -04:00
|
|
|
import homeassistant.util.color as color_util
|
2015-12-30 19:44:02 +00:00
|
|
|
|
2020-04-03 00:49:50 -07:00
|
|
|
from . import VeraDevice
|
2020-09-14 20:06:52 -07:00
|
|
|
from .common import ControllerData, get_controller_data
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-03-09 01:58:11 +11:00
|
|
|
|
2020-04-03 00:49:50 -07:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-05-04 22:36:48 +01:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-03 00:49:50 -07:00
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
2020-09-14 20:06:52 -07:00
|
|
|
controller_data = get_controller_data(hass, entry)
|
2020-04-03 00:49:50 -07:00
|
|
|
async_add_entities(
|
2019-07-31 12:25:30 -07:00
|
|
|
[
|
2020-09-14 20:06:52 -07:00
|
|
|
VeraLight(device, controller_data)
|
2022-01-20 07:46:26 +01:00
|
|
|
for device in controller_data.devices[Platform.LIGHT]
|
2021-02-05 12:20:15 +00:00
|
|
|
],
|
|
|
|
True,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2015-10-27 23:43:06 +00:00
|
|
|
|
2020-09-16 14:23:50 -07:00
|
|
|
class VeraLight(VeraDevice[veraApi.VeraDimmer], LightEntity):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Representation of a Vera Light, including dimmable."""
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2020-09-16 14:23:50 -07:00
|
|
|
def __init__(
|
|
|
|
self, vera_device: veraApi.VeraDimmer, controller_data: ControllerData
|
2021-05-20 17:00:19 +02:00
|
|
|
) -> None:
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Initialize the light."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self._state = False
|
2022-01-20 07:46:26 +01:00
|
|
|
self._color: tuple[float, float] | None = None
|
2017-06-15 16:28:24 -06:00
|
|
|
self._brightness = None
|
2020-09-14 20:06:52 -07:00
|
|
|
VeraDevice.__init__(self, vera_device, controller_data)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2016-02-07 21:45:15 +00:00
|
|
|
|
2015-10-27 23:18:46 +00:00
|
|
|
@property
|
2021-03-18 14:43:52 +01:00
|
|
|
def brightness(self) -> int | None:
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return the brightness of the light."""
|
2017-06-15 16:28:24 -06:00
|
|
|
return self._brightness
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2017-06-08 04:28:03 -06:00
|
|
|
@property
|
2021-03-18 14:43:52 +01:00
|
|
|
def hs_color(self) -> tuple[float, float] | None:
|
2017-06-08 04:28:03 -06:00
|
|
|
"""Return the color of the light."""
|
2017-06-15 16:28:24 -06:00
|
|
|
return self._color
|
2017-06-08 04:28:03 -06:00
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
@property
|
2022-04-27 17:18:35 +02:00
|
|
|
def color_mode(self) -> ColorMode:
|
|
|
|
"""Return the color mode of the light."""
|
|
|
|
if self.vera_device.is_dimmable:
|
|
|
|
if self._color:
|
|
|
|
return ColorMode.HS
|
|
|
|
return ColorMode.BRIGHTNESS
|
|
|
|
return ColorMode.ONOFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_color_modes(self) -> set[ColorMode]:
|
|
|
|
"""Flag supported color modes."""
|
|
|
|
return {self.color_mode}
|
2016-08-16 09:07:07 +03:00
|
|
|
|
2020-09-16 14:23:50 -07:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the light on."""
|
2018-03-18 18:00:29 -04:00
|
|
|
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)
|
2017-06-08 04:28:03 -06:00
|
|
|
elif ATTR_BRIGHTNESS in kwargs and self.vera_device.is_dimmable:
|
2015-10-27 23:18:46 +00:00
|
|
|
self.vera_device.set_brightness(kwargs[ATTR_BRIGHTNESS])
|
|
|
|
else:
|
|
|
|
self.vera_device.switch_on()
|
|
|
|
|
2017-04-04 11:02:09 +01:00
|
|
|
self._state = True
|
2016-11-30 22:33:38 +01:00
|
|
|
self.schedule_update_ha_state(True)
|
2016-02-07 21:45:15 +00:00
|
|
|
|
2020-09-16 14:23:50 -07:00
|
|
|
def turn_off(self, **kwargs: Any):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the light off."""
|
2016-02-07 21:45:15 +00:00
|
|
|
self.vera_device.switch_off()
|
2017-04-04 11:02:09 +01:00
|
|
|
self._state = False
|
2016-11-30 22:33:38 +01:00
|
|
|
self.schedule_update_ha_state()
|
2016-02-07 21:45:15 +00:00
|
|
|
|
|
|
|
@property
|
2020-09-16 14:23:50 -07:00
|
|
|
def is_on(self) -> bool:
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return true if device is on."""
|
2016-03-15 09:17:09 +00:00
|
|
|
return self._state
|
2016-02-07 21:45:15 +00:00
|
|
|
|
2020-09-16 14:23:50 -07:00
|
|
|
def update(self) -> None:
|
2017-04-30 23:10:08 -04:00
|
|
|
"""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()
|
2017-06-19 10:54:13 +03:00
|
|
|
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()
|
2018-03-18 18:00:29 -04:00
|
|
|
rgb = self.vera_device.get_color()
|
|
|
|
self._color = color_util.color_RGB_to_hs(*rgb) if rgb else None
|