From 2dc86364f33411e18e077172fb8a860d559c5a99 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 24 Jul 2023 20:49:40 +0200 Subject: [PATCH] Migrate TPLink to has entity name (#96246) --- homeassistant/components/tplink/entity.py | 3 +- homeassistant/components/tplink/light.py | 1 + homeassistant/components/tplink/sensor.py | 13 ++------- homeassistant/components/tplink/strings.json | 18 ++++++++++++ homeassistant/components/tplink/switch.py | 30 ++++++++++++++++++-- tests/components/tplink/test_switch.py | 4 +-- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 01e124dea1a..4bf076a59bc 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -32,13 +32,14 @@ def async_refresh_after( class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]): """Common base class for all coordinated tplink entities.""" + _attr_has_entity_name = True + def __init__( self, device: SmartDevice, coordinator: TPLinkDataUpdateCoordinator ) -> None: """Initialize the switch.""" super().__init__(coordinator) self.device: SmartDevice = device - self._attr_name = self.device.alias self._attr_unique_id = self.device.device_id @property diff --git a/homeassistant/components/tplink/light.py b/homeassistant/components/tplink/light.py index e4f91f282f6..db7e6ff355e 100644 --- a/homeassistant/components/tplink/light.py +++ b/homeassistant/components/tplink/light.py @@ -162,6 +162,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity): """Representation of a TPLink Smart Bulb.""" _attr_supported_features = LightEntityFeature.TRANSITION + _attr_name = None device: SmartBulb diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index 7471ed8982b..ba4949434f7 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -46,6 +46,7 @@ class TPLinkSensorEntityDescription(SensorEntityDescription): ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( TPLinkSensorEntityDescription( key=ATTR_CURRENT_POWER_W, + translation_key="current_consumption", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, @@ -55,6 +56,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( ), TPLinkSensorEntityDescription( key=ATTR_TOTAL_ENERGY_KWH, + translation_key="total_consumption", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, @@ -64,6 +66,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( ), TPLinkSensorEntityDescription( key=ATTR_TODAY_ENERGY_KWH, + translation_key="today_consumption", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, @@ -75,7 +78,6 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, - name="Voltage", emeter_attr="voltage", precision=1, ), @@ -84,7 +86,6 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, - name="Current", emeter_attr="current", precision=2, ), @@ -155,14 +156,6 @@ class SmartPlugSensor(CoordinatedTPLinkEntity, SensorEntity): f"{legacy_device_id(self.device)}_{self.entity_description.key}" ) - @property - def name(self) -> str: - """Return the name of the Smart Plug. - - Overridden to include the description. - """ - return f"{self.device.alias} {self.entity_description.name}" - @property def native_value(self) -> float | None: """Return the sensors state.""" diff --git a/homeassistant/components/tplink/strings.json b/homeassistant/components/tplink/strings.json index 6daa5c9cb1a..750d422cd0d 100644 --- a/homeassistant/components/tplink/strings.json +++ b/homeassistant/components/tplink/strings.json @@ -25,6 +25,24 @@ "no_devices_found": "[%key:common::config_flow::abort::no_devices_found%]" } }, + "entity": { + "sensor": { + "current_consumption": { + "name": "Current consumption" + }, + "total_consumption": { + "name": "Total consumption" + }, + "today_consumption": { + "name": "Today's consumption" + } + }, + "switch": { + "led": { + "name": "LED" + } + } + }, "services": { "sequence_effect": { "name": "Sequence effect", diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index aa0616447cc..d82308a2e32 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -35,7 +35,7 @@ async def async_setup_entry( # Historically we only add the children if the device is a strip _LOGGER.debug("Initializing strip with %s sockets", len(device.children)) for child in device.children: - entities.append(SmartPlugSwitch(child, coordinator)) + entities.append(SmartPlugSwitchChild(device, coordinator, child)) elif device.is_plug: entities.append(SmartPlugSwitch(device, coordinator)) @@ -49,6 +49,7 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity): device: SmartPlug + _attr_translation_key = "led" _attr_entity_category = EntityCategory.CONFIG def __init__( @@ -57,7 +58,6 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity): """Initialize the LED switch.""" super().__init__(device, coordinator) - self._attr_name = f"{device.alias} LED" self._attr_unique_id = f"{self.device.mac}_led" @property @@ -103,3 +103,29 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity): async def async_turn_off(self, **kwargs: Any) -> None: """Turn the switch off.""" await self.device.turn_off() + + +class SmartPlugSwitchChild(SmartPlugSwitch): + """Representation of an individual plug of a TPLink Smart Plug strip.""" + + def __init__( + self, + device: SmartDevice, + coordinator: TPLinkDataUpdateCoordinator, + plug: SmartDevice, + ) -> None: + """Initialize the switch.""" + super().__init__(device, coordinator) + self._plug = plug + self._attr_unique_id = legacy_device_id(plug) + self._attr_name = plug.alias + + @async_refresh_after + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn the switch on.""" + await self._plug.turn_on() + + @async_refresh_after + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn the switch off.""" + await self._plug.turn_off() diff --git a/tests/components/tplink/test_switch.py b/tests/components/tplink/test_switch.py index ba4419500f4..1e5e03c0f37 100644 --- a/tests/components/tplink/test_switch.py +++ b/tests/components/tplink/test_switch.py @@ -147,7 +147,7 @@ async def test_strip(hass: HomeAssistant) -> None: assert hass.states.get("switch.my_strip") is None for plug_id in range(2): - entity_id = f"switch.plug{plug_id}" + entity_id = f"switch.my_strip_plug{plug_id}" state = hass.states.get(entity_id) assert state.state == STATE_ON @@ -176,7 +176,7 @@ async def test_strip_unique_ids(hass: HomeAssistant) -> None: await hass.async_block_till_done() for plug_id in range(2): - entity_id = f"switch.plug{plug_id}" + entity_id = f"switch.my_strip_plug{plug_id}" entity_registry = er.async_get(hass) assert ( entity_registry.async_get(entity_id).unique_id == f"PLUG{plug_id}DEVICEID"