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>
This commit is contained in:
Jan Rieger 2024-01-31 11:40:23 +01:00 committed by GitHub
parent a3352ce457
commit c587c69915
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 32 deletions

View File

@ -40,12 +40,12 @@ ATTR_VOLUME_SIZE = "Volume Size"
_SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = ( _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="status", key="status",
name="Status", translation_key="status",
icon="mdi:checkbox-marked-circle-outline", icon="mdi:checkbox-marked-circle-outline",
), ),
SensorEntityDescription( SensorEntityDescription(
key="system_temp", key="system_temp",
name="System Temperature", translation_key="system_temp",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
icon="mdi:thermometer", icon="mdi:thermometer",
@ -55,7 +55,7 @@ _SYSTEM_MON_COND: tuple[SensorEntityDescription, ...] = (
_CPU_MON_COND: tuple[SensorEntityDescription, ...] = ( _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="cpu_temp", key="cpu_temp",
name="CPU Temperature", translation_key="cpu_temp",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
icon="mdi:checkbox-marked-circle-outline", icon="mdi:checkbox-marked-circle-outline",
@ -64,7 +64,7 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="cpu_usage", key="cpu_usage",
name="CPU Usage", translation_key="cpu_usage",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip", icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -74,7 +74,7 @@ _CPU_MON_COND: tuple[SensorEntityDescription, ...] = (
_MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = ( _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="memory_free", key="memory_free",
name="Memory Available", translation_key="memory_free",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
@ -85,7 +85,7 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="memory_used", key="memory_used",
name="Memory Used", translation_key="memory_used",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
@ -96,7 +96,7 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="memory_percent_used", key="memory_percent_used",
name="Memory Usage", translation_key="memory_percent_used",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -106,12 +106,12 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
_NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = ( _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="network_link_status", key="network_link_status",
name="Network Link", translation_key="network_link_status",
icon="mdi:checkbox-marked-circle-outline", icon="mdi:checkbox-marked-circle-outline",
), ),
SensorEntityDescription( SensorEntityDescription(
key="network_tx", key="network_tx",
name="Network Up", translation_key="network_tx",
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
@ -122,7 +122,7 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="network_rx", key="network_rx",
name="Network Down", translation_key="network_rx",
native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
@ -135,13 +135,13 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
_DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = ( _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="drive_smart_status", key="drive_smart_status",
name="SMART Status", translation_key="drive_smart_status",
icon="mdi:checkbox-marked-circle-outline", icon="mdi:checkbox-marked-circle-outline",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="drive_temp", key="drive_temp",
name="Temperature", translation_key="drive_temp",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
icon="mdi:thermometer", icon="mdi:thermometer",
@ -152,7 +152,7 @@ _DRIVE_MON_COND: tuple[SensorEntityDescription, ...] = (
_VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = ( _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="volume_size_used", key="volume_size_used",
name="Used Space", translation_key="volume_size_used",
native_unit_of_measurement=UnitOfInformation.BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
@ -163,7 +163,7 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="volume_size_free", key="volume_size_free",
name="Free Space", translation_key="volume_size_free",
native_unit_of_measurement=UnitOfInformation.BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
@ -174,7 +174,7 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="volume_percentage_used", key="volume_percentage_used",
name="Volume Used", translation_key="volume_percentage_used",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -259,6 +259,8 @@ async def async_setup_entry(
class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity): class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
"""Base class for a QNAP sensor.""" """Base class for a QNAP sensor."""
_attr_has_entity_name = True
def __init__( def __init__(
self, self,
coordinator: QnapCoordinator, coordinator: QnapCoordinator,
@ -274,6 +276,7 @@ class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
self._attr_unique_id = f"{unique_id}_{description.key}" self._attr_unique_id = f"{unique_id}_{description.key}"
if monitor_device: if monitor_device:
self._attr_unique_id = f"{self._attr_unique_id}_{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( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)}, identifiers={(DOMAIN, unique_id)},
serial_number=unique_id, serial_number=unique_id,
@ -283,13 +286,6 @@ class QNAPSensor(CoordinatorEntity[QnapCoordinator], SensorEntity):
manufacturer="QNAP", 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): class QNAPCPUSensor(QNAPSensor):
"""A QNAP sensor that monitors CPU stats.""" """A QNAP sensor that monitors CPU stats."""
@ -405,16 +401,6 @@ class QNAPDriveSensor(QNAPSensor):
if self.entity_description.key == "drive_temp": if self.entity_description.key == "drive_temp":
return int(data["temp_c"]) if data["temp_c"] is not None else 0 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 @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""

View File

@ -22,5 +22,54 @@
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]" "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})"
}
}
} }
} }