Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for the light on the Sisyphus Kinetic Art Table."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import aiohttp
9 
10 from homeassistant.components.light import ColorMode, LightEntity
11 from homeassistant.const import CONF_HOST
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import PlatformNotReady
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16 
17 from . import DATA_SISYPHUS
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  hass: HomeAssistant,
24  config: ConfigType,
25  add_entities: AddEntitiesCallback,
26  discovery_info: DiscoveryInfoType | None = None,
27 ) -> None:
28  """Set up a single Sisyphus table."""
29  if not discovery_info:
30  return
31  host = discovery_info[CONF_HOST]
32  try:
33  table_holder = hass.data[DATA_SISYPHUS][host]
34  table = await table_holder.get_table()
35  except aiohttp.ClientError as err:
36  raise PlatformNotReady from err
37 
38  add_entities([SisyphusLight(table_holder.name, table)], update_before_add=True)
39 
40 
42  """Representation of a Sisyphus table as a light."""
43 
44  _attr_color_mode = ColorMode.BRIGHTNESS
45  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
46 
47  def __init__(self, name, table):
48  """Initialize the Sisyphus table."""
49  self._name_name = name
50  self._table_table = table
51 
52  async def async_added_to_hass(self) -> None:
53  """Add listeners after this object has been initialized."""
54  self._table_table.add_listener(self.async_write_ha_stateasync_write_ha_state)
55 
56  async def async_update(self) -> None:
57  """Force update the table state."""
58  await self._table_table.refresh()
59 
60  @property
61  def available(self) -> bool:
62  """Return true if the table is responding to heartbeats."""
63  return self._table_table.is_connected
64 
65  @property
66  def unique_id(self):
67  """Return the UUID of the table."""
68  return self._table_table.id
69 
70  @property
71  def name(self):
72  """Return the ame of the table."""
73  return self._name_name
74 
75  @property
76  def is_on(self):
77  """Return True if the table is on."""
78  return not self._table_table.is_sleeping
79 
80  @property
81  def brightness(self):
82  """Return the current brightness of the table's ring light."""
83  return self._table_table.brightness * 255
84 
85  async def async_turn_off(self, **kwargs: Any) -> None:
86  """Put the table to sleep."""
87  await self._table_table.sleep()
88  _LOGGER.debug("Sisyphus table %s: sleep")
89 
90  async def async_turn_on(self, **kwargs: Any) -> None:
91  """Wake up the table if necessary, optionally changes brightness."""
92  if not self.is_onis_onis_on:
93  await self._table_table.wakeup()
94  _LOGGER.debug("Sisyphus table %s: wakeup")
95 
96  if "brightness" in kwargs:
97  await self._table_table.set_brightness(kwargs["brightness"] / 255.0)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:27