-
Notifications
You must be signed in to change notification settings - Fork 1
/
light.py
265 lines (219 loc) · 8.81 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
""" Represent a light """
from ctypes import Union
import math
from typing import Any
import voluptuous as vol
from enocean.utils import combine_hex
import homeassistant.helpers.config_validation as cv
from homeassistant.core import HomeAssistant
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
PLATFORM_SCHEMA,
ColorMode,
LightEntity,
)
from homeassistant.const import CONF_DEVICE_ID, CONF_MODEL, CONF_NAME
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers import device_registry, entity_registry
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity import DeviceInfo
from enocean.protocol.constants import PACKET, RORG
from .device import EltakoEntity
from .const import DOMAIN, LOGGER, CONF_SENDER_ID, DATA_ELTAKO
from .utils import hex_list_to_str
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_DEVICE_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Required(CONF_SENDER_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Optional(CONF_MODEL, default="F4SR14"): vol.In(["F4SR14", "FUD14"]),
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info,
) -> None:
"""Set up Eltako FS14R Light"""
print("VINCENT : SETUP lights")
device_id = config.get(CONF_DEVICE_ID)
device_name = config.get(CONF_NAME)
sender_id = config.get(CONF_SENDER_ID)
model = config.get(CONF_MODEL)
if model == "F4SR14":
light = F4SR14Entity(device_id, device_name, sender_id)
elif model == "FUD14":
light = FUD14Entity(device_id, device_name, sender_id)
add_entities([light])
# if device_name == "SAM":
# config_entry_id = hass.data[DATA_ELTAKO]["config_entry_id"]
# device_reg = device_registry.async_get(hass)
# dev = device_reg.async_get_or_create(
# config_entry_id=config_entry_id,
# name=light.device_name,
# manufacturer=light.manufacturer,
# model=light.model,
# identifiers={(DOMAIN, light.unique_id)}
# )
# LOGGER.debug("VINCENT : entity id " + str(light.unique_id))
# LOGGER.debug("VINCENT : device id " + str(dev.id))
# ent_registry = entity_registry.async_get(hass)
# ent_registry.async_get_or_create(
# domain=DOMAIN,
# platform="homeassistant_elatko",
# unique_id=light.unique_id,
# original_icon="mdi:lightbulb",
# device_id=device.id
# #original_device_class="light",
# original_name=hex_list_to_str(light.device_id)
# )
# def async_setup_entry(
# hass: HomeAssistant,
# entry: ConfigEntry,
# async_add_entities: AddEntitiesCallback
# ):
# LOGGER.debug("VINCENT : Calling light.async_setup_entry entry=%s", entry)
# device_id = [0x00, 0x00, 0x00, 0x00]
# sender_id = [0x00, 0x00, 0x00, 0x00]
# device_name = "Light_Test"
# light = F4SR14Entity(device_id, device_name, sender_id)
# async_add_entities([light], True)
class EltakoLightEntity(EltakoEntity, LightEntity):
"""Represents a generic Eltako light (F4SR14, FUD14)"""
_is_on = None
_sender_id = None
_color_mode = None
_supported_color_modes = {ColorMode.ONOFF}
def __init__(self, device_id, device_name, sender_id) -> None:
super().__init__(device_id, device_name)
self._is_on = None
self._sender_id = sender_id
@property
def is_on(self):
return self._is_on
@property
def assumed_state(self) -> bool:
return self._is_on is None
@property
def color_mode(self): # -> Union[ColorMode, str, None]:
return self._color_mode
@property
def supported_color_modes(self): # -> Union[set[ColorMode], set[str], None]:
return self._supported_color_modes
@property
def unique_id(self):
_id = self.device_id.copy()
_id.extend(self._sender_id)
return combine_hex(_id)
class F4SR14Entity(EltakoLightEntity):
"""Represents an Eltako F4SR14 Light"""
def __init__(self, device_id, device_name, sender_id):
"""Description"""
super().__init__(device_id, device_name, sender_id)
self._color_mode = ColorMode.ONOFF
self.model = "F4SR14"
def turn_on(self, **kwargs: Any) -> None:
# 0 0 0 0 1 0 0 1
# 0 1 2 3 4 5 6 7
data = [RORG.BS4, 0x01, 0x00, 0x00, 0x09] #
data.extend(self._sender_id)
data.extend([0x00]) # status
optional = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0x00]
# optional = [0x00]
# optional.append(self.device_id)
# optional.append([0x00, 0x00])
self.send_command(data, optional, PACKET.RADIO)
# don't set _is_on to true, wait for callback
# self._is_on = True
def turn_off(self, **kwargs: Any) -> None:
command = [RORG.BS4, 0x01, 0x00, 0x00, 0x08]
command.extend(self._sender_id)
command.extend([0x00]) # status
optional = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x34, 0x00]
self.send_command(command, optional, PACKET.RADIO)
# don't set ._is_on to false, wait for callback
# self._is_on = False
def value_changed(self, packet):
"""Updates the internal state of the device when a packet arrives"""
val = packet.data[1]
self._is_on = val == 0x70
# schedule_update_ha_state()
print("VINCENT: update light - light if on : %s" % (str(self._is_on)))
self.schedule_update_ha_state()
LOGGER.debug("VINCENT : DATA " + str(self.hass.data[DATA_ELTAKO]))
# config_entry_id = self.hass.data[DATA_ELTAKO]["config_entry_id"]
# device_reg = device_registry.async_get(self.hass)
# device_reg.async_get_or_create(
# config_entry_id=config_entry_id,
# name=self.device_name,
# manufacturer=self.manufacturer,
# model=self.model,
# identifiers={(DOMAIN, self.unique_id)}
# )
# ent_registry = entity_registry.async_get(self.hass)
# ent_registry.async_get_or_create(
# domain=DOMAIN,
# platform="homeassistant_elatko",
# unique_id=self.unique_id,
# original_icon="mdi:lightbulb",
# device_id=hex_list_to_str(self.device_id),
# #original_device_class="light",
# original_name=hex_list_to_str(self.device_id)
# )
class FUD14Entity(EltakoLightEntity):
"""Represents an Eltako FUD14 Light"""
def __init__(self, device_id, device_name, sender_id):
"""Description"""
super().__init__(device_id, device_name, sender_id)
self._color_mode = ColorMode.ONOFF
self.model = "FUD14"
self._brightness = 77
self._supported_color_modes = {ColorMode.BRIGHTNESS}
self._color_mode = ColorMode.BRIGHTNESS
def turn_on(self, **kwargs: Any) -> None:
if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None:
self._brightness = brightness
val = math.floor(self._brightness / 256.0 * 100.0)
if val == 0:
val = 1
print("turn FUD14 on : %s (brightness %s)" % (str(val), self._brightness))
data = [0xA5, 0x02, val, 0x00, 0x09]
data.extend(self._sender_id)
data.extend([0x00])
# optional = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0x00]
optional = [0x00]
optional.append(self.device_id)
optional.append([0x00, 0x00])
optional = []
self.send_command(data, optional, 0x01)
# don't set _is_on to true, wait for callback
# self._is_on = True
def turn_off(self, **kwargs: Any) -> None:
command = [0xA5, 0x02, 0x00, 0x00, 0x08]
command.extend(self._sender_id)
command.extend([0x00])
optional = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0x00]
self.send_command(command, optional, 0x01)
# don't set ._is_on to false, wait for callback
# self._is_on = False
@property
def brightness(self):
"""Brightness of the light.
This method is optional. Removing it indicates to Home Assistant
that brightness is not supported for this light.
"""
return self._brightness
def value_changed(self, packet):
"""Updates the internal state of the device when a packet arrives"""
val = packet.data[2]
self._is_on = bool(val != 0)
if val != 0:
self._brightness = math.floor(val * 256 / 100)
# schedule_update_ha_state()
print(
"VINCENT: update light - light if on : %s (%s%%)"
% (str(self._is_on), str(val))
)
self.schedule_update_ha_state()