diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index c2891ed1068..8a18c392ca2 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -421,6 +421,12 @@ class MQTT: retain=will_message.retain, ) + def _is_active_subscription(self, topic: str) -> bool: + """Check if a topic has an active subscription.""" + return topic in self._simple_subscriptions or any( + other.topic == topic for other in self._wildcard_subscriptions + ) + async def async_publish( self, topic: str, payload: PublishPayloadType, qos: int, retain: bool ) -> None: @@ -544,11 +550,11 @@ class MQTT: _raise_on_error(result) return mid - if any(other.topic == topic for other in self.subscriptions): - # Other subscriptions on topic remaining - don't unsubscribe. - return - async with self._paho_lock: + if self._is_active_subscription(topic): + # Other subscriptions on topic remaining - don't unsubscribe. + return + mid = await self.hass.async_add_executor_job(_client_unsubscribe, topic) await self._register_mid(mid)