Skip to content

Commit

Permalink
Migrate QNAP to has entity name (home-assistant#107232)
Browse files Browse the repository at this point in the history
* Migrate QNAP to has entity name

* Update homeassistant/components/qnap/strings.json

Co-authored-by: disforw <[email protected]>

* Apply suggestions from code review

Co-authored-by: disforw <[email protected]>

---------

Co-authored-by: disforw <[email protected]>
  • Loading branch information
jrieger and disforw authored Jan 31, 2024
1 parent a3352ce commit c587c69
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
50 changes: 18 additions & 32 deletions homeassistant/components/qnap/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
_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",
Expand All @@ -55,7 +55,7 @@
_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",
Expand All @@ -64,7 +64,7 @@
),
SensorEntityDescription(
key="cpu_usage",
name="CPU Usage",
translation_key="cpu_usage",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT,
Expand All @@ -74,7 +74,7 @@
_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",
Expand All @@ -85,7 +85,7 @@
),
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",
Expand All @@ -96,7 +96,7 @@
),
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,
Expand All @@ -106,12 +106,12 @@
_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",
Expand All @@ -122,7 +122,7 @@
),
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",
Expand All @@ -135,13 +135,13 @@
_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",
Expand All @@ -152,7 +152,7 @@
_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",
Expand All @@ -163,7 +163,7 @@
),
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",
Expand All @@ -174,7 +174,7 @@
),
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,
Expand Down Expand Up @@ -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,
Expand All @@ -274,6 +276,7 @@ def __init__(
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,
Expand All @@ -283,13 +286,6 @@ def __init__(
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."""
Expand Down Expand Up @@ -405,16 +401,6 @@ def native_value(self):
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."""
Expand Down
49 changes: 49 additions & 0 deletions homeassistant/components/qnap/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
}
}
}
}

0 comments on commit c587c69

Please sign in to comment.