-
Notifications
You must be signed in to change notification settings - Fork 15
/
mqmitsi
executable file
·124 lines (109 loc) · 4.15 KB
/
mqmitsi
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
#!/usr/bin/env python
import argparse
import json
import logging
import signal
import sys
import time
import paho.mqtt.client as mqtt
from mitsi import HeatPump
class Handler(object):
def __init__(
self, serial_port, broker, broker_port, prefix, user=None, password=None
):
self.running = True
self.prefix = prefix
signal.signal(signal.SIGINT, self.cleanup)
signal.signal(signal.SIGTERM, self.cleanup)
self.broker = broker
self.client = mqtt.Client(protocol=mqtt.MQTTv31)
if user:
self.client.username_pw_set(user, password)
will_topic = "connected"
if prefix:
will_topic = "%s/%s" % (prefix, will_topic)
self.client.will_set(will_topic, "0", qos=1, retain=True)
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.connect(broker, port=broker_port)
self.publish("connected", 1, qos=1, retain=True)
self.connected_state = 1
self.client.subscribe("%s/command/#" % prefix)
self.hp = HeatPump(serial_port)
self.hp.connect()
def on_connect(self, client, userdata, flags, rc):
log.info("Connected to MQTT broker: %s", self.broker)
self.publish("connected", 1, qos=1, retain=True)
self.connected_state = 1
self.client.subscribe("%s/command/#" % self.prefix)
self.hp.connect()
self.hp.dirty = True
def publish(self, topic, payload, **kwargs):
topic = "%s/%s" % (self.prefix, topic)
self.client.publish(topic, payload, **kwargs)
def run(self):
while self.running:
self.hp.loop()
if self.hp.valid and self.hp.dirty:
if self.connected_state != 2:
self.publish("connected", 2, qos=1, retain=True)
self.connected_state = 2
self.publish("state", json.dumps(self.hp.to_dict()), retain=True)
self.hp.dirty = False
if self.client.loop() != 0:
log.warning("Disonnected from MQTT broker: %s", self.broker)
try:
self.client.connect(self.broker)
except Exception:
time.sleep(1)
def on_message(self, client, userdata, msg):
log.debug("MQTT Message: %s : %s" % (msg.topic, msg.payload))
topic = msg.topic
if self.prefix:
topic = topic.partition("%s/" % self.prefix)[2]
msg_type, s, topic = topic.partition("/")
if msg_type == "command":
if topic == "state":
try:
state = json.loads(msg.payload)
except ValueError:
log.warning("Invalid JSON: %s" % msg.payload)
try:
self.hp.set(state)
except Exception:
log.error("Failed to set")
def cleanup(self, signum, frame):
self.running = False
self.client.disconnect()
sys.exit(signum)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Monitor & control a Mitsubishi heatpump " "via MQTT.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--mqtt-host", default="localhost", help="MQTT broker")
parser.add_argument("--mqtt-port", default="1883", type=int, help="MQTT port")
parser.add_argument(
"--mqtt-prefix",
default="mqmitsi",
help="MQTT topic prefix prepended to messages.",
)
parser.add_argument(
"--serial-port", default="/dev/ttyAMA0", help="Serial device to communicate on."
)
parser.add_argument("--log", help="Set log level.", default="WARNING")
args = parser.parse_args()
log = logging.getLogger()
log.setLevel(args.log)
console = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)-15s %(levelname)-8s %(message)s")
console.setFormatter(formatter)
console.setLevel(args.log)
log.addHandler(console)
h = Handler(
args.serial_port,
broker=args.mqtt_host,
prefix=args.mqtt_prefix,
broker_port=args.mqtt_port,
)
h.run()