Skip to content

Do not add statistics config entry to source device #148731

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

Merged

Conversation

emontnemery
Copy link
Contributor

Proposed change

Do not add statistics config entry to source device, instead, set the device entry to that of the source device

Background in PR #148346

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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:

@home-assistant
Copy link

Hey there @ThomDietrich, @gjohansson-ST, mind taking a look at this pull request as it has been labeled with an integration (statistics) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of statistics 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 statistics 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.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR changes how statistics config entries are associated with devices: instead of adding a helper entry to the source device, the integration now links the sensor to the source device directly and removes stale helper entries.

  • Tests updated to expect zero helper config entries on devices and added migration/version tests.
  • Sensor initialization now uses async_entity_id_to_device with a guard and removes the old async_device_info_to_link_from_entity call.
  • Migration logic (async_migrate_entry) cleans up existing device links and bumps the minor version to 2.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/components/statistics/test_init.py Updated assertions to expect no helper entries on devices; added migration tests
homeassistant/components/statistics/sensor.py Replaced device info helper import; added guard and async_entity_id_to_device
homeassistant/components/statistics/config_flow.py Added MINOR_VERSION = 2; updated preview call to use keyword args
homeassistant/components/statistics/init.py Added async_migrate_entry logic; removed stale device links on setup
Comments suppressed due to low confidence (3)

homeassistant/components/statistics/sensor.py:678

  • The attribute device_entry is not a standard entity attribute and appears unused; consider removing it or assigning to _attr_device_info if device linking via entity attributes is needed, to adhere to entity conventions.
            self.device_entry = async_entity_id_to_device(

tests/components/statistics/test_init.py:93

  • Capture and call the unsubscribe callback returned by async_track_entity_registry_updated_event in track_entity_registry_actions to remove the listener after tests and avoid potential event listener leakage.
    async_track_entity_registry_updated_event(hass, entity_id, add_event)

homeassistant/components/statistics/init.py:85

  • Guard the call to async_update_entry behind config_entry.minor_version < 2 so that entries already at minor version 2 are not unnecessarily updated, reducing redundant writes to the config entries storage.
        hass.config_entries.async_update_entry(

hass,
source_entity_id,
)
if source_entity_id: # Guard against empty source_entity_id in preview mode
Copy link
Member

Choose a reason for hiding this comment

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

Should this also check we have a unique id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to check that, the device_entry is ignored if the entity's unique ID is None or the entity does not belong to a config entry:

device = entity.device_entry

Copy link
Member

@gjohansson-ST gjohansson-ST Jul 14, 2025

Choose a reason for hiding this comment

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

Sorry, meant more to have it as a check to not even bother with creating a device when it's missing (because then it's preview or its YAML config w/o unique_id set).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see.
I think it's simpler like this though?

@emontnemery emontnemery merged commit 3cb579d into dev Jul 15, 2025
34 checks passed
@emontnemery emontnemery deleted the do_not_add_statistics_config_entry_to_parent_device branch July 15, 2025 20:13
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2025
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.

2 participants