Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for getting statistical data from a DWD Weather Warnings.
2 
3 Data is fetched from DWD:
4 https://rcccm.dwd.de/DE/wetter/warnungen_aktuell/objekt_einbindung/objekteinbindung.html
5 
6 Warnungen vor extremem Unwetter (Stufe 4) # codespell:ignore vor
7 Unwetterwarnungen (Stufe 3)
8 Warnungen vor markantem Wetter (Stufe 2) # codespell:ignore vor
9 Wetterwarnungen (Stufe 1)
10 """
11 
12 from __future__ import annotations
13 
14 from typing import Any
15 
16 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.update_coordinator import CoordinatorEntity
21 
22 from .const import (
23  ADVANCE_WARNING_SENSOR,
24  API_ATTR_WARNING_COLOR,
25  API_ATTR_WARNING_DESCRIPTION,
26  API_ATTR_WARNING_END,
27  API_ATTR_WARNING_HEADLINE,
28  API_ATTR_WARNING_INSTRUCTION,
29  API_ATTR_WARNING_LEVEL,
30  API_ATTR_WARNING_NAME,
31  API_ATTR_WARNING_PARAMETERS,
32  API_ATTR_WARNING_START,
33  API_ATTR_WARNING_TYPE,
34  ATTR_LAST_UPDATE,
35  ATTR_REGION_ID,
36  ATTR_REGION_NAME,
37  ATTR_WARNING_COUNT,
38  CURRENT_WARNING_SENSOR,
39  DOMAIN,
40 )
41 from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
42 
43 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
45  key=CURRENT_WARNING_SENSOR,
46  translation_key=CURRENT_WARNING_SENSOR,
47  ),
49  key=ADVANCE_WARNING_SENSOR,
50  translation_key=ADVANCE_WARNING_SENSOR,
51  ),
52 )
53 
54 
56  hass: HomeAssistant,
57  entry: DwdWeatherWarningsConfigEntry,
58  async_add_entities: AddEntitiesCallback,
59 ) -> None:
60  """Set up entities from config entry."""
61  coordinator = entry.runtime_data
62 
63  unique_id = entry.unique_id
64  assert unique_id
65 
67  DwdWeatherWarningsSensor(coordinator, description, unique_id)
68  for description in SENSOR_TYPES
69  )
70 
71 
73  CoordinatorEntity[DwdWeatherWarningsCoordinator], SensorEntity
74 ):
75  """Representation of a DWD-Weather-Warnings sensor."""
76 
77  _attr_attribution = "Data provided by DWD"
78  _attr_has_entity_name = True
79 
80  def __init__(
81  self,
82  coordinator: DwdWeatherWarningsCoordinator,
83  description: SensorEntityDescription,
84  unique_id: str,
85  ) -> None:
86  """Initialize a DWD-Weather-Warnings sensor."""
87  super().__init__(coordinator)
88 
89  self.entity_descriptionentity_description = description
90  self._attr_unique_id_attr_unique_id = f"{unique_id}-{description.key}"
91 
92  self._attr_device_info_attr_device_info = DeviceInfo(
93  identifiers={(DOMAIN, unique_id)},
94  name=coordinator.api.warncell_name,
95  entry_type=DeviceEntryType.SERVICE,
96  )
97 
98  @property
99  def native_value(self) -> int | None:
100  """Return the state of the sensor."""
101  if self.entity_descriptionentity_description.key == CURRENT_WARNING_SENSOR:
102  return self.coordinator.api.current_warning_level
103 
104  return self.coordinator.api.expected_warning_level
105 
106  @property
107  def extra_state_attributes(self) -> dict[str, Any]:
108  """Return the state attributes of the sensor."""
109  data = {
110  ATTR_REGION_NAME: self.coordinator.api.warncell_name,
111  ATTR_REGION_ID: self.coordinator.api.warncell_id,
112  ATTR_LAST_UPDATE: self.coordinator.api.last_update,
113  }
114 
115  if self.entity_descriptionentity_description.key == CURRENT_WARNING_SENSOR:
116  searched_warnings = self.coordinator.api.current_warnings
117  else:
118  searched_warnings = self.coordinator.api.expected_warnings
119 
120  data[ATTR_WARNING_COUNT] = len(searched_warnings)
121 
122  for i, warning in enumerate(searched_warnings, 1):
123  data[f"warning_{i}_name"] = warning[API_ATTR_WARNING_NAME]
124  data[f"warning_{i}_type"] = warning[API_ATTR_WARNING_TYPE]
125  data[f"warning_{i}_level"] = warning[API_ATTR_WARNING_LEVEL]
126  data[f"warning_{i}_headline"] = warning[API_ATTR_WARNING_HEADLINE]
127  data[f"warning_{i}_description"] = warning[API_ATTR_WARNING_DESCRIPTION]
128  data[f"warning_{i}_instruction"] = warning[API_ATTR_WARNING_INSTRUCTION]
129  data[f"warning_{i}_start"] = warning[API_ATTR_WARNING_START]
130  data[f"warning_{i}_end"] = warning[API_ATTR_WARNING_END]
131  data[f"warning_{i}_parameters"] = warning[API_ATTR_WARNING_PARAMETERS]
132  data[f"warning_{i}_color"] = warning[API_ATTR_WARNING_COLOR]
133 
134  # Dictionary for the attribute containing the complete warning.
135  warning_copy = warning.copy()
136  warning_copy[API_ATTR_WARNING_START] = data[f"warning_{i}_start"]
137  warning_copy[API_ATTR_WARNING_END] = data[f"warning_{i}_end"]
138  data[f"warning_{i}"] = warning_copy
139 
140  return data
141 
142  @property
143  def available(self) -> bool:
144  """Could the device be accessed during the last update call."""
145  return self.coordinator.api.data_valid
None __init__(self, DwdWeatherWarningsCoordinator coordinator, SensorEntityDescription description, str unique_id)
Definition: sensor.py:85
None async_setup_entry(HomeAssistant hass, DwdWeatherWarningsConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:59