Refactored Zigbee backend and remove unused logic for keeping devices up-to-date

This commit is contained in:
Fabio Manganiello 2021-02-06 14:45:50 +01:00
parent 4c5a52417e
commit 314c01ef97
2 changed files with 19 additions and 43 deletions

View file

@ -82,47 +82,29 @@ class ZigbeeMqttBackend(MqttBackend):
:param password: Specify it if the MQTT server requires authentication (default: None) :param password: Specify it if the MQTT server requires authentication (default: None)
""" """
if host:
self.base_topic = base_topic
listeners = [{
'host': host,
'port': port or self._default_mqtt_port,
'tls_cafile': tls_cafile,
'tls_certfile': tls_certfile,
'tls_ciphers': tls_ciphers,
'tls_keyfile': tls_keyfile,
'tls_version': tls_version,
'username': username,
'password': password,
'topics': [
base_topic + '/' + topic
for topic in ['bridge/state', 'bridge/log']
],
}]
else:
plugin = get_plugin('zigbee.mqtt') plugin = get_plugin('zigbee.mqtt')
self.base_topic = plugin.base_topic self.base_topic = base_topic or plugin.base_topic
listeners = [{ listeners = [{
'host': plugin.host, 'host': host or plugin.host,
'port': plugin.port or self._default_mqtt_port, 'port': port or plugin.port or self._default_mqtt_port,
'tls_cafile': plugin.tls_cafile, 'tls_cafile': tls_cafile or plugin.tls_cafile,
'tls_certfile': plugin.tls_certfile, 'tls_certfile': tls_certfile or plugin.tls_certfile,
'tls_ciphers': plugin.tls_ciphers, 'tls_ciphers': tls_ciphers or plugin.tls_ciphers,
'username': plugin.username, 'tls_keyfile': tls_keyfile or plugin.tls_keyfile,
'password': plugin.password, 'tls_version': tls_version or plugin.tls_version,
'username': username or plugin.username ,
'password': password or plugin.password,
'topics': [ 'topics': [
plugin.base_topic + '/' + topic self.base_topic + '/' + topic
for topic in ['bridge/state', 'bridge/log'] for topic in ['bridge/state', 'bridge/log']
], ],
}] }]
super().__init__(subscribe_default_topic=False, listeners=listeners, *args, **kwargs) super().__init__(subscribe_default_topic=False, listeners=listeners, *args, **kwargs)
self._devices = {}
def _process_state_message(self, client, msg): def _process_state_message(self, client, msg):
if msg == 'online': if msg == 'online':
evt = ZigbeeMqttOnlineEvent evt = ZigbeeMqttOnlineEvent
self._refresh_devices(client)
elif msg == 'offline': elif msg == 'offline':
evt = ZigbeeMqttOfflineEvent evt = ZigbeeMqttOfflineEvent
self.logger.warning('zigbee2mqtt service is offline') self.logger.warning('zigbee2mqtt service is offline')
@ -132,9 +114,6 @@ class ZigbeeMqttBackend(MqttBackend):
# noinspection PyProtectedMember # noinspection PyProtectedMember
self.bus.post(evt(host=client._host, port=client._port)) self.bus.post(evt(host=client._host, port=client._port))
def _refresh_devices(self, client):
client.publish(self.base_topic + '/' + 'bridge/config/devices/get')
def _process_log_message(self, client, msg): def _process_log_message(self, client, msg):
msg_type = msg.get('type') msg_type = msg.get('type')
msg = msg.get('message') msg = msg.get('message')
@ -146,13 +125,10 @@ class ZigbeeMqttBackend(MqttBackend):
for dev in (msg or []): for dev in (msg or []):
devices[dev['friendly_name']] = dev devices[dev['friendly_name']] = dev
client.subscribe(self.base_topic + '/' + dev['friendly_name']) client.subscribe(self.base_topic + '/' + dev['friendly_name'])
self._devices = devices
elif msg_type == 'pairing': elif msg_type == 'pairing':
self.bus.post(ZigbeeMqttDevicePairingEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDevicePairingEvent(device=msg, **args))
elif msg_type == 'device_connected': elif msg_type == 'device_connected':
self.bus.post(ZigbeeMqttDeviceConnectedEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDeviceConnectedEvent(device=msg, **args))
self._refresh_devices(client)
elif msg_type in ['device_ban', 'device_banned']: elif msg_type in ['device_ban', 'device_banned']:
self.bus.post(ZigbeeMqttDeviceBannedEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDeviceBannedEvent(device=msg, **args))
elif msg_type in ['device_removed', 'device_force_removed']: elif msg_type in ['device_removed', 'device_force_removed']:
@ -165,7 +141,6 @@ class ZigbeeMqttBackend(MqttBackend):
self.bus.post(ZigbeeMqttDeviceWhitelistedEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDeviceWhitelistedEvent(device=msg, **args))
elif msg_type == 'device_renamed': elif msg_type == 'device_renamed':
self.bus.post(ZigbeeMqttDeviceRenamedEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDeviceRenamedEvent(device=msg, **args))
self._refresh_devices(client)
elif msg_type == 'device_bind': elif msg_type == 'device_bind':
self.bus.post(ZigbeeMqttDeviceBindEvent(device=msg, **args)) self.bus.post(ZigbeeMqttDeviceBindEvent(device=msg, **args))
elif msg_type == 'device_unbind': elif msg_type == 'device_unbind':

View file

@ -844,4 +844,5 @@ class ZigbeeMqttPlugin(MqttPlugin):
self.publish(topic=self._topic('bridge/unbind/' + source), self.publish(topic=self._topic('bridge/unbind/' + source),
msg=target, **self._mqtt_args(**kwargs)) msg=target, **self._mqtt_args(**kwargs))
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: