summaryrefslogtreecommitdiff
path: root/src/home/mqtt/module/temphum.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/home/mqtt/module/temphum.py')
-rw-r--r--src/home/mqtt/module/temphum.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/home/mqtt/module/temphum.py b/src/home/mqtt/module/temphum.py
index 5501f91..83ae34d 100644
--- a/src/home/mqtt/module/temphum.py
+++ b/src/home/mqtt/module/temphum.py
@@ -9,6 +9,7 @@ from ...temphum import BaseSensor
two_digits_precision = lambda x: round(x, 2)
MODULE_NAME = 'MqttTempHumModule'
+DATA_TOPIC = 'temphum/data'
class MqttTemphumDataPayload(MqttPayload):
@@ -45,22 +46,38 @@ class MqttTemphumDataPayload(MqttPayload):
class MqttTempHumModule(MqttModule):
- def __init__(self, sensor: Optional[BaseSensor] = None, *args, **kwargs):
+ def __init__(self,
+ sensor: Optional[BaseSensor] = None,
+ *args, **kwargs):
+ if sensor is not None:
+ kwargs['tick_interval'] = 10
super().__init__(*args, **kwargs)
self._sensor = sensor
def on_connect(self, mqtt: MqttNode):
super().on_connect(mqtt)
- mqtt.subscribe_module('temphum/data', self)
+ mqtt.subscribe_module(DATA_TOPIC, self)
def tick(self):
- pass
+ if not self._sensor:
+ return
+
+ error = 0
+ temp = 0
+ rh = 0
+ try:
+ temp = self._sensor.temperature()
+ rh = self._sensor.humidity()
+ except:
+ error = 1
+ pld = MqttTemphumDataPayload(temp=temp, rh=rh, error=error)
+ self._mqtt_node_ref.publish(DATA_TOPIC, pld.pack())
def handle_payload(self,
mqtt: MqttNode,
topic: str,
payload: bytes) -> Optional[MqttPayload]:
- if topic == 'temphum/data':
+ if topic == DATA_TOPIC:
message = MqttTemphumDataPayload.unpack(payload)
self._logger.debug(message)
return message