Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Util functions for the dwd_weather_warnings integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
6 from homeassistant.core import HomeAssistant
7 from homeassistant.helpers import entity_registry as er
8 
9 from .exceptions import EntityNotFoundError
10 
11 
13  hass: HomeAssistant, registry_id: str
14 ) -> tuple[float, float] | None:
15  """Extract longitude and latitude from a device tracker."""
16  registry = er.async_get(hass)
17  registry_entry = registry.async_get(registry_id)
18  if registry_entry is None:
19  raise EntityNotFoundError(f"Failed to find registry entry {registry_id}")
20 
21  entity = hass.states.get(registry_entry.entity_id)
22  if entity is None:
23  raise EntityNotFoundError(f"Failed to find entity {registry_entry.entity_id}")
24 
25  latitude = entity.attributes.get(ATTR_LATITUDE)
26  if not latitude:
27  raise AttributeError(
28  f"Failed to find attribute '{ATTR_LATITUDE}' in {registry_entry.entity_id}",
29  ATTR_LATITUDE,
30  )
31 
32  longitude = entity.attributes.get(ATTR_LONGITUDE)
33  if not longitude:
34  raise AttributeError(
35  f"Failed to find attribute '{ATTR_LONGITUDE}' in {registry_entry.entity_id}",
36  ATTR_LONGITUDE,
37  )
38 
39  return (latitude, longitude)
tuple[float, float]|None get_position_data(HomeAssistant hass, str registry_id)
Definition: util.py:14