From 907086edbcc1c51cae84169f40a679f31e22d36f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:03:10 +0000 Subject: [PATCH] Move Entity back in --- homeassistant/components/tag/__init__.py | 71 +++++++++++++++++++- homeassistant/components/tag/entity.py | 83 ------------------------ 2 files changed, 69 insertions(+), 85 deletions(-) delete mode 100644 homeassistant/components/tag/entity.py diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py index a5622a81e0b..0462c5bec34 100644 --- a/homeassistant/components/tag/__init__.py +++ b/homeassistant/components/tag/__init__.py @@ -4,7 +4,7 @@ from __future__ import annotations from collections.abc import Callable import logging -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, final import uuid import voluptuous as vol @@ -15,6 +15,7 @@ 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.entity import Entity from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType, VolDictType @@ -23,11 +24,11 @@ import homeassistant.util.dt as dt_util from homeassistant.util.hass_dict import HassKey from .const import DEFAULT_NAME, DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, LOGGER, TAG_ID -from .entity import TagEntity _LOGGER = logging.getLogger(__name__) LAST_SCANNED = "last_scanned" +LAST_SCANNED_BY_DEVICE_ID = "last_scanned_by_device_id" STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 STORAGE_VERSION_MINOR = 3 @@ -357,3 +358,69 @@ async def async_scan_tag( {TAG_ID: tag_id, LAST_SCANNED: dt_util.utcnow(), **extra_kwargs} ) _LOGGER.debug("Tag: %s scanned by device: %s", tag_id, device_id) + + +class TagEntity(Entity): + """Representation of a Tag entity.""" + + _unrecorded_attributes = frozenset({TAG_ID}) + _attr_should_poll = False + + 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 + self._last_device_id: str | None = device_id + self._last_scanned = last_scanned + + @callback + def async_handle_event( + self, device_id: str | None, last_scanned: str | None + ) -> None: + """Handle the Tag scan event.""" + if _LOGGER.isEnabledFor(logging.DEBUG): + _LOGGER.debug( + "Tag %s scanned by device %s at %s, last scanned at %s", + self._tag_id, + device_id, + last_scanned, + self._last_scanned, + ) + self._last_device_id = device_id + self._last_scanned = last_scanned + self.async_write_ha_state() + + @property + @final + def state(self) -> str | None: + """Return the entity state.""" + if ( + not self._last_scanned + or (last_scanned := dt_util.parse_datetime(self._last_scanned)) is None + ): + return None + return last_scanned.isoformat(timespec="milliseconds") + + @property + def extra_state_attributes(self) -> dict[str, Any]: + """Return the state attributes of the sun.""" + return {TAG_ID: self._tag_id, LAST_SCANNED_BY_DEVICE_ID: self._last_device_id} + + async def async_added_to_hass(self) -> None: + """Handle entity which will be added.""" + await super().async_added_to_hass() + 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] diff --git a/homeassistant/components/tag/entity.py b/homeassistant/components/tag/entity.py deleted file mode 100644 index c40bb3fa475..00000000000 --- a/homeassistant/components/tag/entity.py +++ /dev/null @@ -1,83 +0,0 @@ -"""The Tag integration.""" - -from __future__ import annotations - -from collections.abc import Callable -import logging -from typing import Any, final - -from homeassistant.core import callback -from homeassistant.helpers.entity import Entity -import homeassistant.util.dt as dt_util - -from .const import TAG_ID - -_LOGGER = logging.getLogger(__name__) - -LAST_SCANNED_BY_DEVICE_ID = "last_scanned_by_device_id" - - -class TagEntity(Entity): - """Representation of a Tag entity.""" - - _unrecorded_attributes = frozenset({TAG_ID}) - _attr_should_poll = False - - 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 - self._last_device_id: str | None = device_id - self._last_scanned = last_scanned - - @callback - def async_handle_event( - self, device_id: str | None, last_scanned: str | None - ) -> None: - """Handle the Tag scan event.""" - if _LOGGER.isEnabledFor(logging.DEBUG): - _LOGGER.debug( - "Tag %s scanned by device %s at %s, last scanned at %s", - self._tag_id, - device_id, - last_scanned, - self._last_scanned, - ) - self._last_device_id = device_id - self._last_scanned = last_scanned - self.async_write_ha_state() - - @property - @final - def state(self) -> str | None: - """Return the entity state.""" - if ( - not self._last_scanned - or (last_scanned := dt_util.parse_datetime(self._last_scanned)) is None - ): - return None - return last_scanned.isoformat(timespec="milliseconds") - - @property - def extra_state_attributes(self) -> dict[str, Any]: - """Return the state attributes of the sun.""" - return {TAG_ID: self._tag_id, LAST_SCANNED_BY_DEVICE_ID: self._last_device_id} - - async def async_added_to_hass(self) -> None: - """Handle entity which will be added.""" - await super().async_added_to_hass() - 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]