Skip to content

Commit 137db19

Browse files
committed
mqtt: add resource status flag representing current power state
Implements a status flag which gets updated without the need to acquire the resource and gives a user or script the option to just see the power state.
1 parent e33e90a commit 137db19

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

labgrid/remote/exporter.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import asyncio
6+
import json
67
import logging
78
import sys
89
import os
@@ -920,6 +921,62 @@ def _get_params(self):
920921
exports["YKUSHPowerPort"] = YKUSHPowerPortExport
921922

922923

924+
@attr.s(eq=False)
925+
class TasmotaPowerPortExport(ResourceExport):
926+
"""ResourceExport for TasmotaPowerPort devices"""
927+
928+
def __attrs_post_init__(self):
929+
super().__attrs_post_init__()
930+
self.data["cls"] = self.cls
931+
from ..resource import mqtt
932+
933+
local_cls = getattr(mqtt, self.cls)
934+
self.local = local_cls(target=None, name=None, **self.local_params)
935+
936+
self.status = None
937+
938+
import paho.mqtt.client as mqtt
939+
self._client = None
940+
self._client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
941+
self._client.connect(self.local.host)
942+
self._client.on_message = self._on_message
943+
self._client.on_connect = self._on_connect
944+
self._client.loop_start()
945+
946+
def _on_connect(self, client, userdata, flags, reason_code, properties):
947+
client.subscribe(self.local.status_topic)
948+
if self.local.tele_topic:
949+
client.subscribe(self.local.tele_topic)
950+
client.publish(self.local.power_topic, "")
951+
952+
def _on_message(self, client, userdata, msg):
953+
if msg.topic == self.local.tele_topic:
954+
data = json.loads(msg.payload.decode("utf-8"))
955+
power = self.local.power_topic.split('/')[-1]
956+
if power in data:
957+
self.status = data[power]
958+
else:
959+
if msg.payload == b'ON':
960+
self.status = "ON"
961+
elif msg.payload == b'OFF':
962+
self.status = "OFF"
963+
else:
964+
raise ValueError("unexpected payload in status topic "
965+
+ {msg.payload})
966+
967+
def _get_params(self):
968+
"""Helper function to return parameters"""
969+
return {
970+
**self.local_params,
971+
"extra": {
972+
"status": self.status,
973+
}
974+
}
975+
976+
977+
exports["TasmotaPowerPort"] = TasmotaPowerPortExport
978+
979+
923980
class ExporterSession(ApplicationSession):
924981
def onConnect(self):
925982
"""Set up internal datastructures on successful connection:

labgrid/resource/mqtt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ class TasmotaPowerPort(MQTTResource):
6868
validator=attr.validators.instance_of(str))
6969
status_topic = attr.ib(default=None,
7070
validator=attr.validators.instance_of(str))
71+
tele_topic = attr.ib(default=None,
72+
validator=attr.validators.optional(attr.validators.instance_of(str)))

0 commit comments

Comments
 (0)