From c7db8a0bee3c9f568a5c3ad1946c8ba69a2c8228 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 24 Jan 2024 08:23:39 +0100 Subject: [PATCH] Add translation placeholders for TPLink power strip (#108710) --- homeassistant/components/tplink/entity.py | 2 +- homeassistant/components/tplink/sensor.py | 21 ++++++++++++++------ homeassistant/components/tplink/strings.json | 15 ++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 577e1995d4a..987ac455ae1 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -40,7 +40,7 @@ class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]): """Initialize the switch.""" super().__init__(coordinator) self.device: SmartDevice = device - self._attr_unique_id = self.device.device_id + self._attr_unique_id = device.device_id self._attr_device_info = DeviceInfo( connections={(dr.CONNECTION_NETWORK_MAC, device.mac)}, identifiers={(DOMAIN, str(device.device_id))}, diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index e5f7ae332ec..a3bb35840b2 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -108,11 +108,13 @@ def async_emeter_from_device( def _async_sensors_for_device( - device: SmartDevice, coordinator: TPLinkDataUpdateCoordinator + device: SmartDevice, + coordinator: TPLinkDataUpdateCoordinator, + has_parent: bool = False, ) -> list[SmartPlugSensor]: """Generate the sensors for the device.""" return [ - SmartPlugSensor(device, coordinator, description) + SmartPlugSensor(device, coordinator, description, has_parent) for description in ENERGY_SENSORS if async_emeter_from_device(device, description) is not None ] @@ -136,7 +138,7 @@ async def async_setup_entry( # Historically we only add the children if the device is a strip for idx, child in enumerate(parent.children): entities.extend( - _async_sensors_for_device(child, children_coordinators[idx]) + _async_sensors_for_device(child, children_coordinators[idx], True) ) else: entities.extend(_async_sensors_for_device(parent, parent_coordinator)) @@ -154,13 +156,20 @@ class SmartPlugSensor(CoordinatedTPLinkEntity, SensorEntity): device: SmartDevice, coordinator: TPLinkDataUpdateCoordinator, description: TPLinkSensorEntityDescription, + has_parent: bool = False, ) -> None: """Initialize the switch.""" super().__init__(device, coordinator) self.entity_description = description - self._attr_unique_id = ( - f"{legacy_device_id(self.device)}_{self.entity_description.key}" - ) + self._attr_unique_id = f"{legacy_device_id(device)}_{description.key}" + if has_parent: + assert device.alias + self._attr_translation_placeholders = {"device_name": device.alias} + if description.translation_key: + self._attr_translation_key = f"{description.translation_key}_child" + else: + assert description.device_class + self._attr_translation_key = f"{description.device_class.value}_child" @property def native_value(self) -> float | None: diff --git a/homeassistant/components/tplink/strings.json b/homeassistant/components/tplink/strings.json index 3c4711d1632..4aa4a3856bd 100644 --- a/homeassistant/components/tplink/strings.json +++ b/homeassistant/components/tplink/strings.json @@ -67,6 +67,21 @@ }, "today_consumption": { "name": "Today's consumption" + }, + "current_consumption_child": { + "name": "{device_name} current consumption" + }, + "total_consumption_child": { + "name": "{device_name} total consumption" + }, + "today_consumption_child": { + "name": "{device_name} today's consumption" + }, + "current_child": { + "name": "{device_name} current" + }, + "voltage_child": { + "name": "{device_name} voltage" } }, "switch": {