diff --git a/.coveragerc b/.coveragerc index a3666ff0ac4..4b3525e278c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -321,6 +321,7 @@ omit = homeassistant/components/fjaraskupan/binary_sensor.py homeassistant/components/fjaraskupan/const.py homeassistant/components/fjaraskupan/fan.py + homeassistant/components/fjaraskupan/light.py homeassistant/components/fleetgo/device_tracker.py homeassistant/components/flexit/climate.py homeassistant/components/flic/binary_sensor.py diff --git a/homeassistant/components/fjaraskupan/__init__.py b/homeassistant/components/fjaraskupan/__init__.py index 50c07a96c51..59a41e2e83b 100644 --- a/homeassistant/components/fjaraskupan/__init__.py +++ b/homeassistant/components/fjaraskupan/__init__.py @@ -23,7 +23,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DISPATCH_DETECTION, DOMAIN -PLATFORMS = ["binary_sensor", "fan"] +PLATFORMS = ["binary_sensor", "fan", "light"] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/fjaraskupan/light.py b/homeassistant/components/fjaraskupan/light.py new file mode 100644 index 00000000000..8c44460a099 --- /dev/null +++ b/homeassistant/components/fjaraskupan/light.py @@ -0,0 +1,85 @@ +"""Support for lights.""" +from __future__ import annotations + +from fjaraskupan import COMMAND_LIGHT_ON_OFF, Device, State + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + COLOR_MODE_BRIGHTNESS, + LightEntity, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity import DeviceInfo, Entity +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, +) + +from . import DeviceState, async_setup_entry_platform + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up tuya sensors dynamically through tuya discovery.""" + + def _constructor(device_state: DeviceState) -> list[Entity]: + return [ + Light( + device_state.coordinator, device_state.device, device_state.device_info + ) + ] + + async_setup_entry_platform(hass, config_entry, async_add_entities, _constructor) + + +class Light(CoordinatorEntity[State], LightEntity): + """Light device.""" + + def __init__( + self, + coordinator: DataUpdateCoordinator[State], + device: Device, + device_info: DeviceInfo, + ) -> None: + """Init light entity.""" + super().__init__(coordinator) + self._device = device + self._attr_color_mode = COLOR_MODE_BRIGHTNESS + self._attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS} + self._attr_unique_id = device.address + self._attr_device_info = device_info + self._attr_name = device_info["name"] + + async def async_turn_on(self, **kwargs): + """Turn the light on.""" + if ATTR_BRIGHTNESS in kwargs: + await self._device.send_dim(int(kwargs[ATTR_BRIGHTNESS] * (100.0 / 255.0))) + else: + if not self.is_on: + await self._device.send_command(COMMAND_LIGHT_ON_OFF) + self.coordinator.async_set_updated_data(self._device.state) + + async def async_turn_off(self, **kwargs) -> None: + """Turn the entity off.""" + if self.is_on: + await self._device.send_command(COMMAND_LIGHT_ON_OFF) + self.coordinator.async_set_updated_data(self._device.state) + + @property + def is_on(self) -> bool: + """Return True if entity is on.""" + if data := self.coordinator.data: + return data.light_on + return False + + @property + def brightness(self) -> int | None: + """Return the brightness of this light between 0..255.""" + if data := self.coordinator.data: + return int(data.dim_level * (255.0 / 100.0)) + return None