Remove dispatcher from Tag entity (#118671)

* Remove dispatcher from Tag entity

* type

* Don't use  helper

* Del is faster than pop

* Use id in update

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
pull/118744/head
Paulus Schoutsen 2024-06-03 13:39:40 -04:00 committed by GitHub
parent ebe4888c21
commit 3cc13d454f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 17 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Callable
import logging
from typing import TYPE_CHECKING, Any, final
import uuid
@ -14,10 +15,6 @@ from homeassistant.core import Context, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import collection, entity_registry as er
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.storage import Store
@ -245,6 +242,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
).async_setup(hass)
entity_registry = er.async_get(hass)
entity_update_handlers: dict[str, Callable[[str | None, str | None], None]] = {}
async def tag_change_listener(
change_type: str, item_id: str, updated_config: dict
@ -263,6 +261,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await component.async_add_entities(
[
TagEntity(
entity_update_handlers,
entity.name or entity.original_name,
updated_config[CONF_ID],
updated_config.get(LAST_SCANNED),
@ -273,9 +272,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
elif change_type == collection.CHANGE_UPDATED:
# When tags are changed or updated in storage
async_dispatcher_send(
hass,
f"{SIGNAL_TAG_CHANGED}-{updated_config[CONF_ID]}",
if handler := entity_update_handlers.get(updated_config[CONF_ID]):
handler(
updated_config.get(DEVICE_ID),
updated_config.get(LAST_SCANNED),
)
@ -308,6 +306,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
name = entity.name or entity.original_name
entities.append(
TagEntity(
entity_update_handlers,
name,
tag[CONF_ID],
tag.get(LAST_SCANNED),
@ -371,12 +370,14 @@ class TagEntity(Entity):
def __init__(
self,
entity_update_handlers: dict[str, Callable[[str | None, str | None], None]],
name: str,
tag_id: str,
last_scanned: str | None,
device_id: str | None,
) -> None:
"""Initialize the Tag event."""
self._entity_update_handlers = entity_update_handlers
self._attr_name = name
self._tag_id = tag_id
self._attr_unique_id = tag_id
@ -419,10 +420,9 @@ class TagEntity(Entity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{SIGNAL_TAG_CHANGED}-{self._tag_id}",
self.async_handle_event,
)
)
self._entity_update_handlers[self._tag_id] = self.async_handle_event
async def async_will_remove_from_hass(self) -> None:
"""Handle entity being removed."""
await super().async_will_remove_from_hass()
del self._entity_update_handlers[self._tag_id]