-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathlight.py
99 lines (76 loc) · 3.11 KB
/
light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""The IntelliFire Light."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from intellifire4py.control import IntelliFireController
from intellifire4py.model import 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, LOGGER
from .coordinator import IntellifireDataUpdateCoordinator
from .entity import IntellifireEntity
@dataclass(frozen=True)
class IntellifireLightRequiredKeysMixin:
"""Required keys for fan entity."""
set_fn: Callable[[IntelliFireController, int], Awaitable]
value_fn: Callable[[IntelliFirePollData], int]
@dataclass(frozen=True)
class IntellifireLightEntityDescription(
LightEntityDescription, IntellifireLightRequiredKeysMixin
):
"""Describes a light entity."""
INTELLIFIRE_LIGHTS: tuple[IntellifireLightEntityDescription, ...] = (
IntellifireLightEntityDescription(
key="lights",
translation_key="lights",
set_fn=lambda control_api, level: control_api.set_lights(level=level),
value_fn=lambda data: data.light_level,
),
)
class IntellifireLight(IntellifireEntity, LightEntity):
"""Light entity for the fireplace."""
entity_description: IntellifireLightEntityDescription
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
@property
def brightness(self) -> int:
"""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
LOGGER.debug("Disabling Lights - IntelliFire device does not appear to have one")