1
mirror of https://github.com/home-assistant/core synced 2024-07-15 09:42:11 +02:00

Add attr caching support to the geo_location platform (#106432)

This commit is contained in:
J. Nick Koston 2023-12-26 21:46:36 -10:00 committed by GitHub
parent 9b864e8130
commit 2cc6fd1afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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