diff options
| author | jwansek <eddie.atten.ea29@gmail.com> | 2025-05-20 19:51:16 +0100 | 
|---|---|---|
| committer | jwansek <eddie.atten.ea29@gmail.com> | 2025-05-20 19:51:16 +0100 | 
| commit | 11e0dc17cc63f87ecd73c0cf7afffbe171a8bc40 (patch) | |
| tree | 3dc4cb0a0c08901f93e8a96b5714f8d3c1339df3 | |
| parent | afddb7f93903a6b1910833898b2b6a105c153906 (diff) | |
| download | power.eda.gay-11e0dc17cc63f87ecd73c0cf7afffbe171a8bc40.tar.gz power.eda.gay-11e0dc17cc63f87ecd73c0cf7afffbe171a8bc40.zip | |
Added toggling other plug in another thread
| -rw-r--r-- | .gitmodules | 3 | ||||
| m--------- | mqtt-client/TasmotaCLI | 0 | ||||
| -rw-r--r-- | mqtt-client/mqtt-client.py | 38 | 
3 files changed, 29 insertions, 12 deletions
| diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6933443 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "mqtt-client/TasmotaCLI"] +	path = mqtt-client/TasmotaCLI +	url = git@github.com:jwansek/TasmotaCLI.git diff --git a/mqtt-client/TasmotaCLI b/mqtt-client/TasmotaCLI new file mode 160000 +Subproject dd7790dab8d3fbea8f2b58eb4d5aaffc36b3cb0 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) | 
