-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbosch_mqtt.py
executable file
·400 lines (357 loc) · 10.9 KB
/
bosch_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
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
from typing import Any
import time
import os
import click
import logging
from colorlog import ColoredFormatter
import aiohttp
import bosch_thermostat_client as bosch
from bosch_thermostat_client.const import XMPP, HTTP
from bosch_thermostat_client.const.ivt import IVT
from bosch_thermostat_client.const.nefit import NEFIT
from bosch_thermostat_client.const.easycontrol import EASYCONTROL
from bosch_thermostat_client.version import __version__
import json
import asyncio
from functools import wraps
from yaml import load
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
import paho.mqtt.client as mqtt
_LOGGER = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
colorfmt = f"%(log_color)s{fmt}%(reset)s"
logging.getLogger().handlers[0].setFormatter(
ColoredFormatter(
colorfmt,
datefmt=datefmt,
reset=True,
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red",
},
)
)
def on_connect(client, userdata, flags, rc):
if rc != 0:
_LOGGER.error("MQTT Connection returned with result code "+str(rc))
pass
def set_default(ctx, param, value):
if os.path.exists(value):
with open(value, 'r') as f:
config = load(f.read(), Loader=Loader)
ctx.default_map = config
return value
def add_options(options):
def _add_options(func):
for option in reversed(options):
func = option(func)
return func
return _add_options
def mqtt_connect():
c = click.get_current_context()
mqtt_host = c.params["mqtt_host"]
mqtt_port = c.params["mqtt_port"]
mqtt_username = c.params["mqtt_username"]
mqtt_password = c.params["mqtt_password"]
client = mqtt.Client(client_id="bosch-thermostat-mqtt")
if mqtt_username is not None and mqtt_password is not None:
client.username_pw_set(mqtt_username, mqtt_password)
client.on_connect = on_connect
client.connect(host=mqtt_host, port=mqtt_port)
started = time.time()
while time.time() - started < 5.0:
client.loop()
if client.is_connected():
_LOGGER.info("Successfully connected to MQTT broker.")
return client
raise OSError('Connection to MQTT broker failed!')
def mqtt_publish(gateway, result):
client = mqtt_connect()
try:
for res in result:
if type(res) == list:
for r in res:
if 'value' in r:
topic = "bosch/" + gateway.device_model + "-" + gateway.uuid + r.get("id")
_LOGGER.info("Publishing %s", topic)
client.publish(topic=topic, payload=r.get("value"))
finally:
client.disconnect()
async def _scan(gateway, smallscan):
if smallscan:
result = await gateway.smallscan(_type=smallscan.lower())
else:
result = await gateway.rawscan()
mqtt_publish(gateway, result)
async def _runquery(gateway, path):
result = []
for p in path:
result.append(await gateway.raw_query(p))
mqtt_publish(gateway, [result])
async def _execute(device, host, token, password, session_type, fun):
c = click.get_current_context()
daemon = c.params["daemon"]
interval = c.params["interval"]
if daemon:
_LOGGER.info("Executing as daemon, interval is %d seconds.", interval)
while True:
_LOGGER.debug("Trying to connect to gateway.")
if device.upper() in (NEFIT, IVT, EASYCONTROL):
BoschGateway = bosch.gateway_chooser(device_type=device)
else:
_LOGGER.error("Wrong device type.")
return
if session_type == XMPP:
session = asyncio.get_event_loop()
elif session_type == HTTP:
session = aiohttp.ClientSession()
if device.upper() != IVT:
_LOGGER.warn(
"You're using HTTP protocol, but your device probably doesn't support it. Check for mistakes!"
)
else:
_LOGGER.error("Wrong protocol for this device")
return
gateway = BoschGateway(
session=session,
session_type=session_type,
host=host,
access_token=token,
password=password,
)
try:
connected = await gateway.check_connection()
if connected:
_LOGGER.info("Successfully connected to gateway. Found UUID: %s", gateway.uuid)
await fun(gateway)
else:
_LOGGER.error("Couldn't connect to gateway!")
finally:
await gateway.close(force=True)
_LOGGER.info("Disconnected from gateway")
if not daemon:
break;
_LOGGER.info("Sleeping %d seconds...", interval)
time.sleep(interval)
def coro(f):
@wraps(f)
def wrapper(*args, **kwargs):
return asyncio.run(f(*args, **kwargs))
return wrapper
@click.group(no_args_is_help=True)
@click.pass_context
@click.version_option(__version__)
@coro
async def cli(ctx):
"""A tool to run commands against Bosch thermostat."""
pass
_cmd1_options = [
click.option(
"--config",
default="config.yml",
type=click.Path(),
callback=set_default,
is_eager=True,
expose_value=False,
show_default=True,
help="Read configuration from PATH.",
),
click.option(
"--host",
envvar="BOSCH_HOST",
type=str,
required=True,
help="IP address of gateway or SERIAL for XMPP",
),
click.option(
"--token",
envvar="BOSCH_ACCESS_TOKEN",
type=str,
required=True,
help="Token from sticker without dashes.",
),
click.option(
"--password",
envvar="BOSCH_PASSWORD",
type=str,
required=False,
help="Password you set in mobile app.",
),
click.option(
"--protocol",
envvar="BOSCH_PROTOCOL",
type=click.Choice([XMPP, HTTP], case_sensitive=True),
required=True,
help="Bosch protocol. Either XMPP or HTTP.",
),
click.option(
"--device",
envvar="BOSCH_DEVICE",
type=click.Choice([NEFIT, IVT, EASYCONTROL], case_sensitive=False),
required=True,
help="Bosch device type. NEFIT, IVT or EASYCONTROL.",
),
click.option(
"-d",
"--debug",
default=False,
count=True,
help="Set Debug mode. Single debug is debug of this lib. Second d is debug of aioxmpp as well.",
),
]
_scan_options = [
click.option(
"-s",
"--smallscan",
type=click.Choice(["HC", "DHW", "SENSORS", "RECORDINGS"], case_sensitive=False),
help="Scan only single circuit of thermostat.",
),
]
_mqtt_options = [
click.option(
"--daemon",
is_flag=True,
required=False,
default=False,
help="Start as daemon.",
),
click.option(
"--interval",
default="300",
type=int,
show_default=True,
help="Specify polling interval in seconds when started as daemon.",
),
click.option(
"--mqtt-host",
type=str,
required=True,
help="Adress of MQTT host.",
),
click.option(
"--mqtt-port",
default="1883",
type=int,
show_default=True,
help="Port of MQTT host.",
),
click.option(
"--mqtt-username",
type=str,
required=False,
help="Username for MQTT connect.",
),
click.option(
"--mqtt-password",
type=str,
required=False,
help="Password for MQTT connect.",
),
]
@cli.command()
@add_options(_cmd1_options)
@add_options(_scan_options)
@add_options(_mqtt_options)
@click.pass_context
@coro
async def scan(
ctx,
host: str,
token: str,
password: str,
protocol: str,
device: str,
debug: int,
smallscan: str,
mqtt_host: str,
mqtt_port: int,
mqtt_username: str,
mqtt_password: str,
daemon: bool,
interval: int,
):
"""Create rawscan of Bosch thermostat."""
if debug == 0:
logging.basicConfig(level=logging.INFO)
if debug > 0:
logging.basicConfig(
# colorfmt,
datefmt=datefmt,
level=logging.DEBUG,
filename="out.log",
filemode="a",
)
_LOGGER.info("Debug mode active")
_LOGGER.debug(f"Lib version is {bosch.version}")
if debug > 1:
logging.getLogger("aioxmpp").setLevel(logging.DEBUG)
logging.getLogger("aioopenssl").setLevel(logging.DEBUG)
logging.getLogger("aiosasl").setLevel(logging.DEBUG)
logging.getLogger("asyncio").setLevel(logging.DEBUG)
else:
logging.getLogger("aioxmpp").setLevel(logging.WARN)
logging.getLogger("aioopenssl").setLevel(logging.WARN)
logging.getLogger("aiosasl").setLevel(logging.WARN)
logging.getLogger("asyncio").setLevel(logging.WARN)
session_type = protocol.upper()
await _execute(device, host, token, password, session_type, lambda gateway: _scan(gateway, smallscan))
_path_options = [
click.option(
"-p",
"--path",
type=str,
required=True,
multiple=True,
help="Path to run against. Look at rawscan at possible paths. e.g. /gateway/uuid - Can be specified multiple times!",
)
]
@cli.command()
@add_options(_cmd1_options)
@add_options(_path_options)
@add_options(_mqtt_options)
@click.pass_context
@coro
async def query(
ctx,
host: str,
token: str,
password: str,
protocol: str,
device: str,
path: list[str],
debug: int,
mqtt_host: str,
mqtt_port: int,
mqtt_username: str,
mqtt_password: str,
daemon: bool,
interval: int,
):
"""Query values of Bosch thermostat."""
if debug == 0:
logging.basicConfig(level=logging.INFO)
if debug > 0:
_LOGGER.info("Debug mode active")
_LOGGER.debug(f"Lib version is {bosch.version}")
if debug > 1:
logging.getLogger("aioxmpp").setLevel(logging.DEBUG)
logging.getLogger("aioopenssl").setLevel(logging.DEBUG)
logging.getLogger("aiosasl").setLevel(logging.DEBUG)
logging.getLogger("asyncio").setLevel(logging.DEBUG)
else:
logging.getLogger("aioxmpp").setLevel(logging.WARN)
logging.getLogger("aioopenssl").setLevel(logging.WARN)
logging.getLogger("aiosasl").setLevel(logging.WARN)
logging.getLogger("asyncio").setLevel(logging.WARN)
session_type = protocol.upper()
await _execute(device, host, token, password, session_type, lambda gateway: _runquery(gateway, path))
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(cli())