2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Abode Security System lights."""
|
2022-01-10 09:54:09 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-04-07 01:15:35 -08:00
|
|
|
from math import ceil
|
2022-01-10 09:54:09 -05:00
|
|
|
from typing import Any
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2023-01-24 06:44:38 -05:00
|
|
|
from jaraco.abode.devices.light import Light as AbodeLT
|
|
|
|
from jaraco.abode.helpers import constants as CONST
|
2019-10-12 22:02:12 +02:00
|
|
|
|
2017-09-18 08:39:41 -07:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
2022-06-29 11:03:53 +02:00
|
|
|
ColorMode,
|
2020-04-26 18:49:41 +02:00
|
|
|
LightEntity,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-12-24 14:06:14 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-10-11 22:47:14 -08:00
|
|
|
from homeassistant.util.color import (
|
2019-07-31 12:25:30 -07:00
|
|
|
color_temperature_kelvin_to_mired,
|
|
|
|
color_temperature_mired_to_kelvin,
|
|
|
|
)
|
2017-09-18 08:39:41 -07:00
|
|
|
|
2022-01-10 09:54:09 -05:00
|
|
|
from . import AbodeDevice, AbodeSystem
|
2019-10-13 11:01:04 -07:00
|
|
|
from .const import DOMAIN
|
2017-09-18 08:39:41 -07:00
|
|
|
|
|
|
|
|
2021-12-24 14:06:14 +01:00
|
|
|
async def async_setup_entry(
|
2022-01-10 09:54:09 -05:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-12-24 14:06:14 +01:00
|
|
|
) -> None:
|
2019-10-13 11:01:04 -07:00
|
|
|
"""Set up Abode light devices."""
|
2022-01-10 09:54:09 -05:00
|
|
|
data: AbodeSystem = hass.data[DOMAIN]
|
2017-09-18 08:39:41 -07:00
|
|
|
|
2022-08-10 18:09:05 -04:00
|
|
|
async_add_entities(
|
|
|
|
AbodeLight(data, device)
|
|
|
|
for device in data.abode.get_devices(generic_type=CONST.TYPE_LIGHT)
|
|
|
|
)
|
2017-09-18 08:39:41 -07:00
|
|
|
|
|
|
|
|
2020-04-26 18:49:41 +02:00
|
|
|
class AbodeLight(AbodeDevice, LightEntity):
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Representation of an Abode light."""
|
|
|
|
|
2022-01-10 09:54:09 -05:00
|
|
|
_device: AbodeLT
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Turn on the light."""
|
2018-10-11 22:47:14 -08:00
|
|
|
if ATTR_COLOR_TEMP in kwargs and self._device.is_color_capable:
|
|
|
|
self._device.set_color_temp(
|
2019-07-31 12:25:30 -07:00
|
|
|
int(color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
|
|
|
|
)
|
2020-03-07 07:13:39 -08:00
|
|
|
return
|
2018-10-11 22:47:14 -08:00
|
|
|
|
|
|
|
if ATTR_HS_COLOR in kwargs and self._device.is_color_capable:
|
|
|
|
self._device.set_color(kwargs[ATTR_HS_COLOR])
|
2020-03-07 07:13:39 -08:00
|
|
|
return
|
2018-03-18 18:00:29 -04:00
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS in kwargs and self._device.is_dimmable:
|
2020-01-05 14:09:17 +02:00
|
|
|
# Convert Home Assistant brightness (0-255) to Abode brightness (0-99)
|
2018-04-07 01:15:35 -08:00
|
|
|
# If 100 is sent to Abode, response is 99 causing an error
|
|
|
|
self._device.set_level(ceil(kwargs[ATTR_BRIGHTNESS] * 99 / 255.0))
|
2020-03-07 07:13:39 -08:00
|
|
|
return
|
|
|
|
|
|
|
|
self._device.switch_on()
|
2017-09-18 08:39:41 -07:00
|
|
|
|
2022-01-10 09:54:09 -05:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Turn off the light."""
|
|
|
|
self._device.switch_off()
|
|
|
|
|
|
|
|
@property
|
2022-01-10 09:54:09 -05:00
|
|
|
def is_on(self) -> bool:
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Return true if device is on."""
|
2022-01-10 09:54:09 -05:00
|
|
|
return bool(self._device.is_on)
|
2017-09-18 08:39:41 -07:00
|
|
|
|
|
|
|
@property
|
2022-01-10 09:54:09 -05:00
|
|
|
def brightness(self) -> int | None:
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Return the brightness of the light."""
|
|
|
|
if self._device.is_dimmable and self._device.has_brightness:
|
2018-04-07 01:15:35 -08:00
|
|
|
brightness = int(self._device.brightness)
|
|
|
|
# Abode returns 100 during device initialization and device refresh
|
2020-01-05 14:09:17 +02:00
|
|
|
# Convert Abode brightness (0-99) to Home Assistant brightness (0-255)
|
2022-01-10 09:54:09 -05:00
|
|
|
return 255 if brightness == 100 else ceil(brightness * 255 / 99.0)
|
|
|
|
return None
|
2017-09-18 08:39:41 -07:00
|
|
|
|
2018-10-11 22:47:14 -08:00
|
|
|
@property
|
2022-01-10 09:54:09 -05:00
|
|
|
def color_temp(self) -> int | None:
|
2018-10-11 22:47:14 -08:00
|
|
|
"""Return the color temp of the light."""
|
|
|
|
if self._device.has_color:
|
|
|
|
return color_temperature_kelvin_to_mired(self._device.color_temp)
|
2022-01-10 09:54:09 -05:00
|
|
|
return None
|
2018-10-11 22:47:14 -08:00
|
|
|
|
2017-09-18 08:39:41 -07:00
|
|
|
@property
|
2022-01-10 09:54:09 -05:00
|
|
|
def hs_color(self) -> tuple[float, float] | None:
|
2017-09-18 08:39:41 -07:00
|
|
|
"""Return the color of the light."""
|
2022-01-10 09:54:09 -05:00
|
|
|
_hs = None
|
2018-10-11 22:47:14 -08:00
|
|
|
if self._device.has_color:
|
2022-01-10 09:54:09 -05:00
|
|
|
_hs = self._device.color
|
|
|
|
return _hs
|
2017-09-18 08:39:41 -07:00
|
|
|
|
|
|
|
@property
|
2022-06-29 10:42:24 +02:00
|
|
|
def color_mode(self) -> str | None:
|
|
|
|
"""Return the color mode of the light."""
|
|
|
|
if self._device.is_dimmable and self._device.is_color_capable:
|
|
|
|
if self.hs_color is not None:
|
2022-06-29 11:03:53 +02:00
|
|
|
return ColorMode.HS
|
|
|
|
return ColorMode.COLOR_TEMP
|
2022-06-29 10:42:24 +02:00
|
|
|
if self._device.is_dimmable:
|
2022-06-29 11:03:53 +02:00
|
|
|
return ColorMode.BRIGHTNESS
|
|
|
|
return ColorMode.ONOFF
|
2022-06-29 10:42:24 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_color_modes(self) -> set[str] | None:
|
|
|
|
"""Flag supported color modes."""
|
2018-10-11 22:47:14 -08:00
|
|
|
if self._device.is_dimmable and self._device.is_color_capable:
|
2022-06-29 11:03:53 +02:00
|
|
|
return {ColorMode.COLOR_TEMP, ColorMode.HS}
|
2018-07-23 11:16:05 +03:00
|
|
|
if self._device.is_dimmable:
|
2022-06-29 11:03:53 +02:00
|
|
|
return {ColorMode.BRIGHTNESS}
|
|
|
|
return {ColorMode.ONOFF}
|