summaryrefslogtreecommitdiff
path: root/src/home/mqtt/module
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-06-07 02:34:50 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-06-07 02:34:50 +0300
commitc44a3669100f72d108404a8fdccd18f55018c18b (patch)
tree2fe704fd702d503991b9213cdbc87d7d9fd4dcd5 /src/home/mqtt/module
parent5de1896f5be183d600361d70218c6d579f3a5899 (diff)
wip
Diffstat (limited to 'src/home/mqtt/module')
-rw-r--r--src/home/mqtt/module/diagnostics.py7
-rw-r--r--src/home/mqtt/module/ota.py15
-rw-r--r--src/home/mqtt/module/relay.py8
-rw-r--r--src/home/mqtt/module/temphum.py13
4 files changed, 26 insertions, 17 deletions
diff --git a/src/home/mqtt/module/diagnostics.py b/src/home/mqtt/module/diagnostics.py
index c31cce2..fa6cc8e 100644
--- a/src/home/mqtt/module/diagnostics.py
+++ b/src/home/mqtt/module/diagnostics.py
@@ -48,14 +48,17 @@ class DiagnosticsPayload(MqttPayload):
class MqttDiagnosticsModule(MqttModule):
- def init(self, mqtt: MqttNode):
+ def on_connect(self, mqtt: MqttNode):
+ super().on_connect(mqtt)
for topic in ('diag', 'd1ag', 'stat', 'stat1'):
mqtt.subscribe_module(topic, self)
def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]:
+ message = None
if topic in ('stat', 'diag'):
message = DiagnosticsPayload.unpack(payload)
elif topic in ('stat1', 'd1ag'):
message = InitialDiagnosticsPayload.unpack(payload)
- self._logger.debug(message)
+ if message:
+ self._logger.debug(message)
return message
diff --git a/src/home/mqtt/module/ota.py b/src/home/mqtt/module/ota.py
index 5a1a309..e71cccc 100644
--- a/src/home/mqtt/module/ota.py
+++ b/src/home/mqtt/module/ota.py
@@ -42,18 +42,15 @@ class OtaPayload(MqttPayload):
class MqttOtaModule(MqttModule):
_ota_request: Optional[tuple[str, str, int]]
- _mqtt_ref: Optional[MqttNode]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ota_request = None
- self._mqtt_ref = None
- def init(self, mqtt: MqttNode):
+ def on_connect(self, mqtt: MqttNode):
+ super().on_connect(mqtt)
mqtt.subscribe_module("otares", self)
- self._mqtt_ref = mqtt
-
if self._ota_request is not None:
secret, filename, qos = self._ota_request
self._ota_request = None
@@ -67,9 +64,9 @@ class MqttOtaModule(MqttModule):
def do_push_ota(self, secret: str, filename: str, qos: int):
payload = OtaPayload(secret=secret, filename=filename)
- self._mqtt_ref.publish('ota',
- payload=payload.pack(),
- qos=qos)
+ self._mqtt_node_ref.publish('ota',
+ payload=payload.pack(),
+ qos=qos)
def push_ota(self,
secret: str,
@@ -78,4 +75,4 @@ class MqttOtaModule(MqttModule):
if not self._initialized:
self._ota_request = (secret, filename, qos)
else:
- self.do_push_ota(secret, filename, qos) \ No newline at end of file
+ self.do_push_ota(secret, filename, qos)
diff --git a/src/home/mqtt/module/relay.py b/src/home/mqtt/module/relay.py
index bf22bfe..ae88ddb 100644
--- a/src/home/mqtt/module/relay.py
+++ b/src/home/mqtt/module/relay.py
@@ -58,16 +58,16 @@ class MqttRelayState:
class MqttRelayModule(MqttModule):
- def init(self, mqtt: MqttNode):
+ def on_connect(self, mqtt: MqttNode):
+ super().on_connect(mqtt)
mqtt.subscribe_module('relay/switch', self)
mqtt.subscribe_module('relay/status', self)
- @staticmethod
- def switchpower(mqtt: MqttNode,
+ def switchpower(self,
enable: bool,
secret: str):
payload = MqttPowerSwitchPayload(secret=secret, state=enable)
- mqtt.publish('relay/switch', payload=payload.pack())
+ self._mqtt_node_ref.publish('relay/switch', payload=payload.pack())
def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]:
message = None
diff --git a/src/home/mqtt/module/temphum.py b/src/home/mqtt/module/temphum.py
index 0e43f1b..9cdfedb 100644
--- a/src/home/mqtt/module/temphum.py
+++ b/src/home/mqtt/module/temphum.py
@@ -4,6 +4,7 @@ from .._module import MqttModule
from .._payload import MqttPayload
from ...util import HashableEnum
from typing import Optional
+from ...temphum import BaseSensor
two_digits_precision = lambda x: round(x, 2)
@@ -44,9 +45,17 @@ class MqttTempHumNodes(HashableEnum):
class MqttTempHumModule(MqttModule):
- def init(self, mqtt: MqttNode):
+ def __init__(self, sensor: Optional[BaseSensor] = None, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._sensor = sensor
+
+ def on_connect(self, mqtt: MqttNode):
+ super().on_connect(mqtt)
mqtt.subscribe_module('temphum/data', self)
+ def tick(self):
+ pass
+
def handle_payload(self,
mqtt: MqttNode,
topic: str,
@@ -54,4 +63,4 @@ class MqttTempHumModule(MqttModule):
if topic == 'temphum/data':
message = MqttTemphumDataPayload.unpack(payload)
self._logger.debug(message)
- return message \ No newline at end of file
+ return message