-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add protocol upload / download sensors to Deluge (#119203)
* Add Protocol Upload/Download for Deluge * add unit test and fix typo in sensor.py * remove unneeded import * rename/unify the translation keys and entries in const.py * split out const.py items into DelugeSensorType to avoid confusion with DelugeGetSessionStatusKeys * change DelugeGetSessionStatusKeys to be a regular enum to satisfy mypy
- Loading branch information
Showing
6 changed files
with
134 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
"""Constants for the Deluge integration.""" | ||
|
||
import enum | ||
import logging | ||
from typing import Final | ||
|
||
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_RPC_PORT = 58846 | ||
DEFAULT_WEB_PORT = 8112 | ||
DOMAIN: Final = "deluge" | ||
DOWNLOAD_SPEED = "download_speed" | ||
|
||
LOGGER = logging.getLogger(__package__) | ||
|
||
UPLOAD_SPEED = "upload_speed" | ||
|
||
class DelugeGetSessionStatusKeys(enum.Enum): | ||
"""Enum representing the keys that get passed into the Deluge RPC `core.get_session_status` xml rpc method. | ||
You can call `core.get_session_status` with no keys (so an empty list in deluge-client.DelugeRPCClient.call) | ||
to get the full list of possible keys, but it seems to basically be a all of the session statistics | ||
listed on this page: https://www.rasterbar.com/products/libtorrent/manual-ref.html#session-statistics | ||
and a few others | ||
there is also a list of deprecated keys that deluge will translate for you and issue a warning in the log: | ||
https://github.com/deluge-torrent/deluge/blob/7f3f7f69ee78610e95bea07d99f699e9310c4e08/deluge/core/core.py#L58 | ||
""" | ||
|
||
DHT_DOWNLOAD_RATE = "dht_download_rate" | ||
DHT_UPLOAD_RATE = "dht_upload_rate" | ||
DOWNLOAD_RATE = "download_rate" | ||
UPLOAD_RATE = "upload_rate" | ||
|
||
|
||
class DelugeSensorType(enum.StrEnum): | ||
"""Enum that distinguishes the different sensor types that the Deluge integration has. | ||
This is mainly used to avoid passing strings around and to distinguish between similarly | ||
named strings in `DelugeGetSessionStatusKeys`. | ||
""" | ||
|
||
CURRENT_STATUS_SENSOR = "current_status" | ||
DOWNLOAD_SPEED_SENSOR = "download_speed" | ||
UPLOAD_SPEED_SENSOR = "upload_speed" | ||
PROTOCOL_TRAFFIC_UPLOAD_SPEED_SENSOR = "protocol_traffic_upload_speed" | ||
PROTOCOL_TRAFFIC_DOWNLOAD_SPEED_SENSOR = "protocol_traffic_download_speed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Test Deluge sensor.py methods.""" | ||
|
||
from homeassistant.components.deluge.const import DelugeSensorType | ||
from homeassistant.components.deluge.sensor import get_state | ||
|
||
from . import GET_TORRENT_STATUS_RESPONSE | ||
|
||
|
||
def test_get_state() -> None: | ||
"""Tests get_state() with different keys.""" | ||
|
||
download_result = get_state( | ||
GET_TORRENT_STATUS_RESPONSE, DelugeSensorType.DOWNLOAD_SPEED_SENSOR | ||
) | ||
assert download_result == 0.1 # round(98.5 / 1024, 2) | ||
|
||
upload_result = get_state( | ||
GET_TORRENT_STATUS_RESPONSE, DelugeSensorType.UPLOAD_SPEED_SENSOR | ||
) | ||
assert upload_result == 3.4 # round(3462.0 / 1024, 1) | ||
|
||
protocol_upload_result = get_state( | ||
GET_TORRENT_STATUS_RESPONSE, | ||
DelugeSensorType.PROTOCOL_TRAFFIC_UPLOAD_SPEED_SENSOR, | ||
) | ||
assert protocol_upload_result == 7.6 # round(7818.0 / 1024, 1) | ||
|
||
protocol_download_result = get_state( | ||
GET_TORRENT_STATUS_RESPONSE, | ||
DelugeSensorType.PROTOCOL_TRAFFIC_DOWNLOAD_SPEED_SENSOR, | ||
) | ||
assert protocol_download_result == 2.6 # round(2658.0/1024, 1) |