Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Big Ass Fans lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiobafi6 import Device, OffOnAuto
8 
10  ATTR_BRIGHTNESS,
11  ATTR_COLOR_TEMP,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.util.color import (
18  color_temperature_kelvin_to_mired,
19  color_temperature_mired_to_kelvin,
20 )
21 
22 from . import BAFConfigEntry
23 from .entity import BAFEntity
24 
25 
27  hass: HomeAssistant,
28  entry: BAFConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up BAF lights."""
32  device = entry.runtime_data
33  if device.has_light:
34  klass = BAFFanLight if device.has_fan else BAFStandaloneLight
35  async_add_entities([klass(device)])
36 
37 
39  """Representation of a Big Ass Fans light."""
40 
41  _attr_name = None
42 
43  @callback
44  def _async_update_attrs(self) -> None:
45  """Update attrs from device."""
46  self._attr_is_on_attr_is_on = self._device_device.light_mode == OffOnAuto.ON
47  if self._device_device.light_brightness_level is not None:
48  self._attr_brightness_attr_brightness = round(
49  self._device_device.light_brightness_level / 16 * 255
50  )
51 
52  async def async_turn_on(self, **kwargs: Any) -> None:
53  """Turn on the light."""
54  if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None:
55  self._device_device.light_brightness_level = max(int(brightness / 255 * 16), 1)
56  else:
57  self._device_device.light_mode = OffOnAuto.ON
58 
59  async def async_turn_off(self, **kwargs: Any) -> None:
60  """Turn off the light."""
61  self._device_device.light_mode = OffOnAuto.OFF
62 
63 
65  """Representation of a Big Ass Fans light on a fan."""
66 
67  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
68  _attr_color_mode = ColorMode.BRIGHTNESS
69 
70 
72  """Representation of a Big Ass Fans light."""
73 
74  _attr_supported_color_modes = {ColorMode.COLOR_TEMP}
75  _attr_color_mode = ColorMode.COLOR_TEMP
76 
77  def __init__(self, device: Device) -> None:
78  """Init a standalone light."""
79  super().__init__(device)
81  device.light_warmest_color_temperature
82  )
84  device.light_coolest_color_temperature
85  )
86 
87  @callback
88  def _async_update_attrs(self) -> None:
89  """Update attrs from device."""
90  super()._async_update_attrs()
92  self._device_device.light_color_temperature
93  )
94 
95  async def async_turn_on(self, **kwargs: Any) -> None:
96  """Turn on the light."""
97  if (color_temp := kwargs.get(ATTR_COLOR_TEMP)) is not None:
98  self._device_device.light_color_temperature = color_temperature_mired_to_kelvin(
99  color_temp
100  )
101  await super().async_turn_on(**kwargs)
None async_turn_off(self, **Any kwargs)
Definition: light.py:59
None async_turn_on(self, **Any kwargs)
Definition: light.py:52
None async_setup_entry(HomeAssistant hass, BAFConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30
int color_temperature_mired_to_kelvin(float mired_temperature)
Definition: color.py:631
int color_temperature_kelvin_to_mired(float kelvin_temperature)
Definition: color.py:636