Refactor
This commit is contained in:
@@ -4,16 +4,17 @@ from mqtt_manager import MqttManager
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
tevolve = Tevolve()
|
tevolve = Tevolve()
|
||||||
|
devices = tevolve.get_devices()
|
||||||
|
|
||||||
|
mqtt_manager = MqttManager()
|
||||||
|
|
||||||
|
mqtt_manager.client.connect(heaters=devices["nodes"])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tevolve.get_token()
|
tevolve.get_token()
|
||||||
tevolve.get_dev()
|
tevolve.get_dev()
|
||||||
|
|
||||||
|
|
||||||
devices = tevolve.get_devices()
|
|
||||||
mqtt_manager = MqttManager(devices["nodes"])
|
|
||||||
|
|
||||||
mqtt_manager.publish_discovery()
|
mqtt_manager.publish_discovery()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
238
src/main_old.py
Normal file
238
src/main_old.py
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
import time
|
||||||
|
from pytevolve import Tevolve
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import threading
|
||||||
|
import json
|
||||||
|
|
||||||
|
broker_address = "192.168.0.100"
|
||||||
|
|
||||||
|
|
||||||
|
def on_disconnect(client, userdata, rc):
|
||||||
|
|
||||||
|
client.connect(broker_address)
|
||||||
|
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# client.username_pw_set("admin", "bK2F2ZPjyyngmsN6R32s")
|
||||||
|
# client.username_pw_set(username="admin", password ="bK2F2ZPjyyngmsN6R32s")
|
||||||
|
client.connect(broker_address)
|
||||||
|
except:
|
||||||
|
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
client.connect(client)
|
||||||
|
|
||||||
|
|
||||||
|
def on_message(client, userdata, message):
|
||||||
|
new_topic = str(message.topic)
|
||||||
|
|
||||||
|
if new_topic == "hallway/heater/settemp":
|
||||||
|
tevolve.set_temperature("3", str(message.payload.decode("utf-8")))
|
||||||
|
# Assume set temp success (Prevent debounce)
|
||||||
|
client.publish("hallway/heater/currentsettemp", payload=str(message.payload.decode("utf-8")), qos=0, retain=False)
|
||||||
|
|
||||||
|
if new_topic == "hallway/heater/setmode":
|
||||||
|
if str(message.payload.decode("utf-8")) == "heat":
|
||||||
|
mode = "manual"
|
||||||
|
# client.publish("hallway/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
client.publish("hallway/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
else:
|
||||||
|
mode = "off"
|
||||||
|
client.publish("hallway/heater/currentmode", payload="off", qos=0, retain=False)
|
||||||
|
client.publish("hallway/heater/idleaction", payload="off", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if mode == "manual":
|
||||||
|
if float(Tevolve.hallway_current_temp) > float(Tevolve.hallway_set_temp):
|
||||||
|
client.publish("hallway/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
else:
|
||||||
|
client.publish("hallway/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
Tevolve.setmode = mode
|
||||||
|
tevolve.set_mode("3")
|
||||||
|
|
||||||
|
|
||||||
|
if new_topic == "livingroom/heater/settemp":
|
||||||
|
tevolve.set_temperature("2", str(message.payload.decode("utf-8")))
|
||||||
|
# Assume set temp success (Prevent debounce)
|
||||||
|
client.publish("livingroom/heater/currentsettemp", payload=str(message.payload.decode("utf-8")), qos=0,
|
||||||
|
retain=False)
|
||||||
|
|
||||||
|
if new_topic == "livingroom/heater/setmode":
|
||||||
|
# print(str(message.payload.decode("utf-8")))
|
||||||
|
|
||||||
|
if str(message.payload.decode("utf-8")) == "heat":
|
||||||
|
mode = "manual"
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
else:
|
||||||
|
mode = "off"
|
||||||
|
client.publish("livingroom/heater/currentmode", payload="off", qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="off", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
if mode == "manual":
|
||||||
|
if float(Tevolve.livingroom_current_temp) > float(Tevolve.livingroom_set_temp):
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
else:
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
|
||||||
|
Tevolve.setmode = mode
|
||||||
|
tevolve.set_mode("2")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
tevolve = Tevolve()
|
||||||
|
|
||||||
|
tevolve.post_websocket()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
t = threading.Thread(target=tevolve.create_websocket, daemon=True, args=())
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
client = mqtt.Client("ryan-heating")
|
||||||
|
|
||||||
|
# client.username_pw_set("admin", "bK2F2ZPjyyngmsN6R32s")
|
||||||
|
|
||||||
|
client.connect(broker_address)
|
||||||
|
client.on_message = on_message
|
||||||
|
client.on_disconnect = on_disconnect
|
||||||
|
client.loop_start()
|
||||||
|
|
||||||
|
|
||||||
|
client.subscribe("hallway/heater/settemp")
|
||||||
|
client.subscribe("hallway/heater/setmode")
|
||||||
|
|
||||||
|
client.subscribe("livingroom/heater/settemp")
|
||||||
|
client.subscribe("livingroom/heater/setmode")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
livingroom_status = tevolve.get_status("2")
|
||||||
|
hallway_status = tevolve.get_status("3")
|
||||||
|
|
||||||
|
|
||||||
|
Tevolve.hallway_active = hallway_status["active"]
|
||||||
|
Tevolve.hallway_mode = hallway_status["mode"]
|
||||||
|
Tevolve.hallway_set_temp = hallway_status["stemp"]
|
||||||
|
Tevolve.hallway_current_temp = hallway_status["mtemp"]
|
||||||
|
|
||||||
|
|
||||||
|
# Tevolve.livingroom_active = livingroom_status["active"]
|
||||||
|
# Tevolve.livingroom_mode = livingroom_status["mode"]
|
||||||
|
# Tevolve.livingroom_set_temp = livingroom_status["stemp"]
|
||||||
|
# Tevolve.livingroom_current_temp = livingroom_status["mtemp"]
|
||||||
|
|
||||||
|
|
||||||
|
if Tevolve.livingroom_mode == "manual":
|
||||||
|
Tevolve.livingroom_mode = "heat"
|
||||||
|
if Tevolve.hallway_mode == "manual":
|
||||||
|
Tevolve.hallway_mode = "heat"
|
||||||
|
|
||||||
|
client.publish("hallway/heater/currenttemp", payload=Tevolve.hallway_current_temp, qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currenttemp", payload=Tevolve.livingroom_current_temp, qos=0, retain=False)
|
||||||
|
|
||||||
|
client.publish("hallway/heater/currentmode", payload=Tevolve.hallway_mode, qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currentmode", payload=Tevolve.livingroom_mode, qos=0, retain=False)
|
||||||
|
|
||||||
|
client.publish("hallway/heater/currentsettemp", payload=Tevolve.hallway_set_temp, qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currentsettemp", payload=Tevolve.livingroom_set_temp, qos=0, retain=False)
|
||||||
|
|
||||||
|
# Setup initial hallway heater values
|
||||||
|
if Tevolve.hallway_mode == "off":
|
||||||
|
client.publish("hallway/heater/setmode", payload="off", qos=0, retain=False)
|
||||||
|
# client.publish("hallway/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
|
||||||
|
if Tevolve.hallway_mode == "manual":
|
||||||
|
if float(Tevolve.hallway_current_temp) >= float(Tevolve.hallway_set_temp):
|
||||||
|
client.publish("hallway/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
if float(Tevolve.hallway_set_temp) > float(Tevolve.hallway_current_temp):
|
||||||
|
client.publish("hallway/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
# Setup initial living room heater values
|
||||||
|
if Tevolve.livingroom_mode == "off":
|
||||||
|
client.publish("livingroom/heater/setmode", payload="off", qos=0, retain=False)
|
||||||
|
|
||||||
|
if Tevolve.livingroom_mode == "manual":
|
||||||
|
if float(Tevolve.livingroom_current_temp) >= float(Tevolve.livingroom_set_temp):
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
if float(Tevolve.livingroom_set_temp) > float(Tevolve.livingroom_current_temp):
|
||||||
|
client.publish("livignroom/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
if Tevolve.websocket_message != "":
|
||||||
|
|
||||||
|
if "/api/v2" in Tevolve.websocket_message:
|
||||||
|
|
||||||
|
print(Tevolve.websocket_message[30:-1])
|
||||||
|
m = json.loads(Tevolve.websocket_message[30:-1])
|
||||||
|
# print(m)
|
||||||
|
|
||||||
|
if m["path"] == "/htr/3/status":
|
||||||
|
|
||||||
|
Tevolve.hallway_active = m["body"]["active"]
|
||||||
|
Tevolve.hallway_mode = m["body"]["mode"]
|
||||||
|
Tevolve.hallway_set_temp = m["body"]["stemp"]
|
||||||
|
Tevolve.hallway_current_temp = m["body"]["mtemp"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if Tevolve.hallway_mode == "off":
|
||||||
|
client.publish("hallway/heater/setmode", payload="off", qos=0, retain=False)
|
||||||
|
# client.publish("hallway/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
|
||||||
|
if Tevolve.hallway_mode == "manual":
|
||||||
|
|
||||||
|
if float(Tevolve.hallway_set_temp) > float(Tevolve.hallway_current_temp):
|
||||||
|
client.publish("hallway/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
client.publish("hallway/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
|
||||||
|
if float(Tevolve.hallway_set_temp) <= float(Tevolve.hallway_current_temp):
|
||||||
|
client.publish("hallway/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
client.publish("hallway/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
|
||||||
|
client.publish("hallway/heater/currentsettemp", payload=m["body"]["stemp"], qos=0, retain=False)
|
||||||
|
# Publish hallway current temperature
|
||||||
|
client.publish("hallway/heater/currenttemp", payload=m["body"]["mtemp"], qos=0, retain=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if m["path"] == "/htr/2/status":
|
||||||
|
|
||||||
|
Tevolve.livingroom_active = m["body"]["active"]
|
||||||
|
Tevolve.livingroom_mode = m["body"]["mode"]
|
||||||
|
Tevolve.livingroom_set_temp = m["body"]["stemp"]
|
||||||
|
Tevolve.livingroom_current_temp = m["body"]["mtemp"]
|
||||||
|
|
||||||
|
if Tevolve.livingroom_mode == "off":
|
||||||
|
client.publish("livingroom/heater/setmode", payload="off", qos=0, retain=False)
|
||||||
|
|
||||||
|
if Tevolve.livingroom_mode == "manual":
|
||||||
|
if float(Tevolve.livingroom_set_temp) > float(Tevolve.livingroom_current_temp):
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="heating", qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
|
||||||
|
if float(Tevolve.livingroom_set_temp) <= float(Tevolve.livingroom_current_temp):
|
||||||
|
client.publish("livingroom/heater/idleaction", payload="idle", qos=0, retain=False)
|
||||||
|
client.publish("livingroom/heater/currentmode", payload="heat", qos=0, retain=False)
|
||||||
|
|
||||||
|
client.publish("livingroom/heater/currentsettemp", payload=m["body"]["stemp"], qos=0, retain=False)
|
||||||
|
# Publish hallway current temperature
|
||||||
|
client.publish("livingroom/heater/currenttemp", payload=m["body"]["mtemp"], qos=0, retain=False)
|
||||||
|
|
||||||
|
Tevolve.websocket_message = ""
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
@@ -4,12 +4,12 @@ import paho.mqtt.client as mqtt
|
|||||||
|
|
||||||
|
|
||||||
class MqttManager:
|
class MqttManager:
|
||||||
def __init__(self, heaters):
|
def __init__(self):
|
||||||
self.host = "192.168.0.100"
|
self.host = "192.168.0.100"
|
||||||
self.username = "connorroy"
|
self.username = "connorroy"
|
||||||
self.password = "arkreactor7"
|
self.password = "arkreactor7"
|
||||||
|
|
||||||
self.object_ids = heaters
|
self.object_ids = None
|
||||||
|
|
||||||
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||||
self.client.on_connect = self.on_connect
|
self.client.on_connect = self.on_connect
|
||||||
@@ -17,11 +17,16 @@ class MqttManager:
|
|||||||
|
|
||||||
self.client.username_pw_set(username=self.username, password=self.password)
|
self.client.username_pw_set(username=self.username, password=self.password)
|
||||||
|
|
||||||
self.client.connect(self.host, 1883, 60)
|
# self.client.connect(self.host, 1883, 60)
|
||||||
|
|
||||||
self.client.loop_forever()
|
self.client.loop_forever()
|
||||||
|
|
||||||
def on_connect(self, client, userdata, flags, reason_code, properties):
|
def mqtt_connect(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_connect(self, client, userdata, flags, reason_code, properties, heaters=None):
|
||||||
|
|
||||||
|
self.object_ids = heaters
|
||||||
print(f"Connected with result code {reason_code}")
|
print(f"Connected with result code {reason_code}")
|
||||||
|
|
||||||
discovery_topic = "homeassistant/climate/"
|
discovery_topic = "homeassistant/climate/"
|
||||||
|
|||||||
Reference in New Issue
Block a user