Add availbility topic support (#42)
This commit is contained in:
@@ -5,6 +5,7 @@ mqtt:
|
|||||||
password: password
|
password: password
|
||||||
topic_prefix: hostname
|
topic_prefix: hostname
|
||||||
client_id: bt-mqtt-gateway
|
client_id: bt-mqtt-gateway
|
||||||
|
availability_topic: lwt_topic
|
||||||
|
|
||||||
manager:
|
manager:
|
||||||
topic_subscription:
|
topic_subscription:
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
from logger import _LOGGER
|
from logger import _LOGGER
|
||||||
|
|
||||||
|
LWT_ONLINE = 'online'
|
||||||
|
LWT_OFFLINE = 'offline'
|
||||||
|
|
||||||
class MqttClient:
|
class MqttClient:
|
||||||
|
|
||||||
@@ -13,13 +15,17 @@ class MqttClient:
|
|||||||
if self.username and self.password:
|
if self.username and self.password:
|
||||||
self.mqttc.username_pw_set(self.username, self.password)
|
self.mqttc.username_pw_set(self.username, self.password)
|
||||||
|
|
||||||
|
if self.availability_topic:
|
||||||
|
_LOGGER.debug("Setting LWT to: %s" % self.availability_topic)
|
||||||
|
self.mqttc.will_set(self.availability_topic, payload=LWT_OFFLINE, retain=True)
|
||||||
|
|
||||||
def publish(self, messages):
|
def publish(self, messages):
|
||||||
if not messages:
|
if not messages:
|
||||||
return
|
return
|
||||||
|
|
||||||
for m in messages:
|
for m in messages:
|
||||||
topic = "{}/{}".format(self.topic_prefix, m.topic) if self.topic_prefix else m.topic
|
topic = "{}/{}".format(self.topic_prefix, m.topic) if self.topic_prefix else m.topic
|
||||||
self.mqttc.publish(topic, m.payload)
|
self.mqttc.publish(topic, m.payload, retain=m.retain)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def client_id(self):
|
def client_id(self):
|
||||||
@@ -45,6 +51,10 @@ class MqttClient:
|
|||||||
def topic_prefix(self):
|
def topic_prefix(self):
|
||||||
return self._config['topic_prefix'] if 'topic_prefix' in self._config else None
|
return self._config['topic_prefix'] if 'topic_prefix' in self._config else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def availability_topic(self):
|
||||||
|
return self._config['availability_topic'] if 'availability_topic' in self._config else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mqttc(self):
|
def mqttc(self):
|
||||||
return self._mqttc
|
return self._mqttc
|
||||||
@@ -60,11 +70,18 @@ class MqttClient:
|
|||||||
|
|
||||||
self.mqttc.loop_start()
|
self.mqttc.loop_start()
|
||||||
|
|
||||||
|
if self.availability_topic:
|
||||||
|
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_ONLINE, retain=True)])
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self.availability_topic:
|
||||||
|
self.publish([MqttMessage(topic=self.availability_topic, payload=LWT_OFFLINE, retain=True)])
|
||||||
|
|
||||||
class MqttMessage:
|
class MqttMessage:
|
||||||
def __init__(self, topic=None, payload=None):
|
def __init__(self, topic=None, payload=None, retain=False):
|
||||||
self._topic = topic
|
self._topic = topic
|
||||||
self._payload = payload
|
self._payload = payload
|
||||||
|
self._retain = retain
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def topic(self):
|
def topic(self):
|
||||||
@@ -78,6 +95,10 @@ class MqttMessage:
|
|||||||
def payload(self):
|
def payload(self):
|
||||||
return self._payload
|
return self._payload
|
||||||
|
|
||||||
|
@property
|
||||||
|
def retain(self):
|
||||||
|
return self._retain
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user