Use EntityDescription - openweathermap (#56888)

This commit is contained in:
Marc Mueller 2021-10-11 16:18:18 +02:00 committed by GitHub
parent 748d915909
commit 858739949b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 213 additions and 246 deletions

View file

@ -1,13 +1,19 @@
"""Support for the OpenWeatherMap (OWM) service."""
from .abstract_owm_sensor import AbstractOpenWeatherMapSensor
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
ATTR_API_FORECAST,
ATTRIBUTION,
DEFAULT_NAME,
DOMAIN,
ENTRY_NAME,
ENTRY_WEATHER_COORDINATOR,
FORECAST_MONITORED_CONDITIONS,
FORECAST_SENSOR_TYPES,
MONITORED_CONDITIONS,
MANUFACTURER,
WEATHER_SENSOR_TYPES,
)
from .weather_update_coordinator import WeatherUpdateCoordinator
@ -19,37 +25,79 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = domain_data[ENTRY_NAME]
weather_coordinator = domain_data[ENTRY_WEATHER_COORDINATOR]
weather_sensor_types = WEATHER_SENSOR_TYPES
forecast_sensor_types = FORECAST_SENSOR_TYPES
entities = []
for sensor_type in MONITORED_CONDITIONS:
unique_id = f"{config_entry.unique_id}-{sensor_type}"
entities.append(
OpenWeatherMapSensor(
name,
unique_id,
sensor_type,
weather_sensor_types[sensor_type],
weather_coordinator,
)
entities: list[AbstractOpenWeatherMapSensor] = [
OpenWeatherMapSensor(
name,
f"{config_entry.unique_id}-{description.key}",
description,
weather_coordinator,
)
for description in WEATHER_SENSOR_TYPES
]
for sensor_type in FORECAST_MONITORED_CONDITIONS:
unique_id = f"{config_entry.unique_id}-forecast-{sensor_type}"
entities.append(
entities.extend(
[
OpenWeatherMapForecastSensor(
f"{name} Forecast",
unique_id,
sensor_type,
forecast_sensor_types[sensor_type],
f"{config_entry.unique_id}-forecast-{description.key}",
description,
weather_coordinator,
)
)
for description in FORECAST_SENSOR_TYPES
]
)
async_add_entities(entities)
class AbstractOpenWeatherMapSensor(SensorEntity):
"""Abstract class for an OpenWeatherMap sensor."""
_attr_should_poll = False
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
def __init__(
self,
name,
unique_id,
description: SensorEntityDescription,
coordinator: DataUpdateCoordinator,
):
"""Initialize the sensor."""
self.entity_description = description
self._coordinator = coordinator
self._attr_name = f"{name} {description.name}"
self._attr_unique_id = unique_id
split_unique_id = unique_id.split("-")
self._attr_device_info = {
"identifiers": {(DOMAIN, f"{split_unique_id[0]}-{split_unique_id[1]}")},
"name": DEFAULT_NAME,
"manufacturer": MANUFACTURER,
"entry_type": "service",
}
@property
def attribution(self):
"""Return the attribution."""
return ATTRIBUTION
@property
def available(self):
"""Return True if entity is available."""
return self._coordinator.last_update_success
async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self):
"""Get the latest data from OWM and updates the states."""
await self._coordinator.async_request_refresh()
class OpenWeatherMapSensor(AbstractOpenWeatherMapSensor):
"""Implementation of an OpenWeatherMap sensor."""
@ -57,20 +105,17 @@ class OpenWeatherMapSensor(AbstractOpenWeatherMapSensor):
self,
name,
unique_id,
sensor_type,
sensor_configuration,
description: SensorEntityDescription,
weather_coordinator: WeatherUpdateCoordinator,
):
"""Initialize the sensor."""
super().__init__(
name, unique_id, sensor_type, sensor_configuration, weather_coordinator
)
super().__init__(name, unique_id, description, weather_coordinator)
self._weather_coordinator = weather_coordinator
@property
def native_value(self):
"""Return the state of the device."""
return self._weather_coordinator.data.get(self._sensor_type, None)
return self._weather_coordinator.data.get(self.entity_description.key, None)
class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor):
@ -80,14 +125,11 @@ class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor):
self,
name,
unique_id,
sensor_type,
sensor_configuration,
description: SensorEntityDescription,
weather_coordinator: WeatherUpdateCoordinator,
):
"""Initialize the sensor."""
super().__init__(
name, unique_id, sensor_type, sensor_configuration, weather_coordinator
)
super().__init__(name, unique_id, description, weather_coordinator)
self._weather_coordinator = weather_coordinator
@property
@ -95,5 +137,5 @@ class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor):
"""Return the state of the device."""
forecasts = self._weather_coordinator.data.get(ATTR_API_FORECAST)
if forecasts is not None and len(forecasts) > 0:
return forecasts[0].get(self._sensor_type, None)
return forecasts[0].get(self.entity_description.key, None)
return None