X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=rainbow%2Firis%2Fburrow-iris.py;h=7af1fd5c9920bb5a906a8bf68f1581e26eb9d601;hb=90670a11e7a4570d9f1840e5a21e00f3175d4aab;hp=1975b13bdfd99abe107743247eeafae1365c6c9b;hpb=f8153ae993188c75f6fa6b811b2cc3707f0e7371;p=home-hw.git diff --git a/rainbow/iris/burrow-iris.py b/rainbow/iris/burrow-iris.py index 1975b13..7af1fd5 100755 --- a/rainbow/iris/burrow-iris.py +++ b/rainbow/iris/burrow-iris.py @@ -1,15 +1,14 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Iris -- the Burrow's goddess of rainbow # Controls LEDs on the Rainbow according to the state of the house # (c) 2022 Martin Mareš import argparse import asyncio -import asyncio_mqtt +import aiomqtt from datetime import datetime, timedelta import logging from logging.handlers import SysLogHandler -import signal import ssl import sys @@ -120,11 +119,20 @@ def temperature_led(): return None +def ac_led(): + ac = st.get_sensor('air/ac-on') + if ac == 1: + return (0, 1, 0) + else: + return None + + def recalc_leds(): st.set_led(11, None) st.set_led(10, boiler_led()) st.set_led(9, catarium_led()) - st.set_led(8, temperature_led()) + # st.set_led(8, temperature_led()) + st.set_led(8, ac_led()) async def mqtt_process_msg(topic, val): @@ -133,22 +141,24 @@ async def mqtt_process_msg(topic, val): async def mqtt_loop(): - sctx = ssl.SSLContext(ssl.PROTOCOL_TLS) + sctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) sctx.verify_mode = ssl.CERT_REQUIRED sctx.load_cert_chain('/etc/burrow-mqtt/client.crt', '/etc/burrow-mqtt/client.key') sctx.load_verify_locations(cafile='/etc/burrow-mqtt/ca.crt') - will = asyncio_mqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True) + will = aiomqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True) - async with asyncio_mqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will) as mqtt: - global st - st = State(mqtt) - async with mqtt.unfiltered_messages() as messages: - await mqtt.subscribe("burrow/heating/#") - await mqtt.subscribe("burrow/temp/#") - await mqtt.publish("status/iris", "ok", retain=True) - async for msg in messages: - await mqtt_process_msg(msg.topic, msg.payload.decode()) + mqtt = aiomqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will) + await mqtt.connect() + global st + st = State(mqtt) + async with mqtt.messages() as messages: + await mqtt.subscribe("burrow/air/ac-on") + await mqtt.subscribe("burrow/heating/#") + await mqtt.subscribe("burrow/temp/#") + await mqtt.publish("status/iris", "ok", retain=True) + async for msg in messages: + await mqtt_process_msg(msg.topic.value, msg.payload.decode()) async def mqtt_watcher(): @@ -156,7 +166,7 @@ async def mqtt_watcher(): try: logger.info("Starting MQTT") await mqtt_loop() - except asyncio_mqtt.MqttError as error: + except aiomqtt.MqttError as error: logger.error(f"MQTT error: {error}") await asyncio.sleep(10) @@ -199,7 +209,7 @@ if args.debug: else: formatter = logging.Formatter(fmt="%(message)s") # systemd will handle the rest log_handler = SysLogHandler('/dev/log', facility=SysLogHandler.LOG_LOCAL1) - log_handler.ident = 'burrow-telegram: ' + log_handler.ident = 'burrow-iris: ' logger.setLevel(logging.INFO) log_handler.setFormatter(formatter) logger.addHandler(log_handler)