Add entity translations to Deluge (#95184)

* Add entity translations to Deluge

* Update sensor.py

* Fix black
pull/94734/head^2
Joost Lekkerkerker 2023-06-25 12:59:04 +02:00 committed by GitHub
parent 528c206094
commit 58ddd17495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

@ -28,11 +28,11 @@ def get_state(data: dict[str, float], key: str) -> str | float:
download = data[DATA_KEYS[1]] - data[DATA_KEYS[3]]
if key == CURRENT_STATUS:
if upload > 0 and download > 0:
return "Up/Down"
return "seeding_and_downloading"
if upload > 0 and download == 0:
return "Seeding"
return "seeding"
if upload == 0 and download > 0:
return "Downloading"
return "downloading"
return STATE_IDLE
kb_spd = float(upload if key == UPLOAD_SPEED else download) / 1024
return round(kb_spd, 2 if kb_spd < 0.1 else 1)
@ -48,12 +48,14 @@ class DelugeSensorEntityDescription(SensorEntityDescription):
SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = (
DelugeSensorEntityDescription(
key=CURRENT_STATUS,
name="Status",
translation_key="status",
value=lambda data: get_state(data, CURRENT_STATUS),
device_class=SensorDeviceClass.ENUM,
options=["seeding_and_downloading", "seeding", "downloading", "idle"],
),
DelugeSensorEntityDescription(
key=DOWNLOAD_SPEED,
name="Down speed",
translation_key="download_speed",
device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
@ -61,7 +63,7 @@ SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = (
),
DelugeSensorEntityDescription(
key=UPLOAD_SPEED,
name="Up speed",
translation_key="upload_speed",
device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,

View File

@ -19,5 +19,24 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
}
},
"entity": {
"sensor": {
"status": {
"name": "Status",
"state": {
"seeding_and_downloading": "Up/Down",
"seeding": "Seeding",
"downloading": "Downloading",
"idle": "[%key:common::state::idle%]"
}
},
"download_speed": {
"name": "Download speed"
},
"upload_speed": {
"name": "Upload speed"
}
}
}
}

View File

@ -24,6 +24,8 @@ async def async_setup_entry(
class DelugeSwitch(DelugeEntity, SwitchEntity):
"""Representation of a Deluge switch."""
_attr_name = None
def __init__(self, coordinator: DelugeDataUpdateCoordinator) -> None:
"""Initialize the Deluge switch."""
super().__init__(coordinator)