Address late feedback on Deluge config flow (#71497)
Address late feedback on Deluge
This commit is contained in:
parent
4710ad07c4
commit
72dbca4f5b
3 changed files with 54 additions and 44 deletions
|
@ -1,12 +1,16 @@
|
||||||
"""Constants for the Deluge integration."""
|
"""Constants for the Deluge integration."""
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
CONF_WEB_PORT = "web_port"
|
CONF_WEB_PORT = "web_port"
|
||||||
|
CURRENT_STATUS = "current_status"
|
||||||
|
DATA_KEYS = ["upload_rate", "download_rate", "dht_upload_rate", "dht_download_rate"]
|
||||||
DEFAULT_NAME = "Deluge"
|
DEFAULT_NAME = "Deluge"
|
||||||
DEFAULT_RPC_PORT = 58846
|
DEFAULT_RPC_PORT = 58846
|
||||||
DEFAULT_WEB_PORT = 8112
|
DEFAULT_WEB_PORT = 8112
|
||||||
DHT_UPLOAD = 1000
|
DOMAIN: Final = "deluge"
|
||||||
DHT_DOWNLOAD = 1000
|
DOWNLOAD_SPEED = "download_speed"
|
||||||
DOMAIN = "deluge"
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
|
UPLOAD_SPEED = "upload_speed"
|
||||||
|
|
|
@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import LOGGER
|
from .const import DATA_KEYS, LOGGER
|
||||||
|
|
||||||
|
|
||||||
class DelugeDataUpdateCoordinator(DataUpdateCoordinator):
|
class DelugeDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
|
@ -38,16 +38,12 @@ class DelugeDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
"""Get the latest data from Deluge and updates the state."""
|
"""Get the latest data from Deluge and updates the state."""
|
||||||
data = {}
|
data = {}
|
||||||
try:
|
try:
|
||||||
data[Platform.SENSOR] = await self.hass.async_add_executor_job(
|
_data = await self.hass.async_add_executor_job(
|
||||||
self.api.call,
|
self.api.call,
|
||||||
"core.get_session_status",
|
"core.get_session_status",
|
||||||
[
|
DATA_KEYS,
|
||||||
"upload_rate",
|
|
||||||
"download_rate",
|
|
||||||
"dht_upload_rate",
|
|
||||||
"dht_download_rate",
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
data[Platform.SENSOR] = {k.decode(): v for k, v in _data.items()}
|
||||||
data[Platform.SWITCH] = await self.hass.async_add_executor_job(
|
data[Platform.SWITCH] = await self.hass.async_add_executor_job(
|
||||||
self.api.call, "core.get_torrents_status", {}, ["paused"]
|
self.api.call, "core.get_torrents_status", {}, ["paused"]
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
"""Support for monitoring the Deluge BitTorrent client API."""
|
"""Support for monitoring the Deluge BitTorrent client API."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
|
@ -13,25 +17,52 @@ from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from . import DelugeEntity
|
from . import DelugeEntity
|
||||||
from .const import DOMAIN
|
from .const import CURRENT_STATUS, DATA_KEYS, DOMAIN, DOWNLOAD_SPEED, UPLOAD_SPEED
|
||||||
from .coordinator import DelugeDataUpdateCoordinator
|
from .coordinator import DelugeDataUpdateCoordinator
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
||||||
SensorEntityDescription(
|
def get_state(data: dict[str, float], key: str) -> str | float:
|
||||||
key="current_status",
|
"""Get current download/upload state."""
|
||||||
|
upload = data[DATA_KEYS[0]] - data[DATA_KEYS[2]]
|
||||||
|
download = data[DATA_KEYS[1]] - data[DATA_KEYS[3]]
|
||||||
|
if key == CURRENT_STATUS:
|
||||||
|
if upload > 0 and download > 0:
|
||||||
|
return "Up/Down"
|
||||||
|
if upload > 0 and download == 0:
|
||||||
|
return "Seeding"
|
||||||
|
if upload == 0 and download > 0:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DelugeSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""Class to describe a Deluge sensor."""
|
||||||
|
|
||||||
|
value: Callable[[dict[str, float]], Any] = lambda val: val
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = (
|
||||||
|
DelugeSensorEntityDescription(
|
||||||
|
key=CURRENT_STATUS,
|
||||||
name="Status",
|
name="Status",
|
||||||
|
value=lambda data: get_state(data, CURRENT_STATUS),
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
DelugeSensorEntityDescription(
|
||||||
key="download_speed",
|
key=DOWNLOAD_SPEED,
|
||||||
name="Down Speed",
|
name="Down Speed",
|
||||||
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
value=lambda data: get_state(data, DOWNLOAD_SPEED),
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
DelugeSensorEntityDescription(
|
||||||
key="upload_speed",
|
key=UPLOAD_SPEED,
|
||||||
name="Up Speed",
|
name="Up Speed",
|
||||||
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
value=lambda data: get_state(data, UPLOAD_SPEED),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,10 +82,12 @@ async def async_setup_entry(
|
||||||
class DelugeSensor(DelugeEntity, SensorEntity):
|
class DelugeSensor(DelugeEntity, SensorEntity):
|
||||||
"""Representation of a Deluge sensor."""
|
"""Representation of a Deluge sensor."""
|
||||||
|
|
||||||
|
entity_description: DelugeSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DelugeDataUpdateCoordinator,
|
coordinator: DelugeDataUpdateCoordinator,
|
||||||
description: SensorEntityDescription,
|
description: DelugeSensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
@ -65,27 +98,4 @@ class DelugeSensor(DelugeEntity, SensorEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> StateType:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.coordinator.data:
|
return self.entity_description.value(self.coordinator.data[Platform.SENSOR])
|
||||||
data = self.coordinator.data[Platform.SENSOR]
|
|
||||||
upload = data[b"upload_rate"] - data[b"dht_upload_rate"]
|
|
||||||
download = data[b"download_rate"] - data[b"dht_download_rate"]
|
|
||||||
if self.entity_description.key == "current_status":
|
|
||||||
if data:
|
|
||||||
if upload > 0 and download > 0:
|
|
||||||
return "Up/Down"
|
|
||||||
if upload > 0 and download == 0:
|
|
||||||
return "Seeding"
|
|
||||||
if upload == 0 and download > 0:
|
|
||||||
return "Downloading"
|
|
||||||
return STATE_IDLE
|
|
||||||
|
|
||||||
if data:
|
|
||||||
if self.entity_description.key == "download_speed":
|
|
||||||
kb_spd = float(download)
|
|
||||||
kb_spd = kb_spd / 1024
|
|
||||||
return round(kb_spd, 2 if kb_spd < 0.1 else 1)
|
|
||||||
if self.entity_description.key == "upload_speed":
|
|
||||||
kb_spd = float(upload)
|
|
||||||
kb_spd = kb_spd / 1024
|
|
||||||
return round(kb_spd, 2 if kb_spd < 0.1 else 1)
|
|
||||||
return None
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue