2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 lights."""
|
2020-05-08 04:15:42 +00:00
|
|
|
from typing import Callable, Dict
|
|
|
|
|
|
|
|
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
|
2020-05-01 05:36:01 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
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-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(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[list], None],
|
|
|
|
) -> 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
|
|
|
|
return int(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs) -> None:
|
|
|
|
"""Send the turn off command to the ISY994 light device."""
|
2020-04-17 00:26:34 +00:00
|
|
|
self._last_brightness = self.brightness
|
2020-05-08 04:15:42 +00:00
|
|
|
if not 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
|
|
|
|
2020-04-17 00:26:34 +00:00
|
|
|
def on_update(self, event: object) -> None:
|
|
|
|
"""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):
|
|
|
|
self._last_brightness = self._node.status
|
2020-04-17 00:26:34 +00:00
|
|
|
super().on_update(event)
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
# pylint: disable=arguments-differ
|
2016-11-29 16:50:12 +00:00
|
|
|
def 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-05-08 04:15:42 +00:00
|
|
|
if not 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
|
2020-05-08 04:15:42 +00:00
|
|
|
def device_state_attributes(self) -> Dict:
|
2020-05-01 05:36:01 +00:00
|
|
|
"""Return the light attributes."""
|
2020-05-08 04:15:42 +00:00
|
|
|
attribs = super().device_state_attributes
|
|
|
|
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
|
|
|
|
last_state = await self.async_get_last_state()
|
|
|
|
if not last_state:
|
|
|
|
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
|
|
|
|
|
|
|
def set_on_level(self, value):
|
|
|
|
"""Set the ON Level for a device."""
|
|
|
|
self._node.set_on_level(value)
|
|
|
|
|
|
|
|
def set_ramp_rate(self, value):
|
|
|
|
"""Set the Ramp Rate for a device."""
|
|
|
|
self._node.set_ramp_rate(value)
|