Skip to content

Conversation

Markus98
Copy link

This is a PR created based on the discussion in home-assistant/core#151434

Proposed change

This PR implements a feature from the ZHA backlog. It adds an optional postfix attribute to ZHA entities. It is used to differentiate between entities that have the same type and name on the same device. The attribute is needed for proper numbered naming in the UI.

Additional information

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
  • Tests have been added to verify that the new code works.

@TheJulianJES TheJulianJES added the enhancement New feature or request label Aug 31, 2025
Copy link

codecov bot commented Aug 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.91%. Comparing base (ebc3cbf) to head (bfc417b).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #525   +/-   ##
=======================================
  Coverage   96.91%   96.91%           
=======================================
  Files          63       63           
  Lines       10498    10515   +17     
=======================================
+ Hits        10174    10191   +17     
  Misses        324      324           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@TheJulianJES TheJulianJES left a comment

Choose a reason for hiding this comment

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

Awesome, this looks pretty good!
I'll take a closer final look in the next few days, maybe adjust one of the comments, but I think this and the HA implementation are good otherwise.

One note for possible future PRs: it's a bit nicer to commit the regenerated diagnostics separately, since the GitHub diff viewer gets a bit laggy with all the changed files. We squash all PRs when merging them anyway, so it'll still be one commit in the repo.

@TheJulianJES TheJulianJES self-assigned this Sep 2, 2025
@TheJulianJES
Copy link
Contributor

The Matter integration in Home Assistant has a similar feature. However, it uses the endpoint ID for the postfix. For reference, see: https://github.com/home-assistant/core/blob/180f898bfa8b4bc3a1a5a97f64866c64fe1f551e/homeassistant/components/matter/entity.py#L111

I would not do this in the Zigbee integration, as quite a few Zigbee devices start with endpoint 11 or some other number. This would be confusing if there's "Voltage (11)" and "Voltage (12)", for example.
Counting upwards from 1 (if there are duplicate entities) is a better approach, at least for Zigbee.
Directly creating entities (per device, not per endpoint, e.g. quirks v2 entities) also play a role here.

Minor side note:
The Matter implementation also sets its postfix before all entities "are discovered", just by looking at duplicate clusters. The reason for this might be that this was a good option which doesn't require checking other entities, or endpoint IDs are generally used more sensible with Matter.

For us, I think the approach in this PR is already the best.

@TheJulianJES
Copy link
Contributor

TheJulianJES commented Sep 2, 2025

Thanks for linking this!

I'm not sure that approach would work for us here, since we can potentially have almost every entity twice, needing a postfix. Or just once, not needing one.

In that case, we'd need two translation key/value pairs per current one, from what I can see, as we'd have a trailing space otherwise.

We could make use of it for some quirks v2 defined entities though, where e.g. left/right would be used instead. For these, the entities are defined/fixed per device, so we always know exactly which ones exist.
Translation placeholders could be useful here, though the postfix approach from this PR can be, too.

@Markus98
Copy link
Author

Markus98 commented Sep 2, 2025

Thank you for taking a look and I'm glad that it seems good!

One note for possible future PRs: it's a bit nicer to commit the regenerated diagnostics separately, since the GitHub diff viewer gets a bit laggy with all the changed files. We squash all PRs when merging them anyway, so it'll still be one commit in the repo.

Good point, thanks for the note. I'm used to working with atomic commits that do not break tests but I don't need to do that here if we squash anyways.

I would not do this in the Zigbee integration, as quite a few Zigbee devices start with endpoint 11 or some other number. This would be confusing if there's "Voltage (11)" and "Voltage (12)", for example.
Counting upwards from 1 (if there are duplicate entities) is a better approach, at least for Zigbee.

I made the same observation and agree.

Have translation_placeholders been considered?

I also considered implementing it this way, but decided against it for the same reasons @TheJulianJES outlined.

@epenet
Copy link

epenet commented Sep 15, 2025

I don't know if you have thought of this:

  • 3 "production" (kWh) sensors (total / A / B)
  • 3 "power" (W) sensors (total / A / B)
  • only 2 "current" (A) sensors (A / B)

With the automatic suffix, you end up with misalignment of the suffix (current B would end up with same suffix 2 as power A)

Is it really that bad to use the cluster ID?
If we don't want to start number from say 11, we would use a hex string of the cluster ID so that we at least keep the "grouping" of the cluster entities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants