Skip to content

Commit

Permalink
Fix handling of NoneType for torrents in count_torrents_in_states
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
DAcodedBEAT committed Sep 18, 2024
1 parent 12dbabb commit 1d9930e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions homeassistant/components/qbittorrent/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand Down

0 comments on commit 1d9930e

Please sign in to comment.