Migrate AEMET to has entity name (#120284)
parent
fa9bced6b0
commit
4785810dc3
|
@ -10,13 +10,16 @@ from homeassistant.components.weather import Forecast
|
|||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import ATTRIBUTION, DOMAIN
|
||||
from .coordinator import WeatherUpdateCoordinator
|
||||
|
||||
|
||||
class AemetEntity(CoordinatorEntity[WeatherUpdateCoordinator]):
|
||||
"""Define an AEMET entity."""
|
||||
|
||||
_attr_attribution = ATTRIBUTION
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: WeatherUpdateCoordinator,
|
||||
|
|
|
@ -43,7 +43,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
DEGREE,
|
||||
PERCENTAGE,
|
||||
|
@ -86,7 +85,6 @@ from .const import (
|
|||
ATTR_API_WIND_BEARING,
|
||||
ATTR_API_WIND_MAX_SPEED,
|
||||
ATTR_API_WIND_SPEED,
|
||||
ATTRIBUTION,
|
||||
CONDITIONS_MAP,
|
||||
)
|
||||
from .coordinator import WeatherUpdateCoordinator
|
||||
|
@ -366,12 +364,15 @@ async def async_setup_entry(
|
|||
name = domain_data.name
|
||||
coordinator = domain_data.coordinator
|
||||
|
||||
unique_id = config_entry.unique_id
|
||||
assert unique_id is not None
|
||||
|
||||
async_add_entities(
|
||||
AemetSensor(
|
||||
name,
|
||||
coordinator,
|
||||
description,
|
||||
config_entry,
|
||||
unique_id,
|
||||
)
|
||||
for description in FORECAST_SENSORS + WEATHER_SENSORS
|
||||
if dict_nested_value(coordinator.data["lib"], description.keys) is not None
|
||||
|
@ -381,7 +382,6 @@ async def async_setup_entry(
|
|||
class AemetSensor(AemetEntity, SensorEntity):
|
||||
"""Implementation of an AEMET OpenData sensor."""
|
||||
|
||||
_attr_attribution = ATTRIBUTION
|
||||
entity_description: AemetSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
|
@ -389,14 +389,11 @@ class AemetSensor(AemetEntity, SensorEntity):
|
|||
name: str,
|
||||
coordinator: WeatherUpdateCoordinator,
|
||||
description: AemetSensorEntityDescription,
|
||||
config_entry: ConfigEntry,
|
||||
unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
assert config_entry.unique_id is not None
|
||||
unique_id = config_entry.unique_id
|
||||
super().__init__(coordinator, name, unique_id)
|
||||
self.entity_description = description
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
self._attr_unique_id = f"{unique_id}-{description.key}"
|
||||
|
||||
@property
|
||||
|
|
|
@ -28,7 +28,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import AemetConfigEntry
|
||||
from .const import ATTRIBUTION, CONDITIONS_MAP
|
||||
from .const import CONDITIONS_MAP
|
||||
from .coordinator import WeatherUpdateCoordinator
|
||||
from .entity import AemetEntity
|
||||
|
||||
|
@ -43,10 +43,10 @@ async def async_setup_entry(
|
|||
name = domain_data.name
|
||||
weather_coordinator = domain_data.coordinator
|
||||
|
||||
async_add_entities(
|
||||
[AemetWeather(name, config_entry.unique_id, weather_coordinator)],
|
||||
False,
|
||||
)
|
||||
unique_id = config_entry.unique_id
|
||||
assert unique_id is not None
|
||||
|
||||
async_add_entities([AemetWeather(name, unique_id, weather_coordinator)])
|
||||
|
||||
|
||||
class AemetWeather(
|
||||
|
@ -55,7 +55,6 @@ class AemetWeather(
|
|||
):
|
||||
"""Implementation of an AEMET OpenData weather."""
|
||||
|
||||
_attr_attribution = ATTRIBUTION
|
||||
_attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
|
||||
_attr_native_pressure_unit = UnitOfPressure.HPA
|
||||
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS
|
||||
|
@ -63,16 +62,16 @@ class AemetWeather(
|
|||
_attr_supported_features = (
|
||||
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
|
||||
)
|
||||
_attr_name = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
unique_id,
|
||||
name: str,
|
||||
unique_id: str,
|
||||
coordinator: WeatherUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator, name, unique_id)
|
||||
self._attr_name = name
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
@property
|
||||
|
|
Loading…
Reference in New Issue