-
Notifications
You must be signed in to change notification settings - Fork 2
/
mistupisci-mqtt.py
78 lines (63 loc) · 2.04 KB
/
mistupisci-mqtt.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
#!/usr/bin/env python
import os
import paho.mqtt.client as mqtt
from mistupisci import *
import time
import sys
SEND_ROOM_TEMPERATURE_INTERVAL_MS = 60000;
try:
MQTT_HOST = os.environ['MQTT_HOST']
except KeyError:
print("ERROR: missing MQTT_HOST environment variable")
sys.exit(1)
try:
MQTT_PORT = os.environ['MQTT_PORT']
except KeyError:
print("ERROR: missing MQTT_PORT environment variable")
sys.exit(1)
try:
AC_SETTINGS_TOPIC = os.environ['AC_SETTINGS_TOPIC']
except KeyError:
print("ERROR: missing AC_SETTINGS_TOPIC environment variable")
sys.exit(1)
try:
AC_STATUS_TOPIC = os.environ['AC_STATUS_TOPIC']
except KeyError:
print("ERROR: missing AC_STATUS_TOPIC environment variable")
sys.exit(1)
def on_connect(client, userdata, flags, rc):
print('Connected successfully!')
client.subscribe(AC_SETTINGS_TOPIC)
client.subscribe(AC_STATUS_TOPIC)
def on_message(client, userdata, msg):
if msg.topic == AC_SETTINGS_TOPIC:
settings = json.loads(msg.payload)
acSettings = AcSettings(
settings["power"],
settings["mode"],
int(settings["temperature"]),
settings["fan"],
settings["vane"],
)
logger.info(settings["power"])
ac.setAcSettings(acSettings)
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
print('Trying to connect...')
client.connect(MQTT_HOST, int(MQTT_PORT), 60)
ac = AirConditioner()
ac.connect()
ac.autoUpdate = True
lastTemperatureSent = millis()
while(True):
try:
ac.sync()
# client.publish("morsellif/air_conditioner/settings", ac.actualSettings.toJSON())
if millis() > (lastTemperatureSent + SEND_ROOM_TEMPERATURE_INTERVAL_MS):
client.publish(AC_STATUS_TOPIC, ac.actualStatus.toJSON())
lastTemperatureSent = millis()
client.loop_start()
except KeyboardInterrupt:
break