Skip to content

Fix Glances integration leaving stale entities for removed devices#169690

Draft
serl wants to merge 2 commits intohome-assistant:devfrom
serl:fix/glances-auto-remove-stale-entities
Draft

Fix Glances integration leaving stale entities for removed devices#169690
serl wants to merge 2 commits intohome-assistant:devfrom
serl:fix/glances-auto-remove-stale-entities

Conversation

@serl
Copy link
Copy Markdown

@serl serl commented May 3, 2026

Proposed change

The Glances integration created sensor entities once at setup and never removed them. When the underlying device disappeared on the remote host (most commonly Docker bridge networks like veth*/br-*, but also ZFS datasets), the entity stayed registered forever as STATE_UNAVAILABLE. Users in the linked issue reported thousands of dead entities accumulating, breaking dashboards and the entity picker.

This PR refactors homeassistant/components/glances/sensor.py so entity creation is dynamic, modeled on the canonical pattern used by homeassistant/components/iotawatt/sensor.py (dynamic add+remove via a coordinator listener plus er.async_remove in _handle_coordinator_update):

  • async_setup_entry keeps a created set and registers a coordinator listener that adds entities for new (sensor_type, sensor_label, param) keys appearing in coordinator.data.
  • GlancesSensor._handle_coordinator_update calls entity_registry.async_remove(self.entity_id) when the entity's parent type dict is present in coordinator.data but its label is missing — meaning the underlying network/disk was deleted on the host. A missing parent type is still treated as a transient API gap and leaves the entity unavailable, preserving the existing test_sensor_removed behavior.

Tests added in tests/components/glances/test_sensor.py:

  • test_dynamic_sensor_auto_removed: when eth0 disappears from the network block on the next poll, both test-eth0-rx and test-eth0-tx are removed from the entity registry, while test-lo-rx remains.
  • test_dynamic_sensor_auto_added: when a new interface eth1 appears on the next poll, the new entity is registered and has a non-unavailable state.

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

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Glances created entities once at setup and never removed them. When the
underlying object disappeared on the remote host (most commonly Docker
bridge networks like veth*/br-* which churn whenever containers are
recreated, but also ZFS datasets), the entity stayed registered forever
as STATE_UNAVAILABLE.

Adapt the iotawatt pattern: on each coordinator update, add entities for
new device labels and remove entities (from the entity registry) when
their device label disappears from a populated parent type dict. A
missing parent type is treated as a transient API gap and leaves the
entity unavailable, preserving the existing test_sensor_removed
behavior.

Closes home-assistant#124563
Copilot AI review requested due to automatic review settings May 3, 2026 14:30
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @serl

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 3, 2026

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

Code owner commands

Code owners of glances can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign glances Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

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 updates the Glances integration so sensor entities can track dynamic device lists instead of only being created once at setup. It aims to prevent stale Glances entities from accumulating in Home Assistant when remote interfaces, filesystems, or similar resources disappear.

Changes:

  • Refactors Glances sensor setup to add newly discovered entities on coordinator updates instead of only during initial setup.
  • Removes dynamic Glances sensor entities from the entity registry when their backing device label disappears from an otherwise present data block.
  • Adds sensor tests covering dynamic network-entity removal and creation on subsequent coordinator refreshes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
homeassistant/components/glances/sensor.py Adds dynamic entity discovery and auto-removal logic for Glances sensors.
tests/components/glances/test_sensor.py Adds regression tests for dynamic network sensor removal and addition.

Comment on lines +305 to +321
created: set[tuple[str, str, str]] = set()

for sensor_type, sensors in coordinator.data.items():
if sensor_type in ["fs", "diskio", "sensors", "raid", "gpu", "network"]:
entities.extend(
GlancesSensor(
coordinator,
sensor_description,
sensor_label,
)
for sensor_label, params in sensors.items()
for param in params
if (sensor_description := SENSOR_TYPES.get((sensor_type, param)))
)
else:
entities.extend(
GlancesSensor(
coordinator,
sensor_description,
)
for sensor in sensors
if (sensor_description := SENSOR_TYPES.get((sensor_type, sensor)))
)
@callback
def _add_new_entities() -> None:
new_entities: list[GlancesSensor] = []
for sensor_type, sensors in coordinator.data.items():
if sensor_type in DYNAMIC_TYPES:
for sensor_label, params in sensors.items():
for param in params:
key = (sensor_type, sensor_label, param)
if key in created:
continue
if (
description := SENSOR_TYPES.get((sensor_type, param))
) is None:
continue
created.add(key)
Entities registered before the dynamic-removal behavior (or while
Home Assistant was offline) had no live GlancesSensor instance to
trigger _handle_coordinator_update, so they remained in the registry
as STATE_UNAVAILABLE forever.

Add a one-time sweep in async_setup_entry that removes registry
entries whose dynamic device label is no longer present in the API
response, mirroring the runtime guard: only remove when at least one
candidate parent type dict is present and the label is missing from
every present parent.

Static singleton entries (empty sensor_label) are skipped so they
remain unavailable through transient outages, matching the existing
test_sensor_removed behavior.
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @serl

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

entities for removed bridge networks of [glances] integration are left over as unavailable

2 participants