mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve type hints in rtorrent (#145222)
This commit is contained in:
parent
8df447091d
commit
a38e033e13
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
import xmlrpc.client
|
||||
|
||||
import voluptuous as vol
|
||||
@ -126,6 +127,9 @@ def format_speed(speed):
|
||||
return round(kb_spd, 2 if kb_spd < 0.1 else 1)
|
||||
|
||||
|
||||
type RTorrentData = tuple[float, float, list, list, list, list, list]
|
||||
|
||||
|
||||
class RTorrentSensor(SensorEntity):
|
||||
"""Representation of an rtorrent sensor."""
|
||||
|
||||
@ -135,12 +139,12 @@ class RTorrentSensor(SensorEntity):
|
||||
"""Initialize the sensor."""
|
||||
self.entity_description = description
|
||||
self.client = rtorrent_client
|
||||
self.data = None
|
||||
self.data: RTorrentData | None = None
|
||||
|
||||
self._attr_name = f"{client_name} {description.name}"
|
||||
self._attr_available = False
|
||||
|
||||
def update(self):
|
||||
def update(self) -> None:
|
||||
"""Get the latest data from rtorrent and updates the state."""
|
||||
multicall = xmlrpc.client.MultiCall(self.client)
|
||||
multicall.throttle.global_up.rate()
|
||||
@ -152,7 +156,7 @@ class RTorrentSensor(SensorEntity):
|
||||
multicall.d.multicall2("", "leeching", "d.down.rate=")
|
||||
|
||||
try:
|
||||
self.data = multicall()
|
||||
self.data = cast(RTorrentData, multicall())
|
||||
self._attr_available = True
|
||||
except (xmlrpc.client.ProtocolError, OSError) as ex:
|
||||
_LOGGER.error("Connection to rtorrent failed (%s)", ex)
|
||||
@ -164,14 +168,16 @@ class RTorrentSensor(SensorEntity):
|
||||
all_torrents = self.data[2]
|
||||
stopped_torrents = self.data[3]
|
||||
complete_torrents = self.data[4]
|
||||
up_torrents = self.data[5]
|
||||
down_torrents = self.data[6]
|
||||
|
||||
uploading_torrents = 0
|
||||
for up_torrent in self.data[5]:
|
||||
for up_torrent in up_torrents:
|
||||
if up_torrent[0]:
|
||||
uploading_torrents += 1
|
||||
|
||||
downloading_torrents = 0
|
||||
for down_torrent in self.data[6]:
|
||||
for down_torrent in down_torrents:
|
||||
if down_torrent[0]:
|
||||
downloading_torrents += 1
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user