Add entity name translations to System Monitor (#107952)

This commit is contained in:
G Johansson 2024-01-16 21:39:03 +01:00 committed by GitHub
parent 60ab360fe7
commit 0ba0f57439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 132 additions and 40 deletions

View File

@ -79,12 +79,14 @@ class SysMonitorSensorEntityDescription(SensorEntityDescription):
"""Description for System Monitor sensor entities.""" """Description for System Monitor sensor entities."""
mandatory_arg: bool = False mandatory_arg: bool = False
placeholder: str | None = None
SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"disk_free": SysMonitorSensorEntityDescription( "disk_free": SysMonitorSensorEntityDescription(
key="disk_free", key="disk_free",
name="Disk free", translation_key="disk_free",
placeholder="mount_point",
native_unit_of_measurement=UnitOfInformation.GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
@ -92,7 +94,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"disk_use": SysMonitorSensorEntityDescription( "disk_use": SysMonitorSensorEntityDescription(
key="disk_use", key="disk_use",
name="Disk use", translation_key="disk_use",
placeholder="mount_point",
native_unit_of_measurement=UnitOfInformation.GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
@ -100,49 +103,52 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"disk_use_percent": SysMonitorSensorEntityDescription( "disk_use_percent": SysMonitorSensorEntityDescription(
key="disk_use_percent", key="disk_use_percent",
name="Disk use (percent)", translation_key="disk_use_percent",
placeholder="mount_point",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"ipv4_address": SysMonitorSensorEntityDescription( "ipv4_address": SysMonitorSensorEntityDescription(
key="ipv4_address", key="ipv4_address",
name="IPv4 address", translation_key="ipv4_address",
placeholder="ip_address",
icon="mdi:ip-network", icon="mdi:ip-network",
mandatory_arg=True, mandatory_arg=True,
), ),
"ipv6_address": SysMonitorSensorEntityDescription( "ipv6_address": SysMonitorSensorEntityDescription(
key="ipv6_address", key="ipv6_address",
name="IPv6 address", translation_key="ipv6_address",
placeholder="ip_address",
icon="mdi:ip-network", icon="mdi:ip-network",
mandatory_arg=True, mandatory_arg=True,
), ),
"last_boot": SysMonitorSensorEntityDescription( "last_boot": SysMonitorSensorEntityDescription(
key="last_boot", key="last_boot",
name="Last boot", translation_key="last_boot",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
), ),
"load_15m": SysMonitorSensorEntityDescription( "load_15m": SysMonitorSensorEntityDescription(
key="load_15m", key="load_15m",
name="Load (15m)", translation_key="load_15m",
icon=get_cpu_icon(), icon=get_cpu_icon(),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"load_1m": SysMonitorSensorEntityDescription( "load_1m": SysMonitorSensorEntityDescription(
key="load_1m", key="load_1m",
name="Load (1m)", translation_key="load_1m",
icon=get_cpu_icon(), icon=get_cpu_icon(),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"load_5m": SysMonitorSensorEntityDescription( "load_5m": SysMonitorSensorEntityDescription(
key="load_5m", key="load_5m",
name="Load (5m)", translation_key="load_5m",
icon=get_cpu_icon(), icon=get_cpu_icon(),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"memory_free": SysMonitorSensorEntityDescription( "memory_free": SysMonitorSensorEntityDescription(
key="memory_free", key="memory_free",
name="Memory free", 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",
@ -150,7 +156,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"memory_use": SysMonitorSensorEntityDescription( "memory_use": SysMonitorSensorEntityDescription(
key="memory_use", key="memory_use",
name="Memory use", translation_key="memory_use",
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",
@ -158,14 +164,15 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"memory_use_percent": SysMonitorSensorEntityDescription( "memory_use_percent": SysMonitorSensorEntityDescription(
key="memory_use_percent", key="memory_use_percent",
name="Memory use (percent)", translation_key="memory_use_percent",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"network_in": SysMonitorSensorEntityDescription( "network_in": SysMonitorSensorEntityDescription(
key="network_in", key="network_in",
name="Network in", translation_key="network_in",
placeholder="interface",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:server-network", icon="mdi:server-network",
@ -174,7 +181,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"network_out": SysMonitorSensorEntityDescription( "network_out": SysMonitorSensorEntityDescription(
key="network_out", key="network_out",
name="Network out", translation_key="network_out",
placeholder="interface",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:server-network", icon="mdi:server-network",
@ -183,21 +191,24 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"packets_in": SysMonitorSensorEntityDescription( "packets_in": SysMonitorSensorEntityDescription(
key="packets_in", key="packets_in",
name="Packets in", translation_key="packets_in",
placeholder="interface",
icon="mdi:server-network", icon="mdi:server-network",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
mandatory_arg=True, mandatory_arg=True,
), ),
"packets_out": SysMonitorSensorEntityDescription( "packets_out": SysMonitorSensorEntityDescription(
key="packets_out", key="packets_out",
name="Packets out", translation_key="packets_out",
placeholder="interface",
icon="mdi:server-network", icon="mdi:server-network",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
mandatory_arg=True, mandatory_arg=True,
), ),
"throughput_network_in": SysMonitorSensorEntityDescription( "throughput_network_in": SysMonitorSensorEntityDescription(
key="throughput_network_in", key="throughput_network_in",
name="Network throughput in", translation_key="throughput_network_in",
placeholder="interface",
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -205,7 +216,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"throughput_network_out": SysMonitorSensorEntityDescription( "throughput_network_out": SysMonitorSensorEntityDescription(
key="throughput_network_out", key="throughput_network_out",
name="Network throughput out", translation_key="throughput_network_out",
placeholder="interface",
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE, device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -213,27 +225,28 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"process": SysMonitorSensorEntityDescription( "process": SysMonitorSensorEntityDescription(
key="process", key="process",
name="Process", translation_key="process",
placeholder="process",
icon=get_cpu_icon(), icon=get_cpu_icon(),
mandatory_arg=True, mandatory_arg=True,
), ),
"processor_use": SysMonitorSensorEntityDescription( "processor_use": SysMonitorSensorEntityDescription(
key="processor_use", key="processor_use",
name="Processor use", translation_key="processor_use",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon=get_cpu_icon(), icon=get_cpu_icon(),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"processor_temperature": SysMonitorSensorEntityDescription( "processor_temperature": SysMonitorSensorEntityDescription(
key="processor_temperature", key="processor_temperature",
name="Processor temperature", translation_key="processor_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"swap_free": SysMonitorSensorEntityDescription( "swap_free": SysMonitorSensorEntityDescription(
key="swap_free", key="swap_free",
name="Swap free", translation_key="swap_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:harddisk", icon="mdi:harddisk",
@ -241,7 +254,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"swap_use": SysMonitorSensorEntityDescription( "swap_use": SysMonitorSensorEntityDescription(
key="swap_use", key="swap_use",
name="Swap use", translation_key="swap_use",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE, device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
@ -249,7 +262,7 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
), ),
"swap_use_percent": SysMonitorSensorEntityDescription( "swap_use_percent": SysMonitorSensorEntityDescription(
key="swap_use_percent", key="swap_use_percent",
name="Swap use (percent)", translation_key="swap_use_percent",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -587,7 +600,10 @@ class SystemMonitorSensor(SensorEntity):
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = sensor_description 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._attr_unique_id: str = slugify(f"{sensor_description.key}_{argument}")
self._sensor_registry = sensor_registry self._sensor_registry = sensor_registry
self._argument: str = argument self._argument: str = argument

View File

@ -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"
}
}
} }
} }

View File

@ -23,37 +23,37 @@
# name: test_sensor[System Monitor Disk free /media/share - state] # name: test_sensor[System Monitor Disk free /media/share - state]
'0.2' '0.2'
# --- # ---
# name: test_sensor[System Monitor Disk use (percent) / - attributes] # name: test_sensor[System Monitor Disk usage / - attributes]
ReadOnlyDict({ ReadOnlyDict({
'friendly_name': 'System Monitor Disk use (percent) /', 'friendly_name': 'System Monitor Disk usage /',
'icon': 'mdi:harddisk', 'icon': 'mdi:harddisk',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%', 'unit_of_measurement': '%',
}) })
# --- # ---
# name: test_sensor[System Monitor Disk use (percent) / - state] # name: test_sensor[System Monitor Disk usage / - state]
'60.0' '60.0'
# --- # ---
# name: test_sensor[System Monitor Disk use (percent) /home/notexist/ - attributes] # name: test_sensor[System Monitor Disk usage /home/notexist/ - attributes]
ReadOnlyDict({ ReadOnlyDict({
'friendly_name': 'System Monitor Disk use (percent) /home/notexist/', 'friendly_name': 'System Monitor Disk usage /home/notexist/',
'icon': 'mdi:harddisk', 'icon': 'mdi:harddisk',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%', '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' '60.0'
# --- # ---
# name: test_sensor[System Monitor Disk use (percent) /media/share - attributes] # name: test_sensor[System Monitor Disk usage /media/share - attributes]
ReadOnlyDict({ ReadOnlyDict({
'friendly_name': 'System Monitor Disk use (percent) /media/share', 'friendly_name': 'System Monitor Disk usage /media/share',
'icon': 'mdi:harddisk', 'icon': 'mdi:harddisk',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%', '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' '60.0'
# --- # ---
# name: test_sensor[System Monitor Disk use / - attributes] # name: test_sensor[System Monitor Disk use / - attributes]
@ -167,15 +167,15 @@
# name: test_sensor[System Monitor Memory free - state] # name: test_sensor[System Monitor Memory free - state]
'40.0' '40.0'
# --- # ---
# name: test_sensor[System Monitor Memory use (percent) - attributes] # name: test_sensor[System Monitor Memory usage - attributes]
ReadOnlyDict({ ReadOnlyDict({
'friendly_name': 'System Monitor Memory use (percent)', 'friendly_name': 'System Monitor Memory usage',
'icon': 'mdi:memory', 'icon': 'mdi:memory',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%', 'unit_of_measurement': '%',
}) })
# --- # ---
# name: test_sensor[System Monitor Memory use (percent) - state] # name: test_sensor[System Monitor Memory usage - state]
'40.0' '40.0'
# --- # ---
# name: test_sensor[System Monitor Memory use - attributes] # name: test_sensor[System Monitor Memory use - attributes]
@ -374,15 +374,15 @@
# name: test_sensor[System Monitor Swap free - state] # name: test_sensor[System Monitor Swap free - state]
'40.0' '40.0'
# --- # ---
# name: test_sensor[System Monitor Swap use (percent) - attributes] # name: test_sensor[System Monitor Swap usage - attributes]
ReadOnlyDict({ ReadOnlyDict({
'friendly_name': 'System Monitor Swap use (percent)', 'friendly_name': 'System Monitor Swap usage',
'icon': 'mdi:harddisk', 'icon': 'mdi:harddisk',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>, 'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%', 'unit_of_measurement': '%',
}) })
# --- # ---
# name: test_sensor[System Monitor Swap use (percent) - state] # name: test_sensor[System Monitor Swap usage - state]
'60.0' '60.0'
# --- # ---
# name: test_sensor[System Monitor Swap use - attributes] # name: test_sensor[System Monitor Swap use - attributes]