Removing a group member device leaves a stale group entity behind

Open
#875 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
embedded-iot

Research direction

Start at Gateway.device_removed(), which pops the device from self._devices and schedules Device.on_remove() but never touches its groups. Compare it with the group_member_removed path, which already calls Group.clear_caches() and re-runs discover_group_entities(), and check that Group.members (a cached_property) is recomputed. Done means that after removing a group member device, Group.members reports the remaining count, the group entity is removed once fewer than two members remain, and the removed device's entity subscription is dropped.

Written by the indexing model from the issue text.

Description

Removing a member device from the network — as opposed to removing its endpoint from the group — leaves the group's entity fully stale: the group entity survives, Group.members keeps reporting the removed device, and the group holds a subscription to an entity that has already been torn down.

Why it happens

Gateway.device_removed() pops the device from self._devices and schedules Device.on_remove(), but it never touches the groups that device belonged to — it does not call Group.clear_caches() and it does not re-run discover_group_entities(). Nothing else does either: zigpy's ControllerApplication._remove_device() only does self.devices.pop(device.ieee, None) and does not remove the device's endpoints from its groups, so no group_member_removed event is emitted.

Group.members is a cached_property that filters on member_ieee in self._gateway.devices, so it would return the right answer if it were recomputed — but without clear_caches() it keeps the pre-removal value.

Reproduction

Create a two-light group, then remove one of the member devices:

gateway.devices:  2          # coordinator + the remaining light
group.members:    2          # stale cached_property, should be 1
group_entities:   ['light_zha_group_0x0002']            # should be gone (only one member left)
entity_unsubs:    ['light_zha_group_0x0002',
                   '01:2d:6f:00:0a:90:69:e8-1',
                   '02:2d:6f:00:0a:90:69:e8-1']         # 02:… is the removed device's entity

The group entity stays registered and functional, backed by a single member, and the subscription to the removed device's light entity keeps the torn-down entity object alive.

Relation to #849

#849 fixes the teardown paths that do emit events — a group dropping below two members, a single platform losing eligibility, and group removal. This one is a different entry point (Gateway.device_removed) and is deliberately out of scope there. A fix probably means, for each group the removed device belonged to, calling clear_caches() and re-running discover_group_entities() — which is exactly what group_member_removed already does.

Dominant language
Python
Stars
96
Forks
84
Avg merge
2d 4h
Merged PRs (30d)
30

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zigpy/zha

All issues in zigpy/zha

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.