Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for ISY lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from pyisy.constants import ISY_VALUE_UNKNOWN
8 from pyisy.helpers import NodeProperty
9 from pyisy.nodes import Node
10 
11 from homeassistant.components.light import ColorMode, LightEntity
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import Platform
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.device_registry import DeviceInfo
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.restore_state import RestoreEntity
18 
19 from .const import _LOGGER, CONF_RESTORE_LIGHT_STATE, DOMAIN, UOM_PERCENTAGE
20 from .entity import ISYNodeEntity
21 from .models import IsyData
22 
23 ATTR_LAST_BRIGHTNESS = "last_brightness"
24 
25 
27  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
28 ) -> None:
29  """Set up the ISY light platform."""
30  isy_data: IsyData = hass.data[DOMAIN][entry.entry_id]
31  devices: dict[str, DeviceInfo] = isy_data.devices
32  isy_options = entry.options
33  restore_light_state = isy_options.get(CONF_RESTORE_LIGHT_STATE, False)
34 
36  ISYLightEntity(node, restore_light_state, devices.get(node.primary_node))
37  for node in isy_data.nodes[Platform.LIGHT]
38  )
39 
40 
42  """Representation of an ISY light device."""
43 
44  _attr_color_mode = ColorMode.BRIGHTNESS
45  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
46 
47  def __init__(
48  self,
49  node: Node,
50  restore_light_state: bool,
51  device_info: DeviceInfo | None = None,
52  ) -> None:
53  """Initialize the ISY light device."""
54  super().__init__(node, device_info=device_info)
55  self._last_brightness_last_brightness: int | None = None
56  self._restore_light_state_restore_light_state = restore_light_state
57 
58  @property
59  def is_on(self) -> bool:
60  """Get whether the ISY light is on."""
61  if self._node_node.status == ISY_VALUE_UNKNOWN:
62  return False
63  return int(self._node_node.status) != 0
64 
65  @property
66  def brightness(self) -> int | None:
67  """Get the brightness of the ISY light."""
68  if self._node_node.status == ISY_VALUE_UNKNOWN:
69  return None
70  # Special Case for ISY Z-Wave Devices using % instead of 0-255:
71  if self._node_node.uom == UOM_PERCENTAGE:
72  return round(cast(float, self._node_node.status) * 255.0 / 100.0)
73  return int(self._node_node.status)
74 
75  async def async_turn_off(self, **kwargs: Any) -> None:
76  """Send the turn off command to the ISY light device."""
77  self._last_brightness_last_brightness = self.brightnessbrightnessbrightness
78  if not await self._node_node.turn_off():
79  _LOGGER.debug("Unable to turn off light")
80 
81  @callback
82  def async_on_update(self, event: NodeProperty) -> None:
83  """Save brightness in the update event from the ISY Node."""
84  if self._node_node.status not in (0, ISY_VALUE_UNKNOWN):
85  self._last_brightness_last_brightness = self._node_node.status
86  if self._node_node.uom == UOM_PERCENTAGE:
87  self._last_brightness_last_brightness = round(self._node_node.status * 255.0 / 100.0)
88  else:
89  self._last_brightness_last_brightness = self._node_node.status
90  super().async_on_update(event)
91 
92  async def async_turn_on(self, brightness: int | None = None, **kwargs: Any) -> None:
93  """Send the turn on command to the ISY light device."""
94  if self._restore_light_state_restore_light_state and brightness is None and self._last_brightness_last_brightness:
95  brightness = self._last_brightness_last_brightness
96  # Special Case for ISY Z-Wave Devices using % instead of 0-255:
97  if brightness is not None and self._node_node.uom == UOM_PERCENTAGE:
98  brightness = round(brightness * 100.0 / 255.0)
99  if not await self._node_node.turn_on(val=brightness):
100  _LOGGER.debug("Unable to turn on light")
101 
102  @property
103  def extra_state_attributes(self) -> dict[str, Any]:
104  """Return the light attributes."""
105  attribs = super().extra_state_attributes
106  attribs[ATTR_LAST_BRIGHTNESS] = self._last_brightness_last_brightness
107  return attribs
108 
109  async def async_added_to_hass(self) -> None:
110  """Restore last_brightness on restart."""
111  await super().async_added_to_hass()
112 
113  self._last_brightness_last_brightness = self.brightnessbrightnessbrightness or 255
114  if not (last_state := await self.async_get_last_stateasync_get_last_state()):
115  return
116 
117  if last_brightness := last_state.attributes.get(ATTR_LAST_BRIGHTNESS):
118  self._last_brightness_last_brightness = last_brightness
None async_on_update(self, NodeProperty event)
Definition: light.py:82
None async_turn_on(self, int|None brightness=None, **Any kwargs)
Definition: light.py:92
None __init__(self, Node node, bool restore_light_state, DeviceInfo|None device_info=None)
Definition: light.py:52
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:28