This repository was archived by the owner on Jul 25, 2022. It is now read-only.
forked from bdraco/baf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight.py
More file actions
110 lines (91 loc) · 3.6 KB
/
light.py
File metadata and controls
110 lines (91 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Support for Big Ass Fans lights."""
from __future__ import annotations
from typing import Any
from aiobafi6 import Device, OffOnAuto
from homeassistant import config_entries
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ColorMode,
LightEntity,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import (
color_temperature_kelvin_to_mired,
color_temperature_mired_to_kelvin,
)
from .const import DOMAIN
from .entity import BAFEntity
from .models import BAFData
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up BAF lights."""
data: BAFData = hass.data[DOMAIN][entry.entry_id]
device = data.device
if not device.has_light:
return
if not device.has_fan:
async_add_entities([BAFStandaloneLight(device)])
else:
async_add_entities([BAFFanLight(device)])
class BAFLight(BAFEntity, LightEntity):
"""Representation of a Big Ass Fans light."""
@callback
def _async_update_attrs(self) -> None:
"""Update attrs from device."""
device = self._device
self._attr_is_on = device.light_mode == OffOnAuto.ON
if self._device.light_brightness_level is not None:
self._attr_brightness = int(
min(255, self._device.light_brightness_level * 16)
)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None:
# set the brightness, which will also turn on/off light
if brightness == 255:
brightness = 256 # this will end up as 16 which is max
self._device.light_brightness_level = int(brightness / 16)
else:
self._device.light_mode = OffOnAuto.ON
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
self._device.light_mode = OffOnAuto.OFF
class BAFFanLight(BAFLight):
"""Representation of a Big Ass Fans light on a fan."""
def __init__(self, device: Device) -> None:
"""Init a fan light."""
super().__init__(device, device.name)
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
self._attr_color_mode = ColorMode.BRIGHTNESS
class BAFStandaloneLight(BAFLight):
"""Representation of a Big Ass Fans light."""
def __init__(self, device: Device) -> None:
"""Init a standalone light."""
super().__init__(device, f"{device.name} Light")
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP}
self._attr_color_mode = ColorMode.COLOR_TEMP
self._attr_min_mireds = color_temperature_kelvin_to_mired(
device.light_warmest_color_temperature
)
self._attr_max_mireds = color_temperature_kelvin_to_mired(
device.light_coolest_color_temperature
)
@callback
def _async_update_attrs(self) -> None:
"""Update attrs from device."""
super()._async_update_attrs()
self._attr_color_temp = color_temperature_kelvin_to_mired(
self._device.light_color_temperature
)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
if (color_temp := kwargs.get(ATTR_COLOR_TEMP)) is not None:
self._device.light_color_temperature = color_temperature_mired_to_kelvin(
color_temp
)
await super().async_turn_on(**kwargs)