#!/usr/bin/env python3 import argparse import re import time import signal import logging import sys import schedule import paho.mqtt.client as mqtt from eq3bt import Thermostat class Zimmer: name = None thermostat = None zimmer = [Zimmer, Zimmer] zimmerName = ["alexander", "wohnzimmer" ] zimmerThermostat = ["00:1A:22:16:4B:B4", "00:1A:22:16:25:C3"] for i in range(len(zimmer)): zimmer[i].name = zimmerName[i] zimmer[i].thermostat = zimmerThermostat[i] def on_connect(client, userdata, flags, rc): log.debug("Connected with result code " + str(rc)) for i in zimmer: client.subscribe("zimmer/" + i.name + "/heizung/temperatur") client.subscribe("zimmer/" + i.name + "/heizung/status") sendReadings() def on_publish(client, userdata, mid): log.debug("mid: "+str(mid)) def on_message(client, userdata, message): msg = message.payload.decode("utf-8") log.debug('received message: %s from %s', format(msg), format(message.topic)) if message.topic.split("/", -1)[2] == "heizung": if message.topic.split("/", -1)[3] == "temperatur": for i in zimmer: tmpThermostat = Thermostat(i.thermostat) if message.topic.split("/", -1)[1] == i.name: tmpThermostat.mode= 3 tmpThermostat.target_temperature = round(float(msg),1) time.sleep(10) sendReadings() def sendReadings(): log.debug('read target temperature from thermostats') for i in zimmer: tmpThermostat = Thermostat(i.thermostat) tmpThermostat.update() if tmpThermostat.target_temperature > 17.0: client.publish("zimmer/" + i.name + "/heizung/status", 1, qos=1, retain=True) else: client.publish("zimmer/" + i.name + "/heizung/status", 0, qos=1, retain=True) time.sleep(10) log.debug('sent readings') def terminate(signum, frame): log.warn('SIGTERM received. Shutting down!') log.info('stopping mqtt client') client.loop_stop() log.info('disconnecting mqtt client') client.disconnect() log.info('heizberry stopped all functions; exit') sys.exit(0) def getArgs(): parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") return parser.parse_args() if __name__ == '__main__': signal.signal(signal.SIGINT, terminate) args = getArgs() logging.basicConfig(filename='/var/log/heizberry/heizberry_io.log', level=logging.WARN, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') log = logging.getLogger('heizberry') if args.verbose: log.setLevel(logging.DEBUG) log.info('Loglevel set to DEBUG') else: log.setLevel(logging.WARN) log.debug('start mqtt client') client = mqtt.Client("localThermostatIo") client.on_connect = on_connect client.on_message = on_message client.connect("192.168.1.24", 1883, 60) log.debug('connected mqtt client') client.loop_start() log.debug('schedule periodic readings') schedule.every(60).seconds.do(sendReadings) time.sleep(10)