* Upgrade QBittorrent integration to show torrents This brings the QBittorrent integration to be more in line with the Transmission integration. It updates how the integration is written, along with adding sensors for Active Torrents, Inactive Torrents, Paused Torrents, Total Torrents, Seeding Torrents, Started Torrents. * Remove unused stuff * Correct name in comments * Make get torrents a service with a response * Add new sensors * remove service * Add service with response to get torrents list This adds a service with a response to be able to get the list of torrents within qBittorrent * update * update from rebase * Update strings.json * Update helpers.py * Update to satisfy lint * add func comment * fix lint issues * another update attempt * Fix helpers * Remove unneccesary part in services.yaml and add translations * Fix return * Add tests * Fix test * Improve tests * Fix issue from rebase * Add icon for get_torrents service * Make get torrents a service with a response * remove service * Add service with response to get torrents list This adds a service with a response to be able to get the list of torrents within qBittorrent * Update to satisfy lint * Handle multiple installed integrations * fix lint issue * Set return types for helper methods * Create the service method in async_setup * Add CONFIG_SCHEMA * Add get_all_torrents service * fix lint issues * Add return types and ServiceValidationError(s) * Fix naming * Update translations * Fix tests
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""Helper functions for qBittorrent."""
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
from qbittorrent.client import Client
|
|
|
|
|
|
def setup_client(url: str, username: str, password: str, verify_ssl: bool) -> Client:
|
|
"""Create a qBittorrent client."""
|
|
client = Client(url, verify=verify_ssl)
|
|
client.login(username, password)
|
|
# Get an arbitrary attribute to test if connection succeeds
|
|
client.get_alternative_speed_status()
|
|
return client
|
|
|
|
|
|
def seconds_to_hhmmss(seconds) -> str:
|
|
"""Convert seconds to HH:MM:SS format."""
|
|
if seconds == 8640000:
|
|
return "None"
|
|
|
|
minutes, seconds = divmod(seconds, 60)
|
|
hours, minutes = divmod(minutes, 60)
|
|
return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
|
|
|
|
|
|
def format_unix_timestamp(timestamp) -> str:
|
|
"""Format a UNIX timestamp to a human-readable date."""
|
|
dt_object = datetime.fromtimestamp(timestamp, tz=UTC)
|
|
return dt_object.isoformat()
|
|
|
|
|
|
def format_progress(torrent) -> str:
|
|
"""Format the progress of a torrent."""
|
|
progress = torrent["progress"]
|
|
progress = float(progress) * 100
|
|
return f"{progress:.2f}"
|
|
|
|
|
|
def format_torrents(torrents: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
|
|
"""Format a list of torrents."""
|
|
value = {}
|
|
for torrent in torrents:
|
|
value[torrent["name"]] = format_torrent(torrent)
|
|
|
|
return value
|
|
|
|
|
|
def format_torrent(torrent) -> dict[str, Any]:
|
|
"""Format a single torrent."""
|
|
value = {}
|
|
value["id"] = torrent["hash"]
|
|
value["added_date"] = format_unix_timestamp(torrent["added_on"])
|
|
value["percent_done"] = format_progress(torrent)
|
|
value["status"] = torrent["state"]
|
|
value["eta"] = seconds_to_hhmmss(torrent["eta"])
|
|
value["ratio"] = "{:.2f}".format(float(torrent["ratio"]))
|
|
|
|
return value
|