mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00: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
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, final
|
from typing import TYPE_CHECKING, Any, final
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
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.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.typing import ConfigType
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_DISTANCE = "distance"
|
ATTR_DISTANCE = "distance"
|
||||||
@ -51,7 +57,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return await component.async_unload_entry(entry)
|
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."""
|
"""Base class for an external event with an associated geolocation."""
|
||||||
|
|
||||||
# Entity Properties
|
# Entity Properties
|
||||||
@ -68,22 +82,22 @@ class GeolocationEvent(Entity):
|
|||||||
return round(self.distance, 1)
|
return round(self.distance, 1)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def source(self) -> str:
|
def source(self) -> str:
|
||||||
"""Return source value of this external event."""
|
"""Return source value of this external event."""
|
||||||
return self._attr_source
|
return self._attr_source
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def distance(self) -> float | None:
|
def distance(self) -> float | None:
|
||||||
"""Return distance value of this external event."""
|
"""Return distance value of this external event."""
|
||||||
return self._attr_distance
|
return self._attr_distance
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def latitude(self) -> float | None:
|
def latitude(self) -> float | None:
|
||||||
"""Return latitude value of this external event."""
|
"""Return latitude value of this external event."""
|
||||||
return self._attr_latitude
|
return self._attr_latitude
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def longitude(self) -> float | None:
|
def longitude(self) -> float | None:
|
||||||
"""Return longitude value of this external event."""
|
"""Return longitude value of this external event."""
|
||||||
return self._attr_longitude
|
return self._attr_longitude
|
||||||
|
Loading…
x
Reference in New Issue
Block a user