aboutsummaryrefslogtreecommitdiffstats
path: root/mqtt-client
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt-client')
-rw-r--r--mqtt-client/Dockerfile6
m---------mqtt-client/TasmotaCLI0
-rw-r--r--mqtt-client/mqtt-client.py126
-rw-r--r--mqtt-client/requirements.txt2
4 files changed, 124 insertions, 10 deletions
diff --git a/mqtt-client/Dockerfile b/mqtt-client/Dockerfile
index ad59f9b..be304a1 100644
--- a/mqtt-client/Dockerfile
+++ b/mqtt-client/Dockerfile
@@ -1,10 +1,12 @@
-FROM debian:11-slim
+FROM debian:12-slim
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update -y
RUN apt-get install -y python3-pip iputils-ping
COPY . /app
WORKDIR /app
-RUN pip3 install -r requirements.txt
+RUN pip3 install --break-system-packages -r requirements.txt
+RUN pip3 install --break-system-packages docker
+RUN pip3 install --break-system-packages -r TasmotaCLI/requirements.txt
ENTRYPOINT ["python3"]
CMD ["mqtt-client.py"]
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 a441e6d..cd38944 100644
--- a/mqtt-client/mqtt-client.py
+++ b/mqtt-client/mqtt-client.py
@@ -1,25 +1,65 @@
import paho.mqtt.client as paho
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
+import prometheus_client
+import threading
+import requests
+import asyncio
+import time
import json
+import sys
import os
+sys.path.insert(1, os.path.join(os.path.dirname(__file__), "TasmotaCLI"))
+import tasmotaMQTTClient
+import tasmotaHTTPClient
+
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"],
org = os.environ["DOCKER_INFLUXDB_INIT_ORG"]
)
self.influxc.ping()
+ self.tasmota_power_prom = prometheus_client.Gauge(
+ "tasmota_power",
+ "Power metrics as reported by Tasmota-flashed plugs",
+ labelnames = ["plug", "field"]
+ )
+ self.humidity_prom = prometheus_client.Gauge(
+ "humidity",
+ "Humidity as reported by a zigbee device over MQTT",
+ labelnames = ["location"]
+ )
+ self.temperature_prom = prometheus_client.Gauge(
+ "temperature",
+ "Temperature as reported by a zigbee device over MQTT",
+ labelnames = ["location"]
+ )
+ self.doorsensor_prom = prometheus_client.Enum(
+ "door_sensor",
+ "Door sensor state change as reported by zigbee door sensor over MQTT",
+ states = ["opened", "closed"],
+ labelnames = ["location"]
+ )
+ self.door_opened_counter = prometheus_client.Counter(
+ "door_opened",
+ "Door sensor opened as reported by zigbee door sensor over MQTT",
+ labelnames = ["location"]
+ )
+
+ # print(self.send_raw_tasmota_http("192.168.5.6", os.environ["MQTT_PASSWD"], "Power"))
- 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")
@@ -46,13 +86,80 @@ class MQTTClient:
fields = {k: v for k, v in msg_j["ENERGY"].items() if k not in {"TotalStartTime"}}
self.append_influxdb(fields, "tasmota_power", {"plug": location})
+ for k, v in fields.items():
+ self.tasmota_power_prom.labels(plug = location, field = k).set(v)
+
def handle_zigbee(self, msg_j):
+ def toggle_firestick(status_before):
+ print("Starting thread...")
+
+ if status_before == "OFF":
+ print("TV was formerly off, so its being turned on, so we're going to turn the firestick on.")
+ tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaFirestick", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "OFF")
+ print("Waiting...")
+ time.sleep(8)
+ tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaFirestick", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "ON")
+ print("Turned firestick on.")
+ else:
+ print("TV was formerly on, so its being turned off, so we're going to turn the firestick off.")
+ tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaFirestick", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "ON")
+ print("Waiting...")
+ time.sleep(8)
+ tasmotaMQTTClient.MQTTClient(MQTT_HOST, "TasmotaFirestick", os.environ["MQTT_USER"], os.environ["MQTT_PASSWD"], "OFF")
+ print("Turned firestick off.")
+
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 zigbee_id == "0x7327" and friendlyname == "TVButton" and "Power" in fields.keys():
+ if fields["Power"] == 2:
+ print("TV Zigbee button pressed, toggling TasmotaTV Tasmota Plug")
+ status_before = self.get_http_power_status("192.168.5.6", os.environ["MQTT_PASSWD"])
+ self.toggle_plug("TasmotaTV")
+ threading.Thread(target = toggle_firestick, args = (status_before, )).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")
+
+ if "Humidity" in fields.keys():
+ fields["Humidity"] = float(fields["Humidity"])
+ self.humidity_prom.labels(location = friendlyname).set(fields["Humidity"])
+ elif "Temperature" in fields.keys():
+ fields["Temperature"] = float(fields["Temperature"])
+ self.temperature_prom.labels(location = friendlyname).set(fields["Temperature"])
+ elif "ZoneStatus" in fields.keys() and "Contact" in fields.keys():
+ if fields["ZoneStatus"] == 1 and fields["Contact"] == 1:
+ self.doorsensor_prom.labels(location = friendlyname).state("opened")
+ self.door_opened_counter.labels(location = friendlyname).inc()
+ elif fields["ZoneStatus"] == 0 and fields["Contact"] == 0:
+ self.doorsensor_prom.labels(location = friendlyname).state("closed")
+
+ if "Read" not in fields.keys():
+ self.append_influxdb(fields, "zigbee", {"friendlyname": friendlyname, "id": zigbee_id})
+
+ def set_plug(self, friendlyname, payload):
+ t = "cmnd/TasmotaPlug/%s/Power" % friendlyname
+ 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 send_raw_tasmota_http(self, host, password, command):
+ req = requests.get("http://%s/cm" % host, params = {
+ "cmnd": str(command),
+ "user": "admin",
+ "password": password
+ })
+ return req.json()
+
+ def get_http_power_status(self, host, password):
+ return self.send_raw_tasmota_http(host, password, "Power")["POWER"]
def append_influxdb(self, fields, measurement_name, tags):
points = [{"measurement": measurement_name, "tags": tags, "fields": fields}]
@@ -69,10 +176,13 @@ if __name__ == "__main__":
if os.path.exists(env_path):
import dotenv
dotenv.load_dotenv(dotenv_path = env_path)
- INFLUXDB_HOST = "localhost"
- MQTT_HOST = "localhost"
+ INFLUXDB_HOST = "dns.athome"
+ MQTT_HOST = "dns.athome"
+ PROM_HOST = "dns.athome"
else:
INFLUXDB_HOST = "influxdb"
MQTT_HOST = "mqtt"
+ PROM_HOST = "prometheus"
+ prometheus_client.start_http_server(8000)
mqtt_client = MQTTClient()
diff --git a/mqtt-client/requirements.txt b/mqtt-client/requirements.txt
index ac151c7..6566b93 100644
--- a/mqtt-client/requirements.txt
+++ b/mqtt-client/requirements.txt
@@ -1,3 +1,5 @@
paho-mqtt==1.6.1
python-dotenv
influxdb-client
+prometheus-client
+requests