Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix qbittorrent error when torrent count is 0 #126146

Merged

Conversation

DAcodedBEAT
Copy link
Contributor

@DAcodedBEAT DAcodedBEAT commented Sep 17, 2024

Proposed change

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.

Here was the traceback when looking at the home assistant logs:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity_platform.py", line 595, in _async_add_entities
    await coro
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity_platform.py", line 909, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1366, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1011, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/sensor/__init__.py", line 542, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/qbittorrent/sensor.py", line 170, in native_value
    return self.entity_description.value_fn(self.coordinator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/qbittorrent/sensor.py", line 101, in <lambda>
    value_fn=lambda coordinator: count_torrents_in_states(coordinator, []),
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/homeassistant/components/qbittorrent/sensor.py", line 181, in count_torrents_in_states
    return len(torrents)
           ^^^^^^^^^^^^^
TypeError: object of type 'NoneType' has no len()

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DAcodedBEAT

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft September 17, 2024 19:18
@home-assistant
Copy link

Hey there @geoffreylagaisse, @finder39, mind taking a look at this pull request as it has been labeled with an integration (qbittorrent) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of qbittorrent can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign qbittorrent Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@finder39
Copy link
Contributor

Clean and simple. Nicely done and thank you!

@DAcodedBEAT DAcodedBEAT force-pushed the fix-torrent-count-in-states branch from 439bff7 to 3d30301 Compare September 17, 2024 21:50
… 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.
@DAcodedBEAT DAcodedBEAT force-pushed the fix-torrent-count-in-states branch from 3d30301 to 1d9930e Compare September 18, 2024 16:04
@DAcodedBEAT
Copy link
Contributor Author

Clean and simple. Nicely done and thank you!

@finder39 thanks for the quick review! how long do these PRs typically take to get approved and merged by core maintainers?

I would love to see less stacktraces in my logs 😅

@joostlek joostlek changed the title Fix handling of NoneType for torrents in count_torrents_in_states function Fix qbittorrent torrents being 0 Sep 19, 2024
@joostlek joostlek changed the title Fix qbittorrent torrents being 0 Fix qbittorrent error when torrent count is 0 Sep 19, 2024
@joostlek joostlek added this to the 2024.9.3 milestone Sep 19, 2024
@joostlek
Copy link
Member

I would love to see less stacktraces in my logs

Workaround is to start downloading more torrents right :P

@joostlek joostlek merged commit 8ca3310 into home-assistant:dev Sep 19, 2024
26 checks passed
@finder39
Copy link
Contributor

Clean and simple. Nicely done and thank you!

@finder39 thanks for the quick review! how long do these PRs typically take to get approved and merged by core maintainers?

I would love to see less stacktraces in my logs 😅

It's not too long as you can see, especially on smaller PRs like this :)

@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
@DAcodedBEAT DAcodedBEAT deleted the fix-torrent-count-in-states branch September 21, 2024 05:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

qbittorent integration causing errors in 2024.7.1
5 participants