Introduce mqttMessage object

This commit is contained in:
zewelor
2017-11-23 22:16:29 +01:00
parent 7b8e1dad12
commit 939d1e66c6
4 changed files with 36 additions and 19 deletions
+27 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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