From 8675bc6554bf0248c3cd88ab42417c0ddbb83d6b Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 18 Jul 2023 20:56:50 +0200 Subject: [PATCH] Migrate Tradfri to has entity name (#96248) --- homeassistant/components/tradfri/base_class.py | 3 ++- homeassistant/components/tradfri/cover.py | 2 ++ homeassistant/components/tradfri/fan.py | 1 + homeassistant/components/tradfri/light.py | 1 + homeassistant/components/tradfri/sensor.py | 8 ++------ homeassistant/components/tradfri/strings.json | 10 ++++++++++ homeassistant/components/tradfri/switch.py | 2 ++ tests/components/tradfri/test_sensor.py | 4 ++-- 8 files changed, 22 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/tradfri/base_class.py b/homeassistant/components/tradfri/base_class.py index 5a84ad5719c..c7154c19f15 100644 --- a/homeassistant/components/tradfri/base_class.py +++ b/homeassistant/components/tradfri/base_class.py @@ -37,6 +37,8 @@ def handle_error( class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]): """Base Tradfri device.""" + _attr_has_entity_name = True + def __init__( self, device_coordinator: TradfriDeviceDataUpdateCoordinator, @@ -52,7 +54,6 @@ class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]): self._device_id = self._device.id self._api = handle_error(api) - self._attr_name = self._device.name self._attr_unique_id = f"{self._gateway_id}-{self._device.id}" diff --git a/homeassistant/components/tradfri/cover.py b/homeassistant/components/tradfri/cover.py index 976a48906fc..c51918b4a4f 100644 --- a/homeassistant/components/tradfri/cover.py +++ b/homeassistant/components/tradfri/cover.py @@ -40,6 +40,8 @@ async def async_setup_entry( class TradfriCover(TradfriBaseEntity, CoverEntity): """The platform class required by Home Assistant.""" + _attr_name = None + def __init__( self, device_coordinator: TradfriDeviceDataUpdateCoordinator, diff --git a/homeassistant/components/tradfri/fan.py b/homeassistant/components/tradfri/fan.py index d6bb91a4979..a26dfa1d9a0 100644 --- a/homeassistant/components/tradfri/fan.py +++ b/homeassistant/components/tradfri/fan.py @@ -54,6 +54,7 @@ async def async_setup_entry( class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity): """The platform class required by Home Assistant.""" + _attr_name = None _attr_supported_features = FanEntityFeature.PRESET_MODE | FanEntityFeature.SET_SPEED def __init__( diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index 32160c6a130..df35301b373 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -49,6 +49,7 @@ async def async_setup_entry( class TradfriLight(TradfriBaseEntity, LightEntity): """The platform class required by Home Assistant.""" + _attr_name = None _attr_supported_features = LightEntityFeature.TRANSITION def __init__( diff --git a/homeassistant/components/tradfri/sensor.py b/homeassistant/components/tradfri/sensor.py index 3eb4d72effd..383eec8a8fb 100644 --- a/homeassistant/components/tradfri/sensor.py +++ b/homeassistant/components/tradfri/sensor.py @@ -24,7 +24,6 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import UNDEFINED from .base_class import TradfriBaseEntity from .const import ( @@ -89,7 +88,7 @@ SENSOR_DESCRIPTIONS_BATTERY: tuple[TradfriSensorEntityDescription, ...] = ( SENSOR_DESCRIPTIONS_FAN: tuple[TradfriSensorEntityDescription, ...] = ( TradfriSensorEntityDescription( key="aqi", - name="air quality", + translation_key="aqi", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, icon="mdi:air-filter", @@ -97,7 +96,7 @@ SENSOR_DESCRIPTIONS_FAN: tuple[TradfriSensorEntityDescription, ...] = ( ), TradfriSensorEntityDescription( key="filter_life_remaining", - name="filter time left", + translation_key="filter_life_remaining", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfTime.HOURS, icon="mdi:clock-outline", @@ -203,9 +202,6 @@ class TradfriSensor(TradfriBaseEntity, SensorEntity): self._attr_unique_id = f"{self._attr_unique_id}-{description.key}" - if description.name is not UNDEFINED: - self._attr_name = f"{self._attr_name}: {description.name}" - self._refresh() # Set initial state def _refresh(self) -> None: diff --git a/homeassistant/components/tradfri/strings.json b/homeassistant/components/tradfri/strings.json index 34d7e89929a..0a9a86bd23a 100644 --- a/homeassistant/components/tradfri/strings.json +++ b/homeassistant/components/tradfri/strings.json @@ -20,5 +20,15 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]" } + }, + "entity": { + "sensor": { + "aqi": { + "name": "Air quality" + }, + "filter_life_remaining": { + "name": "Filter time left" + } + } } } diff --git a/homeassistant/components/tradfri/switch.py b/homeassistant/components/tradfri/switch.py index e0e2467ca4b..2f6f1996157 100644 --- a/homeassistant/components/tradfri/switch.py +++ b/homeassistant/components/tradfri/switch.py @@ -40,6 +40,8 @@ async def async_setup_entry( class TradfriSwitch(TradfriBaseEntity, SwitchEntity): """The platform class required by Home Assistant.""" + _attr_name = None + def __init__( self, device_coordinator: TradfriDeviceDataUpdateCoordinator, diff --git a/tests/components/tradfri/test_sensor.py b/tests/components/tradfri/test_sensor.py index 23391c8e875..d301638ec5d 100644 --- a/tests/components/tradfri/test_sensor.py +++ b/tests/components/tradfri/test_sensor.py @@ -61,7 +61,7 @@ async def test_battery_sensor( remote_control: Device, ) -> None: """Test that a battery sensor is correctly added.""" - entity_id = "sensor.test" + entity_id = "sensor.test_battery" device = remote_control mock_gateway.mock_devices.append(device) await setup_integration(hass) @@ -92,7 +92,7 @@ async def test_cover_battery_sensor( blind: Blind, ) -> None: """Test that a battery sensor is correctly added for a cover (blind).""" - entity_id = "sensor.test" + entity_id = "sensor.test_battery" device = blind.device mock_gateway.mock_devices.append(device) await setup_integration(hass)