forked from sillyfrog/blindsmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblindmanager
More file actions
executable file
·418 lines (367 loc) · 15.7 KB
/
Copy pathblindmanager
File metadata and controls
executable file
·418 lines (367 loc) · 15.7 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
import paho.mqtt.client
import time
import json
import threading
import queue
import sys
from datetime import datetime
HOST = None
DOWN = "down"
UP = "up"
STOP = "stop"
LIMIT = "limit"
DIRECTION = "direction"
PAIR = "pair"
COMMANDS = [DOWN, UP, STOP, LIMIT, DIRECTION]
FROM_REMOTE = "fromremote"
FROM_MQTT = "frommqtt"
HOMIE_SEND_DELAY = 0 # 0.5
HOMIE_STATE_KEYS = {
"$homie": "Homie ESP Version",
"$name": "Device Name",
"$mac": "MAC Address",
"$localip": "Local IP",
"$fw/name": "Firmware Name",
"$fw/version": "Firmware Version",
"$implementation": "Processor",
"$implementation/version": "Homie Implementation",
"$implementation/ota/enabled": "OTA Enabled",
"$stats/uptime": "Uptime",
}
# Approximate frequency to publish state updates
STATE_PUBLISH_FREQUENCY = 300
def blindkey(remote_id, channel):
"""Makes a consistent tuple to use as a key for a given remote_id and channel"""
if type(remote_id) == str:
remote_id = int(remote_id, 16)
if type(channel) == str:
channel = int(channel)
return (remote_id, channel)
def parsepayload(payload):
"""Parse an MQTT payload, returning a tuple
(command, remote_id, channel)
"""
command, remote_id, channel = payload.strip().split(",")
command = command.lower()
if command not in COMMANDS:
raise ValueError(f"Invalid command: {command}")
remote_id = int(remote_id, 16)
channel = int(channel)
return command, remote_id, channel
def logerror(msg):
print("ERROR!: " + msg)
class Blind:
def __init__(self, config):
self.queue = queue.SimpleQueue()
self.position = 50
self.action = STOP
self.target_position = None
self.distance_sec_open = 100 / config["time_open"]
self.distance_sec_close = 100 / config["time_close"]
self.homie_id = config["homie_id"]
self.name = config["name"]
self.remote_id = int(config["remote_id"], 16)
self.channel = int(config.get("channel", 1))
self.device_class = config.get("device_class", "shade")
self.topic = config["topic"]
self.commands = {UP: config["up"], DOWN: config["down"], STOP: config["stop"]}
def run(self, manager):
self.manager = manager
self.thread = threading.Thread(target=self.runner, daemon=True)
self.thread.start()
def runner(self):
while 1:
dopublish = False
sendstop = False
try:
if self.action == STOP:
timeout = None
else:
timeout = 1
source, command = self.queue.get(timeout=timeout)
except queue.Empty:
source = None
if source == FROM_REMOTE:
# Got a command from the remote, act on it
dopublish = True
if command == STOP:
if self.action != STOP:
self.action = STOP
self.target_position = None
elif command in (UP, DOWN):
self.action = command
elif source == FROM_MQTT:
if command is None:
sendstop = True
else:
percentage = int(command)
if percentage == 51:
self.sendcommand(DOWN)
self.action = DOWN
self.target_position = 0
elif percentage == 100 or percentage > self.position:
self.sendcommand(UP)
self.action = UP
elif percentage == 0:
self.sendcommand(STOP)
self.action = DOWN
print(f"{datetime.now()} {self.name} close using stop. Target position {self.target_position}")
elif percentage < self.position:
self.sendcommand(DOWN)
self.action = DOWN
else:
sendstop = True
if self.action == UP:
dopublish = True
self.position += self.distance_sec_open
if self.target_position is not None:
if self.position >= self.target_position:
self.position = self.target_position
self.action = STOP
if self.target_position != 100:
sendstop = True
self.target_position = None
elif self.position >= 100:
self.position = 100
self.action = STOP
self.target_position = None
elif self.action == DOWN:
dopublish = True
self.position -= self.distance_sec_close
if self.target_position is not None:
if self.position <= self.target_position:
self.position = self.target_position
self.action = STOP
if self.target_position != 0:
sendstop = True
self.target_position = None
elif self.position <= 0:
self.position = 0
self.action = STOP
if sendstop:
self.sendcommand(STOP)
self.action = STOP
self.target_position = None
if dopublish:
self.publish()
def publish(self):
self.manager.publishblinds(self.topic, self.position)
def gotremotecommand(self, command):
self.queue.put((FROM_REMOTE, command))
def gotmqttcommand(self, percentage):
"""Got a command from MQTT, typically a percentag to open to.
If percentage is None, this is treated as STOP"""
if percentage is None:
self.queue.put((FROM_MQTT, None))
self.target_position = None
return
if percentage >= 100:
percentage = 100
elif percentage <= 0:
percentage = 0
self.target_position = percentage
self.queue.put((FROM_MQTT, percentage))
def sendcommand(self, command):
# Get the homie_id, and build the command to send
# self.manager.publishhomie(self.homie_id, f"{command.upper()},{hex(self.remote_id)},{self.channel}"
self.manager.publishhomie(self.homie_id, self.commands[command])
print(f"{datetime.now()} {self.name} {self.homie_id} CMD: {self.commands[command]}")
def hassconfig(self):
"""Return a JSON dict of the configuration for this cover.
This can be used to broadcast via MQTT the configuration to Home Assistant.
"""
config = {
"name": self.name,
"device_class": self.device_class,
"position_topic": f"blinds/{self.topic}/position",
"set_position_topic": f"blinds/{self.topic}/position/set",
"command_topic": f"blinds/{self.topic}/command/set",
"availability_topic": f"homie/{self.homie_id}/$state",
"payload_available": "ready",
"unique_id": f"blinds_{self.topic}",
"device": {"identifiers": [self.homie_id]},
"json_attributes_topic": f"blinds/homie/{self.homie_id}/state",
}
return config
class BlindManager:
def __init__(self, config):
self._config = config
self.blinds = {}
self.blindsbytopic = {}
self.homie_ids = set()
self.homie_state = {}
for blindconf in config["blinds"]:
self.homie_ids.add(blindconf["homie_id"])
channel = blindconf.get("channel", 1)
remote_id = blindconf["remote_id"]
if type(remote_id) == str:
remote_id = int(remote_id, 16)
blind = Blind(blindconf)
self.blinds[blindkey(remote_id, channel)] = blind
self.blindsbytopic[blindconf["topic"]] = blind
for homie_id in self.homie_ids:
self.homie_state[homie_id] = {}
self.publishqueue = queue.SimpleQueue()
self.next_state_publish = time.time() + STATE_PUBLISH_FREQUENCY
self.ignorereceived = config.get("ignorereceived", False)
def run(self):
self.client = paho.mqtt.client.Client()
self.client.on_connect = self.onmqttconnect
self.client.on_message = self.onmqttmessage
self.client.connect(self._config["host"])
self.client.loop_start()
# Start all of the blind instances
for blind in self.blinds.values():
blind.run(self)
# Publish the HA config
self.client.publish(
f"homeassistant/cover/blinds/{blind.topic}/config",
json.dumps(blind.hassconfig()),
retain=True,
)
for homie_id in self.homie_ids:
self.client.publish(
f"homeassistant/sensor/blinds/{homie_id}/config",
json.dumps(
{
"name": f"Blind controller status ({homie_id})",
"state_topic": f"homie/{homie_id}/$stats/signal",
"availability_topic": f"homie/{homie_id}/$state",
"payload_available": "ready",
"optimistic": "true",
"json_attributes_topic": f"blinds/homie/{homie_id}/state",
"unit_of_measurement": "%",
"icon": "mdi:information-outline",
"unique_id": f"status_{homie_id}",
"device": {
"identifiers": [homie_id],
"name": f"Blinds Controller ({homie_id})",
"manufacturer": "bgdev, Inc.",
"model": "Python Docker, Wemos 433 Mhz"
},
}
),
retain=True,
)
# Publish incoming commands, ratelimiting calls to the homie queue
homiedelayed = []
lasthomiesend = 0
while 1:
try:
if homiedelayed:
timeout = HOMIE_SEND_DELAY
else:
timeout = STATE_PUBLISH_FREQUENCY / 2
try:
pubtype, topic, payload = self.publishqueue.get(timeout=timeout)
except queue.Empty:
if homiedelayed:
pubtype = "homie"
topic, payload = homiedelayed.pop(0)
else:
pubtype = None
if pubtype == "blinds":
self.client.publish(f"blinds/{topic}/position", payload, retain=True)
elif pubtype == "homie":
if (time.time() - lasthomiesend) < HOMIE_SEND_DELAY:
print("Sending homie too fast, delaying...", homiedelayed)
homiedelayed.append((topic, payload))
else:
self.client.publish(f"homie/{topic}/command/send/set", payload)
lasthomiesend = time.time()
if time.time() > self.next_state_publish:
self.publishhomiestate()
self.next_state_publish = time.time() + STATE_PUBLISH_FREQUENCY
except Exception as e:
logerror(f"Error in publishing loop: {e}")
time.sleep(5)
def publishhomiestate(self):
"""Publish the state of all the Homie devices for HA"""
for homie_id, state in self.homie_state.items():
self.client.publish(f"blinds/homie/{homie_id}/state", json.dumps(state))
def onmqttconnect(self, client, userdata, flags, rc):
for homie_id in self.homie_ids:
self.client.subscribe(f"homie/{homie_id}/#")
print(f"homie/{homie_id}/#")
self.client.subscribe("blinds/+/#")
def onmqttmessage(self, client, userdata, msg):
try:
parts = msg.topic.split("/")
payload = msg.payload.decode()
if parts[0] == "homie":
if parts[-1] == "received":
if self.ignorereceived:
return
command, remote_id, channel = parsepayload(payload)
if channel == 0:
# find all potential channels on this remote
keys = []
for channel in range(1, 16):
keys.append(blindkey(remote_id, channel))
else:
keys = [blindkey(remote_id, channel)]
for key in keys:
if key not in self.blinds:
continue
blind = self.blinds[key]
blind.gotremotecommand(command)
elif parts[2].startswith("$"):
# It's a type of state field, see if we want it and update the status
keystr = "/".join(parts[2:])
if keystr in HOMIE_STATE_KEYS and parts[1] in self.homie_state:
# print(f"adding stuff to homie_state[{parts[1]}][{HOMIE_STATE_KEYS[keystr]}] : {payload}")
self.homie_state[parts[1]][HOMIE_STATE_KEYS[keystr]] = payload
elif parts[0] == "blinds":
topic = parts[1]
if topic not in self.blindsbytopic:
return
blind = self.blindsbytopic[topic]
# Handle the retained position message
if parts[-1] == "position" and not msg.topic.endswith("/set"):
# This is the broker sending us the last known position
try:
saved_position = int(payload)
# Only update if the blind isn't already moving
if blind.action == STOP:
blind.position = saved_position
print(f"Restored {blind.name} to saved position: {saved_position}")
except ValueError:
pass
# Existing logic for 'set' commands...
elif parts[-1] == "set":
topic = parts[1]
if topic not in self.blindsbytopic:
return
if parts[-2] == "position":
# Take some action with this blind
blind = self.blindsbytopic[topic]
percentage = int(payload)
blind.gotmqttcommand(percentage)
elif parts[-2] == "command":
# Take some action with this blind
blind = self.blindsbytopic[topic]
payload = payload.upper()
if payload == "CLOSE":
blind.gotmqttcommand(0)
elif payload == "OPEN":
blind.gotmqttcommand(100)
elif payload == "STOP":
blind.gotmqttcommand(None)
except Exception as e:
logerror(
f"Error with topic: {msg.topic}, payload: {msg.payload}: {e} ({e!r})"
)
def publishhomie(self, homie_id, payload):
self.publishqueue.put(("homie", homie_id, payload))
def publishblinds(self, topic, position):
self.publishqueue.put(("blinds", topic, int(position)))
def readconfig():
config = json.load(open("./config.json"))
return config
def main():
config = readconfig()
manager = BlindManager(config)
manager.run()
if __name__ == "__main__":
main()