2021-03-16 22:00:45 +01:00
|
|
|
import time
|
2021-03-17 07:34:55 +01:00
|
|
|
|
2021-03-16 21:09:22 +01:00
|
|
|
import paho.mqtt.client as mqtt
|
2021-03-16 22:00:45 +01:00
|
|
|
import json
|
|
|
|
import threading
|
2021-03-16 21:09:22 +01:00
|
|
|
|
2021-03-16 22:00:45 +01:00
|
|
|
# initial scene
|
2021-03-17 07:34:55 +01:00
|
|
|
from heater.modules.heater import Heater
|
|
|
|
from heater.modules.watcher import Watcher
|
|
|
|
|
2021-03-16 22:00:45 +01:00
|
|
|
scene = "default"
|
2021-03-16 21:09:22 +01:00
|
|
|
|
2021-03-17 07:34:55 +01:00
|
|
|
watcher = Watcher({
|
|
|
|
"office1": Heater(topic="zigbee2mqtt/heater1", set_topic="zigbee2mqtt/heater1/set"),
|
|
|
|
"office2": Heater(topic="zigbee2mqtt/heater2", set_topic="zigbee2mqtt/heater2/set"),
|
|
|
|
"bedroom": Heater(topic="zigbee2mqtt/heater3", set_topic="zigbee2mqtt/heater3/set"),
|
|
|
|
})
|
|
|
|
|
2021-03-16 21:09:22 +01:00
|
|
|
|
|
|
|
# The callback for when the client receives a CONNACK response from the server.
|
2021-03-16 22:00:45 +01:00
|
|
|
def on_connect(client, _userdata, _flags, rc):
|
2021-03-16 21:09:22 +01:00
|
|
|
print("Connected with result code " + str(rc))
|
|
|
|
|
2021-03-16 22:00:45 +01:00
|
|
|
threading.Thread(target=loop_thread, args=(client,), daemon=True).start()
|
|
|
|
|
2021-03-16 21:09:22 +01:00
|
|
|
# Subscribing in on_connect() means that if we lose the connection and
|
|
|
|
# reconnect then subscriptions will be renewed.
|
|
|
|
client.subscribe("control/lights/set")
|
2021-03-17 07:34:55 +01:00
|
|
|
for topic in watcher.get_topics():
|
|
|
|
client.subscribe(topic)
|
|
|
|
watcher.pull_values(client)
|
2021-03-16 21:09:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# The callback for when a PUBLISH message is received from the server.
|
2021-03-16 22:00:45 +01:00
|
|
|
def on_message(client, _userdata, msg):
|
|
|
|
global scene
|
|
|
|
(topic, payload) = parse_message(msg)
|
2021-03-17 07:34:55 +01:00
|
|
|
if topic == "control/lights/set":
|
|
|
|
print("set scene %s -> %s" % (scene, payload['scene']))
|
|
|
|
scene = payload['scene']
|
|
|
|
update_scene(client)
|
|
|
|
else:
|
|
|
|
print("got %s" % topic)
|
|
|
|
watcher.update_actual_heating_point_for_topic(topic, payload)
|
|
|
|
#watcher.publish(client)
|
2021-03-16 22:00:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_message(msg):
|
|
|
|
m_decode = str(msg.payload.decode("utf-8", "ignore"))
|
|
|
|
payload = json.loads(m_decode) # decode json data
|
|
|
|
return msg.topic, payload
|
|
|
|
|
|
|
|
|
2021-03-17 07:34:55 +01:00
|
|
|
def update_scene(client):
|
2021-03-17 09:09:44 +01:00
|
|
|
if scene in ["night", "outside"]:
|
2021-03-17 07:34:55 +01:00
|
|
|
watcher.update("office1", 10)
|
|
|
|
watcher.update("office2", 10)
|
|
|
|
watcher.update("bedroom", 13)
|
2021-03-17 09:09:44 +01:00
|
|
|
elif scene in ["default", "up-bright", "up-dark", "half", "down"]:
|
2021-03-17 07:34:55 +01:00
|
|
|
watcher.update("office1", 23)
|
|
|
|
watcher.update("office2", 23)
|
|
|
|
watcher.update("bedroom", 18)
|
2021-03-16 22:00:45 +01:00
|
|
|
else:
|
2021-03-17 07:34:55 +01:00
|
|
|
watcher.update("office1", 10)
|
|
|
|
watcher.update("office2", 10)
|
|
|
|
watcher.update("bedroom", 13)
|
|
|
|
|
|
|
|
watcher.publish(client)
|
2021-03-16 21:09:22 +01:00
|
|
|
|
|
|
|
|
2021-03-16 22:00:45 +01:00
|
|
|
def loop_thread(client):
|
|
|
|
while True:
|
2021-03-17 07:34:55 +01:00
|
|
|
watcher.publish(client)
|
|
|
|
time.sleep(120)
|
2021-03-16 21:09:22 +01:00
|
|
|
|
|
|
|
|
2021-03-16 22:00:45 +01:00
|
|
|
if __name__ == "__main__":
|
2021-03-17 07:34:55 +01:00
|
|
|
mqttClient = mqtt.Client()
|
|
|
|
mqttClient.on_connect = on_connect
|
|
|
|
mqttClient.on_message = on_message
|
|
|
|
mqttClient.username_pw_set("homeassistant", password="password")
|
|
|
|
mqttClient.connect("pepe.private", 1883, 60)
|
2021-03-16 22:00:45 +01:00
|
|
|
# Blocking call that processes network traffic, dispatches callbacks and
|
|
|
|
# handles reconnecting.
|
|
|
|
# Other loop*() functions are available that give a threaded interface and a
|
|
|
|
# manual interface.
|
|
|
|
mqttClient.loop_forever()
|