Skip to content

Don't crash the custom integrations sensor while the loader rescans - #2094

Open
Zedas030 wants to merge 1 commit into
frenck:mainfrom
Zedas030:fix/custom-integrations-sensor-cache
Open

Zedas030 wants to merge 1 commit into
frenck:mainfrom
Zedas030:fix/custom-integrations-sensor-cache

Conversation

@Zedas030

Copy link
Copy Markdown

Description

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.

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.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)
  • Other

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Custom integrations sensor

Layer / File(s) Summary
Guard custom-integrations count
custom_components/spook/ectoplasms/homeassistant/sensor.py, tests/ectoplasms/homeassistant/test_sensor.py
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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: frenck

Merge Risk: Minimal · up to 8048e

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed [#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 Passed 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.

Share

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.

Files selected for processing: 2

  • custom_components/spook/ectoplasms/homeassistant/sensor.py
  • tests/ectoplasms/homeassistant/test_sensor.py

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant