You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Don't crash the custom integrations sensor while the loader rescans - #2094
sensor.custom_integrations counted with len(hass.data["custom_components"]). That is the loader's lazy cache, which is not always a dict: it is missing until something asks the loader for the custom integrations, it is dropped again when Spook clears it to pick up a freshly linked sub integration, and while the loader scans the folder it holds an asyncio.Future. Depending on the moment, the sensor raised KeyError: 'custom_components' when it was added, or TypeError: object of type '_asyncio.Future' has no len() on a later update.
The count now only uses the cache when it is a dict, and reports unknown otherwise. The sensor already updates again on the next component_loaded event, so it gets its number back once the scan is done.
Both come from the same line, so one fix for both.
How has this been tested?
Added tests to tests/ectoplasms/homeassistant/test_sensor.py for a filled cache, a missing cache and a scan in progress. The last two fail without the change with exactly the errors from the two issues. Full test suite passes (6300 passed, 1 xfailed). Ruff check and format are clean, pylint has no messages on the changed files. I haven't run it on a live Home Assistant instance.
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
No actionable comments were generated in the recent review.
Walkthrough
The custom-integrations sensor now reads the loader cache through a helper. The helper returns the entry count when the cache is a dictionary, and returns None when the cache is absent or has another value.
The sensor uses a helper that counts entries only when the loader cache is a dictionary. Tests cover a populated cache, an absent cache, and a cache containing a future.
The sensor no longer raises while the loader rescans or when the cache is missing at startup. It reports unknown and recovers after the scan finishes. The change is small, covered by tests, and easy to revert, so merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: the custom integrations sensor no longer crashes while the loader rescans. The raw summary confirms the sensor now counts the cache only when it is a dict and reports …
Description check
The description states the root cause (len() on the loader's lazy cache, which can be missing or an asyncio.Future), the fix (count only when the cache is a dict, otherwise report unknown), the linked…
Linked Issues check
[#2083, #2084] The reviewed head adds _count_custom_integrations in custom_components/spook/ectoplasms/homeassistant/sensor.py. It reads hass.data.get(loader.DATA_CUSTOM_COMPONENTS) and returns …
Out of Scope Changes check
The change set, as described by the change summary (sensor.py +16/-1, test_sensor.py +27/-0), is limited to the custom_component sensor's value function and its tests. The helper `_count_custom_inte…
Full details: Title check
Explanation
The title names the main change: the custom integrations sensor no longer crashes while the loader rescans. The raw summary confirms the sensor now counts the cache only when it is a dict and reports unknown otherwise. The title is specific and matches the objectives of PR #2094 and the linked issues #2083 and #2084.
Full details: Description check
Explanation
The description states the root cause (len() on the loader's lazy cache, which can be missing or an asyncio.Future), the fix (count only when the cache is a dict, otherwise report unknown), the linked issues, and the tests added. This matches the raw summary of the sensor and test changes.
Full details: Linked Issues check
Explanation
[#2083, #2084] The reviewed head adds _count_custom_integrations in custom_components/spook/ectoplasms/homeassistant/sensor.py. It reads hass.data.get(loader.DATA_CUSTOM_COMPONENTS) and returns None unless the value is a dict. This removes the direct subscript that raised KeyError: 'custom_components' (#2084) when the key was absent. It also removes the len() call on the asyncio.Future held during a loader scan (#2083). The custom_component description now uses this helper as value_fn. Its update_events is {EVENT_COMPONENT_LOADED}, so the sensor recomputes its value after the scan. The PR description reports new tests in tests/ectoplasms/homeassistant/test_sensor.py for a filled cache, a missing cache, and a scan in progress. I could not read the test diff from the checkout, so the test coverage claim rests on the PR description and the change summary, not on direct inspection. The coding requirements in both issues are covered by the source change.
Full details: Out of Scope Changes check
Explanation
The change set, as described by the change summary (sensor.py +16/-1, test_sensor.py +27/-0), is limited to the custom_component sensor's value function and its tests. The helper _count_custom_integrations is used only by that sensor. No unrelated sensors or behavior are changed in the summarized evidence. I could not read the full text diff from the checkout, so this verdict relies on the change summary and the file content at the reviewed head.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A rabbit checks the loader’s cache, And counts the entries in the stash. If scans are still underway, It leaves the value blank that day. Then hops along when counts return.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 1d2bebf5-784c-4e29-bc5b-93375934efa2
Commits
Reviewing files that changed from the base of the PR and between c2ed5ce and 8048e61.
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
sensor.custom_integrationscounted withlen(hass.data["custom_components"]). That is the loader's lazy cache, which is not always a dict: it is missing until something asks the loader for the custom integrations, it is dropped again when Spook clears it to pick up a freshly linked sub integration, and while the loader scans the folder it holds anasyncio.Future. Depending on the moment, the sensor raisedKeyError: 'custom_components'when it was added, orTypeError: object of type '_asyncio.Future' has no len()on a later update.The count now only uses the cache when it is a dict, and reports unknown otherwise. The sensor already updates again on the next
component_loadedevent, so it gets its number back once the scan is done.Motivation and Context
Fixes #2083
Fixes #2084
Both come from the same line, so one fix for both.
How has this been tested?
Added tests to
tests/ectoplasms/homeassistant/test_sensor.pyfor a filled cache, a missing cache and a scan in progress. The last two fail without the change with exactly the errors from the two issues. Full test suite passes (6300 passed, 1 xfailed). Ruff check and format are clean, pylint has no messages on the changed files. I haven't run it on a live Home Assistant instance.Types of changes
Checklist