Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Component providing Lights for UniFi Protect."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from uiprotect.data import Light, ModelType, ProtectAdoptableDeviceModel
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .data import ProtectDeviceType, UFPConfigEntry
15 from .entity import ProtectDeviceEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  entry: UFPConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up lights for UniFi Protect integration."""
26  data = entry.runtime_data
27 
28  @callback
29  def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
30  if device.model is ModelType.LIGHT and device.can_write(
31  data.api.bootstrap.auth_user
32  ):
33  async_add_entities([ProtectLight(data, device)])
34 
35  data.async_subscribe_adopt(_add_new_device)
37  ProtectLight(data, device)
38  for device in data.get_by_types({ModelType.LIGHT})
39  if device.can_write(data.api.bootstrap.auth_user)
40  )
41 
42 
43 def unifi_brightness_to_hass(value: int) -> int:
44  """Convert unifi brightness 1..6 to hass format 0..255."""
45  return min(255, round((value / 6) * 255))
46 
47 
48 def hass_to_unifi_brightness(value: int) -> int:
49  """Convert hass brightness 0..255 to unifi 1..6 scale."""
50  return max(1, round((value / 255) * 6))
51 
52 
54  """A Ubiquiti UniFi Protect Light Entity."""
55 
56  device: Light
57 
58  _attr_icon = "mdi:spotlight-beam"
59  _attr_color_mode = ColorMode.BRIGHTNESS
60  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
61  _state_attrs = ("_attr_available", "_attr_is_on", "_attr_brightness")
62 
63  @callback
64  def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:
65  super()._async_update_device_from_protect(device)
66  updated_device = self.devicedevice
67  self._attr_is_on_attr_is_on = updated_device.is_light_on
69  updated_device.light_device_settings.led_level
70  )
71 
72  async def async_turn_on(self, **kwargs: Any) -> None:
73  """Turn the light on."""
74  hass_brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightnessbrightness)
75  unifi_brightness = hass_to_unifi_brightness(hass_brightness)
76 
77  _LOGGER.debug("Turning on light with brightness %s", unifi_brightness)
78  await self.devicedevice.set_light(True, unifi_brightness)
79 
80  async def async_turn_off(self, **kwargs: Any) -> None:
81  """Turn the light off."""
82  _LOGGER.debug("Turning off light")
83  await self.devicedevice.set_light(False)
None _async_update_device_from_protect(self, ProtectDeviceType device)
Definition: light.py:64
None async_setup_entry(HomeAssistant hass, UFPConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:24