mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Migrate mqtt mixin async_added_to_hass inner functions to bound methods (#118280)
This commit is contained in:
parent
fb95b91507
commit
a3c3f938a7
@ -823,8 +823,19 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
"""Subscribe to discovery updates."""
|
||||
await super().async_added_to_hass()
|
||||
self._removed_from_hass = False
|
||||
discovery_hash: tuple[str, str] | None = (
|
||||
self._discovery_data[ATTR_DISCOVERY_HASH] if self._discovery_data else None
|
||||
if not self._discovery_data:
|
||||
return
|
||||
discovery_hash: tuple[str, str] = self._discovery_data[ATTR_DISCOVERY_HASH]
|
||||
debug_info.add_entity_discovery_data(
|
||||
self.hass, self._discovery_data, self.entity_id
|
||||
)
|
||||
# Set in case the entity has been removed and is re-added,
|
||||
# for example when changing entity_id
|
||||
set_discovery_hash(self.hass, discovery_hash)
|
||||
self._remove_discovery_updated = async_dispatcher_connect(
|
||||
self.hass,
|
||||
MQTT_DISCOVERY_UPDATED.format(*discovery_hash),
|
||||
self._async_discovery_callback,
|
||||
)
|
||||
|
||||
async def _async_remove_state_and_registry_entry(
|
||||
@ -846,10 +857,9 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
await self.async_remove(force_remove=True)
|
||||
|
||||
async def _async_process_discovery_update(
|
||||
self,
|
||||
payload: MQTTDiscoveryPayload,
|
||||
discovery_update: Callable[
|
||||
[MQTTDiscoveryPayload], Coroutine[Any, Any, None]
|
||||
],
|
||||
discovery_update: Callable[[MQTTDiscoveryPayload], Coroutine[Any, Any, None]],
|
||||
discovery_data: DiscoveryInfoType,
|
||||
) -> None:
|
||||
"""Process discovery update."""
|
||||
@ -858,16 +868,16 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
finally:
|
||||
send_discovery_done(self.hass, discovery_data)
|
||||
|
||||
async def _async_process_discovery_update_and_remove(
|
||||
payload: MQTTDiscoveryPayload, discovery_data: DiscoveryInfoType
|
||||
) -> None:
|
||||
async def _async_process_discovery_update_and_remove(self) -> None:
|
||||
"""Process discovery update and remove entity."""
|
||||
if TYPE_CHECKING:
|
||||
assert self._discovery_data
|
||||
self._cleanup_discovery_on_remove()
|
||||
await _async_remove_state_and_registry_entry(self)
|
||||
send_discovery_done(self.hass, discovery_data)
|
||||
await self._async_remove_state_and_registry_entry()
|
||||
send_discovery_done(self.hass, self._discovery_data)
|
||||
|
||||
@callback
|
||||
def discovery_callback(payload: MQTTDiscoveryPayload) -> None:
|
||||
def _async_discovery_callback(self, payload: MQTTDiscoveryPayload) -> None:
|
||||
"""Handle discovery update.
|
||||
|
||||
If the payload has changed we will create a task to
|
||||
@ -876,13 +886,14 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
As this callback can fire when nothing has changed, this
|
||||
is a normal function to avoid task creation until it is needed.
|
||||
"""
|
||||
if TYPE_CHECKING:
|
||||
assert self._discovery_data
|
||||
discovery_hash: tuple[str, str] = self._discovery_data[ATTR_DISCOVERY_HASH]
|
||||
_LOGGER.debug(
|
||||
"Got update for entity with hash: %s '%s'",
|
||||
discovery_hash,
|
||||
payload,
|
||||
)
|
||||
if TYPE_CHECKING:
|
||||
assert self._discovery_data
|
||||
old_payload: DiscoveryInfoType
|
||||
old_payload = self._discovery_data[ATTR_DISCOVERY_PAYLOAD]
|
||||
debug_info.update_entity_discovery_data(self.hass, payload, self.entity_id)
|
||||
@ -890,16 +901,14 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
# Empty payload: Remove component
|
||||
_LOGGER.info("Removing component: %s", self.entity_id)
|
||||
self.hass.async_create_task(
|
||||
_async_process_discovery_update_and_remove(
|
||||
payload, self._discovery_data
|
||||
)
|
||||
self._async_process_discovery_update_and_remove()
|
||||
)
|
||||
elif self._discovery_update:
|
||||
if old_payload != self._discovery_data[ATTR_DISCOVERY_PAYLOAD]:
|
||||
if old_payload != payload:
|
||||
# Non-empty, changed payload: Notify component
|
||||
_LOGGER.info("Updating component: %s", self.entity_id)
|
||||
self.hass.async_create_task(
|
||||
_async_process_discovery_update(
|
||||
self._async_process_discovery_update(
|
||||
payload, self._discovery_update, self._discovery_data
|
||||
)
|
||||
)
|
||||
@ -908,21 +917,6 @@ class MqttDiscoveryUpdateMixin(Entity):
|
||||
_LOGGER.debug("Ignoring unchanged update for: %s", self.entity_id)
|
||||
send_discovery_done(self.hass, self._discovery_data)
|
||||
|
||||
if discovery_hash:
|
||||
if TYPE_CHECKING:
|
||||
assert self._discovery_data is not None
|
||||
debug_info.add_entity_discovery_data(
|
||||
self.hass, self._discovery_data, self.entity_id
|
||||
)
|
||||
# Set in case the entity has been removed and is re-added,
|
||||
# for example when changing entity_id
|
||||
set_discovery_hash(self.hass, discovery_hash)
|
||||
self._remove_discovery_updated = async_dispatcher_connect(
|
||||
self.hass,
|
||||
MQTT_DISCOVERY_UPDATED.format(*discovery_hash),
|
||||
discovery_callback,
|
||||
)
|
||||
|
||||
async def async_removed_from_registry(self) -> None:
|
||||
"""Clear retained discovery topic in broker."""
|
||||
if not self._removed_from_hass and self._discovery_data is not None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user