Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """The IntelliFire Light."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from intellifire4py.control import IntelliFireController
10 from intellifire4py.model import IntelliFirePollData
11 
13  ATTR_BRIGHTNESS,
14  ColorMode,
15  LightEntity,
16  LightEntityDescription,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN, LOGGER
23 from .coordinator import IntellifireDataUpdateCoordinator
24 from .entity import IntellifireEntity
25 
26 
27 @dataclass(frozen=True)
29  """Required keys for fan entity."""
30 
31  set_fn: Callable[[IntelliFireController, int], Awaitable]
32  value_fn: Callable[[IntelliFirePollData], int]
33 
34 
35 @dataclass(frozen=True)
37  LightEntityDescription, IntellifireLightRequiredKeysMixin
38 ):
39  """Describes a light entity."""
40 
41 
42 INTELLIFIRE_LIGHTS: tuple[IntellifireLightEntityDescription, ...] = (
44  key="lights",
45  translation_key="lights",
46  set_fn=lambda control_api, level: control_api.set_lights(level=level),
47  value_fn=lambda data: data.light_level,
48  ),
49 )
50 
51 
53  """Light entity for the fireplace."""
54 
55  entity_description: IntellifireLightEntityDescription
56  _attr_color_mode = ColorMode.BRIGHTNESS
57  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
58 
59  @property
60  def brightness(self) -> int:
61  """Return the current brightness 0-255."""
62  return 85 * self.entity_descriptionentity_description.value_fn(self.coordinator.read_api.data)
63 
64  @property
65  def is_on(self):
66  """Return true if light is on."""
67  return self.entity_descriptionentity_description.value_fn(self.coordinator.read_api.data) >= 1
68 
69  async def async_turn_on(self, **kwargs: Any) -> None:
70  """Instruct the light to turn on."""
71  if ATTR_BRIGHTNESS in kwargs:
72  light_level = int(kwargs[ATTR_BRIGHTNESS] / 85)
73  else:
74  light_level = 2
75 
76  await self.entity_descriptionentity_description.set_fn(self.coordinator.control_api, light_level)
77  await self.coordinator.async_request_refresh()
78 
79  async def async_turn_off(self, **kwargs: Any) -> None:
80  """Instruct the light to turn off."""
81  await self.entity_descriptionentity_description.set_fn(self.coordinator.control_api, 0)
82  await self.coordinator.async_request_refresh()
83 
84 
86  hass: HomeAssistant,
87  entry: ConfigEntry,
88  async_add_entities: AddEntitiesCallback,
89 ) -> None:
90  """Set up the fans."""
91  coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
92 
93  if coordinator.data.has_light:
95  IntellifireLight(coordinator=coordinator, description=description)
96  for description in INTELLIFIRE_LIGHTS
97  )
98  return
99  LOGGER.debug("Disabling Lights - IntelliFire device does not appear to have one")
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:89