From 6970a8a87afb038a28dbe08bd89273b68d9388c8 Mon Sep 17 00:00:00 2001 From: Jeef Date: Mon, 9 Jan 2023 15:16:39 -0700 Subject: [PATCH] Add IntelliFire lights (#79816) --- .coveragerc | 1 + .../components/intellifire/__init__.py | 1 + homeassistant/components/intellifire/light.py | 97 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 homeassistant/components/intellifire/light.py diff --git a/.coveragerc b/.coveragerc index 8170fec993e..a864544fe37 100644 --- a/.coveragerc +++ b/.coveragerc @@ -583,6 +583,7 @@ omit = homeassistant/components/intellifire/coordinator.py homeassistant/components/intellifire/entity.py homeassistant/components/intellifire/fan.py + homeassistant/components/intellifire/light.py homeassistant/components/intellifire/number.py homeassistant/components/intellifire/sensor.py homeassistant/components/intellifire/switch.py diff --git a/homeassistant/components/intellifire/__init__.py b/homeassistant/components/intellifire/__init__.py index e4e4f1a66c9..81ef383dfab 100644 --- a/homeassistant/components/intellifire/__init__.py +++ b/homeassistant/components/intellifire/__init__.py @@ -24,6 +24,7 @@ PLATFORMS = [ Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.FAN, + Platform.LIGHT, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH, diff --git a/homeassistant/components/intellifire/light.py b/homeassistant/components/intellifire/light.py new file mode 100644 index 00000000000..f1fd81ab452 --- /dev/null +++ b/homeassistant/components/intellifire/light.py @@ -0,0 +1,97 @@ +"""The IntelliFire Light.""" +from __future__ import annotations + +from collections.abc import Awaitable, Callable +from dataclasses import dataclass +from typing import Any + +from intellifire4py import IntellifireControlAsync, IntellifirePollData + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ColorMode, + LightEntity, + LightEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import IntellifireDataUpdateCoordinator +from .entity import IntellifireEntity + + +@dataclass +class IntellifireLightRequiredKeysMixin: + """Required keys for fan entity.""" + + set_fn: Callable[[IntellifireControlAsync, int], Awaitable] + value_fn: Callable[[IntellifirePollData], bool] + + +@dataclass +class IntellifireLightEntityDescription( + LightEntityDescription, IntellifireLightRequiredKeysMixin +): + """Describes a light entity.""" + + +INTELLIFIRE_LIGHTS: tuple[IntellifireLightEntityDescription, ...] = ( + IntellifireLightEntityDescription( + key="lights", + name="Lights", + has_entity_name=True, + set_fn=lambda control_api, level: control_api.set_lights(level=level), + value_fn=lambda data: data.light_level, + ), +) + + +class IntellifireLight(IntellifireEntity, LightEntity): + """This is a Light entity for the fireplace.""" + + entity_description: IntellifireLightEntityDescription + _attr_color_mode = ColorMode.BRIGHTNESS + _attr_supported_color_modes = {ColorMode.BRIGHTNESS} + + @property + def brightness(self): + """Return the current brightness 0-255.""" + return 85 * self.entity_description.value_fn(self.coordinator.read_api.data) + + @property + def is_on(self): + """Return true if light is on.""" + return self.entity_description.value_fn(self.coordinator.read_api.data) >= 1 + + async def async_turn_on(self, **kwargs: Any) -> None: + """Instruct the light to turn on.""" + if ATTR_BRIGHTNESS in kwargs: + light_level = int(kwargs[ATTR_BRIGHTNESS] / 85) + else: + light_level = 2 + + await self.entity_description.set_fn(self.coordinator.control_api, light_level) + await self.coordinator.async_request_refresh() + + async def async_turn_off(self, **kwargs: Any) -> None: + """Instruct the light to turn off.""" + await self.entity_description.set_fn(self.coordinator.control_api, 0) + await self.coordinator.async_request_refresh() + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the fans.""" + coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + if coordinator.data.has_light: + async_add_entities( + IntellifireLight(coordinator=coordinator, description=description) + for description in INTELLIFIRE_LIGHTS + ) + return