Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Vera lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pyvera as veraApi
8 
10  ATTR_BRIGHTNESS,
11  ATTR_HS_COLOR,
12  ENTITY_ID_FORMAT,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import Platform
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 import homeassistant.util.color as color_util
21 
22 from .common import ControllerData, get_controller_data
23 from .entity import VeraEntity
24 
25 
27  hass: HomeAssistant,
28  entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the sensor config entry."""
32  controller_data = get_controller_data(hass, entry)
34  [
35  VeraLight(device, controller_data)
36  for device in controller_data.devices[Platform.LIGHT]
37  ],
38  True,
39  )
40 
41 
42 class VeraLight(VeraEntity[veraApi.VeraDimmer], LightEntity):
43  """Representation of a Vera Light, including dimmable."""
44 
45  _attr_is_on = False
46  _attr_hs_color: tuple[float, float] | None = None
47  _attr_brightness: int | None = None
48 
49  def __init__(
50  self, vera_device: veraApi.VeraDimmer, controller_data: ControllerData
51  ) -> None:
52  """Initialize the light."""
53  VeraEntity.__init__(self, vera_device, controller_data)
54  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.vera_id)
55 
56  @property
57  def color_mode(self) -> ColorMode:
58  """Return the color mode of the light."""
59  if self.vera_device.is_dimmable:
60  if self._attr_hs_color_attr_hs_color:
61  return ColorMode.HS
62  return ColorMode.BRIGHTNESS
63  return ColorMode.ONOFF
64 
65  @property
66  def supported_color_modes(self) -> set[ColorMode]:
67  """Flag supported color modes."""
68  return {self.color_modecolor_modecolor_mode}
69 
70  def turn_on(self, **kwargs: Any) -> None:
71  """Turn the light on."""
72  if ATTR_HS_COLOR in kwargs and self._attr_hs_color_attr_hs_color:
73  rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
74  self.vera_device.set_color(rgb)
75  elif ATTR_BRIGHTNESS in kwargs and self.vera_device.is_dimmable:
76  self.vera_device.set_brightness(kwargs[ATTR_BRIGHTNESS])
77  else:
78  self.vera_device.switch_on()
79 
80  self._attr_is_on_attr_is_on_attr_is_on = True
81  self.schedule_update_ha_stateschedule_update_ha_state(True)
82 
83  def turn_off(self, **kwargs: Any) -> None:
84  """Turn the light off."""
85  self.vera_device.switch_off()
86  self._attr_is_on_attr_is_on_attr_is_on = False
87  self.schedule_update_ha_stateschedule_update_ha_state()
88 
89  def update(self) -> None:
90  """Call to update state."""
91  super().update()
92  self._attr_is_on_attr_is_on_attr_is_on = self.vera_device.is_switched_on()
93  if self.vera_device.is_dimmable:
94  # If it is dimmable, both functions exist. In case color
95  # is not supported, it will return None
96  self._attr_brightness_attr_brightness = self.vera_device.get_brightness()
97  rgb = self.vera_device.get_color()
98  self._attr_hs_color_attr_hs_color = color_util.color_RGB_to_hs(*rgb) if rgb else None
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
None turn_on(self, **Any kwargs)
Definition: light.py:70
None turn_off(self, **Any kwargs)
Definition: light.py:83
None __init__(self, veraApi.VeraDimmer vera_device, ControllerData controller_data)
Definition: light.py:51
set[ColorMode] supported_color_modes(self)
Definition: light.py:66
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
ControllerData get_controller_data(HomeAssistant hass, ConfigEntry config_entry)
Definition: common.py:40
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30