Fix state saving when sharing topics for MQTT entities (#79421)

* Do not write old state sharing availability topic

* Add a test

* Support for all availability topics

* delay async_write_ha_state till last callback

* Process write req after processing callback jobs

* Do not count subscription callbacks

* Simplify

* Stale docsting

* No topic needed for delays state write

* No need to clear when reloading

* Move test to test_mixins.py

* Only set up sensor platform for test
This commit is contained in:
Jan Bouwhuis 2022-10-11 10:49:54 +02:00 committed by GitHub
parent 6f7cb158d8
commit 8aa30cce26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 164 additions and 56 deletions

View file

@ -26,9 +26,12 @@ class EntitySubscription:
qos: int = attr.ib(default=0)
encoding: str = attr.ib(default="utf-8")
def resubscribe_if_necessary(self, hass, other):
def resubscribe_if_necessary(
self, hass: HomeAssistant, other: EntitySubscription | None
) -> None:
"""Re-subscribe to the new topic if necessary."""
if not self._should_resubscribe(other):
assert other
self.unsubscribe_callback = other.unsubscribe_callback
return
@ -56,7 +59,7 @@ class EntitySubscription:
return
self.unsubscribe_callback = await self.subscribe_task
def _should_resubscribe(self, other):
def _should_resubscribe(self, other: EntitySubscription | None) -> bool:
"""Check if we should re-subscribe to the topic using the old state."""
if other is None:
return True