forked from gjbadros/hass-vantage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.py
233 lines (198 loc) · 7.99 KB
/
light.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
Support for Vantage lights.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.vantage/
"""
import logging
import asyncio
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_KELVIN,
ATTR_RGB_COLOR,
ATTR_TRANSITION,
ATTR_HS_COLOR,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_TRANSITION,
LIGHT_TURN_ON_SCHEMA,
DOMAIN,
LightEntity,
)
from homeassistant.util.color import (
color_hs_to_RGB,
color_temperature_to_rgb,
color_RGB_to_hs,
color_temperature_mired_to_kelvin,
color_temperature_kelvin_to_mired,
)
from homeassistant.helpers import entity_platform
from homeassistant.helpers.service import async_extract_entity_ids
from ..vantage import VantageDevice, VANTAGE_DEVICES, VANTAGE_CONTROLLER
_LOGGER = logging.getLogger(__name__)
VANTAGE_SET_STATE_SCHEMA = LIGHT_TURN_ON_SCHEMA
SERVICE_VANTAGE_SET_STATE = "set_state"
DEPENDENCIES = ["vantage"]
# pylint: disable=unused-argument
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the Vantage lights."""
devs = []
async def async_handle_set_state(call):
entity_ids = await async_extract_entity_ids(hass, call, True)
if entity_ids:
entities = [entity for entity in devs if entity.entity_id in entity_ids]
# tasks = []
for light in entities:
await light.set_state(**call.data)
# task = light.set_state(**call.data)
# tasks.append(hass.async_create_task(task))
# if tasks:
# await asyncio.wait(tasks)
for (area_name, device) in hass.data[VANTAGE_DEVICES]["light"]:
dev = VantageLight(area_name, device, hass.data[VANTAGE_CONTROLLER])
devs.append(dev)
async_add_devices(devs, True)
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SERVICE_VANTAGE_SET_STATE,
VANTAGE_SET_STATE_SCHEMA, "set_state"
)
return True
def to_vantage_level(level):
"""Convert the given HASS light level (0-255) to Vantage (0.0-100.0)."""
return float((level * 100) / 255)
def to_hass_level(level):
"""Convert the given Vantage (0.0-100.0) light level to HASS (0-255)."""
return int((level * 255) / 100)
class VantageLight(VantageDevice, LightEntity):
"""Representation of a Vantage Light, including dimmable."""
def __init__(self, area_name, vantage_device, controller):
"""Initialize the light."""
self._prev_brightness = None
VantageDevice.__init__(self, area_name, vantage_device, controller)
@property
def supported_features(self):
"""Flag supported features."""
return (
SUPPORT_BRIGHTNESS
| SUPPORT_TRANSITION
| (SUPPORT_COLOR_TEMP if self._vantage_device.support_color_temp else 0)
| (SUPPORT_COLOR if self._vantage_device.support_color else 0)
)
@property
def brightness(self):
"""Return the brightness of the light."""
new_brightness = to_hass_level(self._vantage_device.last_level())
if new_brightness != 0:
self._prev_brightness = new_brightness
return new_brightness
@property
def color_temp(self):
"""Return the color temperature of the light."""
ct = self._vantage_device._color_temp
return color_temperature_kelvin_to_mired(ct)
@property
def rgb_color(self):
"""Return the RGB color value."""
return self._vantage_device.rgb
def color_temperature_to_dw_27k41k(self, kelvin):
"""Convert a kelvin color temperature to a pair of values
for a dual-white LED fixture with a 27K white and a 41K white
light source."""
if kelvin < 2700:
red = 255
blue = 0
elif kelvin > 4100:
red = 0
blue = 255
else:
frac = (kelvin - 2700) / (4100 - 2700)
blue = frac * 255
red = 255 - blue
# max_color = max(red, blue)
# ratio = 255 / max_color * self.brightness / 255
answer = (red, 0, blue)
_LOGGER.debug("using %s for color temp %s", answer, kelvin)
return answer
@property
def hs_color(self):
"""Return the HS color value."""
# rgb = self._vantage_device.rgb
# hs = color_RGB_to_hs(*rgb)
# return hs # self._vantage_device.hs
return self._vantage_device.hs
def _set_level(self, brightness):
"""Set the level, including other dirty properties."""
self._vantage_device.level = to_vantage_level(brightness)
async def async_turn_on(self, **kwargs):
if ATTR_BRIGHTNESS in kwargs and self._vantage_device.is_dimmable:
brightness = kwargs[ATTR_BRIGHTNESS]
elif self._prev_brightness == 0:
brightness = 255
else:
brightness = self._prev_brightness
self._prev_brightness = brightness
kwargs[ATTR_BRIGHTNESS] = brightness
self.hass.async_create_task(self.set_state(**kwargs))
def _set_ramp(self, **kwargs):
transition_time_in_s = kwargs.get(ATTR_TRANSITION, 1)
self._vantage_device.set_ramp_sec(transition_time_in_s,
transition_time_in_s,
transition_time_in_s)
async def set_state(self, **kwargs):
"""Turn the light on."""
_LOGGER.debug("light.set_state(%s) to %s",
self._vantage_device, kwargs)
self._set_ramp(**kwargs)
if ATTR_BRIGHTNESS in kwargs:
# TODO: is_dimmable test fails for GROUP load types
# and self._vantage_device.is_dimmable:
brightness = kwargs[ATTR_BRIGHTNESS]
self._set_level(brightness)
if ATTR_RGB_COLOR in kwargs:
_LOGGER.debug("%s set via ATTR_RGB_COLOR", self)
self._vantage_device.rgb = kwargs[ATTR_RGB_COLOR]
elif ATTR_HS_COLOR in kwargs:
_LOGGER.debug("%s set via ATTR_HS_COLOR", self)
hs_color = kwargs[ATTR_HS_COLOR]
# rgb = color_hs_to_RGB(*hs_color)
# self._vantage_device.rgb = [*rgb]
self._vantage_device.hs = hs_color
elif ATTR_COLOR_TEMP in kwargs or ATTR_KELVIN in kwargs:
if ATTR_KELVIN in kwargs:
_LOGGER.debug(
"%s set via ATTR_KELVIN - %s", self, kwargs[ATTR_KELVIN]
)
kelvin = kwargs[ATTR_KELVIN]
else:
_LOGGER.debug(
"%s set via ATTR_COLOR_TEMP - %s", self, kwargs[ATTR_COLOR_TEMP]
)
# Color temp in HA is in mireds:
# https://en.wikipedia.org/wiki/Mired
# M = 1000000/KELVIN_TEMP
kelvin = int(color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
_LOGGER.debug("%s vantage color temp kelvin = %s", self, kelvin)
if self._vantage_device._dmx_color:
# do conversion
rgb = color_temperature_to_rgb(kelvin)
self._vantage_device.rgb = [*rgb]
elif self._vantage_device._load_type == "DW":
rgb = self.color_temperature_to_dw_27k41k(kelvin)
self._vantage_device.rgb = [*rgb]
self._vantage_device.color_temp = kelvin
await self.async_update_ha_state()
async def async_turn_off(self, **kwargs):
"""Turn the light off."""
self._set_ramp(**kwargs)
self._vantage_device.level = 0
await self.async_update_ha_state()
@property
def is_on(self):
"""Return true if device is on."""
return self._vantage_device.last_level() > 0
async def async_update(self):
"""Call when forcing a refresh of the device."""
if self._prev_brightness is None:
self._prev_brightness = to_hass_level(self._vantage_device.level)