1 """Support for Geolocation."""
3 from __future__
import annotations
5 from datetime
import timedelta
7 from typing
import Any, final
9 from propcache
import cached_property
20 _LOGGER = logging.getLogger(__name__)
22 DOMAIN =
"geo_location"
23 DATA_COMPONENT: HassKey[EntityComponent[GeolocationEvent]] =
HassKey(DOMAIN)
24 ENTITY_ID_FORMAT = DOMAIN +
".{}"
25 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
26 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
29 ATTR_DISTANCE =
"distance"
30 ATTR_SOURCE =
"source"
36 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
37 """Set up the Geolocation component."""
38 component = hass.data[DATA_COMPONENT] = EntityComponent[GeolocationEvent](
39 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
41 await component.async_setup(config)
46 """Set up a config entry."""
51 """Unload a config entry."""
55 CACHED_PROPERTIES_WITH_ATTR_ = {
64 """Base class for an external event with an associated geolocation."""
68 _attr_distance: float |
None =
None
69 _attr_latitude: float |
None =
None
70 _attr_longitude: float |
None =
None
74 def state(self) -> float | None:
75 """Return the state of the sensor."""
76 if self.
distancedistance
is not None:
77 return round(self.
distancedistance, 1)
82 """Return source value of this external event."""
83 return self._attr_source
87 """Return distance value of this external event."""
88 return self._attr_distance
92 """Return latitude value of this external event."""
93 return self._attr_latitude
97 """Return longitude value of this external event."""
98 return self._attr_longitude
103 """Return the state attributes of this external event."""
104 data: dict[str, Any] = {ATTR_SOURCE: self.
sourcesource}
105 if self.
latitudelatitude
is not None:
106 data[ATTR_LATITUDE] = round(self.
latitudelatitude, 5)
108 data[ATTR_LONGITUDE] = round(self.
longitudelongitude, 5)
float|None latitude(self)
float|None distance(self)
dict[str, Any] state_attributes(self)
float|None longitude(self)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)