Heizberry/heizberry.py

117 lines
2.9 KiB
Python
Raw Normal View History

2019-02-20 17:59:07 +00:00
#!/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
2019-03-09 20:04:20 +00:00
thermostatBallpit = Thermostat('00:1A:22:0D:BE:EE')
2019-02-20 17:59:07 +00:00
thermostatBallpit = Thermostat('00:1A:22:0D:BE:EE')
2019-03-09 20:04:20 +00:00
temperature_off = 12
temperature_on = 19
2019-02-20 17:59:07 +00:00
def on_connect(client, userdata, flags, rc):
log.debug("Connected with result code " + str(rc))
2019-03-09 20:04:20 +00:00
client.subscribe("foobar/oben/baellebad/heizung/action")
2019-02-20 17:59:07 +00:00
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))
2019-03-09 20:04:20 +00:00
if (message.topic == "foobar/oben/baellebad/heizung/action"):
2019-02-20 17:59:07 +00:00
if (msg == "on"):
thermostatBallpit.target_temperature=temperature_on
2019-03-09 20:04:20 +00:00
else:
if (msg == "off"):
thermostatBallpit.target_temperature=temperature_off
else:
thermostatBallpit.target_temperature=int(msg)
2019-02-20 17:59:07 +00:00
def sendReadings():
log.debug('read target temperature from thermostats')
# Bällebad
# Update readings
thermostatBallpit.update()
# Send target temperatures
temp=thermostatBallpit.target_temperature
2019-03-09 20:04:20 +00:00
if(temp == temperature_on):
temp = str(temp) + " on"
2019-02-20 17:59:07 +00:00
2019-03-09 20:04:20 +00:00
if(temp == temperature_off):
temp = str(temp) + " off"
2019-02-20 17:59:07 +00:00
2019-03-09 20:04:20 +00:00
client.publish("foobar/oben/baellebad/heizung/status", temp, qos=1, retain=True)
log.debug('sent readings')
2019-02-20 17:59:07 +00:00
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)
2019-03-09 20:04:20 +00:00
log.debug('start mqtt client')
2019-02-20 17:59:07 +00:00
client = mqtt.Client("Heizberry_oben")
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.42.0.244", 1883, 60)
2019-03-09 20:04:20 +00:00
log.debug('connected mqtt client')
2019-02-20 17:59:07 +00:00
client.loop_start()
log.debug('schedule periodic readings')
2019-03-09 20:04:20 +00:00
schedule.every(60).seconds.do(sendReadings)
time.sleep(10)
while True:
schedule.run_pending()
time.sleep(1)
# sendReadings()
# time.sleep(60)