Heizberry/heizberry.py
2019-02-20 18:59:07 +01:00

100 lines
2.5 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
thermostatBallpit = Thermostat('00:1A:22:0D:BE:EE')
global temperature_off = 12
global temperature_on = 19
def on_connect(client, userdata, flags, rc):
log.debug("Connected with result code " + str(rc))
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 == "foobar/oben/baellebad/heizung/action")
if (msg == "on"):
thermostatBallpit.target_temperature=temperature_on
else
if (msg == "on"):
thermostatBallpit.target_temperature=temperature_on
else
thermostatBallpit.target_temperature=msg
def sendReadings():
log.debug('read target temperature from thermostats')
# Bällebad
# Update readings
thermostatBallpit.update()
# Send target temperatures
temp=thermostatBallpit.target_temperature
if(temp == temperature_on)
temp = temp + " on"
if(temp == temperature_off)
temp = temp + " off"
client.publish("foobar/oben/baellebad/status", temp, qos=1, retain=True)
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(
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)
client = mqtt.Client("Heizberry_oben")
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.42.0.244", 1883, 60)
client.loop_start()
log.debug('schedule periodic readings')
schedule.every(1).minutes.do(sendReadings)