Speed up removing MQTT subscriptions (#118088)

This commit is contained in:
J. Nick Koston 2024-05-24 22:28:14 -10:00 committed by GitHub
parent ffcc9100a6
commit ad638dbcc5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View File

@ -429,10 +429,10 @@ class MQTT:
self.config_entry = config_entry
self.conf = conf
self._simple_subscriptions: defaultdict[str, list[Subscription]] = defaultdict(
list
self._simple_subscriptions: defaultdict[str, set[Subscription]] = defaultdict(
set
)
self._wildcard_subscriptions: list[Subscription] = []
self._wildcard_subscriptions: set[Subscription] = set()
# _retained_topics prevents a Subscription from receiving a
# retained message more than once per topic. This prevents flooding
# already active subscribers when new subscribers subscribe to a topic
@ -789,9 +789,9 @@ class MQTT:
The caller is responsible clearing the cache of _matching_subscriptions.
"""
if subscription.is_simple_match:
self._simple_subscriptions[subscription.topic].append(subscription)
self._simple_subscriptions[subscription.topic].add(subscription)
else:
self._wildcard_subscriptions.append(subscription)
self._wildcard_subscriptions.add(subscription)
@callback
def _async_untrack_subscription(self, subscription: Subscription) -> None:

View File

@ -529,16 +529,16 @@ async def test_non_unique_triggers(
async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press")
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[0].data["some"] == "press1"
assert calls[1].data["some"] == "press2"
all_calls = {calls[0].data["some"], calls[1].data["some"]}
assert all_calls == {"press1", "press2"}
# Trigger second config references to same trigger
# and triggers both attached instances.
async_fire_mqtt_message(hass, "foobar/triggers/button2", "long_press")
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[0].data["some"] == "press1"
assert calls[1].data["some"] == "press2"
all_calls = {calls[0].data["some"], calls[1].data["some"]}
assert all_calls == {"press1", "press2"}
# Removing the first trigger will clean up
calls.clear()