-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
370 lines (296 loc) · 11.8 KB
/
__init__.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""BPL Hassio Integration"""
import logging
import socket
import threading
import datetime
import time
import math
_LOGGER = logging.getLogger(__name__)
#delay between reconnects
INTERVAL_RECONNECT = 2
#send heartbeat packet to controller every x seconds
SEND_HEARTBEAT_SECONDS = 15
# after these many retries, component gives up
CONNECT_RETRIES = 20
#receive heartbeat packet from controller every x seconds
#if not heartbeat received, connection will be restarted
RECEIVE_HEARTBEAT_TIMEOUT = 60
from homeassistant.components.sensor import PLATFORM_SCHEMA
#from homeassistant.const import (CONF_HOST, CONF_PORT, CONF_NAME)
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.cover import CoverEntity
from homeassistant.components.light import LightEntity
from homeassistant.const import (
STATE_CLOSED,
STATE_CLOSING,
STATE_OPEN,
STATE_OPENING
)
_LOGGER = logging.getLogger(__name__)
DEFAULT_HOST = '192.168.1.10'
DEFAULT_PORT = 30001
CONF_HOST = "host"
CONF_PORT = "port"
DOMAIN = "bpl"
DATA_DOMAIN = DOMAIN
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
#vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
#vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
}
)
}, extra=vol.ALLOW_EXTRA
)
def setup(hass, config):
host = DEFAULT_HOST
port = DEFAULT_PORT
sensors = [
BPLLight(name="Living room Ceiling light", bpl_id=2, unique_id='living_room_ceiling_light'),
BPLLight(name="Living room Chandelier", bpl_id=1, unique_id='living_room_chandelier'),
BPLLight(name="Dining Chandelier", bpl_id=34, unique_id='dining_room_chandelier'),
BPLLight(name="Dining Fan", bpl_id=35, unique_id='dining_room_ceiling_light'),
BPLLight(name="Master bedroom light 1", bpl_id=12, unique_id='mbr_light_1'),
BPLLight(name="Master bedroom light 2", bpl_id=13, unique_id='mbr_light_2'),
BPLLight(name="Master bedroom light 3", bpl_id=15, unique_id='mbr_light_3'),
BPLLight(name="Master bedroom light 4", bpl_id=16, unique_id='mbr_light_4'),
BPLLight(name="Kids room fan", bpl_id=32, unique_id='kids_fan'),
BPLLight(name="Kids room light", bpl_id=33, unique_id='kids_light'),
BPLLight(name="Guest room light 1", bpl_id=21, unique_id='guest_light_1'),
BPLLight(name="Guest room light 2", bpl_id=22, unique_id='guest_light_2'),
BPLLight(name="Entertainment room light 1", bpl_id=19, unique_id='entertainment_light_1'),
BPLLight(name="Entertainment room light 2", bpl_id=20, unique_id='entertainment_light_2'),
BPLCurtain(name="Living room curtain", bpl_id=3, unique_id='living_curtain')
]
monitor = BPLMonitor(host=host, port=port, sensors=sensors)
hass.data[DOMAIN] = {
'sensors':sensors,
'monitor':monitor
}
_LOGGER.debug("loading bpl sensor platforms")
hass.helpers.discovery.load_platform('light', DOMAIN, {}, config)
hass.helpers.discovery.load_platform('cover', DOMAIN, {}, config)
for sensor in sensors:
sensor.set_monitor(monitor)
monitor.connect()
if monitor.sock is None:
return False
else:
return True
class BPLCurtain(CoverEntity) :
def __init__(self, name, bpl_id, unique_id):
"""Initialize the sensor."""
self._state = STATE_CLOSED
self._attributes = {}
self._name = name
self.bplid = bpl_id
self._unique_id = unique_id
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of the device."""
return self._name
def open_cover(self, **kwargs) -> None:
"""Open the cover."""
self._state = STATE_OPENING
_LOGGER.debug("command to set cover to open")
if self.hass is not None:
self.schedule_update_ha_state()
def close_cover(self, **kwargs) -> None:
"""Close cover."""
self._state = STATE_CLOSING
_LOGGER.debug("command to set cover to close")
if self.hass is not None:
self.schedule_update_ha_state()
def stop_cover(self, **kwargs):
"""Stop the cover."""
self._state = STATE_CLOSED
_LOGGER.debug("command to stop cover")
if self.hass is not None:
self.schedule_update_ha_state()
async def async_added_to_hass(self):
self.schedule_update_ha_state()
@property
def is_opening(self):
"""Return if the cover is opening or not."""
return self._state == STATE_OPENING
@property
def is_closing(self):
"""Return if the cover is closing or not."""
return self._state == STATE_CLOSING
@property
def is_closed(self):
"""Return if the cover is closed or not."""
return self._state == STATE_CLOSED
def set_monitor(self, monitor):
_LOGGER.debug("set monitor")
class BPLLight(LightEntity):
"""Implementation of a Fritz!Box call monitor."""
def __init__(self, name, bpl_id, unique_id):
"""Initialize the sensor."""
self._state = False
self._attributes = {}
self._name = name
self.bplid = bpl_id
self._unique_id = unique_id
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def is_on(self):
"""Return true if it is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn the light on."""
self._state = True
_LOGGER.debug("command to turn on, sending to controller")
self._monitor.send_command(self.bplid, 'CMD_ON')
def turn_off(self, **kwargs):
"""Turn the light off."""
self._state = False
self._monitor.send_command(self.bplid, 'CMD_OFF')
_LOGGER.debug("command to turn off, sending to controller")
def set_state(self, state):
self._state = state
if self.hass is not None:
self.schedule_update_ha_state()
def set_monitor(self, monitor):
self._monitor = monitor
# now get latest state
async def async_added_to_hass(self):
self.schedule_update_ha_state()
class BPLMonitor(object):
"""Event listener to monitor controller."""
def __init__(self, host, port, sensors):
"""Initialize BPL monitor instance."""
self.host = host
self.port = port
self.sock = None
self._sensors = sensors
self.counter = 1
self.last_response_time = 0
self.last_request_time = 0
self.got_first_response = False
self.connect_count = 0
def connect(self):
if self.connect_count > CONNECT_RETRIES :
_LOGGER.error("Gave up connection retry after "+str(self.connect_count)+" times")
return
else :
self.connect_count+=1
if self.sock is None :
"""Connect to the BPL"""
self.counter = 1
_LOGGER.debug("starting connect to %s on port %s",self.host, self.port)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(5)
self.got_first_response = False
try:
self.sock.connect((self.host, self.port))
threading.Thread(target=self._listen, daemon=True).start()
except :
self.sock = None
_LOGGER.error("Cannot connect to %s on port %s",
self.host, self.port)
time.sleep(INTERVAL_RECONNECT)
self.connect()
def reconnect(self):
if not (self.sock is None) :
self.sock.close()
self.connect()
def _listen(self):
"""Listen to changes"""
self.connect_count = 0
_LOGGER.debug("BPL Connection success, now listening")
while True:
try:
response = self.sock.recv(2048)
except socket.timeout:
# if no response after 10 seconds, just recv again
if not (self.sock is None) :
self.check_and_send_heartbeat()
continue
except :
continue
response = str(response, "utf-8")
if not response:
# if the response is empty, the connection has been lost.
# try to reconnect
_LOGGER.error("response from controller empty, possible broken socket")
self.sock = None
while self.sock is None:
self.connect()
time.sleep(INTERVAL_RECONNECT)
break
else:
_LOGGER.debug("received message from controller %s",response)
self.last_response_time = time.time()
lines = response.splitlines()
for line in lines:
self._parse(line)
if not self.got_first_response :
self.got_first_response = True
self.process_states_on_connect()
self.check_and_send_heartbeat()
return
def process_states_on_connect(self) :
for sensor in self._sensors :
self.send_command(sensor.bplid,"GET_STATE")
def check_and_send_heartbeat(self) :
time_since_last_ack = time.time() - self.last_response_time
if time_since_last_ack> RECEIVE_HEARTBEAT_TIMEOUT :
# this means we havent got a ack in last x seconds
# could be a bad state in sock, hence reconnect
_LOGGER.error("possible bad state sock, no ackowledgment response since "+str(time_since_last_ack)+" seconds, forcing reconnect")
self.reconnect()
else :
time_since_last_heartbeat = time.time() - self.last_request_time
if time_since_last_heartbeat > SEND_HEARTBEAT_SECONDS :
_LOGGER.debug("sending HEARTBEAT, time since ack = "+str(math.floor(time_since_last_ack))+" seconds")
self.send_command(0,"HEARTBEAT")
def send_command(self, id, cmd):
try:
if not (self.sock is None) :
cmdstr = ':'+str(self.counter)+':'+str(id)+':0:'+cmd+':0:'
self.sock.send(cmdstr.encode())
self.last_request_time = time.time()
self.counter+=1
time.sleep(0.1) # hack to avoid sending bulk commands which dont work with BPL
else :
self.connect()
except :
self.sock = None
self.connect()
def _parse(self, line):
"""Parse the information and set the sensor states."""
pieces = line.split(":")
if pieces[1].isnumeric():
device = int(pieces[1])
if device > 0 :
state = pieces[3]
sensor = next((sensor for sensor in self._sensors if sensor.bplid == device), None)
if not sensor :
_LOGGER.warning("Device not understood :"+str(device)+", ignoring state update :"+state)
return
if state == 'CMD_ON' :
sensor.set_state(True)
elif state == 'CMD_OFF':
sensor.set_state(False)
elif state == 'EV_CURTAIN_OPEN' :
sensor.open_cover()
elif state =='EV_CURTAIN_CLOSE' :
sensor.close_cover()
elif state == 'EV_CURTAIN_STOP' :
sensor.stop_cover()
else :
_LOGGER.warning("Command not understand command "+line+" for device :"+str(device))