forked from nirnachmani/Pixie-Plus-for-Home-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light.py
265 lines (222 loc) · 9.45 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Platform for Pixie Plus light integration."""
from __future__ import annotations
import logging
import time
import async_timeout
from . import pixiepluslogin
import asyncio
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_TRANSITION,
ATTR_RGB_COLOR,
ATTR_WHITE,
ColorMode,
LightEntity,
LightEntityFeature,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant import config_entries
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers import device_registry as dr
from typing import Any
from .const import (
DOMAIN,
hardware_list,
has_dimming,
has_color,
has_white,
supported_features,
effect_list,
is_light,
is_switch,
is_cover,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
# config: ConfigType,
config_entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Pixie Plus Light platform."""
# Assigning configuration variables from HA config
# The configuration check takes care they are present.
# passing the Pixie Plus devices list with data about all the lights - list of dictionaries, eacy dictionary is a light
coordinator = hass.data[DOMAIN][config_entry.entry_id]
# can the above be coordinator = config_entry.data
# adding entities
async_add_entities(
PixiePlusLight(coordinator, idx)
for idx, ent in enumerate(coordinator.data)
if ((str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2)) in is_light)
or (
((str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2)) not in is_light)
and (
str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2) not in is_switch
)
and (str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2) not in is_cover)
)
)
class PixiePlusLight(CoordinatorEntity, LightEntity):
"""Representation of a Pixie Plus Light."""
def __init__(self, coordinator, idx):
"""Initialize a Pixie Plus Light."""
super().__init__(coordinator)
self.idx = idx
self._name = self.coordinator.data[self.idx]["name"]
self._mac = self.coordinator.data[self.idx]["mac"]
self._id = self.coordinator.data[self.idx]["id"]
self._state = self.coordinator.data[self.idx]["state"]
self._type = self.coordinator.data[self.idx]["type"]
self._stype = self.coordinator.data[self.idx]["stype"]
self._applicationid = self.coordinator.data[self.idx]["applicationid"]
self._installationid = self.coordinator.data[self.idx]["installationid"]
self._clientkey = self.coordinator.data[self.idx]["clientkey"]
self._userid = self.coordinator.data[self.idx]["userid"]
self._homeid = self.coordinator.data[self.idx]["homeid"]
self._livegroup_objectid = self.coordinator.data[self.idx]["livegroup_objectid"]
self._sessiontoken = self.coordinator.data[self.idx]["sessiontoken"]
self._attr_is_on = self.coordinator.data[self.idx]["state"]
self._attr_unique_id = self.coordinator.data[self.idx]["mac"]
self._attr_has_entity_name = True
self._attr_name = None
self._model_no = str(self._type).zfill(2) + str(self._stype).zfill(2)
self._supported_color_modes: set[ColorMode | str] = set()
self._attr_effect_list = []
if (self._model_no in has_dimming) and (self._model_no not in has_color):
self._supported_color_modes.add(ColorMode.BRIGHTNESS)
if self._model_no in has_dimming:
self._brightness = self.coordinator.data[self.idx]["br_cur"]
if self._model_no in has_color:
self._supported_color_modes.add(ColorMode.RGB)
self._rgb_color = ()
if self._model_no in has_white:
self._white = self.coordinator.data[self.idx]["br_cur"]
if (self._model_no not in has_dimming) and (self._model_no not in has_color):
self._supported_color_modes.add(ColorMode.ONOFF)
if self._model_no in supported_features:
if "EFFECT" in supported_features[self._model_no]:
self._attr_supported_features |= LightEntityFeature.EFFECT
if "FLASH" in supported_features[self._model_no]:
self._attr_supported_features |= LightEntityFeature.FLASH
if "TRANSITION" in supported_features[self._model_no]:
self._attr_supported_features |= LightEntityFeature.TRANSITION
if self._model_no in effect_list:
self._attr_effect_list = effect_list[self._model_no]
@property
def device_info(self):
if self._model_no in hardware_list:
model = hardware_list[self._model_no]
else:
model = "Unknown model, assuming is light"
_LOGGER.warning(
f"adding unknown device '{self._name}', model no {self._model_no}, assuming is light with on/off functionality"
)
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self._mac)
},
"name": self._name,
"manufacturer": "SAL - Pixie Plus",
"model": model,
"via_device": (DOMAIN, "Pixie Plus Hub"),
}
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_is_on = self.coordinator.data[self.idx]["state"]
self._state = self.coordinator.data[self.idx]["state"]
if self._model_no in has_dimming:
self._brightness = self.coordinator.data[self.idx]["br_cur"]
if self._model_no in has_white:
self._white = self.coordinator.data[self.idx]["br_cur"]
self.async_write_ha_state()
'''
@property
def name(self) -> str:
"""Return the display name of this light."""
return self._name
'''
@property
def brightness(self) -> int | None:
if self._model_no in has_dimming:
return self.coordinator.data[self.idx]["br_cur"]
else:
return None
@property
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
if (self._model_no in has_dimming) and (self._model_no not in has_color):
return ColorMode.BRIGHTNESS
elif self._model_no in has_color:
return ColorMode.RGB
else:
return ColorMode.ONOFF
@property
def supported_color_modes(self) -> set | None:
"""Flag supported features."""
return self._supported_color_modes
@property
def rgb_color(self) -> tuple[int, int, int] | None:
if self._model_no in has_color:
return self._rgb_color
else:
return None
@property
def is_on(self) -> bool | None:
"""Return true if light is on."""
return self._state
async def async_turn_on(self, **kwargs: Any) -> None:
# Instructs the light to turn on.
other = {}
if self._model_no in has_dimming:
self._brightness = kwargs.get(ATTR_BRIGHTNESS, self._brightness)
if self._model_no in has_color:
rgb_color = kwargs.get(ATTR_RGB_COLOR)
if rgb_color:
self._rgb_color = rgb_color
other.update({"rgb_color": self._rgb_color})
if self._model_no in supported_features:
if "EFFECT" in supported_features[self._model_no]:
effect = kwargs.get(ATTR_EFFECT)
other.update({"effect": effect})
if "FLASH" in supported_features[self._model_no]:
flash = kwargs.get(ATTR_FLASH)
other.update({"flash": flash})
if "TRANSITION" in supported_features[self._model_no]:
transition = kwargs.get(ATTR_TRANSITION)
other.update({"transition": transition})
if self._model_no in has_white:
white = kwargs.get(ATTR_WHITE)
other.update({"white": white})
else:
other = {}
if self._model_no in has_dimming and self._brightness > 0:
brightness_hex = f"{int(self._brightness):02x}"
else:
brightness_hex = "on"
await pixiepluslogin.change_light(self, brightness_hex, other)
# assumes success - will get a push update after few second and will adjust according to the real state
self.coordinator.data[self.idx]["state"] = "True"
if self._model_no in has_dimming:
self.coordinator.data[self.idx]["br_cur"] = self._brightness
self.coordinator.async_set_updated_data(self.coordinator.data)
# await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
other = {}
await pixiepluslogin.change_light(self, "00", other)
self.coordinator.data[self.idx]["state"] = ""
self.coordinator.async_set_updated_data(self.coordinator.data)
# await self.coordinator.async_request_refresh()