-
Notifications
You must be signed in to change notification settings - Fork 2
/
hysen-mqtt.py
298 lines (245 loc) · 9.67 KB
/
hysen-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
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
#!/usr/bin/env python3
import asyncio
import json
import logging
import os
import re
import time
import traceback
import broadlink
import paho.mqtt.client as mqtt
import paho.mqtt.subscribe as subscribe
POLL_INTERVAL = 30.0
LOGGER = logging.getLogger(__name__)
class DevicesNotFoundError(Exception):
pass
def get_config():
host = os.environ['MQTT_HOST']
port = int(os.environ['MQTT_PORT'])
user = os.environ['MQTT_USER']
password = os.environ['MQTT_PASSWORD']
local_ip_address = os.environ['LOCAL_IP_ADDR'] \
if 'LOCAL_IP_ADDR' in os.environ and os.environ['LOCAL_IP_ADDR'] \
else None
return host, port, user, password, local_ip_address
class HysenMQTTConnector:
def __init__(self, device, host, port, user, password):
self._device = device
self._device_id = self.get_device_id()
self._mqtt_client = self._build_mqtt_client(host, port, user, password)
def _build_mqtt_client(self, host, port, user, password):
client = mqtt.Client()
client.on_connect = self._on_connect
client.on_message = self._on_message
self._set_last_will(client)
client.username_pw_set(user, password)
client.connect(host, port)
client.loop_start()
return client
def _on_connect(self, client, userdata, flags, rc):
LOGGER.debug("Connected with result code %d", rc)
try:
self.subscribe_topics()
self.publish_configuration()
self.publish_available()
self.publish_state()
except Exception as err:
traceback.print_tb(err.__traceback__)
def _on_message(self, client, userdata, message):
payload = message.payload.decode('utf-8')
topic = message.topic
match = re.match(r"^homeassistant/climate/{}/(.*?)$".format(
self._device_id), topic)
command = match.group(1)
handlers = {
"targetTempCmd": self.set_target_temperature,
"thermostatModeCmd": self.set_thermostat_mode
}
LOGGER.debug("Calling handler for %s: %s(%s)",
self._device_id, command, payload)
handlers[command](payload)
try:
self.publish_state()
except Exception as err:
traceback.print_tb(err.__traceback__)
def set_target_temperature(self, payload):
temperature = float(payload)
LOGGER.debug("Setting %s target temperature to %f",
self._device_id, temperature)
self._device.set_temp(temperature)
def set_thermostat_mode(self, payload):
power = {
"off": 0,
"heat": 1
}
LOGGER.debug("Setting %s power mode to %f",
self._device_id, payload)
self._device.set_power(power[payload])
def set_time(self):
cur_time = time.localtime()
LOGGER.info('Setting %s time', self._device_id)
self._device.set_time(
cur_time.tm_hour,
cur_time.tm_min,
cur_time.tm_sec,
cur_time.tm_wday+1)
def set_deadzone(self, deadzone=1):
LOGGER.info('Setting %s deadzone', self._device_id)
status = self._device.get_full_status()
loop_mode = status['loop_mode']
sensor = status['sensor']
osv = status['osv']
dif = deadzone
svh = status['svh']
svl = status['svl']
adj = status['room_temp_adj']
fre = status['fre']
poweron = status['poweron']
self._device.set_advanced(loop_mode, sensor, osv, dif, svh, svl,
adj, fre, poweron)
def get_device_id(self):
dev_type = self._device.type
dev_mac_str = ''.join('{:02x}'.format(x) for x in self._device.mac)
return '{}_{}'.format(dev_type.lower().replace(' ', '_'), dev_mac_str)
def publish_configuration(self):
config_topic = 'homeassistant/climate/{}/config'.format(self._device_id)
device_mac = self._device_id.rsplit('_', 1)[-1]
device_name = '{} {}'.format(self._device.type, device_mac)
base = 'homeassistant/climate/{}'.format(self._device_id)
device_payload = {
"identifiers": [ device_mac ],
"name": device_name,
"model": self._device.type,
"manufacturer": "Broadlink"
}
config_payload = {
"name": device_name,
"unique_id": device_mac,
"mode_cmd_t": base + "/thermostatModeCmd",
"mode_stat_t": base + "/state",
"mode_stat_tpl": "{{ value_json.mode }}",
"avty_t": base + "/available",
"pl_avail": "online",
"pl_not_avail": "offline",
"temp_cmd_t": base + "/targetTempCmd",
"temp_stat_t": base + "/state",
"temp_stat_tpl": "{{ value_json.target_temp }}",
"curr_temp_t": base + "/state",
"curr_temp_tpl": "{{ value_json.current_temp }}",
"action_topic": base + "/state",
"action_template": "{{ value_json.action }}",
"min_temp": "5",
"max_temp": "35",
"temp_step": "0.5",
"modes": ["off", "heat"],
"device": device_payload
}
LOGGER.info('Publishing config for %s', self._device_id)
self._mqtt_client.publish(config_topic,
json.dumps(config_payload),
retain=True)
def publish_state(self):
state_topic = 'homeassistant/climate/{}/state'.format(self._device_id)
status = self._device.get_full_status()
mode = "heat" if status["power"] else "off"
if mode == "off":
action = "off"
else:
action = "heating" if status["active"] else "idle"
target_temp = str(status["thermostat_temp"])
current_temp = str(status["external_temp"])
state_payload = {
"mode": mode,
"target_temp": target_temp,
"current_temp": current_temp,
"action": action,
}
LOGGER.info('Publishing state for %s', self._device_id)
self._mqtt_client.publish(state_topic, json.dumps(state_payload), qos=1)
def publish_available(self):
availability_topic = 'homeassistant/climate/{}/available'.format(
self._device_id)
availability_payload = 'online'
LOGGER.info('Publishing availability for %s', self._device_id)
self._mqtt_client.publish(
availability_topic, availability_payload, retain=True, qos=1)
def subscribe_topics(self):
topics = ["targetTempCmd", "thermostatModeCmd"]
base = "homeassistant/climate/{}/".format(self._device_id)
for topic in topics:
full_topic = base + topic
LOGGER.debug('Subscribing %s to %s', self._device_id, full_topic)
self._mqtt_client.subscribe(full_topic, 0)
async def set_time_coro(self):
while True:
sleep_future = asyncio.sleep(30*60)
try:
self.set_time()
self.set_deadzone()
except Exception as err:
traceback.print_tb(err.__traceback__)
await sleep_future
def _set_last_will(self, client):
will_topic = 'homeassistant/climate/{}/available'.format(
self._device_id)
will_payload = 'offline'
client.will_set(will_topic, will_payload, retain=True)
async def publish_config_coro(self):
while True:
await asyncio.sleep(10*60)
try:
self.publish_configuration()
except Exception as err:
traceback.print_tb(err.__traceback__)
async def publish_available_coro(self):
while True:
await asyncio.sleep(10*60)
try:
self.publish_available()
except Exception as err:
traceback.print_tb(err.__traceback__)
async def publish_state_coro(self):
while True:
sleep_future = asyncio.sleep(POLL_INTERVAL)
try:
self.publish_state()
except Exception as err:
traceback.print_tb(err.__traceback__)
await sleep_future
async def start_tasks(self):
LOGGER.info("Starting recurring background tasks for %s",
self._device_id)
tasks = []
tasks.append(asyncio.create_task(self.publish_available_coro()))
tasks.append(asyncio.create_task(self.publish_state_coro()))
tasks.append(asyncio.create_task(self.set_time_coro()))
for task in tasks:
await task
async def get_devices(timeout=10, local_ip_address=None):
LOGGER.info("Discovering broadlink devices")
all_devices = broadlink.discover(
timeout=timeout,
local_ip_address=local_ip_address)
hysen_devices = [dev for dev in all_devices
if isinstance(dev, broadlink.hysen)]
LOGGER.info("Found %d Hysen devices", len(hysen_devices))
return hysen_devices
async def main():
LOGGER.setLevel(logging.DEBUG)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s:%(name)s:%(levelname)s:%(message)s")
host, port, user, password, local_ip_address = get_config()
devices = await get_devices(local_ip_address=local_ip_address)
connectors = []
tasks = []
for device in devices:
device.auth()
connector = HysenMQTTConnector(device, host, port, user, password)
connectors.append(connector)
tasks.append(asyncio.create_task(connector.start_tasks()))
# these tasks should never finish
for task in tasks:
await task
if __name__ == '__main__':
asyncio.run(main())