1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Debounce and group MQTT subscriptions (#88862)

* Debounce and group mqtt subscriptions

* Cleanup

* Do not cooldown on resubscribe

* Remove lock from task

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* ruff

* Longer initial cool down. Manages unsubscribes

* Own lock for access to self._pending_subscriptions

* adjust

* Subscribe to highest QoS when sharing subscription

* do not block _pending_subscriptions_lock with io

* Test the highest qos is subscribed at

* Cleanup max qos

* Follow up comments part 1

* Make docstr more generic

* Make max qos update thread safe

* Add lock on clearing _max_qos when resubscribing

* Wait for linger task

* User copy

* Check for key before cleaning up

* Fix lingering task

* Do not use a lock

* do not await _async_queue_subscriptions

* Replace copy with assignment

* Update max qos before returning

* Do not iterate if max_qos == 0

* Do not ieterate subs if max qos == 0

* Set initial cooldown correctly

* Ensure discovery cooldown ends after subscribing

* plan last subscribe with debouncer timeout

* cooldown if self._pending_subscriptions is set

* Revert format changes

* Remove stale assingnment self._last_subscribe

* Remove not used property

* Also check while for pending subscriptions

* revert first added sleep()

* Optimize

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Jan Bouwhuis
2023-03-14 11:13:55 +01:00
committed by GitHub
parent 03b204f445
commit ec1b8b616f
3 changed files with 361 additions and 38 deletions

View File

@@ -1374,6 +1374,8 @@ async def test_complex_discovery_topic_prefix(
@patch("homeassistant.components.mqtt.PLATFORMS", [])
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
async def test_mqtt_integration_discovery_subscribe_unsubscribe(
hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
@@ -1392,6 +1394,7 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
):
await async_start(hass, "homeassistant", entry)
await hass.async_block_till_done()
await hass.async_block_till_done()
mqtt_client_mock.subscribe.assert_any_call("comp/discovery/#", 0)
assert not mqtt_client_mock.unsubscribe.called
@@ -1418,6 +1421,8 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
@patch("homeassistant.components.mqtt.PLATFORMS", [])
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
async def test_mqtt_discovery_unsubscribe_once(
hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
@@ -1436,6 +1441,7 @@ async def test_mqtt_discovery_unsubscribe_once(
):
await async_start(hass, "homeassistant", entry)
await hass.async_block_till_done()
await hass.async_block_till_done()
mqtt_client_mock.subscribe.assert_any_call("comp/discovery/#", 0)
assert not mqtt_client_mock.unsubscribe.called