Introduce mqttMessage object
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import paho.mqtt.publish as publish
|
||||
|
||||
|
||||
class MqttClient:
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
|
||||
def publish(self, messages):
|
||||
publish.multiple(messages,
|
||||
publish.multiple(list(map(lambda m: m.as_dict, messages)),
|
||||
hostname=self.hostname,
|
||||
auth={'username': self.username, 'password': self.password})
|
||||
|
||||
@@ -20,3 +21,28 @@ class MqttClient:
|
||||
@property
|
||||
def password(self):
|
||||
return self._config['password']
|
||||
|
||||
|
||||
class MqttMessage:
|
||||
def __init__(self, topic=None, payload=None):
|
||||
self._topic = topic
|
||||
self._payload = payload
|
||||
|
||||
@property
|
||||
def topic(self):
|
||||
return self._topic
|
||||
|
||||
@topic.setter
|
||||
def topic(self, new_topic):
|
||||
self._topic = new_topic
|
||||
|
||||
@property
|
||||
def payload(self):
|
||||
return self._payload
|
||||
|
||||
@property
|
||||
def as_dict(self):
|
||||
return {
|
||||
'topic': self.topic,
|
||||
'payload': self.payload
|
||||
}
|
||||
|
||||
+3
-4
@@ -1,5 +1,7 @@
|
||||
import time
|
||||
|
||||
from mqtt import MqttMessage
|
||||
|
||||
REQUIREMENTS = ['bluepy', 'interruptingcow']
|
||||
|
||||
# Bluepy might need special settings
|
||||
@@ -10,10 +12,7 @@ class MiscaleWorker():
|
||||
self._mac = mac
|
||||
|
||||
def status_update(self):
|
||||
return [{
|
||||
'topic': 'weight/kg',
|
||||
'payload': self._get_weight(),
|
||||
}]
|
||||
return [MqttMessage(topic='weight/kg', payload= self._get_weight())]
|
||||
|
||||
def _get_weight(self):
|
||||
from bluepy import btle
|
||||
|
||||
+5
-13
@@ -1,3 +1,5 @@
|
||||
from mqtt import MqttMessage
|
||||
|
||||
REQUIREMENTS = ['python-eq3bt']
|
||||
|
||||
monitoredAttrs = ["low_battery", "valve_state", "target_temperature"]
|
||||
@@ -24,20 +26,10 @@ class ThermostatWorker:
|
||||
thermostat.update()
|
||||
|
||||
for attr in monitoredAttrs:
|
||||
ret.append({
|
||||
'topic': self.format_topic(name, attr),
|
||||
'payload': getattr(thermostat, attr),
|
||||
})
|
||||
ret.append(MqttMessage(topic=self.format_topic(name, attr), payload=getattr(thermostat, attr)))
|
||||
|
||||
ret.append({
|
||||
'topic': self.format_topic(name, 'mode'),
|
||||
'payload': haModesMapping[thermostat.mode],
|
||||
})
|
||||
|
||||
ret.append({
|
||||
'topic': self.format_topic(name, 'away'),
|
||||
'payload': 'ON' if thermostat.mode == Mode.Away else 'OFF'
|
||||
})
|
||||
ret.append(MqttMessage(topic=self.format_topic(name, 'mode'), payload=haModesMapping[thermostat.mode]))
|
||||
ret.append(MqttMessage(topic=self.format_topic(name, 'away'), payload='ON' if thermostat.mode == Mode.Away else 'OFF'))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ class WorkersManager:
|
||||
def update(self, worker_name):
|
||||
messages = self.workers[worker_name].status_update()
|
||||
for message in messages:
|
||||
message['topic'] = '/'.join([self._config[worker_name]['topic_prefix'], message['topic']])
|
||||
message.topic = '/'.join([self._config[worker_name]['topic_prefix'], message.topic])
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
Reference in New Issue
Block a user