mirror of
https://github.com/c3e/Heizberry.git
synced 2024-04-01 15:11:26 +00:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f4f1ee9fd | |||
1aa07e8ff2 | |||
d9fbd1af5a | |||
15a004c59a |
138
heizberry.py
138
heizberry.py
@ -1,138 +0,0 @@
|
|||||||
#!/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')
|
|
||||||
thermostatCantina = Thermostat('00:1A:22:0D:C2:5D')
|
|
||||||
|
|
||||||
|
|
||||||
temperature_off = 12
|
|
||||||
temperature_on = 19
|
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
|
||||||
log.debug("Connected with result code " + str(rc))
|
|
||||||
|
|
||||||
client.subscribe("foobar/oben/baellebad/heizung/action")
|
|
||||||
client.subscribe("foobar/oben/cantina/heizung/action")
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
# Baellebad
|
|
||||||
if (message.topic == "foobar/oben/baellebad/heizung/action"):
|
|
||||||
thermostatBallpit.mode="manual"
|
|
||||||
if (msg == "on"):
|
|
||||||
thermostatBallpit.target_temperature=temperature_on
|
|
||||||
else:
|
|
||||||
if (msg == "off"):
|
|
||||||
thermostatBallpit.target_temperature=temperature_off
|
|
||||||
else:
|
|
||||||
thermostatBallpit.target_temperature=round(float(msg),1)
|
|
||||||
|
|
||||||
# Cantina
|
|
||||||
if (message.topic == "foobar/oben/cantina/heizung/action"):
|
|
||||||
thermostatCantina.mode="manual"
|
|
||||||
if (msg == "on"):
|
|
||||||
thermostatCantina.target_temperature=temperature_on
|
|
||||||
else:
|
|
||||||
if (msg == "off"):
|
|
||||||
thermostatCantina.target_temperature=temperature_off
|
|
||||||
else:
|
|
||||||
thermostatCantina.target_temperature=round(float(msg),1)
|
|
||||||
sendReadings()
|
|
||||||
|
|
||||||
def sendReadings():
|
|
||||||
log.debug('read target temperature from thermostats')
|
|
||||||
|
|
||||||
# Bällebad
|
|
||||||
# Update readings
|
|
||||||
thermostatBallpit.update()
|
|
||||||
|
|
||||||
# Send target temperatures
|
|
||||||
temp=thermostatBallpit.target_temperature
|
|
||||||
client.publish("foobar/oben/baellebad/heizung/status", temp, qos=1, retain=True)
|
|
||||||
if(temp == temperature_on):
|
|
||||||
client.publish("foobar/oben/baellebad/heizung/status", "on", qos=1, retain=True)
|
|
||||||
if(temp == temperature_off):
|
|
||||||
client.publish("foobar/oben/baellebad/heizung/status", "off", qos=1, retain=True)
|
|
||||||
|
|
||||||
# Cantina
|
|
||||||
# Update readings
|
|
||||||
thermostatCantina.update()
|
|
||||||
|
|
||||||
# Send target temperatures
|
|
||||||
temp=thermostatCantina.target_temperature
|
|
||||||
client.publish("foobar/oben/cantina/heizung/status", temp, qos=1, retain=True)
|
|
||||||
if(temp == temperature_on):
|
|
||||||
client.publish("foobar/oben/cantina/heizung/status", "on", qos=1, retain=True)
|
|
||||||
if(temp == temperature_off):
|
|
||||||
client.publish("foobar/oben/cantina/heizung/status", "off", 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(
|
|
||||||
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("Heizberry_oben")
|
|
||||||
client.on_connect = on_connect
|
|
||||||
client.on_message = on_message
|
|
||||||
client.connect("10.42.0.244", 1883, 60)
|
|
||||||
log.debug('connected mqtt client')
|
|
||||||
|
|
||||||
client.loop_start()
|
|
||||||
|
|
||||||
log.debug('schedule periodic readings')
|
|
||||||
schedule.every(60).seconds.do(sendReadings)
|
|
||||||
|
|
||||||
time.sleep(10)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
schedule.run_pending()
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
130
heizberry_control.py
Executable file
130
heizberry_control.py
Executable file
@ -0,0 +1,130 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
import signal
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import schedule
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
|
||||||
|
class Zimmer:
|
||||||
|
name = None
|
||||||
|
automatic = None
|
||||||
|
temperaturSet = None
|
||||||
|
statusActual = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
zimmer = [Zimmer, Zimmer]
|
||||||
|
zimmerName =["alexander", "wohnzimmer"]
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(len(zimmer)):
|
||||||
|
zimmer[i].name = zimmerName[i]
|
||||||
|
zimmer[i].automatic = 0
|
||||||
|
zimmer[i].temperaturSet = 0.0
|
||||||
|
zimmer[i].statusActual = 0
|
||||||
|
|
||||||
|
|
||||||
|
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/homeassistant/automatic")
|
||||||
|
client.subscribe("zimmer/" + i.name + "/heizung/homeassistant/temperatur")
|
||||||
|
client.subscribe("zimmer/" + i.name + "/heizung/homeassistant/status")
|
||||||
|
client.subscribe("zimmer/" + i.name + "/heizung/temperatur")
|
||||||
|
client.subscribe("zimmer/" + i.name + "/heizung/status")
|
||||||
|
# client.subscribe("zimmer/" + i.name + "/sensoren/tuer/status")
|
||||||
|
# client.subscribe("zimmer/" + i.name + "/sensoren/fenster/status")
|
||||||
|
# client.subscribe("zimmer/" + i.name + "/sensoren/temperatur")
|
||||||
|
|
||||||
|
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] == "homeassistant":
|
||||||
|
if message.topic.split("/", -1)[4] == "automatic":
|
||||||
|
for i in zimmer:
|
||||||
|
if message.topic.split("/", -1)[1] == i.name:
|
||||||
|
i.automatic = bool(msg)
|
||||||
|
if message.topic.split("/", -1)[4] == "temperatur":
|
||||||
|
for i in zimmer:
|
||||||
|
if message.topic.split("/", -1)[1] == i.name:
|
||||||
|
i.temperaturSet = round(float(msg),1)
|
||||||
|
if message.topic.split("/", -1)[3] == "status":
|
||||||
|
for i in zimmer:
|
||||||
|
if message.topic.split("/", -1)[1] == i.name:
|
||||||
|
i.statusActual = bool(msg)
|
||||||
|
|
||||||
|
|
||||||
|
sendReadings()
|
||||||
|
|
||||||
|
def sendReadings():
|
||||||
|
|
||||||
|
for i in zimmer:
|
||||||
|
|
||||||
|
if i.automatic:
|
||||||
|
client.publish("zimmer/" + i.name + "/heizung/temperatur", i.temperaturSet, qos=1, retain=True)
|
||||||
|
client.publish("zimmer/" + i.name + "/heizung/homeassistant/status", i.statusActual, 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_control.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("localThermostatControl")
|
||||||
|
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)
|
13
heizberry_control.service
Normal file
13
heizberry_control.service
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=homeassistent_thermostat
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/srv/homeassistent_services/heizberry_control.py
|
||||||
|
|
||||||
|
Restart=always
|
||||||
|
RestartSec=20
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
116
heizberry_io.py
Executable file
116
heizberry_io.py
Executable file
@ -0,0 +1,116 @@
|
|||||||
|
#!/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 = [Thermostat('00:1A:22:16:4B:B4'), Thermostat('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:
|
||||||
|
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)
|
@ -1,10 +1,10 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=heizberry
|
Description=homeassistent_thermostat
|
||||||
After=network-online.target
|
After=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=/srv/heizberry/heizberry.py
|
ExecStart=/srv/homeassistent_services/heizberry_io.py
|
||||||
|
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=20
|
RestartSec=20
|
Reference in New Issue
Block a user