mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
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>
This commit is contained in:
parent
ebe4888c21
commit
3cc13d454f
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, final
|
from typing import TYPE_CHECKING, Any, final
|
||||||
import uuid
|
import uuid
|
||||||
@ -14,10 +15,6 @@ from homeassistant.core import Context, HomeAssistant, callback
|
|||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import collection, entity_registry as er
|
from homeassistant.helpers import collection, entity_registry as er
|
||||||
import homeassistant.helpers.config_validation as cv
|
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 import Entity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
@ -245,6 +242,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
).async_setup(hass)
|
).async_setup(hass)
|
||||||
|
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
|
entity_update_handlers: dict[str, Callable[[str | None, str | None], None]] = {}
|
||||||
|
|
||||||
async def tag_change_listener(
|
async def tag_change_listener(
|
||||||
change_type: str, item_id: str, updated_config: dict
|
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(
|
await component.async_add_entities(
|
||||||
[
|
[
|
||||||
TagEntity(
|
TagEntity(
|
||||||
|
entity_update_handlers,
|
||||||
entity.name or entity.original_name,
|
entity.name or entity.original_name,
|
||||||
updated_config[CONF_ID],
|
updated_config[CONF_ID],
|
||||||
updated_config.get(LAST_SCANNED),
|
updated_config.get(LAST_SCANNED),
|
||||||
@ -273,12 +272,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
elif change_type == collection.CHANGE_UPDATED:
|
elif change_type == collection.CHANGE_UPDATED:
|
||||||
# When tags are changed or updated in storage
|
# When tags are changed or updated in storage
|
||||||
async_dispatcher_send(
|
if handler := entity_update_handlers.get(updated_config[CONF_ID]):
|
||||||
hass,
|
handler(
|
||||||
f"{SIGNAL_TAG_CHANGED}-{updated_config[CONF_ID]}",
|
updated_config.get(DEVICE_ID),
|
||||||
updated_config.get(DEVICE_ID),
|
updated_config.get(LAST_SCANNED),
|
||||||
updated_config.get(LAST_SCANNED),
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# Deleted tags
|
# Deleted tags
|
||||||
elif change_type == collection.CHANGE_REMOVED:
|
elif change_type == collection.CHANGE_REMOVED:
|
||||||
@ -308,6 +306,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
name = entity.name or entity.original_name
|
name = entity.name or entity.original_name
|
||||||
entities.append(
|
entities.append(
|
||||||
TagEntity(
|
TagEntity(
|
||||||
|
entity_update_handlers,
|
||||||
name,
|
name,
|
||||||
tag[CONF_ID],
|
tag[CONF_ID],
|
||||||
tag.get(LAST_SCANNED),
|
tag.get(LAST_SCANNED),
|
||||||
@ -371,12 +370,14 @@ class TagEntity(Entity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
entity_update_handlers: dict[str, Callable[[str | None, str | None], None]],
|
||||||
name: str,
|
name: str,
|
||||||
tag_id: str,
|
tag_id: str,
|
||||||
last_scanned: str | None,
|
last_scanned: str | None,
|
||||||
device_id: str | None,
|
device_id: str | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Tag event."""
|
"""Initialize the Tag event."""
|
||||||
|
self._entity_update_handlers = entity_update_handlers
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._tag_id = tag_id
|
self._tag_id = tag_id
|
||||||
self._attr_unique_id = tag_id
|
self._attr_unique_id = tag_id
|
||||||
@ -419,10 +420,9 @@ class TagEntity(Entity):
|
|||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Handle entity which will be added."""
|
"""Handle entity which will be added."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self.async_on_remove(
|
self._entity_update_handlers[self._tag_id] = self.async_handle_event
|
||||||
async_dispatcher_connect(
|
|
||||||
self.hass,
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
f"{SIGNAL_TAG_CHANGED}-{self._tag_id}",
|
"""Handle entity being removed."""
|
||||||
self.async_handle_event,
|
await super().async_will_remove_from_hass()
|
||||||
)
|
del self._entity_update_handlers[self._tag_id]
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user