From 2cc6fd1afb5cac2cb9be7edb72c639e174f6efba Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 26 Dec 2023 21:46:36 -1000 Subject: [PATCH] Add attr caching support to the geo_location platform (#106432) --- .../components/geo_location/__init__.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/geo_location/__init__.py b/homeassistant/components/geo_location/__init__.py index af64443ca283..c5e91d32b203 100644 --- a/homeassistant/components/geo_location/__init__.py +++ b/homeassistant/components/geo_location/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations from datetime import timedelta import logging -from typing import Any, final +from typing import TYPE_CHECKING, Any, final from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE @@ -16,6 +16,12 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType +if TYPE_CHECKING: + from functools import cached_property +else: + from homeassistant.backports.functools import cached_property + + _LOGGER = logging.getLogger(__name__) ATTR_DISTANCE = "distance" @@ -51,7 +57,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return await component.async_unload_entry(entry) -class GeolocationEvent(Entity): +CACHED_PROPERTIES_WITH_ATTR_ = { + "source", + "distance", + "latitude", + "longitude", +} + + +class GeolocationEvent(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Base class for an external event with an associated geolocation.""" # Entity Properties @@ -68,22 +82,22 @@ class GeolocationEvent(Entity): return round(self.distance, 1) return None - @property + @cached_property def source(self) -> str: """Return source value of this external event.""" return self._attr_source - @property + @cached_property def distance(self) -> float | None: """Return distance value of this external event.""" return self._attr_distance - @property + @cached_property def latitude(self) -> float | None: """Return latitude value of this external event.""" return self._attr_latitude - @property + @cached_property def longitude(self) -> float | None: """Return longitude value of this external event.""" return self._attr_longitude