From a38e033e13a25770502b3ff67c19e039c1013f38 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 19 May 2025 15:53:41 +0200 Subject: [PATCH] Improve type hints in rtorrent (#145222) --- homeassistant/components/rtorrent/sensor.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index 70fe7919edb..367542ca8c2 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -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