2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 lights."""
|
2021-03-18 09:02:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN
|
2015-04-04 10:13:27 +00:00
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
DOMAIN as LIGHT,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
LightEntity,
|
|
|
|
)
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-18 19:15:47 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-01 05:36:01 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2015-04-04 10:13:27 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
from .const import (
|
|
|
|
_LOGGER,
|
|
|
|
CONF_RESTORE_LIGHT_STATE,
|
|
|
|
DOMAIN as ISY994_DOMAIN,
|
|
|
|
ISY994_NODES,
|
2020-11-06 20:07:37 +00:00
|
|
|
UOM_PERCENTAGE,
|
2020-05-09 19:49:00 +00:00
|
|
|
)
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from .helpers import migrate_old_unique_ids
|
2020-09-08 15:31:08 +00:00
|
|
|
from .services import async_setup_light_services
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2020-05-01 05:36:01 +00:00
|
|
|
ATTR_LAST_BRIGHTNESS = "last_brightness"
|
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-23 08:11:58 +00:00
|
|
|
hass: HomeAssistant,
|
2020-05-09 19:49:00 +00:00
|
|
|
entry: ConfigEntry,
|
2021-04-30 18:38:59 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-05-09 19:49:00 +00:00
|
|
|
) -> bool:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Set up the ISY994 light platform."""
|
2020-05-09 19:49:00 +00:00
|
|
|
hass_isy_data = hass.data[ISY994_DOMAIN][entry.entry_id]
|
|
|
|
isy_options = entry.options
|
|
|
|
restore_light_state = isy_options.get(CONF_RESTORE_LIGHT_STATE, False)
|
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
2020-05-09 19:49:00 +00:00
|
|
|
for node in hass_isy_data[ISY994_NODES][LIGHT]:
|
|
|
|
devices.append(ISYLightEntity(node, restore_light_state))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
await migrate_old_unique_ids(hass, LIGHT, devices)
|
|
|
|
async_add_entities(devices)
|
2020-05-11 15:58:58 +00:00
|
|
|
async_setup_light_services(hass)
|
2015-04-04 10:13:27 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYLightEntity(ISYNodeEntity, LightEntity, RestoreEntity):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Representation of an ISY994 light device."""
|
2015-04-04 10:13:27 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
def __init__(self, node, restore_light_state) -> None:
|
2020-04-17 00:26:34 +00:00
|
|
|
"""Initialize the ISY994 light device."""
|
|
|
|
super().__init__(node)
|
2020-05-01 05:36:01 +00:00
|
|
|
self._last_brightness = None
|
2020-05-09 19:49:00 +00:00
|
|
|
self._restore_light_state = restore_light_state
|
2020-04-17 00:26:34 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Get whether the ISY994 light is on."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
2018-09-30 07:21:27 +00:00
|
|
|
return False
|
2020-05-12 02:32:19 +00:00
|
|
|
return int(self._node.status) != 0
|
2015-08-29 00:17:07 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
2017-01-20 06:22:33 +00:00
|
|
|
def brightness(self) -> float:
|
|
|
|
"""Get the brightness of the ISY994 light."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
2020-11-06 20:07:37 +00:00
|
|
|
# Special Case for ISY Z-Wave Devices using % instead of 0-255:
|
|
|
|
if self._node.uom == UOM_PERCENTAGE:
|
|
|
|
return round(self._node.status * 255.0 / 100.0)
|
2020-05-12 02:32:19 +00:00
|
|
|
return int(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn off command to the ISY994 light device."""
|
2020-04-17 00:26:34 +00:00
|
|
|
self._last_brightness = self.brightness
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_off():
|
2017-06-21 07:38:12 +00:00
|
|
|
_LOGGER.debug("Unable to turn off light")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
@callback
|
|
|
|
def async_on_update(self, event: object) -> None:
|
2020-04-17 00:26:34 +00:00
|
|
|
"""Save brightness in the update event from the ISY994 Node."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status not in (0, ISY_VALUE_UNKNOWN):
|
2021-05-18 19:15:47 +00:00
|
|
|
self._last_brightness = self._node.status
|
2020-11-06 20:07:37 +00:00
|
|
|
if self._node.uom == UOM_PERCENTAGE:
|
|
|
|
self._last_brightness = round(self._node.status * 255.0 / 100.0)
|
|
|
|
else:
|
|
|
|
self._last_brightness = self._node.status
|
2021-05-18 19:15:47 +00:00
|
|
|
super().async_on_update(event)
|
2020-04-17 00:26:34 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
# pylint: disable=arguments-differ
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_turn_on(self, brightness=None, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn on command to the ISY994 light device."""
|
2020-05-09 19:49:00 +00:00
|
|
|
if self._restore_light_state and brightness is None and self._last_brightness:
|
2020-04-17 00:26:34 +00:00
|
|
|
brightness = self._last_brightness
|
2020-11-06 20:07:37 +00:00
|
|
|
# Special Case for ISY Z-Wave Devices using % instead of 0-255:
|
|
|
|
if brightness is not None and self._node.uom == UOM_PERCENTAGE:
|
|
|
|
brightness = round(brightness * 100.0 / 255.0)
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_on(val=brightness):
|
2017-05-10 10:06:57 +00:00
|
|
|
_LOGGER.debug("Unable to turn on light")
|
2016-09-20 03:16:51 +00:00
|
|
|
|
2020-05-01 05:36:01 +00:00
|
|
|
@property
|
2021-03-18 09:02:00 +00:00
|
|
|
def extra_state_attributes(self) -> dict:
|
2020-05-01 05:36:01 +00:00
|
|
|
"""Return the light attributes."""
|
2021-03-11 15:57:47 +00:00
|
|
|
attribs = super().extra_state_attributes
|
2020-05-08 04:15:42 +00:00
|
|
|
attribs[ATTR_LAST_BRIGHTNESS] = self._last_brightness
|
|
|
|
return attribs
|
2020-05-01 05:36:01 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS
|
|
|
|
|
2020-05-01 05:36:01 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Restore last_brightness on restart."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
self._last_brightness = self.brightness or 255
|
2021-10-30 14:29:07 +00:00
|
|
|
if not (last_state := await self.async_get_last_state()):
|
2020-05-01 05:36:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if (
|
|
|
|
ATTR_LAST_BRIGHTNESS in last_state.attributes
|
|
|
|
and last_state.attributes[ATTR_LAST_BRIGHTNESS]
|
|
|
|
):
|
|
|
|
self._last_brightness = last_state.attributes[ATTR_LAST_BRIGHTNESS]
|
2020-05-11 15:58:58 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_set_on_level(self, value):
|
2020-05-11 15:58:58 +00:00
|
|
|
"""Set the ON Level for a device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
await self._node.set_on_level(value)
|
2020-05-11 15:58:58 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_set_ramp_rate(self, value):
|
2020-05-11 15:58:58 +00:00
|
|
|
"""Set the Ramp Rate for a device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
await self._node.set_ramp_rate(value)
|