-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
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
Do not add statistics config entry to source device #148731
Conversation
Hey there @ThomDietrich, @gjohansson-ST, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this 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 oldasync_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
intrack_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
behindconfig_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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
core/homeassistant/helpers/entity_platform.py
Line 846 in 562f486
device = entity.device_entry |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
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
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: