diff options
Diffstat (limited to 'src/home/mqtt/module')
-rw-r--r-- | src/home/mqtt/module/diagnostics.py | 6 | ||||
-rw-r--r-- | src/home/mqtt/module/ota.py | 4 | ||||
-rw-r--r-- | src/home/mqtt/module/relay.py | 21 | ||||
-rw-r--r-- | src/home/mqtt/module/temphum.py | 10 |
4 files changed, 30 insertions, 11 deletions
diff --git a/src/home/mqtt/module/diagnostics.py b/src/home/mqtt/module/diagnostics.py index 8b5ea16..c31cce2 100644 --- a/src/home/mqtt/module/diagnostics.py +++ b/src/home/mqtt/module/diagnostics.py @@ -1,5 +1,6 @@ from ..mqtt import MqttPayload, MqttPayloadCustomField from .._node import MqttNode, MqttModule +from typing import Optional MODULE_NAME = 'MqttDiagnosticsModule' @@ -51,9 +52,10 @@ class MqttDiagnosticsModule(MqttModule): for topic in ('diag', 'd1ag', 'stat', 'stat1'): mqtt.subscribe_module(topic, self) - def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes): + def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]: if topic in ('stat', 'diag'): message = DiagnosticsPayload.unpack(payload) elif topic in ('stat1', 'd1ag'): message = InitialDiagnosticsPayload.unpack(payload) - self._logger.debug(message)
\ No newline at end of file + self._logger.debug(message) + return message diff --git a/src/home/mqtt/module/ota.py b/src/home/mqtt/module/ota.py index 1d472d1..86d6839 100644 --- a/src/home/mqtt/module/ota.py +++ b/src/home/mqtt/module/ota.py @@ -1,5 +1,6 @@ import hashlib +from typing import Optional from ..mqtt import MqttPayload from .._node import MqttModule, MqttNode @@ -43,10 +44,11 @@ class MqttOtaModule(MqttModule): def init(self, mqtt: MqttNode): mqtt.subscribe_module("otares", self) - def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes): + def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]: if topic == 'otares': message = OtaResultPayload.unpack(payload) self._logger.debug(message) + return message # def push_ota(self, # node_id, diff --git a/src/home/mqtt/module/relay.py b/src/home/mqtt/module/relay.py index 16877f6..721ceac 100644 --- a/src/home/mqtt/module/relay.py +++ b/src/home/mqtt/module/relay.py @@ -1,7 +1,6 @@ -import paho.mqtt.client as mqtt -import re import datetime +from typing import Optional from .. import MqttModule, MqttPayload, MqttNode MODULE_NAME = 'MqttRelayModule' @@ -22,6 +21,18 @@ class MqttPowerSwitchPayload(MqttPayload): state: bool +class MqttPowerStatusPayload(MqttPayload): + FORMAT = '=B' + PACKER = { + 'opened': lambda n: int(n), + } + UNPACKER = { + 'opened': lambda n: bool(n), + } + + opened: bool + + class MqttRelayState: enabled: bool update_time: datetime.datetime @@ -57,9 +68,11 @@ class MqttRelayModule(MqttModule): payload = MqttPowerSwitchPayload(secret=secret, state=enable) mqtt.publish('relay/switch', payload=payload.pack()) - def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes): + def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]: if topic != 'relay/switch': return message = MqttPowerSwitchPayload.unpack(payload) - self._logger.debug(message)
\ No newline at end of file + self._logger.debug(message) + + return message
\ No newline at end of file diff --git a/src/home/mqtt/module/temphum.py b/src/home/mqtt/module/temphum.py index e1c4567..0e43f1b 100644 --- a/src/home/mqtt/module/temphum.py +++ b/src/home/mqtt/module/temphum.py @@ -3,13 +3,14 @@ from .._node import MqttNode from .._module import MqttModule from .._payload import MqttPayload from ...util import HashableEnum +from typing import Optional two_digits_precision = lambda x: round(x, 2) MODULE_NAME = 'MqttTempHumModule' -class TempHumDataPayload(MqttPayload): +class MqttTemphumDataPayload(MqttPayload): FORMAT = '=ddb' UNPACKER = { 'temp': two_digits_precision, @@ -49,7 +50,8 @@ class MqttTempHumModule(MqttModule): def handle_payload(self, mqtt: MqttNode, topic: str, - payload: bytes): + payload: bytes) -> Optional[MqttPayload]: if topic == 'temphum/data': - message = TempHumDataPayload.unpack(payload) - self._logger.debug(message)
\ No newline at end of file + message = MqttTemphumDataPayload.unpack(payload) + self._logger.debug(message) + return message
\ No newline at end of file |