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:
parent
9b864e8130
commit
2cc6fd1afb
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user