From 0ba0f57439eae802d49f3e7a68d91f5cc6ce9dc5 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 16 Jan 2024 21:39:03 +0100 Subject: [PATCH] Add entity name translations to System Monitor (#107952) --- .../components/systemmonitor/sensor.py | 66 ++++++++++------ .../components/systemmonitor/strings.json | 76 +++++++++++++++++++ .../systemmonitor/snapshots/test_sensor.ambr | 30 ++++---- 3 files changed, 132 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index 1a48e34d0e9..c7fdb4226d1 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -79,12 +79,14 @@ class SysMonitorSensorEntityDescription(SensorEntityDescription): """Description for System Monitor sensor entities.""" mandatory_arg: bool = False + placeholder: str | None = None SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { "disk_free": SysMonitorSensorEntityDescription( key="disk_free", - name="Disk free", + translation_key="disk_free", + placeholder="mount_point", native_unit_of_measurement=UnitOfInformation.GIBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:harddisk", @@ -92,7 +94,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "disk_use": SysMonitorSensorEntityDescription( key="disk_use", - name="Disk use", + translation_key="disk_use", + placeholder="mount_point", native_unit_of_measurement=UnitOfInformation.GIBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:harddisk", @@ -100,49 +103,52 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "disk_use_percent": SysMonitorSensorEntityDescription( key="disk_use_percent", - name="Disk use (percent)", + translation_key="disk_use_percent", + placeholder="mount_point", native_unit_of_measurement=PERCENTAGE, icon="mdi:harddisk", state_class=SensorStateClass.MEASUREMENT, ), "ipv4_address": SysMonitorSensorEntityDescription( key="ipv4_address", - name="IPv4 address", + translation_key="ipv4_address", + placeholder="ip_address", icon="mdi:ip-network", mandatory_arg=True, ), "ipv6_address": SysMonitorSensorEntityDescription( key="ipv6_address", - name="IPv6 address", + translation_key="ipv6_address", + placeholder="ip_address", icon="mdi:ip-network", mandatory_arg=True, ), "last_boot": SysMonitorSensorEntityDescription( key="last_boot", - name="Last boot", + translation_key="last_boot", device_class=SensorDeviceClass.TIMESTAMP, ), "load_15m": SysMonitorSensorEntityDescription( key="load_15m", - name="Load (15m)", + translation_key="load_15m", icon=get_cpu_icon(), state_class=SensorStateClass.MEASUREMENT, ), "load_1m": SysMonitorSensorEntityDescription( key="load_1m", - name="Load (1m)", + translation_key="load_1m", icon=get_cpu_icon(), state_class=SensorStateClass.MEASUREMENT, ), "load_5m": SysMonitorSensorEntityDescription( key="load_5m", - name="Load (5m)", + translation_key="load_5m", icon=get_cpu_icon(), state_class=SensorStateClass.MEASUREMENT, ), "memory_free": SysMonitorSensorEntityDescription( key="memory_free", - name="Memory free", + translation_key="memory_free", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:memory", @@ -150,7 +156,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "memory_use": SysMonitorSensorEntityDescription( key="memory_use", - name="Memory use", + translation_key="memory_use", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:memory", @@ -158,14 +164,15 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "memory_use_percent": SysMonitorSensorEntityDescription( key="memory_use_percent", - name="Memory use (percent)", + translation_key="memory_use_percent", native_unit_of_measurement=PERCENTAGE, icon="mdi:memory", state_class=SensorStateClass.MEASUREMENT, ), "network_in": SysMonitorSensorEntityDescription( key="network_in", - name="Network in", + translation_key="network_in", + placeholder="interface", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:server-network", @@ -174,7 +181,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "network_out": SysMonitorSensorEntityDescription( key="network_out", - name="Network out", + translation_key="network_out", + placeholder="interface", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:server-network", @@ -183,21 +191,24 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "packets_in": SysMonitorSensorEntityDescription( key="packets_in", - name="Packets in", + translation_key="packets_in", + placeholder="interface", icon="mdi:server-network", state_class=SensorStateClass.TOTAL_INCREASING, mandatory_arg=True, ), "packets_out": SysMonitorSensorEntityDescription( key="packets_out", - name="Packets out", + translation_key="packets_out", + placeholder="interface", icon="mdi:server-network", state_class=SensorStateClass.TOTAL_INCREASING, mandatory_arg=True, ), "throughput_network_in": SysMonitorSensorEntityDescription( key="throughput_network_in", - name="Network throughput in", + translation_key="throughput_network_in", + placeholder="interface", native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE, state_class=SensorStateClass.MEASUREMENT, @@ -205,7 +216,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "throughput_network_out": SysMonitorSensorEntityDescription( key="throughput_network_out", - name="Network throughput out", + translation_key="throughput_network_out", + placeholder="interface", native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE, state_class=SensorStateClass.MEASUREMENT, @@ -213,27 +225,28 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "process": SysMonitorSensorEntityDescription( key="process", - name="Process", + translation_key="process", + placeholder="process", icon=get_cpu_icon(), mandatory_arg=True, ), "processor_use": SysMonitorSensorEntityDescription( key="processor_use", - name="Processor use", + translation_key="processor_use", native_unit_of_measurement=PERCENTAGE, icon=get_cpu_icon(), state_class=SensorStateClass.MEASUREMENT, ), "processor_temperature": SysMonitorSensorEntityDescription( key="processor_temperature", - name="Processor temperature", + translation_key="processor_temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), "swap_free": SysMonitorSensorEntityDescription( key="swap_free", - name="Swap free", + translation_key="swap_free", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:harddisk", @@ -241,7 +254,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "swap_use": SysMonitorSensorEntityDescription( key="swap_use", - name="Swap use", + translation_key="swap_use", native_unit_of_measurement=UnitOfInformation.MEBIBYTES, device_class=SensorDeviceClass.DATA_SIZE, icon="mdi:harddisk", @@ -249,7 +262,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { ), "swap_use_percent": SysMonitorSensorEntityDescription( key="swap_use_percent", - name="Swap use (percent)", + translation_key="swap_use_percent", native_unit_of_measurement=PERCENTAGE, icon="mdi:harddisk", state_class=SensorStateClass.MEASUREMENT, @@ -587,7 +600,10 @@ class SystemMonitorSensor(SensorEntity): ) -> None: """Initialize the sensor.""" self.entity_description = sensor_description - self._attr_name: str = f"{sensor_description.name} {argument}".rstrip() + if self.entity_description.placeholder: + self._attr_translation_placeholders = { + self.entity_description.placeholder: argument + } self._attr_unique_id: str = slugify(f"{sensor_description.key}_{argument}") self._sensor_registry = sensor_registry self._argument: str = argument diff --git a/homeassistant/components/systemmonitor/strings.json b/homeassistant/components/systemmonitor/strings.json index 88ecad4b107..ff1fbc221ee 100644 --- a/homeassistant/components/systemmonitor/strings.json +++ b/homeassistant/components/systemmonitor/strings.json @@ -21,5 +21,81 @@ } } } + }, + "entity": { + "sensor": { + "disk_free": { + "name": "Disk free {mount_point}" + }, + "disk_use": { + "name": "Disk use {mount_point}" + }, + "disk_use_percent": { + "name": "Disk usage {mount_point}" + }, + "ipv4_address": { + "name": "IPv4 address {ip_address}" + }, + "ipv6_address": { + "name": "IPv6 address {ip_address}" + }, + "last_boot": { + "name": "Last boot" + }, + "load_15m": { + "name": "Load (15m)" + }, + "load_1m": { + "name": "Load (1m)" + }, + "load_5m": { + "name": "Load (5m)" + }, + "memory_free": { + "name": "Memory free" + }, + "memory_use": { + "name": "Memory use" + }, + "memory_use_percent": { + "name": "Memory usage" + }, + "network_in": { + "name": "Network in {interface}" + }, + "network_out": { + "name": "Network out {interface}" + }, + "packets_in": { + "name": "Packets in {interface}" + }, + "packets_out": { + "name": "Packets out {interface}" + }, + "throughput_network_in": { + "name": "Network throughput in {interface}" + }, + "throughput_network_out": { + "name": "Network throughput out {interface}" + }, + "process": { + "name": "Process {process}" + }, + "processor_use": { + "name": "Processor use" + }, + "processor_temperature": { + "name": "Processor temperature" + }, + "swap_free": { + "name": "Swap free" + }, + "swap_use": { + "name": "Swap use" + }, + "swap_use_percent": { + "name": "Swap usage" + } + } } } diff --git a/tests/components/systemmonitor/snapshots/test_sensor.ambr b/tests/components/systemmonitor/snapshots/test_sensor.ambr index be32e1f54ef..d39b23c8107 100644 --- a/tests/components/systemmonitor/snapshots/test_sensor.ambr +++ b/tests/components/systemmonitor/snapshots/test_sensor.ambr @@ -23,37 +23,37 @@ # name: test_sensor[System Monitor Disk free /media/share - state] '0.2' # --- -# name: test_sensor[System Monitor Disk use (percent) / - attributes] +# name: test_sensor[System Monitor Disk usage / - attributes] ReadOnlyDict({ - 'friendly_name': 'System Monitor Disk use (percent) /', + 'friendly_name': 'System Monitor Disk usage /', 'icon': 'mdi:harddisk', 'state_class': , 'unit_of_measurement': '%', }) # --- -# name: test_sensor[System Monitor Disk use (percent) / - state] +# name: test_sensor[System Monitor Disk usage / - state] '60.0' # --- -# name: test_sensor[System Monitor Disk use (percent) /home/notexist/ - attributes] +# name: test_sensor[System Monitor Disk usage /home/notexist/ - attributes] ReadOnlyDict({ - 'friendly_name': 'System Monitor Disk use (percent) /home/notexist/', + 'friendly_name': 'System Monitor Disk usage /home/notexist/', 'icon': 'mdi:harddisk', 'state_class': , 'unit_of_measurement': '%', }) # --- -# name: test_sensor[System Monitor Disk use (percent) /home/notexist/ - state] +# name: test_sensor[System Monitor Disk usage /home/notexist/ - state] '60.0' # --- -# name: test_sensor[System Monitor Disk use (percent) /media/share - attributes] +# name: test_sensor[System Monitor Disk usage /media/share - attributes] ReadOnlyDict({ - 'friendly_name': 'System Monitor Disk use (percent) /media/share', + 'friendly_name': 'System Monitor Disk usage /media/share', 'icon': 'mdi:harddisk', 'state_class': , 'unit_of_measurement': '%', }) # --- -# name: test_sensor[System Monitor Disk use (percent) /media/share - state] +# name: test_sensor[System Monitor Disk usage /media/share - state] '60.0' # --- # name: test_sensor[System Monitor Disk use / - attributes] @@ -167,15 +167,15 @@ # name: test_sensor[System Monitor Memory free - state] '40.0' # --- -# name: test_sensor[System Monitor Memory use (percent) - attributes] +# name: test_sensor[System Monitor Memory usage - attributes] ReadOnlyDict({ - 'friendly_name': 'System Monitor Memory use (percent)', + 'friendly_name': 'System Monitor Memory usage', 'icon': 'mdi:memory', 'state_class': , 'unit_of_measurement': '%', }) # --- -# name: test_sensor[System Monitor Memory use (percent) - state] +# name: test_sensor[System Monitor Memory usage - state] '40.0' # --- # name: test_sensor[System Monitor Memory use - attributes] @@ -374,15 +374,15 @@ # name: test_sensor[System Monitor Swap free - state] '40.0' # --- -# name: test_sensor[System Monitor Swap use (percent) - attributes] +# name: test_sensor[System Monitor Swap usage - attributes] ReadOnlyDict({ - 'friendly_name': 'System Monitor Swap use (percent)', + 'friendly_name': 'System Monitor Swap usage', 'icon': 'mdi:harddisk', 'state_class': , 'unit_of_measurement': '%', }) # --- -# name: test_sensor[System Monitor Swap use (percent) - state] +# name: test_sensor[System Monitor Swap usage - state] '60.0' # --- # name: test_sensor[System Monitor Swap use - attributes]