Heizberry/heizberry_io.py
2021-04-16 17:14:07 +02:00

117 lines
3.0 KiB
Python
Executable File

#!/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]
zimmerName = ["alexander"]
zimmerThermostat = [Thermostat('00:1A:22:16:4B:B4')]
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:
if message.topic.split("/", -1)[1] == i.name:
i.thermostat.mode= 3
i.thermostat.target_temperature = round(float(msg),1)
sendReadings()
def sendReadings():
log.debug('read target temperature from thermostats')
for i in zimmer:
i.thermostat.update()
if i.thermostat.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)
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/homeassistant_services/heizberry_io.log', level=logging.DEBUG, 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)