`base_topic` param in `zigbee.mqtt` renamed to `topic_prefix`.

This is for sake of consistency with other integrations (like
`zwave.mqtt`) that also use the same parameter name for the MQTT topic
prefix.
This commit is contained in:
Fabio Manganiello 2023-09-07 21:32:56 +02:00
parent 8ab7b6bebc
commit ddd8f1afdc
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 20 additions and 11 deletions

View File

@ -226,7 +226,7 @@ class ZigbeeMqttPlugin(
# MQTT settings # MQTT settings
mqtt: mqtt:
# MQTT base topic for zigbee2mqtt MQTT messages # MQTT base topic for zigbee2mqtt MQTT messages
base_topic: zigbee2mqtt topic_prefix: zigbee2mqtt
# MQTT server URL # MQTT server URL
server: 'mqtt://localhost' server: 'mqtt://localhost'
# MQTT server authentication, uncomment if required: # MQTT server authentication, uncomment if required:
@ -294,7 +294,8 @@ class ZigbeeMqttPlugin(
self, self,
host: str, host: str,
port: int = 1883, port: int = 1883,
base_topic: str = 'zigbee2mqtt', topic_prefix: str = 'zigbee2mqtt',
base_topic: Optional[str] = None,
timeout: int = 10, timeout: int = 10,
tls_certfile: Optional[str] = None, tls_certfile: Optional[str] = None,
tls_keyfile: Optional[str] = None, tls_keyfile: Optional[str] = None,
@ -307,8 +308,10 @@ class ZigbeeMqttPlugin(
""" """
:param host: Default MQTT broker where ``zigbee2mqtt`` publishes its messages. :param host: Default MQTT broker where ``zigbee2mqtt`` publishes its messages.
:param port: Broker listen port (default: 1883). :param port: Broker listen port (default: 1883).
:param base_topic: Topic prefix, as specified in :param topic_prefix: Prefix for the published topics, as specified in
``/opt/zigbee2mqtt/data/configuration.yaml`` (default: '``zigbee2mqtt``'). ``/opt/zigbee2mqtt/data/configuration.yaml`` (default: '``zigbee2mqtt``').
:param base_topic: Legacy alias for ``topic_prefix`` (default:
'``zigbee2mqtt``').
:param timeout: If the command expects from a response, then this :param timeout: If the command expects from a response, then this
timeout value will be used (default: 60 seconds). timeout value will be used (default: 60 seconds).
:param tls_cafile: If the connection requires TLS/SSL, specify the :param tls_cafile: If the connection requires TLS/SSL, specify the
@ -326,11 +329,17 @@ class ZigbeeMqttPlugin(
:param password: If the connection requires user authentication, specify :param password: If the connection requires user authentication, specify
the password (default: None) the password (default: None)
""" """
if base_topic:
self.logger.warning(
'base_topic is deprprecated, please use topic_prefix instead'
)
topic_prefix = base_topic
super().__init__( super().__init__(
host=host, host=host,
port=port, port=port,
topics=[ topics=[
f'{base_topic}/{topic}' f'{topic_prefix}/{topic}'
for topic in [ for topic in [
'bridge/state', 'bridge/state',
'bridge/log', 'bridge/log',
@ -351,7 +360,7 @@ class ZigbeeMqttPlugin(
# Append a unique suffix to the client ID to avoid client name clashes # Append a unique suffix to the client ID to avoid client name clashes
# with other MQTT plugins. # with other MQTT plugins.
self.client_id += '-zigbee-mqtt' self.client_id += '-zigbee-mqtt'
self.base_topic = base_topic self.topic_prefix = topic_prefix
self.timeout = timeout self.timeout = timeout
self._info = ZigbeeInfo() self._info = ZigbeeInfo()
self._devices_meta: Dict[str, dict] = {} self._devices_meta: Dict[str, dict] = {}
@ -550,7 +559,7 @@ class ZigbeeMqttPlugin(
) )
client.on_message = msg_callback client.on_message = msg_callback
client.connect() client.connect()
client.subscribe(self.base_topic + '/bridge/#') client.subscribe(self.topic_prefix + '/bridge/#')
client.loop_start() client.loop_start()
for event in info_ready_events.values(): for event in info_ready_events.values():
@ -590,7 +599,7 @@ class ZigbeeMqttPlugin(
Utility method that construct a topic prefixed by the configured base Utility method that construct a topic prefixed by the configured base
topic. topic.
""" """
return f'{self.base_topic}/{topic}' return f'{self.topic_prefix}/{topic}'
@staticmethod @staticmethod
def _parse_response(response: Union[dict, Response]) -> dict: def _parse_response(response: Union[dict, Response]) -> dict:
@ -2211,7 +2220,7 @@ class ZigbeeMqttPlugin(
""" """
def handler(client: MqttClient, _, msg: mqtt.MQTTMessage): def handler(client: MqttClient, _, msg: mqtt.MQTTMessage):
topic = msg.topic[len(self.base_topic) + 1 :] topic = msg.topic[len(self.topic_prefix) + 1 :]
data = msg.payload.decode() data = msg.payload.decode()
if not data: if not data:
return return
@ -2296,7 +2305,7 @@ class ZigbeeMqttPlugin(
devices = {} devices = {}
for dev in text or []: for dev in text or []:
devices[dev['friendly_name']] = dev devices[dev['friendly_name']] = dev
client.subscribe(self.base_topic + '/' + dev['friendly_name']) client.subscribe(self.topic_prefix + '/' + dev['friendly_name'])
elif msg_type == 'pairing': elif msg_type == 'pairing':
self._bus.post(ZigbeeMqttDevicePairingEvent(device=text, **args)) self._bus.post(ZigbeeMqttDevicePairingEvent(device=text, **args))
elif msg_type in ['device_ban', 'device_banned']: elif msg_type in ['device_ban', 'device_banned']:
@ -2349,7 +2358,7 @@ class ZigbeeMqttPlugin(
# Subscribe to updates from all the known devices # Subscribe to updates from all the known devices
event_args = {'host': client.host, 'port': client.port} event_args = {'host': client.host, 'port': client.port}
client.subscribe( client.subscribe(
*[self.base_topic + '/' + device for device in devices_info.keys()] *[self.topic_prefix + '/' + device for device in devices_info.keys()]
) )
for name, device in devices_info.items(): for name, device in devices_info.items():
@ -2365,7 +2374,7 @@ class ZigbeeMqttPlugin(
payload = self._build_device_get_request(exposes) payload = self._build_device_get_request(exposes)
if payload: if payload:
client.publish( client.publish(
self.base_topic + '/' + name + '/get', self.topic_prefix + '/' + name + '/get',
json.dumps(payload), json.dumps(payload),
) )