From 11e0dc17cc63f87ecd73c0cf7afffbe171a8bc40 Mon Sep 17 00:00:00 2001 From: jwansek Date: Tue, 20 May 2025 19:51:16 +0100 Subject: Added toggling other plug in another thread --- mqtt-client/mqtt-client.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'mqtt-client/mqtt-client.py') diff --git a/mqtt-client/mqtt-client.py b/mqtt-client/mqtt-client.py index dcb8614..18dcd82 100644 --- a/mqtt-client/mqtt-client.py +++ b/mqtt-client/mqtt-client.py @@ -1,12 +1,17 @@ import paho.mqtt.client as paho from influxdb_client import InfluxDBClient, Point, WritePrecision from influxdb_client.client.write_api import SYNCHRONOUS +import threading import time import json +import sys import os +sys.path.insert(1, os.path.join(os.path.dirname(__file__), "TasmotaCLI")) +import tasmotaMQTTClient + class MQTTClient: - def __init__(self): + def __init__(self, mqtt_client_name = "reg.reaweb.uk/mqtt-client", loop_forever = True): self.influxc = InfluxDBClient( url = "http://%s:8086" % INFLUXDB_HOST, token = os.environ["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"], @@ -14,13 +19,15 @@ class MQTTClient: ) self.influxc.ping() - self.mqttc = paho.Client('power-listener', clean_session = True) - self.mqttc.on_connect = self._on_connect_cb - self.mqttc.on_message = self._on_message_cb + self.mqttc = paho.Client(mqtt_client_name, clean_session = True) + if loop_forever: + self.mqttc.on_connect = self._on_connect_cb + self.mqttc.on_message = self._on_message_cb self.mqttc.username_pw_set(os.environ["MQTT_USER"], password = os.environ["MQTT_PASSWD"]) self.mqttc.connect(MQTT_HOST, 1883, 60) - self.mqttc.loop_forever() + if loop_forever: + self.mqttc.loop_forever() def _on_connect_cb(self, mqtt, userdata, flags, rc): #print("Connected to broker") @@ -48,33 +55,40 @@ class MQTTClient: self.append_influxdb(fields, "tasmota_power", {"plug": location}) def handle_zigbee(self, msg_j): + def toggle_geoffery(): + print("Starting thread...") + tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaGeoffery", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "OFF") + print("Waiting...") + time.sleep(8) + tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaGeoffery", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "ON") + zigbee_id = list(msg_j["ZbReceived"].keys())[0] fields = msg_j["ZbReceived"][zigbee_id] friendlyname = fields.pop("Name") del fields["Device"] print("Zigbee device '%s' reported: %s" % (friendlyname, str(fields))) - self.append_influxdb(fields, "zigbee", {"friendlyname": friendlyname, "id": zigbee_id}) + if "Read" not in fields.keys(): + self.append_influxdb(fields, "zigbee", {"friendlyname": friendlyname, "id": zigbee_id}) if zigbee_id == "0x7327" and friendlyname == "TVButton" and "Power" in fields.keys(): if fields["Power"] == 2: print("TV Zigbee button pressed, toggling TasmotaTV Tasmota Plug") self.toggle_plug("TasmotaTV") - time.sleep(8) - self.toggle_plug("TasmotaGeoffery") - time.sleep(0.5) - self.toggle_plug("TasmotaGeoffery") + threading.Thread(target = toggle_geoffery, args = ()).start() if zigbee_id == "0x74B3" and friendlyname == "HarveyButton" and "Power" in fields.keys(): if fields["Power"] == 2: print("Harvey's button pressed, toggling TasmotaHarveyPC Plug") self.toggle_plug("TasmotaHarveyPC") - def toggle_plug(self, friendlyname): + def set_plug(self, friendlyname, payload): t = "cmnd/TasmotaPlug/%s/Power" % friendlyname - payload = "TOGGLE" self.mqttc.publish(t, payload = payload) print("Send payload '%s' to %s" % (payload, t)) + def toggle_plug(self, friendlyname): + self.set_plug(friendlyname, "TOGGLE") + def append_influxdb(self, fields, measurement_name, tags): points = [{"measurement": measurement_name, "tags": tags, "fields": fields}] write_api = self.influxc.write_api(write_options = SYNCHRONOUS) -- cgit v1.2.3