1 """Support for UK Met Office weather service."""
3 from __future__
import annotations
7 from datapoint.Element
import Element
12 SensorEntityDescription,
27 DataUpdateCoordinator,
30 from .
import get_device_info
35 METOFFICE_COORDINATES,
36 METOFFICE_DAILY_COORDINATOR,
37 METOFFICE_HOURLY_COORDINATOR,
41 VISIBILITY_DISTANCE_CLASSES,
43 from .data
import MetOfficeData
45 ATTR_LAST_UPDATE =
"last_update"
46 ATTR_SENSOR_ID =
"sensor_id"
47 ATTR_SITE_ID =
"site_id"
48 ATTR_SITE_NAME =
"site_name"
51 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
55 icon=
"mdi:label-outline",
56 entity_registry_enabled_default=
False,
61 icon=
"mdi:weather-sunny",
62 entity_registry_enabled_default=
True,
67 device_class=SensorDeviceClass.TEMPERATURE,
68 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
70 entity_registry_enabled_default=
True,
73 key=
"feels_like_temperature",
74 name=
"Feels like temperature",
75 device_class=SensorDeviceClass.TEMPERATURE,
76 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
78 entity_registry_enabled_default=
False,
83 native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
86 suggested_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
87 device_class=SensorDeviceClass.WIND_SPEED,
88 entity_registry_enabled_default=
True,
92 name=
"Wind direction",
93 icon=
"mdi:compass-outline",
94 entity_registry_enabled_default=
False,
99 native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
102 suggested_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
103 device_class=SensorDeviceClass.WIND_SPEED,
104 entity_registry_enabled_default=
False,
110 entity_registry_enabled_default=
False,
113 key=
"visibility_distance",
114 name=
"Visibility distance",
115 native_unit_of_measurement=UnitOfLength.KILOMETERS,
117 entity_registry_enabled_default=
False,
122 native_unit_of_measurement=UV_INDEX,
123 icon=
"mdi:weather-sunny-alert",
124 entity_registry_enabled_default=
True,
128 name=
"Probability of precipitation",
129 native_unit_of_measurement=PERCENTAGE,
130 icon=
"mdi:weather-rainy",
131 entity_registry_enabled_default=
True,
136 device_class=SensorDeviceClass.HUMIDITY,
137 native_unit_of_measurement=PERCENTAGE,
139 entity_registry_enabled_default=
False,
145 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
147 """Set up the Met Office weather sensor platform."""
148 hass_data = hass.data[DOMAIN][entry.entry_id]
153 hass_data[METOFFICE_HOURLY_COORDINATOR],
158 for description
in SENSOR_TYPES
162 hass_data[METOFFICE_DAILY_COORDINATOR],
167 for description
in SENSOR_TYPES
174 CoordinatorEntity[DataUpdateCoordinator[MetOfficeData]], SensorEntity
176 """Implementation of a Met Office current weather condition sensor."""
178 _attr_attribution = ATTRIBUTION
179 _attr_has_entity_name =
True
183 coordinator: DataUpdateCoordinator[MetOfficeData],
184 hass_data: dict[str, Any],
186 description: SensorEntityDescription,
188 """Initialize the sensor."""
192 mode_label =
"3-hourly" if use_3hourly
else "daily"
195 coordinates=hass_data[METOFFICE_COORDINATES], name=hass_data[METOFFICE_NAME]
197 self.
_attr_name_attr_name = f
"{description.name} {mode_label}"
198 self.
_attr_unique_id_attr_unique_id = f
"{description.key}_{hass_data[METOFFICE_COORDINATES]}"
200 self.
_attr_unique_id_attr_unique_id = f
"{self._attr_unique_id}_{MODE_DAILY}"
202 self.
entity_descriptionentity_description.entity_registry_enabled_default
and use_3hourly
207 """Return the state of the sensor."""
210 if self.
entity_descriptionentity_description.key ==
"visibility_distance" and hasattr(
211 self.coordinator.data.now,
"visibility"
213 value = VISIBILITY_DISTANCE_CLASSES.get(
214 self.coordinator.data.now.visibility.value
218 self.coordinator.data.now,
"visibility"
220 value = VISIBILITY_CLASSES.get(self.coordinator.data.now.visibility.value)
225 value = CONDITION_MAP.get(self.coordinator.data.now.weather.value)
227 elif hasattr(self.coordinator.data.now, self.
entity_descriptionentity_description.key):
228 value = getattr(self.coordinator.data.now, self.
entity_descriptionentity_description.key)
230 if isinstance(value, Element):
237 """Return the icon for the entity card."""
243 elif value ==
"partlycloudy":
244 value =
"partly-cloudy"
245 value = f
"mdi:weather-{value}"
251 """Return the state attributes of the device."""
253 ATTR_LAST_UPDATE: self.coordinator.data.now.date,
255 ATTR_SITE_ID: self.coordinator.data.site.location_id,
256 ATTR_SITE_NAME: self.coordinator.data.site.name,
StateType native_value(self)
dict[str, Any] extra_state_attributes(self)
_attr_entity_registry_enabled_default
None __init__(self, DataUpdateCoordinator[MetOfficeData] coordinator, dict[str, Any] hass_data, bool use_3hourly, SensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo get_device_info(str coordinates, str name)