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:
parent
6f7cb158d8
commit
8aa30cce26
23 changed files with 164 additions and 56 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue