1 """Component providing Lights for UniFi Protect."""
3 from __future__
import annotations
8 from uiprotect.data
import Light, ModelType, ProtectAdoptableDeviceModel
14 from .data
import ProtectDeviceType, UFPConfigEntry
15 from .entity
import ProtectDeviceEntity
17 _LOGGER = logging.getLogger(__name__)
22 entry: UFPConfigEntry,
23 async_add_entities: AddEntitiesCallback,
25 """Set up lights for UniFi Protect integration."""
26 data = entry.runtime_data
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
35 data.async_subscribe_adopt(_add_new_device)
38 for device
in data.get_by_types({ModelType.LIGHT})
39 if device.can_write(data.api.bootstrap.auth_user)
44 """Convert unifi brightness 1..6 to hass format 0..255."""
45 return min(255, round((value / 6) * 255))
49 """Convert hass brightness 0..255 to unifi 1..6 scale."""
50 return max(1, round((value / 255) * 6))
54 """A Ubiquiti UniFi Protect Light Entity."""
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")
66 updated_device = self.
devicedevice
69 updated_device.light_device_settings.led_level
73 """Turn the light on."""
74 hass_brightness = kwargs.get(ATTR_BRIGHTNESS, self.
brightnessbrightness)
77 _LOGGER.debug(
"Turning on light with brightness %s", unifi_brightness)
78 await self.
devicedevice.set_light(
True, unifi_brightness)
81 """Turn the light off."""
82 _LOGGER.debug(
"Turning off light")
83 await self.
devicedevice.set_light(
False)
int|None brightness(self)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None _async_update_device_from_protect(self, ProtectDeviceType device)
None async_setup_entry(HomeAssistant hass, UFPConfigEntry entry, AddEntitiesCallback async_add_entities)
int hass_to_unifi_brightness(int value)
int unifi_brightness_to_hass(int value)