Fix qbittorrent error when torrent count is 0 (#126146)

Fix handling of `NoneType` for torrents in `count_torrents_in_states` function

Added a check to handle cases where the 'torrents' data is None, avoiding a `TypeError` when attempting to get the length of a `NoneType` object. The function now returns 0 if 'torrents' is None, ensuring robust behavior when no torrent data is available.
This commit is contained in:
Arun Philip 2024-09-19 04:34:27 -04:00 committed by Franck Nijhof
parent 991114eb7f
commit b336cae118
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3

View File

@ -177,8 +177,12 @@ def count_torrents_in_states(
# When torrents are not in the returned data, there are none, return 0.
try:
torrents = cast(Mapping[str, Mapping], coordinator.data.get("torrents"))
if torrents is None:
return 0
if not states:
return len(torrents)
return len(
[torrent for torrent in torrents.values() if torrent.get("state") in states]
)