Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Firmata light output."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_MAXIMUM, CONF_MINIMUM, CONF_NAME, CONF_PIN
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .board import FirmataPinType
15 from .const import CONF_INITIAL_STATE, CONF_PIN_MODE, DOMAIN
16 from .entity import FirmataPinEntity
17 from .pin import FirmataBoardPin, FirmataPinUsedException, FirmataPWMOutput
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  hass: HomeAssistant,
24  config_entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up the Firmata lights."""
28  new_entities = []
29 
30  board = hass.data[DOMAIN][config_entry.entry_id]
31  for light in board.lights:
32  pin = light[CONF_PIN]
33  pin_mode = light[CONF_PIN_MODE]
34  initial = light[CONF_INITIAL_STATE]
35  minimum = light[CONF_MINIMUM]
36  maximum = light[CONF_MAXIMUM]
37  api = FirmataPWMOutput(board, pin, pin_mode, initial, minimum, maximum)
38  try:
39  api.setup()
40  except FirmataPinUsedException:
41  _LOGGER.error(
42  "Could not setup light on pin %s since pin already in use",
43  light[CONF_PIN],
44  )
45  continue
46  name = light[CONF_NAME]
47  light_entity = FirmataLight(api, config_entry, name, pin)
48  new_entities.append(light_entity)
49 
50  async_add_entities(new_entities)
51 
52 
54  """Representation of a light on a Firmata board."""
55 
56  _attr_color_mode = ColorMode.BRIGHTNESS
57  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
58 
59  def __init__(
60  self,
61  api: FirmataBoardPin,
62  config_entry: ConfigEntry,
63  name: str,
64  pin: FirmataPinType,
65  ) -> None:
66  """Initialize the light pin entity."""
67  super().__init__(api, config_entry, name, pin)
68 
69  # Default first turn on to max
70  self._last_on_level_last_on_level = 255
71 
72  async def async_added_to_hass(self) -> None:
73  """Set up a light."""
74  await self._api_api.start_pin()
75 
76  @property
77  def is_on(self) -> bool:
78  """Return true if light is on."""
79  return self._api_api.state > 0
80 
81  @property
82  def brightness(self) -> int:
83  """Return the brightness of the light."""
84  return self._api_api.state
85 
86  async def async_turn_on(self, **kwargs: Any) -> None:
87  """Turn on light."""
88  level = kwargs.get(ATTR_BRIGHTNESS, self._last_on_level_last_on_level)
89  await self._api_api.set_level(level)
90  self.async_write_ha_stateasync_write_ha_state()
91  self._last_on_level_last_on_level = level
92 
93  async def async_turn_off(self, **kwargs: Any) -> None:
94  """Turn off light."""
95  await self._api_api.set_level(0)
96  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, FirmataBoardPin api, ConfigEntry config_entry, str name, FirmataPinType pin)
Definition: light.py:65
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:26