-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (49 loc) · 1.78 KB
/
app.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
from asyncio_mqtt import Client, MqttError
from bedjet import BedJet
from config import MQTT
import asyncio
import logging
async def run(bedjets):
async with Client(
MQTT['host'],
username=MQTT['username'],
password=MQTT['password']
) as client:
for mac, bedjet in bedjets.items():
bedjet.mqtt_client = client
async with client.filtered_messages('bedjet/+/+/set') as messages:
await client.subscribe('bedjet/#')
async for message in messages:
splittopic = message.topic.split('/')
mac = splittopic[1]
command_type = splittopic[2]
command_value = message.payload.decode()
bedjet = bedjets.get(mac)
if not bedjet:
continue
if command_type == 'hvac-mode':
await bedjet.set_hvac_mode(command_value)
if command_type == 'target-temperature':
await bedjet.set_temperature(command_value)
if command_type == 'fan-mode':
await bedjet.set_fan_mode(command_value)
async def connect_bedjets():
bedjets = {}
bedjet_arr = await BedJet.discover()
for bedjet in bedjet_arr:
bedjets[bedjet.mac] = bedjet
bedjet.mqtt_topic = f'bedjet/{bedjet.mac}'
await bedjet.connect_and_subscribe()
return bedjets
async def main():
reconnect_interval = 3
bedjets = await connect_bedjets()
while True:
try:
await run(bedjets)
except MqttError as error:
logging.error(
f'Error "{error}". Reconnecting in {reconnect_interval} seconds.')
finally:
await asyncio.sleep(reconnect_interval)
asyncio.run(main())