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