Fix handling of `NoneType` for torrents in `count_torrents_in_states` function
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
- [x] 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
- This PR fixes or closes issue: fixes #121358
- This PR is related to issue: #121358
- Link to documentation pull request:
Checklist
- [ ] The code change is tested and works locally.
- [ ] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [ ] I have followed the development checklist
- [x] 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:
- [x] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [x] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [x] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [x] 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:
- [x] I have reviewed two other open pull requests in this repository.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
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 closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign qbittorrentRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Clean and simple. Nicely done and thank you!
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 😅
I would love to see less stacktraces in my logs
Workaround is to start downloading more torrents right :P
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 :)