From 0f1fb837b9eadbd5ad9fc1835a2cc62e6ea30df8 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Thu, 29 Jun 2023 10:07:17 +0200 Subject: [PATCH] delete mqtt scripts --- mqtt/.gitignore | 2 - mqtt/fyrtur.py | 147 ------------------------------------ mqtt/heater.py | 170 ------------------------------------------ mqtt/requirements.txt | 1 - mqtt/shell.nix | 20 ----- 5 files changed, 340 deletions(-) delete mode 100644 mqtt/.gitignore delete mode 100644 mqtt/fyrtur.py delete mode 100644 mqtt/heater.py delete mode 100644 mqtt/requirements.txt delete mode 100644 mqtt/shell.nix diff --git a/mqtt/.gitignore b/mqtt/.gitignore deleted file mode 100644 index f425bf4..0000000 --- a/mqtt/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -venv -**/__pycache__ \ No newline at end of file diff --git a/mqtt/fyrtur.py b/mqtt/fyrtur.py deleted file mode 100644 index 604ad1a..0000000 --- a/mqtt/fyrtur.py +++ /dev/null @@ -1,147 +0,0 @@ -import time -from enum import Enum -from typing import Dict - -import paho.mqtt.client as mqtt -import json -import threading - -scene = "up-dark" - - -class Position(Enum): - UP = 1 - DOWN = 2 - HALF = 3 - - -class Fyrtur: - def __init__(self, topic, top, bottom): - self.topic = topic - self.top = top - self.bottom = bottom - self.current_position = 100 - self.wanted_position = 100 - - def update_position(self, payload): - self.current_position = payload["position"] - - def needs_publish(self): - return self.wanted_position != self.current_position - - def topic_and_payload_for_set(self): - payload = {"position": self.wanted_position} - return ("%s/set" % self.topic), json.dumps(payload) - - -class FyrturWatcher: - def __init__(self, fyrturs: Dict[str, Fyrtur]): - self.fyrturs = fyrturs - - def get_topics(self): - return [fyrtur.topic for fyrtur in self.fyrturs.values()] - - def update_position(self, topic, payload): - for fyrtur in self.fyrturs.values(): - if fyrtur.topic == topic: - fyrtur.update_position(payload) - return - - def update(self, name, position: Position): - fyrtur: Fyrtur = self.fyrturs.get(name) - if position == Position.UP: - fyrtur.wanted_position = fyrtur.top - elif position == Position.DOWN: - fyrtur.wanted_position = fyrtur.bottom - elif position == Position.HALF: - fyrtur.wanted_position = round( - (fyrtur.top - fyrtur.bottom) / 2 + fyrtur.bottom - ) - - def publish(self, client): - for fyrtur in self.fyrturs.values(): - if fyrtur.needs_publish(): - topic, payload = fyrtur.topic_and_payload_for_set() - client.publish(topic, payload) - time.sleep(2) - - -watcher = FyrturWatcher( - { - "office1": Fyrtur(topic="zigbee2mqtt/office_fyrtur_1", top=100, bottom=16), - "office2": Fyrtur(topic="zigbee2mqtt/office_fyrtur_2", top=100, bottom=22), - "bedroom": Fyrtur(topic="zigbee2mqtt/bedroom_fyrtur_1", top=100, bottom=16), - } -) - - -# The callback for when the client receives a CONNACK response from the server. -def on_connect(client, _userdata, _flags, rc): - print("Connected with result code " + str(rc)) - - threading.Thread(target=loop_thread, args=(client,), daemon=True).start() - - # Subscribing in on_connect() means that if we lose the connection and - # reconnect then subscriptions will be renewed. - client.subscribe("control/lights/set") - for topic in watcher.get_topics(): - client.subscribe(topic) - - -# The callback for when a PUBLISH message is received from the server. -def on_message(client, _userdata, msg): - global scene - (topic, payload) = parse_message(msg) - 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_position(topic, payload) - - -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 - - -def update_scene(client): - if scene in ["night", "down"]: - watcher.update("office1", Position.DOWN) - watcher.update("office2", Position.DOWN) - watcher.update("bedroom", Position.DOWN) - elif scene in ["default", "up-bright", "up-dark", "outside"]: - watcher.update("office1", Position.UP) - watcher.update("office2", Position.UP) - watcher.update("bedroom", Position.UP) - elif scene in ["half"]: - watcher.update("office1", Position.HALF) - watcher.update("office2", Position.HALF) - watcher.update("bedroom", Position.HALF) - else: - watcher.update("office1", Position.UP) - watcher.update("office2", Position.UP) - watcher.update("bedroom", Position.UP) - - watcher.publish(client) - - -def loop_thread(client): - while True: - watcher.publish(client) - time.sleep(120) - - -if __name__ == "__main__": - 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) - # 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() diff --git a/mqtt/heater.py b/mqtt/heater.py deleted file mode 100644 index bbb21da..0000000 --- a/mqtt/heater.py +++ /dev/null @@ -1,170 +0,0 @@ -import json -import paho.mqtt.client as mqtt -import threading -import time -from typing import Dict - - -class Heater: - def __init__(self, topic): - self.not_initialized_yet = True - self.wanted_temperature = 14 - self.actual_temperature = 14 - self.topic = topic - - def needs_publish(self): - if self.not_initialized_yet: - return True - else: - return self.wanted_temperature != self.actual_temperature - - def update_actual_heating_point(self, payload): - heating_setpoint = int(payload["current_heating_setpoint"]) - if self.not_initialized_yet: - self.not_initialized_yet = False - self.wanted_temperature = heating_setpoint - print( - "%s: update wanted temperature %d" - % (self.topic, self.actual_temperature) - ) - self.actual_temperature = heating_setpoint - print( - "%s: update actual temperature %d" % (self.topic, self.actual_temperature) - ) - - def topic_and_payload_for_query(self): - payload = { - "current_heating_setpoint": "", - "occupied_heating_setpoint": "", - "unoccupied_heating_setpoint": "", - "local_temperature": "", - # "pi_heating_demand": "", - # "system_mode": "", - } - return ("%s/get" % self.topic), json.dumps(payload) - - def topic_and_payload_for_set(self): - payload = { - "system_mode": "auto", - # "current_heating_setpoint": str(self.wanted_temperature), - "occupied_heating_setpoint": str(self.wanted_temperature), - "unoccupied_heating_setpoint": str(self.wanted_temperature), - "eurotronic_host_flags": {"window_open": True}, - } - return ("%s/set" % self.topic), json.dumps(payload) - - -class Watcher: - def __init__(self, heater: Dict[str, Heater]): - self.heater = heater - - def publish(self, client): - for heater in self.heater.values(): - if heater.needs_publish(): - topic, payload = heater.topic_and_payload_for_set() - client.publish(topic, payload) - time.sleep(2) - - def update(self, name, temperature): - heater: Heater = self.heater.get(name) - heater.wanted_temperature = temperature - - def get_topics(self): - return [heater.topic for heater in self.heater.values()] - - def update_actual_heating_point_for_topic(self, topic, payload): - for heater in self.heater.values(): - if heater.topic == topic: - heater.update_actual_heating_point(payload) - return - - def pull_values(self, client): - for heater in self.heater.values(): - topic, payload = heater.topic_and_payload_for_query() - client.publish(topic, payload) - - -scene = "default" - -watcher = Watcher( - { - "office1": Heater(topic="zigbee2mqtt/office_heater_1"), - "office2": Heater(topic="zigbee2mqtt/office_heater_2"), - "bedroom": Heater(topic="zigbee2mqtt/bedroom_heater_1"), - "storage": Heater(topic="zigbee2mqtt/storage_heater_1"), - } -) - - -# The callback for when the client receives a CONNACK response from the server. -def on_connect(client, _userdata, _flags, rc): - print("Connected with result code " + str(rc)) - - threading.Thread(target=loop_thread, args=(client,), daemon=True).start() - - # Subscribing in on_connect() means that if we lose the connection and - # reconnect then subscriptions will be renewed. - client.subscribe("control/lights/set") - for topic in watcher.get_topics(): - client.subscribe(topic) - watcher.pull_values(client) - - -# The callback for when a PUBLISH message is received from the server. -def on_message(client, _userdata, msg): - global scene - (topic, payload) = parse_message(msg) - 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) - - -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 - - -def update_scene(client): - if scene in ["night", "outside"]: - watcher.update("office1", 14) - watcher.update("office2", 14) - watcher.update("bedroom", 14) - watcher.update("storage", 14) - elif scene in ["default", "up-bright", "up-dark", "half", "down"]: - watcher.update("office1", 25) - watcher.update("office2", 25) - watcher.update("bedroom", 18) - watcher.update("storage", 18) - else: - watcher.update("office1", 14) - watcher.update("office2", 14) - watcher.update("bedroom", 14) - watcher.update("storage", 14) - - watcher.publish(client) - - -def loop_thread(client): - while True: - watcher.publish(client) - watcher.pull_values(client) - time.sleep(120) - - -if __name__ == "__main__": - 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) - - # 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() diff --git a/mqtt/requirements.txt b/mqtt/requirements.txt deleted file mode 100644 index d173f65..0000000 --- a/mqtt/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -paho-mqtt \ No newline at end of file diff --git a/mqtt/shell.nix b/mqtt/shell.nix deleted file mode 100644 index fc69085..0000000 --- a/mqtt/shell.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ pkgs ? import { } }: -let - - myPython = pkgs.python3.withPackages - (python-packages: with python-packages; [ paho-mqtt ]); - - startServer = pkgs.writers.writeBashBin "start-server" '' - ${myPython}/bin/python ./heater.py - ''; - - reformat = pkgs.writers.writeBashBin "reformat" '' - ${pkgs.black}/bin/black --exclude venv ${toString ./.} - ''; - -in -pkgs.mkShell { - - buildInputs = with pkgs; [ myPython startServer reformat ]; - -}