Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Geolocation."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any, final
8 
9 from propcache import cached_property
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import config_validation as cv
15 from homeassistant.helpers.entity import Entity
16 from homeassistant.helpers.entity_component import EntityComponent
17 from homeassistant.helpers.typing import ConfigType
18 from homeassistant.util.hass_dict import HassKey
19 
20 _LOGGER = logging.getLogger(__name__)
21 
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
27 SCAN_INTERVAL = timedelta(seconds=60)
28 
29 ATTR_DISTANCE = "distance"
30 ATTR_SOURCE = "source"
31 
32 
33 # mypy: disallow-any-generics
34 
35 
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
40  )
41  await component.async_setup(config)
42  return True
43 
44 
45 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
46  """Set up a config entry."""
47  return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
48 
49 
50 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
51  """Unload a config entry."""
52  return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
53 
54 
55 CACHED_PROPERTIES_WITH_ATTR_ = {
56  "source",
57  "distance",
58  "latitude",
59  "longitude",
60 }
61 
62 
63 class GeolocationEvent(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
64  """Base class for an external event with an associated geolocation."""
65 
66  # Entity Properties
67  _attr_source: str
68  _attr_distance: float | None = None
69  _attr_latitude: float | None = None
70  _attr_longitude: float | None = None
71 
72  @final
73  @property
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)
78  return None
79 
80  @cached_property
81  def source(self) -> str:
82  """Return source value of this external event."""
83  return self._attr_source
84 
85  @cached_property
86  def distance(self) -> float | None:
87  """Return distance value of this external event."""
88  return self._attr_distance
89 
90  @cached_property
91  def latitude(self) -> float | None:
92  """Return latitude value of this external event."""
93  return self._attr_latitude
94 
95  @cached_property
96  def longitude(self) -> float | None:
97  """Return longitude value of this external event."""
98  return self._attr_longitude
99 
100  @final
101  @property
102  def state_attributes(self) -> dict[str, Any]:
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)
107  if self.longitudelongitude is not None:
108  data[ATTR_LONGITUDE] = round(self.longitudelongitude, 5)
109  return data
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:45
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:50
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:36