support for xiaomi aqara thermometer and few other fixes (#18)
* import pip._internal.main to work with pip version > 10 * make auth optional and add support for an optional port * add worker for xiaomi aqara thermometer * add support for optional global publishing topic_prefix * Update config.yaml.example
This commit is contained in:
@@ -6,34 +6,53 @@ class MqttClient:
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
self._mqttc = mqtt.Client(client_id='bt-mqtt-gateway', clean_session=False)
|
||||
|
||||
if self.username is not None and self.password is not None:
|
||||
self._auth = {'username': self.username, 'password': self.password}
|
||||
else:
|
||||
self._auth = None
|
||||
|
||||
def publish(self, messages):
|
||||
if messages is None:
|
||||
if messages is None or len(messages) == 0:
|
||||
return
|
||||
|
||||
|
||||
if self.topic_prefix is not None:
|
||||
for m in messages:
|
||||
m.topic = "{}/{}".format(self.topic_prefix, m.topic)
|
||||
|
||||
publish.multiple(list(map(lambda m: m.as_dict, messages)),
|
||||
hostname=self.hostname,
|
||||
auth={'username': self.username, 'password': self.password})
|
||||
port=self.port,
|
||||
auth=self._auth)
|
||||
|
||||
@property
|
||||
def hostname(self):
|
||||
return self._config['host']
|
||||
|
||||
@property
|
||||
def port(self):
|
||||
return self._config['port'] if 'port' in self._config else 1883
|
||||
|
||||
@property
|
||||
def username(self):
|
||||
return self._config['username']
|
||||
return self._config['username'] if 'username' in self._config else None
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
return self._config['password']
|
||||
return self._config['password'] if 'password' in self._config else None
|
||||
|
||||
@property
|
||||
def topic_prefix(self):
|
||||
return self._config['topic_prefix'] if 'topic_prefix' in self._config else None
|
||||
|
||||
@property
|
||||
def mqttc(self):
|
||||
return self._mqttc
|
||||
|
||||
def callbacks_subscription(self, callbacks):
|
||||
self.mqttc.username_pw_set(self.username, self.password)
|
||||
self.mqttc.connect(self.hostname)
|
||||
if self.username is not None and self.password is not None:
|
||||
self.mqttc.username_pw_set(self.username, self.password)
|
||||
self.mqttc.connect(self.hostname, port=self.port)
|
||||
|
||||
for topic, callback in callbacks:
|
||||
self.mqttc.message_callback_add(topic, callback)
|
||||
|
||||
Reference in New Issue
Block a user