mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +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."""
|
"""Subscribe to discovery updates."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self._removed_from_hass = False
|
self._removed_from_hass = False
|
||||||
discovery_hash: tuple[str, str] | None = (
|
if not self._discovery_data:
|
||||||
self._discovery_data[ATTR_DISCOVERY_HASH] if self._discovery_data else None
|
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(
|
async def _async_remove_state_and_registry_entry(
|
||||||
@ -846,10 +857,9 @@ class MqttDiscoveryUpdateMixin(Entity):
|
|||||||
await self.async_remove(force_remove=True)
|
await self.async_remove(force_remove=True)
|
||||||
|
|
||||||
async def _async_process_discovery_update(
|
async def _async_process_discovery_update(
|
||||||
|
self,
|
||||||
payload: MQTTDiscoveryPayload,
|
payload: MQTTDiscoveryPayload,
|
||||||
discovery_update: Callable[
|
discovery_update: Callable[[MQTTDiscoveryPayload], Coroutine[Any, Any, None]],
|
||||||
[MQTTDiscoveryPayload], Coroutine[Any, Any, None]
|
|
||||||
],
|
|
||||||
discovery_data: DiscoveryInfoType,
|
discovery_data: DiscoveryInfoType,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Process discovery update."""
|
"""Process discovery update."""
|
||||||
@ -858,16 +868,16 @@ class MqttDiscoveryUpdateMixin(Entity):
|
|||||||
finally:
|
finally:
|
||||||
send_discovery_done(self.hass, discovery_data)
|
send_discovery_done(self.hass, discovery_data)
|
||||||
|
|
||||||
async def _async_process_discovery_update_and_remove(
|
async def _async_process_discovery_update_and_remove(self) -> None:
|
||||||
payload: MQTTDiscoveryPayload, discovery_data: DiscoveryInfoType
|
|
||||||
) -> None:
|
|
||||||
"""Process discovery update and remove entity."""
|
"""Process discovery update and remove entity."""
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
assert self._discovery_data
|
||||||
self._cleanup_discovery_on_remove()
|
self._cleanup_discovery_on_remove()
|
||||||
await _async_remove_state_and_registry_entry(self)
|
await self._async_remove_state_and_registry_entry()
|
||||||
send_discovery_done(self.hass, discovery_data)
|
send_discovery_done(self.hass, self._discovery_data)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def discovery_callback(payload: MQTTDiscoveryPayload) -> None:
|
def _async_discovery_callback(self, payload: MQTTDiscoveryPayload) -> None:
|
||||||
"""Handle discovery update.
|
"""Handle discovery update.
|
||||||
|
|
||||||
If the payload has changed we will create a task to
|
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
|
As this callback can fire when nothing has changed, this
|
||||||
is a normal function to avoid task creation until it is needed.
|
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(
|
_LOGGER.debug(
|
||||||
"Got update for entity with hash: %s '%s'",
|
"Got update for entity with hash: %s '%s'",
|
||||||
discovery_hash,
|
discovery_hash,
|
||||||
payload,
|
payload,
|
||||||
)
|
)
|
||||||
if TYPE_CHECKING:
|
|
||||||
assert self._discovery_data
|
|
||||||
old_payload: DiscoveryInfoType
|
old_payload: DiscoveryInfoType
|
||||||
old_payload = self._discovery_data[ATTR_DISCOVERY_PAYLOAD]
|
old_payload = self._discovery_data[ATTR_DISCOVERY_PAYLOAD]
|
||||||
debug_info.update_entity_discovery_data(self.hass, payload, self.entity_id)
|
debug_info.update_entity_discovery_data(self.hass, payload, self.entity_id)
|
||||||
@ -890,16 +901,14 @@ class MqttDiscoveryUpdateMixin(Entity):
|
|||||||
# Empty payload: Remove component
|
# Empty payload: Remove component
|
||||||
_LOGGER.info("Removing component: %s", self.entity_id)
|
_LOGGER.info("Removing component: %s", self.entity_id)
|
||||||
self.hass.async_create_task(
|
self.hass.async_create_task(
|
||||||
_async_process_discovery_update_and_remove(
|
self._async_process_discovery_update_and_remove()
|
||||||
payload, self._discovery_data
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
elif self._discovery_update:
|
elif self._discovery_update:
|
||||||
if old_payload != self._discovery_data[ATTR_DISCOVERY_PAYLOAD]:
|
if old_payload != payload:
|
||||||
# Non-empty, changed payload: Notify component
|
# Non-empty, changed payload: Notify component
|
||||||
_LOGGER.info("Updating component: %s", self.entity_id)
|
_LOGGER.info("Updating component: %s", self.entity_id)
|
||||||
self.hass.async_create_task(
|
self.hass.async_create_task(
|
||||||
_async_process_discovery_update(
|
self._async_process_discovery_update(
|
||||||
payload, self._discovery_update, self._discovery_data
|
payload, self._discovery_update, self._discovery_data
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -908,21 +917,6 @@ class MqttDiscoveryUpdateMixin(Entity):
|
|||||||
_LOGGER.debug("Ignoring unchanged update for: %s", self.entity_id)
|
_LOGGER.debug("Ignoring unchanged update for: %s", self.entity_id)
|
||||||
send_discovery_done(self.hass, self._discovery_data)
|
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:
|
async def async_removed_from_registry(self) -> None:
|
||||||
"""Clear retained discovery topic in broker."""
|
"""Clear retained discovery topic in broker."""
|
||||||
if not self._removed_from_hass and self._discovery_data is not None:
|
if not self._removed_from_hass and self._discovery_data is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user