Home Assistant Unofficial Reference 2024.12.1
helper.py
Go to the documentation of this file.
1 """Helper class for the Ambient Weather Network integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from .const import (
8  API_LAST_DATA,
9  API_STATION_COORDS,
10  API_STATION_INFO,
11  API_STATION_LOCATION,
12  API_STATION_NAME,
13  API_STATION_TYPE,
14 )
15 
16 
17 def get_station_name(station: dict[str, Any]) -> str:
18  """Pick a station name.
19 
20  Station names can be empty, in which case we construct the name from
21  the location and device type.
22  """
23  if name := station.get(API_STATION_INFO, {}).get(API_STATION_NAME):
24  return str(name)
25  location = (
26  station.get(API_STATION_INFO, {})
27  .get(API_STATION_COORDS, {})
28  .get(API_STATION_LOCATION)
29  )
30  station_type = station.get(API_LAST_DATA, {}).get(API_STATION_TYPE)
31  return f"{location}{'' if location is None or station_type is None else ' '}{station_type}"
str get_station_name(dict[str, Any] station)
Definition: helper.py:17
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88