|
| 1 | +"""Platform for Lunatone light integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from homeassistant.components.light import ColorMode, LightEntity |
| 9 | +from homeassistant.core import HomeAssistant, callback |
| 10 | +from homeassistant.helpers.device_registry import DeviceInfo |
| 11 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 12 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 13 | + |
| 14 | +from .const import DOMAIN |
| 15 | +from .coordinator import LunatoneConfigEntry, LunatoneDevicesDataUpdateCoordinator |
| 16 | + |
| 17 | +PARALLEL_UPDATES = 0 |
| 18 | +STATUS_UPDATE_DELAY = 0.04 |
| 19 | + |
| 20 | + |
| 21 | +async def async_setup_entry( |
| 22 | + hass: HomeAssistant, |
| 23 | + config_entry: LunatoneConfigEntry, |
| 24 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 25 | +) -> None: |
| 26 | + """Set up the Lunatone Light platform.""" |
| 27 | + coordinator_info = config_entry.runtime_data.coordinator_info |
| 28 | + coordinator_devices = config_entry.runtime_data.coordinator_devices |
| 29 | + |
| 30 | + async_add_entities( |
| 31 | + [ |
| 32 | + LunatoneLight( |
| 33 | + coordinator_devices, device_id, coordinator_info.data.device.serial |
| 34 | + ) |
| 35 | + for device_id in coordinator_devices.data |
| 36 | + ] |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +class LunatoneLight( |
| 41 | + CoordinatorEntity[LunatoneDevicesDataUpdateCoordinator], LightEntity |
| 42 | +): |
| 43 | + """Representation of a Lunatone light.""" |
| 44 | + |
| 45 | + _attr_color_mode = ColorMode.ONOFF |
| 46 | + _attr_supported_color_modes = {ColorMode.ONOFF} |
| 47 | + _attr_has_entity_name = True |
| 48 | + _attr_name = None |
| 49 | + _attr_should_poll = False |
| 50 | + |
| 51 | + def __init__( |
| 52 | + self, |
| 53 | + coordinator: LunatoneDevicesDataUpdateCoordinator, |
| 54 | + device_id: int, |
| 55 | + interface_serial_number: int, |
| 56 | + ) -> None: |
| 57 | + """Initialize a LunatoneLight.""" |
| 58 | + super().__init__(coordinator=coordinator) |
| 59 | + self._device_id = device_id |
| 60 | + self._interface_serial_number = interface_serial_number |
| 61 | + self._device = self.coordinator.data.get(self._device_id) |
| 62 | + self._attr_unique_id = f"{interface_serial_number}-device{device_id}" |
| 63 | + |
| 64 | + @property |
| 65 | + def device_info(self) -> DeviceInfo: |
| 66 | + """Return the device info.""" |
| 67 | + assert self.unique_id |
| 68 | + name = self._device.name if self._device is not None else None |
| 69 | + return DeviceInfo( |
| 70 | + identifiers={(DOMAIN, self.unique_id)}, |
| 71 | + name=name, |
| 72 | + via_device=(DOMAIN, str(self._interface_serial_number)), |
| 73 | + ) |
| 74 | + |
| 75 | + @property |
| 76 | + def available(self) -> bool: |
| 77 | + """Return True if entity is available.""" |
| 78 | + return super().available and self._device is not None |
| 79 | + |
| 80 | + @property |
| 81 | + def is_on(self) -> bool: |
| 82 | + """Return True if light is on.""" |
| 83 | + return self._device is not None and self._device.is_on |
| 84 | + |
| 85 | + @callback |
| 86 | + def _handle_coordinator_update(self) -> None: |
| 87 | + """Handle updated data from the coordinator.""" |
| 88 | + self._device = self.coordinator.data.get(self._device_id) |
| 89 | + self.async_write_ha_state() |
| 90 | + |
| 91 | + async def async_turn_on(self, **kwargs: Any) -> None: |
| 92 | + """Instruct the light to turn on.""" |
| 93 | + assert self._device |
| 94 | + await self._device.switch_on() |
| 95 | + await asyncio.sleep(STATUS_UPDATE_DELAY) |
| 96 | + await self.coordinator.async_refresh() |
| 97 | + |
| 98 | + async def async_turn_off(self, **kwargs: Any) -> None: |
| 99 | + """Instruct the light to turn off.""" |
| 100 | + assert self._device |
| 101 | + await self._device.switch_off() |
| 102 | + await asyncio.sleep(STATUS_UPDATE_DELAY) |
| 103 | + await self.coordinator.async_refresh() |
0 commit comments