2 # Iris -- the Burrow's goddess of rainbow
3 # Controls LEDs on the Rainbow according to the state of the house
4 # (c) 2022 Martin Mareš <mj@ucw.cz>
9 from datetime import datetime, timedelta
11 from logging.handlers import SysLogHandler
18 def __init__(self, mqtt):
20 self.leds = ["?"] * 12
21 self.new_leds = ["?"] * 12
25 self.now = datetime.now()
27 def received_msg(self, topic, val):
28 self.attrs[topic] = val
29 logger.debug(f'MQTT: {topic} -> {val}')
31 def get(self, key, default=None):
32 topic = "burrow/" + key
33 return self.attrs.get(topic, default)
35 def get_status(self, key, default=None):
36 topic = "status/" + key
37 return self.attrs.get(topic, default)
39 def get_sensor(self, key, default=None, timeout=120):
40 topic = "burrow/" + key
41 if topic in self.attrs:
42 s = self.attrs[topic].split(" ")
43 if len(s) >= 2 and timeout is not None and int(s[1]) < self.now.timestamp() - timeout:
44 logger.debug(f"< {key} EXPIRED")
47 logger.debug(f"< {key} = {s[0]}")
50 logger.debug(f"< {key} UNDEFINED")
53 async def set(self, key, val):
55 topic = "burrow/" + key
56 logger.debug(f'> {key} = {val}')
57 await self.mqtt.publish(topic, val, retain=True)
59 def set_led(self, i, color=None):
64 self.new_leds[i] = f"{r} {g} {b} iris"
66 async def update_leds(self):
67 for i in range(len(self.leds)):
68 if self.new_leds[i] != self.leds[i]:
69 await self.set(f"lights/rainbow/{i}", self.new_leds[i])
70 self.leds[i] = self.new_leds[i]
73 st = None # Current State
78 stat = st.get_status('bsb', 'ok')
82 err = st.get_sensor('heating/error', 0, timeout=None)
86 if st.get_sensor('heating/circuit1/pump', 0, timeout=3600) > 0:
89 if st.get_sensor('heating/circuit2/active', 0, timeout=3600) > 0:
92 if st.get_sensor('heating/water/active', 0, timeout=3600) > 0:
99 temp = st.get_sensor('temp/catarium')
115 def temperature_led():
116 for sensor in ['loft', 'ursarium', 'garage', 'terarium']:
117 if st.get_sensor(f"temp/{sensor}", timeout=7200) is None:
125 st.set_led(10, boiler_led())
126 st.set_led(9, catarium_led())
127 st.set_led(8, temperature_led())
130 async def mqtt_process_msg(topic, val):
131 st.received_msg(topic, val)
135 async def mqtt_loop():
136 sctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
137 sctx.verify_mode = ssl.CERT_REQUIRED
138 sctx.load_cert_chain('/etc/burrow-mqtt/client.crt', '/etc/burrow-mqtt/client.key')
139 sctx.load_verify_locations(cafile='/etc/burrow-mqtt/ca.crt')
141 will = asyncio_mqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True)
143 async with asyncio_mqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will) as mqtt:
146 async with mqtt.unfiltered_messages() as messages:
147 await mqtt.subscribe("burrow/heating/#")
148 await mqtt.subscribe("burrow/temp/#")
149 await mqtt.publish("status/iris", "ok", retain=True)
150 async for msg in messages:
151 await mqtt_process_msg(msg.topic, msg.payload.decode())
154 async def mqtt_watcher():
157 logger.info("Starting MQTT")
159 except asyncio_mqtt.MqttError as error:
160 logger.error(f"MQTT error: {error}")
161 await asyncio.sleep(10)
164 async def led_watcher():
166 await led_event.wait()
168 logger.debug('Recalculating LEDs')
172 await st.update_leds()
173 await asyncio.sleep(0.1)
177 global loop, led_event
178 loop = asyncio.get_event_loop()
179 led_event = asyncio.Event()
181 loop.create_task(mqtt_watcher()),
182 loop.create_task(led_watcher()),
184 for coro in asyncio.as_completed(coros):
186 done.result() # The coroutine probably died of an exception, which is raised here.
189 parser = argparse.ArgumentParser(description='The Goddess of Rainbow in the Burrow')
190 parser.add_argument('--debug', default=False, action='store_true', help='Run in debug mode')
191 args = parser.parse_args()
193 logger = logging.getLogger()
195 formatter = logging.Formatter(fmt="%(asctime)s %(name)s.%(levelname)s: %(message)s", datefmt='%Y-%m-%d %H:%M:%S')
196 log_handler = logging.StreamHandler(stream=sys.stdout)
197 logger.setLevel(logging.DEBUG)
198 logging.getLogger('mqtt').setLevel(logging.DEBUG)
200 formatter = logging.Formatter(fmt="%(message)s") # systemd will handle the rest
201 log_handler = SysLogHandler('/dev/log', facility=SysLogHandler.LOG_LOCAL1)
202 log_handler.ident = 'burrow-telegram: '
203 logger.setLevel(logging.INFO)
204 log_handler.setFormatter(formatter)
205 logger.addHandler(log_handler)