From c587c699158d36dbf0b61690187e4463b612d2c7 Mon Sep 17 00:00:00 2001
From: Jan Rieger <jrieger@users.noreply.github.com>
Date: Wed, 31 Jan 2024 11:40:23 +0100
Subject: [PATCH] Migrate QNAP to has entity name (#107232)

* Migrate QNAP to has entity name

* Update homeassistant/components/qnap/strings.json

Co-authored-by: disforw <github@benzona.net>

* Apply suggestions from code review

Co-authored-by: disforw <github@benzona.net>

---------

Co-authored-by: disforw <github@benzona.net>
---
 homeassistant/components/qnap/sensor.py    | 50 ++++++++--------------
 homeassistant/components/qnap/strings.json | 49 +++++++++++++++++++++
 2 files changed, 67 insertions(+), 32 deletions(-)

diff --git a/homeassistant/components/qnap/sensor.py b/homeassistant/components/qnap/sensor.py
index e84124a96bd..f0d34f7c697 100644
--- a/homeassistant/components/qnap/sensor.py
+++ b/homeassistant/components/qnap/sensor.py
@@ -40,12 +40,12 @@ ATTR_VOLUME_SIZE = "Volume Size"
 _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="status",
-        name="Status",
+        translation_key="status",
         icon="mdi:checkbox-marked-circle-outline",
     ),
     SensorEntityDescription(
         key="system_temp",
-        name="System Temperature",
+        translation_key="system_temp",
         native_unit_of_measurement=UnitOfTemperature.CELSIUS,
         device_class=SensorDeviceClass.TEMPERATURE,
         icon="mdi:thermometer",
@@ -55,7 +55,7 @@ _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
 _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="cpu_temp",
-        name="CPU Temperature",
+        translation_key="cpu_temp",
         native_unit_of_measurement=UnitOfTemperature.CELSIUS,
         device_class=SensorDeviceClass.TEMPERATURE,
         icon="mdi:checkbox-marked-circle-outline",
@@ -64,7 +64,7 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="cpu_usage",
-        name="CPU Usage",
+        translation_key="cpu_usage",
         native_unit_of_measurement=PERCENTAGE,
         icon="mdi:chip",
         state_class=SensorStateClass.MEASUREMENT,
@@ -74,7 +74,7 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
 _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="memory_free",
-        name="Memory Available",
+        translation_key="memory_free",
         native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
         device_class=SensorDeviceClass.DATA_SIZE,
         icon="mdi:memory",
@@ -85,7 +85,7 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="memory_used",
-        name="Memory Used",
+        translation_key="memory_used",
         native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
         device_class=SensorDeviceClass.DATA_SIZE,
         icon="mdi:memory",
@@ -96,7 +96,7 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="memory_percent_used",
-        name="Memory Usage",
+        translation_key="memory_percent_used",
         native_unit_of_measurement=PERCENTAGE,
         icon="mdi:memory",
         state_class=SensorStateClass.MEASUREMENT,
@@ -106,12 +106,12 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
 _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="network_link_status",
-        name="Network Link",
+        translation_key="network_link_status",
         icon="mdi:checkbox-marked-circle-outline",
     ),
     SensorEntityDescription(
         key="network_tx",
-        name="Network Up",
+        translation_key="network_tx",
         native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
         device_class=SensorDeviceClass.DATA_RATE,
         icon="mdi:upload",
@@ -122,7 +122,7 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="network_rx",
-        name="Network Down",
+        translation_key="network_rx",
         native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
         device_class=SensorDeviceClass.DATA_RATE,
         icon="mdi:download",
@@ -135,13 +135,13 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
 _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="drive_smart_status",
-        name="SMART Status",
+        translation_key="drive_smart_status",
         icon="mdi:checkbox-marked-circle-outline",
         entity_registry_enabled_default=False,
     ),
     SensorEntityDescription(
         key="drive_temp",
-        name="Temperature",
+        translation_key="drive_temp",
         native_unit_of_measurement=UnitOfTemperature.CELSIUS,
         device_class=SensorDeviceClass.TEMPERATURE,
         icon="mdi:thermometer",
@@ -152,7 +152,7 @@ _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
 _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
     SensorEntityDescription(
         key="volume_size_used",
-        name="Used Space",
+        translation_key="volume_size_used",
         native_unit_of_measurement=UnitOfInformation.BYTES,
         device_class=SensorDeviceClass.DATA_SIZE,
         icon="mdi:chart-pie",
@@ -163,7 +163,7 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="volume_size_free",
-        name="Free Space",
+        translation_key="volume_size_free",
         native_unit_of_measurement=UnitOfInformation.BYTES,
         device_class=SensorDeviceClass.DATA_SIZE,
         icon="mdi:chart-pie",
@@ -174,7 +174,7 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
     ),
     SensorEntityDescription(
         key="volume_percentage_used",
-        name="Volume Used",
+        translation_key="volume_percentage_used",
         native_unit_of_measurement=PERCENTAGE,
         icon="mdi:chart-pie",
         state_class=SensorStateClass.MEASUREMENT,
@@ -259,6 +259,8 @@ async def async_setup_entry(
 class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
     """Base class for a QNAP sensor."""
 
+    _attr_has_entity_name = True
+
     def __init__(
         self,
         coordinator: QnapCoordinator,
@@ -274,6 +276,7 @@ class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
         self._attr_unique_id = f"{unique_id}_{description.key}"
         if monitor_device:
             self._attr_unique_id = f"{self._attr_unique_id}_{monitor_device}"
+            self._attr_translation_placeholders = {"monitor_device": monitor_device}
         self._attr_device_info = DeviceInfo(
             identifiers={(DOMAIN, unique_id)},
             serial_number=unique_id,
@@ -283,13 +286,6 @@ class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
             manufacturer="QNAP",
         )
 
-    @property
-    def name(self):
-        """Return the name of the sensor, if any."""
-        if self.monitor_device is not None:
-            return f"{self.device_name} {self.entity_description.name} ({self.monitor_device})"
-        return f"{self.device_name} {self.entity_description.name}"
-
 
 class QNAPCPUSensor(QNAPSensor):
     """A QNAP sensor that monitors CPU stats."""
@@ -405,16 +401,6 @@ class QNAPDriveSensor(QNAPSensor):
         if self.entity_description.key == "drive_temp":
             return int(data["temp_c"]) if data["temp_c"] is not None else 0
 
-    @property
-    def name(self):
-        """Return the name of the sensor, if any."""
-        server_name = self.coordinator.data["system_stats"]["system"]["name"]
-
-        return (
-            f"{server_name} {self.entity_description.name} (Drive"
-            f" {self.monitor_device})"
-        )
-
     @property
     def extra_state_attributes(self):
         """Return the state attributes."""
diff --git a/homeassistant/components/qnap/strings.json b/homeassistant/components/qnap/strings.json
index d535b9f0e87..ddceb487e2d 100644
--- a/homeassistant/components/qnap/strings.json
+++ b/homeassistant/components/qnap/strings.json
@@ -22,5 +22,54 @@
       "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
       "unknown": "[%key:common::config_flow::error::unknown%]"
     }
+  },
+  "entity": {
+    "sensor": {
+      "status": {
+        "name": "Status"
+      },
+      "system_temp": {
+        "name": "System temperature"
+      },
+      "cpu_temp": {
+        "name": "CPU temperature"
+      },
+      "cpu_usage": {
+        "name": "CPU usage"
+      },
+      "memory_free": {
+        "name": "Memory available"
+      },
+      "memory_used": {
+        "name": "Memory used"
+      },
+      "memory_percent_used": {
+        "name": "Memory usage"
+      },
+      "network_link_status": {
+        "name": "{monitor_device} link"
+      },
+      "network_tx": {
+        "name": "{monitor_device} upload"
+      },
+      "network_rx": {
+        "name": "{monitor_device} download"
+      },
+      "drive_smart_status": {
+        "name": "Drive {monitor_device} status"
+      },
+      "drive_temp": {
+        "name": "Drive {monitor_device} temperature"
+      },
+      "volume_size_used": {
+        "name": "Used space ({monitor_device})"
+      },
+      "volume_size_free": {
+        "name": "Free space ({monitor_device})"
+      },
+      "volume_percentage_used": {
+        "name": "Volume used ({monitor_device})"
+      }
+    }
   }
 }