Compare commits
5 Commits
d3b55cb028
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1640ae7087 | |||
| a46405f4fd | |||
| 1cb68fa011 | |||
| 875f487b4c | |||
| 9c781359e1 |
22
src/climate.json
Normal file
22
src/climate.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"dev": {
|
||||
"ids": ["ea334450945afc"],
|
||||
"name": "atc smart controller",
|
||||
"mf": "Tevolve"
|
||||
},
|
||||
|
||||
"uniq_id": "",
|
||||
"name": "{name}",
|
||||
"state_topic": "homeassistant/climate/{uid}/state",
|
||||
"modes": ["heat", "off"],
|
||||
"mode_command_topic": "{uid}/heater/setmode",
|
||||
"mode_state_topic": "{uid}/heater/currentmode",
|
||||
"min_temp": 7,
|
||||
"max_temp": 30,
|
||||
"temp_step": 0.5,
|
||||
"current_temperature_topic": "{uid}/heater/currenttemp",
|
||||
"temperature_command_topic": "{uid}/heater/settemp",
|
||||
"temperature_state_topic": "{uid}/heater/currentsettemp",
|
||||
"action_topic": "{uid}/heater/idleaction",
|
||||
"availability_topic": "{uid}/availablity/state"
|
||||
}
|
||||
12
src/device.json
Normal file
12
src/device.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"dev": {
|
||||
"ids": ["ea334450945afc"],
|
||||
"name": "atc smart controller",
|
||||
"mf": "Tevolve"
|
||||
},
|
||||
"o": {
|
||||
"name":"tevolve2mqtt"
|
||||
},
|
||||
|
||||
"qos": 2
|
||||
}
|
||||
233
src/main.py
233
src/main.py
@@ -1,238 +1,41 @@
|
||||
import time
|
||||
from pytevolve import Tevolve
|
||||
import paho.mqtt.client as mqtt
|
||||
from mqtt_manager import MqttManager
|
||||
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)
|
||||
host = "192.168.0.100"
|
||||
|
||||
|
||||
|
||||
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)
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
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.get_token()
|
||||
tevolve.get_dev()
|
||||
tevolve.get_devices()
|
||||
|
||||
tevolve.post_websocket()
|
||||
time.sleep(1)
|
||||
status = tevolve.get_status()
|
||||
|
||||
mqtt_manager = MqttManager()
|
||||
|
||||
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")
|
||||
# threading.Thread(target=mqtt_manager.start_mqtt, args=(host,)).start()
|
||||
|
||||
|
||||
|
||||
livingroom_status = tevolve.get_status("2")
|
||||
hallway_status = tevolve.get_status("3")
|
||||
while 1:
|
||||
if mqtt_manager.is_connected == True:
|
||||
|
||||
mqtt_manager.publish_discovery(tevolve.devices)
|
||||
|
||||
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"]
|
||||
mqtt_manager.publish_heaters(status)
|
||||
break
|
||||
|
||||
|
||||
# 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"]
|
||||
# Update Device States
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
tevolve.get_sid()
|
||||
# test.post_websocket()
|
||||
# test.create_websocket()
|
||||
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)
|
||||
|
||||
132
src/mqtt_manager.py
Normal file
132
src/mqtt_manager.py
Normal file
@@ -0,0 +1,132 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import paho.mqtt.client as mqtt
|
||||
import threading
|
||||
|
||||
class MqttManager:
|
||||
def __init__(self):
|
||||
self.host = "192.168.0.100"
|
||||
self.username = "connorroy"
|
||||
self.password = "arkreactor7"
|
||||
self.is_connected = False
|
||||
self.object_ids = None
|
||||
|
||||
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||
self.client.on_connect = self.on_connect
|
||||
self.client.ondisconnect = self.on_disconnect
|
||||
self.client.on_message = self.on_message
|
||||
|
||||
self.client.username_pw_set(username=self.username, password=self.password)
|
||||
|
||||
self.client.connect(self.host, 1883, 60)
|
||||
threading.Thread(target=self.client.loop_forever).start()
|
||||
# self.client.loop_forever()
|
||||
|
||||
|
||||
|
||||
def publish_heaters(self, rad_data):
|
||||
# self.object_ids = heaters
|
||||
|
||||
discovery_topic = "homeassistant/climate/"
|
||||
|
||||
json_payload = self._load_json()
|
||||
|
||||
|
||||
for device_index, device in enumerate(self.object_ids["nodes"]):
|
||||
|
||||
self.client.subscribe(topic=json_payload[device_index]["state_topic"])
|
||||
|
||||
if device["lost"] is True:
|
||||
|
||||
|
||||
self.client.publish(topic=json_payload[device_index]["availability_topic"], qos=0, retain=False,
|
||||
payload="offline")
|
||||
else:
|
||||
self.client.publish(topic=json_payload[device_index]["availability_topic"], qos=0, retain=False,
|
||||
payload="online")
|
||||
self.client.subscribe(topic=json_payload[device_index]["mode_state_topic"])
|
||||
|
||||
|
||||
if rad_data[device_index][device["name"]]["mode"] == "manual":
|
||||
mode = "heat"
|
||||
else:
|
||||
mode = "off"
|
||||
|
||||
|
||||
|
||||
|
||||
self.client.publish(topic=json_payload[device_index]["mode_state_topic"], qos=0, retain=False,
|
||||
payload=mode)
|
||||
self.client.publish(topic=json_payload[device_index]["action_topic"], qos=0, retain=False,
|
||||
payload="heating")
|
||||
|
||||
|
||||
# self.client.subscribe(topic=json_payload[device_index]["current_temperature_topic"])
|
||||
|
||||
self.client.publish(topic=json_payload[device_index]["current_temperature_topic"], qos=0, retain=False,
|
||||
payload="17")
|
||||
|
||||
|
||||
|
||||
def on_connect(self, client, userdata, flags, reason_code, properties):
|
||||
|
||||
print("Connected")
|
||||
self.is_connected = True
|
||||
|
||||
|
||||
|
||||
# # Subscribing in on_connect() means that if we lose the connection and
|
||||
# # reconnect then subscriptions will be renewed.
|
||||
# client.subscribe("$SYS/#")
|
||||
|
||||
def on_disconnect(self, client, userdata, flags, reason):
|
||||
print("Disconnected")
|
||||
self.is_connected = False
|
||||
|
||||
|
||||
def on_message(self, client, userdata, msg):
|
||||
print(msg.topic + " " + str(msg.payload))
|
||||
|
||||
|
||||
|
||||
"Adds devices via mqtt discovery"
|
||||
def publish_discovery(self, heaters):
|
||||
|
||||
self.object_ids = heaters
|
||||
|
||||
path = Path(__file__).parent
|
||||
with open(str(path) + "/device.json") as j:
|
||||
data = json.load(j)
|
||||
|
||||
climate_json = self._load_json()
|
||||
for index, i in enumerate(self.object_ids["nodes"]):
|
||||
|
||||
climate_json[index]["uniq_id"] = i["uid"]
|
||||
discovery_topic = "homeassistant/climate/" + i["uid"] + "/config"
|
||||
|
||||
|
||||
self.client.publish(topic=discovery_topic, payload=json.dumps(climate_json[index]), qos=0, retain=False)
|
||||
|
||||
def _load_json(self):
|
||||
path = Path(__file__).parent
|
||||
json_list = []
|
||||
with open(str(path) + "/climate.json") as j:
|
||||
data = json.load(j)
|
||||
|
||||
for dev in self.object_ids["nodes"]:
|
||||
new_data = data.copy()
|
||||
for i in new_data:
|
||||
if "{name}" in str(new_data.get(i)):
|
||||
new_data[i] = new_data[i].replace("{name}", dev["name"])
|
||||
elif "{uid}" in str(new_data.get(i)):
|
||||
new_data[i] = new_data[i].replace("{uid}", dev["uid"])
|
||||
json_list.append(new_data)
|
||||
return json_list
|
||||
|
||||
# test = MqttManager()
|
||||
|
||||
|
||||
# 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.
|
||||
@@ -23,6 +23,7 @@ class Tevolve:
|
||||
self.token_primary = ""
|
||||
self.token_refresh = ""
|
||||
self.sid = ""
|
||||
self.devices = []
|
||||
|
||||
setmode = ""
|
||||
setTemperature = ""
|
||||
@@ -90,8 +91,6 @@ class Tevolve:
|
||||
|
||||
x = requests.post(url, headers=headers, data=data)
|
||||
|
||||
print(x)
|
||||
|
||||
if x.status_code == 200:
|
||||
Tevolve.token = x.json()["access_token"]
|
||||
else:
|
||||
@@ -111,6 +110,26 @@ class Tevolve:
|
||||
# Tevolve.token_manager()
|
||||
|
||||
|
||||
def get_devices(self):
|
||||
try:
|
||||
endpoint = "/devs/"+self.dev_id+"/mgr/nodes"
|
||||
|
||||
header = {'content-type': 'application/json',
|
||||
"Authorization": "Bearer " + self.token_primary}
|
||||
|
||||
url = str(self.api_url + endpoint)
|
||||
devices_request = requests.get(str(self.api_url + endpoint), headers=header).json()
|
||||
|
||||
self.devices = devices_request
|
||||
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def post_websocket(self):
|
||||
|
||||
# Tevolve.token_manager()
|
||||
@@ -209,30 +228,8 @@ class Tevolve:
|
||||
if response.status_code == 201 or response.status_code == 200:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_mode(heater_id):
|
||||
headers = {
|
||||
'host': 'api-tevolve.termoweb.net',
|
||||
'origin': 'https://tevolve.termoweb.net',
|
||||
'content-type': 'application/json',
|
||||
'accept': 'application/json, text/plain, */*',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) '
|
||||
'Version/15.4 Safari/605.1.15',
|
||||
'authorization': 'Bearer ' + Tevolve.token,
|
||||
'referer': 'https://tevolve.termoweb.net/',
|
||||
|
||||
}
|
||||
url = "https://api-tevolve.termoweb.net/api/v2/devs/082e858131ff012c51/htr/" + heater_id + "/status"
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 201 or response.status_code == 200:
|
||||
mode = response.json()["mode"]
|
||||
if mode == "manual":
|
||||
return "heat"
|
||||
|
||||
|
||||
def get_status(heater_id, self):
|
||||
|
||||
def get_mode(self):
|
||||
headers = {
|
||||
'host': 'api-tevolve.termoweb.net',
|
||||
'origin': 'https://tevolve.termoweb.net',
|
||||
@@ -244,11 +241,38 @@ class Tevolve:
|
||||
'referer': 'https://tevolve.termoweb.net/',
|
||||
|
||||
}
|
||||
url = "https://api-tevolve.termoweb.net/api/v2/devs/082e858131ff012c51/htr/" + heater_id + "/status"
|
||||
url = "https://api-tevolve.termoweb.net/api/v2/devs/082e858131ff012c51/htr/" + self.heater_id + "/status"
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 201 or response.status_code == 200:
|
||||
return response.json()
|
||||
mode = response.json()["mode"]
|
||||
if mode == "manual":
|
||||
return "heat"
|
||||
|
||||
|
||||
def get_status(self):
|
||||
result = []
|
||||
try:
|
||||
for index ,i in enumerate(self.devices["nodes"]):
|
||||
headers = {
|
||||
'host': 'api-tevolve.termoweb.net',
|
||||
'origin': 'https://tevolve.termoweb.net',
|
||||
'content-type': 'application/json',
|
||||
'accept': 'application/json, text/plain, */*',
|
||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) '
|
||||
'Version/15.4 Safari/605.1.15',
|
||||
'authorization': 'Bearer ' + self.token_primary,
|
||||
'referer': 'https://tevolve.termoweb.net/',
|
||||
|
||||
}
|
||||
url = "https://api-tevolve.termoweb.net/api/v2/devs/"+self.dev_id+"/htr/"+str(i["addr"])+"/status"
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 201 or response.status_code == 200:
|
||||
result.append({i["name"]: response.json()})
|
||||
except Exception as e:
|
||||
raise e
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def set_temperature(heater_id, set_temperature):
|
||||
@@ -281,10 +305,21 @@ if __name__ == '__main__':
|
||||
test = Tevolve()
|
||||
test.get_token()
|
||||
test.get_dev()
|
||||
|
||||
test.get_devices()
|
||||
|
||||
|
||||
for device in test.devices["nodes"]:
|
||||
if device["name"] == "Hallway":
|
||||
test.get_status(device["addr"])
|
||||
|
||||
test.get_sid()
|
||||
|
||||
|
||||
test.post_websocket()
|
||||
test.create_websocket()
|
||||
|
||||
|
||||
while 1:
|
||||
if test.websocket_message != "":
|
||||
print(test.websocket_message)
|
||||
|
||||
Reference in New Issue
Block a user