From 4785810dc3811ad25b1b947cb4af8bd37682d2b8 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 24 Jun 2024 08:54:46 +0200 Subject: [PATCH] Migrate AEMET to has entity name (#120284) --- homeassistant/components/aemet/entity.py | 5 ++++- homeassistant/components/aemet/sensor.py | 13 +++++-------- homeassistant/components/aemet/weather.py | 17 ++++++++--------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/aemet/entity.py b/homeassistant/components/aemet/entity.py index f48eaa1593d..562d82fd9c7 100644 --- a/homeassistant/components/aemet/entity.py +++ b/homeassistant/components/aemet/entity.py @@ -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, diff --git a/homeassistant/components/aemet/sensor.py b/homeassistant/components/aemet/sensor.py index cafb9be8a70..83d490f7fe2 100644 --- a/homeassistant/components/aemet/sensor.py +++ b/homeassistant/components/aemet/sensor.py @@ -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 diff --git a/homeassistant/components/aemet/weather.py b/homeassistant/components/aemet/weather.py index 9c905941f62..341b81d71c4 100644 --- a/homeassistant/components/aemet/weather.py +++ b/homeassistant/components/aemet/weather.py @@ -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