Declare capability attributes on ZclPlatformEntity and refresh capabilities on their events

Open
#1,082 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
devtools

Research direction

Start with ZclPlatformEntity and its handle_attribute_updated() in zha/application/platforms, then compare AnalogOutputNumber, Cover and NumberConfigurationEntity in number/init.py and cover/init.py. Check how _server_cluster_config and AttrConfig are declared, and where Device.recompute_entities() is used by tests. Done means a single declared attribute list on the base class refreshes capabilities and emits state, and both number classes and Cover migrate to it.

Written by the indexing model from the issue text.

Description

code quality entities
Summary

Entities whose capabilities (range, features, device class, name) come from cluster attributes compute them when they are added. Device.recompute_entities() recomputes them too, but only the tests call it, so a later change to one of those attributes leaves the entity stale until the next restart. Three classes refresh on attribute events today, each in its own way:

  • AnalogOutputNumber: _capability_attribute_ids (attribute ids), checked in its handle_attribute_updated(), since zigpy/zha#917 (number/init.py, handler).
  • Cover: a hard-coded window_covering_type branch in handle_attribute_updated() (cover/init.py).
  • NumberConfigurationEntity: _capability_attribute_names (attribute names), since zigpy/zha#1059, used by the min/max heat setpoint limit numbers (number/init.py, handler).

The idea: one declaration on ZclPlatformEntity, handled in one place (recompute the capabilities, then emit the state), so a class opts in by listing its attributes instead of overriding handle_attribute_updated(). Raised on zigpy/zha#1059.

Design points
  • Per cluster. Some inputs live on a cluster other than self._cluster (the Thermostat's fan cluster, the Light's color cluster), so a flat set of attribute names or ids is not enough. One option is a flag on AttrConfig in _server_cluster_config (e.g. capability=True): that config already lists these attributes per cluster with their read settings, and the base class would subscribe to the flagged ones. The other is a class attribute mapping cluster id to attribute ids.
  • Ids or names. The two number classes key by different things today; the shared version should pick one and migrate both.
  • Subclass handlers. Most handle_attribute_updated() overrides return early for attributes they don't track, so the generic refresh has to run independently of them, not as a branch a subclass can skip.
  • Light can't opt in as is. Light.recompute_capabilities() also seeds live state from the cache and would bypass the transition buffer, see zigpy/zha#940 section 2. Its state seeding has to move out first.
Candidates on dev that don't refresh today
  • StartUpColorTemperatureConfigurationEntity: color_temp_physical_min / color_temp_physical_max set the range.
  • AnalogInputSensor: description, application_type, the units and resolution (the example in zigpy/zha#940).
  • BinaryInputWithDescription / BinaryOutputSwitch: description sets the fallback name.
  • IASZone: zone_type sets the device class and translation key.
  • Thermostat: ctrl_sequence_of_oper decides the TARGET_TEMPERATURE_RANGE feature (the HVAC mode list itself is read live).
  • SmartEnergyMetering: unit_of_measure picks the entity description.

Most of these are fixed by the device, so the realistic trigger is a first successful read after the entity was added, the re-initialization case of zigpy/zha#940 section 2. A read fires an attribute event, so an event-driven refresh of the declared attributes is the generic form of that issue's "per-class refreshes on attribute events" option.

Related
  • zigpy/zha#940: compute capabilities centrally, and stale capabilities after re-initialization.
  • zigpy/zha#917 and zigpy/zha#1059: the two per-class refreshes above.
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.