-
Notifications
You must be signed in to change notification settings - Fork 4
/
light.py
236 lines (190 loc) · 8.52 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
"""Support for Eltako light sources."""
from __future__ import annotations
import math
from typing import Any
from eltakobus.util import AddressExpression
from eltakobus.eep import *
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
PLATFORM_SCHEMA,
ColorMode,
LightEntity,
)
from homeassistant import config_entries
from homeassistant.const import CONF_ID, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .device import EltakoEntity
from .const import CONF_ID_REGEX, CONF_EEP, CONF_SENDER, DOMAIN, MANUFACTURER, DATA_ELTAKO, ELTAKO_CONFIG, ELTAKO_GATEWAY, LOGGER
async def async_setup_entry(
hass: HomeAssistant,
config_entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Eltako light platform."""
config: ConfigType = hass.data[DATA_ELTAKO][ELTAKO_CONFIG]
gateway = hass.data[DATA_ELTAKO][ELTAKO_GATEWAY]
entities: list[EltakoSensor] = []
if Platform.LIGHT in config:
for entity_config in config[Platform.LIGHT]:
dev_id = AddressExpression.parse(entity_config.get(CONF_ID))
dev_name = entity_config.get(CONF_NAME)
eep_string = entity_config.get(CONF_EEP)
sender_config = entity_config.get(CONF_SENDER)
sender_id = AddressExpression.parse(sender_config.get(CONF_ID))
sender_eep_string = sender_config.get(CONF_EEP)
try:
dev_eep = EEP.find(eep_string)
sender_eep = EEP.find(sender_eep_string)
except:
LOGGER.warning("Could not find EEP %s for device with address %s", eep_string, dev_id.plain_address())
continue
else:
if dev_eep in [A5_38_08]:
entities.append(EltakoDimmableLight(gateway, dev_id, dev_name, dev_eep, sender_id, sender_eep))
elif dev_eep in [M5_38_08]:
entities.append(EltakoSwitchableLight(gateway, dev_id, dev_name, dev_eep, sender_id, sender_eep))
async_add_entities(entities)
class EltakoDimmableLight(EltakoEntity, LightEntity):
"""Representation of an Eltako light source."""
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
def __init__(self, gateway, dev_id, dev_name, dev_eep, sender_id, sender_eep):
"""Initialize the Eltako light source."""
super().__init__(gateway, dev_id, dev_name)
self._dev_eep = dev_eep
self._on_state = False
self._attr_brightness = 50
self._sender_id = sender_id
self._sender_eep = sender_eep
self._attr_unique_id = f"{DOMAIN}_{dev_id.plain_address().hex()}"
self.entity_id = f"light.{self.unique_id}"
@property
def name(self):
"""Return the name of the device if any."""
return None
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={
(DOMAIN, self.dev_id.plain_address().hex())
},
name=self.dev_name,
manufacturer=MANUFACTURER,
model=self._dev_eep.eep_string,
via_device=(DOMAIN, self.gateway.unique_id),
)
@property
def is_on(self):
"""If light is on."""
return self._on_state
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light source on or sets a specific dimmer value."""
self._attr_brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
address, _ = self._sender_id
if self._sender_eep == A5_38_08:
dimming = CentralCommandDimming(int(self.brightness / 255.0 * 100.0), 0, 1, 0, 0, 1)
msg = A5_38_08(command=0x02, dimming=dimming).encode_message(address)
self.send_message(msg)
# Don't set state instead wait for response from actor so that real state of light is displayed.
# self._on_state = True
def turn_off(self, **kwargs: Any) -> None:
"""Turn the light source off."""
address, _ = self._sender_id
if self._sender_eep == A5_38_08:
dimming = CentralCommandDimming(0, 0, 1, 0, 0, 0)
msg = A5_38_08(command=0x02, dimming=dimming).encode_message(address)
self.send_message(msg)
# Don't set state instead wait for response from actor so that real state of light is displayed.
# self._attr_brightness = 0
# self._on_state = False
def value_changed(self, msg):
"""Update the internal state of this device.
Dimmer devices like Eltako FUD61 send telegram in different RORGs.
We only care about the 4BS (0xA5).
"""
try:
decoded = self._dev_eep.decode_message(msg)
except Exception as e:
LOGGER.warning("Could not decode message: %s", str(e))
return
if self._dev_eep in [A5_38_08]:
if decoded.command == 0x01:
if decoded.switching.learn_button != 1:
return
self._on_state = decoded.switching.switching_command
elif decoded.command == 0x02:
if decoded.dimming.learn_button != 1:
return
if decoded.dimming.dimming_range == 0:
self._attr_brightness = int((decoded.dimming.dimming_value / 100.0) * 255.0)
elif decoded.dimming.dimming_range == 1:
self._attr_brightness = decoded.dimming.dimming_value
self._on_state = decoded.dimming.switching_command
else:
return
self.schedule_update_ha_state()
class EltakoSwitchableLight(EltakoEntity, LightEntity):
"""Representation of an Eltako light source."""
_attr_color_mode = ColorMode.ONOFF
_attr_supported_color_modes = {ColorMode.ONOFF}
def __init__(self, gateway, dev_id, dev_name, dev_eep, sender_id, sender_eep):
"""Initialize the Eltako light source."""
super().__init__(gateway, dev_id, dev_name)
self._dev_eep = dev_eep
self._on_state = False
self._sender_id = sender_id
self._sender_eep = sender_eep
self._attr_unique_id = f"{DOMAIN}_{dev_id.plain_address().hex()}"
self.entity_id = f"light.{self.unique_id}"
@property
def name(self):
"""Return the name of the device if any."""
return None
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={
(DOMAIN, self.dev_id.plain_address().hex())
},
name=self.dev_name,
manufacturer=MANUFACTURER,
model=self._dev_eep.eep_string,
via_device=(DOMAIN, self.gateway.unique_id),
)
@property
def is_on(self):
"""If light is on."""
return self._on_state
def turn_on(self, **kwargs: Any) -> None:
"""Turn the light source on or sets a specific dimmer value."""
address, _ = self._sender_id
if self._sender_eep == A5_38_08:
switching = CentralCommandSwitching(0, 1, 0, 0, 1)
msg = A5_38_08(command=0x01, switching=switching).encode_message(address)
self.send_message(msg)
# Don't set state instead wait for response from actor so that real state of light is displayed.
# self._on_state = True
def turn_off(self, **kwargs: Any) -> None:
"""Turn the light source off."""
address, _ = self._sender_id
if self._sender_eep == A5_38_08:
switching = CentralCommandSwitching(0, 1, 0, 0, 0)
msg = A5_38_08(command=0x01, switching=switching).encode_message(address)
self.send_message(msg)
# Don't set state instead wait for response from actor so that real state of light is displayed.
# self._on_state = False
def value_changed(self, msg):
"""Update the internal state of this device."""
try:
decoded = self._dev_eep.decode_message(msg)
except Exception as e:
LOGGER.warning("Could not decode message: %s", str(e))
return
if self._dev_eep in [M5_38_08]:
self._on_state = decoded.state
self.schedule_update_ha_state()