Support for commands handling

This commit is contained in:
zewelor
2017-11-26 21:57:12 +01:00
parent 09d1332b92
commit 6a1c7b0181
8 changed files with 160 additions and 70 deletions
+12 -2
View File
@@ -1,12 +1,11 @@
from logger import _LOGGER
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
class MqttClient:
def __init__(self, config):
self._config = config
def publish(self, messages):
_LOGGER.debug(messages)
publish.multiple(list(map(lambda m: m.as_dict, messages)),
hostname=self.hostname,
auth={'username': self.username, 'password': self.password})
@@ -23,6 +22,17 @@ class MqttClient:
def password(self):
return self._config['password']
def callbacks_subscription(self, callbacks):
mqttc = mqtt.Client()
mqttc.username_pw_set(self.username, self.password)
mqttc.connect(self.hostname)
for topic, callback in callbacks:
mqttc.message_callback_add(topic, callback)
mqttc.subscribe(topic)
mqttc.loop_start()
class MqttMessage:
def __init__(self, topic=None, payload=None):