8000 Remove redundant `"number"` translation key and `description` by TheJulianJES · Pull Request #444 · zigpy/zha · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove redundant "number" translation key and description #444

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

Merged
merged 7 commits into from
May 3, 2025

Conversation

TheJulianJES
Copy link
Contributor
@TheJulianJES TheJulianJES commented May 2, 2025

Proposed change

This removes redundant calls to set the fallback name to the ZCL description attribute in on_add of the sensor and number platforms, as this is already done in recompute_capabilities.
Calling this in recompute_capabilities also makes sure that the device is initialized (i.e. the attributes read once).

Furthermore, the default "number" translation key is also removed for the number platform to align with the sensor platform. Home Assistant will still default to the "Number" (US-EN) translation, same as before, if neither translation key and nor fallback name are provided by ZHA.

This allows for a cleaner implementation of setting the fallback name to the description name, without needing to unset the translation key or potentially needing to re-set it to number if the description attribute no longer has a value.
(Assuming recompute_capabilities can dynamically be called later at runtime, e.g. after a firmware update.)

Additional information

Addresses comments in #197 (review)


Old PR description of initial version (no longer relevant)

Proposed change

This removes the redundant hasattr(ch, "description") check for AnalogOutputNumber and AnalogInputSensor.
For the latter, getting the ZCL description attribute is also moved into the on_add method of AnalogInputSensor, as it's not needed in the base Sensor class.

The typing for _attr_translation_key in AnalogOutputNumber is also fixed to include the option of it being None if the description attribute has a value.

I can also remove the walrus operator again if that's preferred. It's often used in HA code though.

Important

Also, we set the fallback name to the description in recompute_capabilities for all three platforms supporting the description attribute (binary sensor, sensor, and number), as well as initially setting it in on_add.
For the number and analog sensor platforms, we also unset the translation key in on_add only.

However, on_add is called before the cluster handlers are initialized (so attributes read once).
If a device is freshly paired, even if a description attribute is present, the translation key would take priority over the fallback name, since on_add is executed before the attribute is read.
After a restart of HA, this behavior will change, as the attribute is already cached then.

It also seems a bit redundant to have this logic both in on_add and partly also in recompute_capabilities.
So, can we just remove it from on_add and include unsetting the translation key if description is present in recompute_capabilities?

Copy link
codecov bot commented May 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.91%. Comparing base (758d8b1) to head (839bfa3).
Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #444      +/-   ##
==========================================
- Coverage   96.92%   96.91%   -0.01%     
==========================================
  Files          63       63              
  Lines       10362    10353       -9     
==========================================
- Hits        10043    10034       -9     
  Misses        319      319              

☔ 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.

@puddly
Copy link
Contributor
puddly commented May 2, 2025

I totally agree with the on_add / recompute_capabilities problem, it definitely should be in the latter.

@TheJulianJES
Copy link
Contributor Author
TheJulianJES commented May 2, 2025

Want me to (re-)move it in a subsequent PR or here?

@TheJulianJES TheJulianJES marked this pull request as ready for review May 2, 2025 22:42
@TheJulianJES TheJulianJES marked this pull request as draft May 2, 2025 22:52
@TheJulianJES
Copy link
Contributor Author

I'll put it in here. It's mostly just code removal.

@TheJulianJES
Copy link
Contributor Author
TheJulianJES commented May 2, 2025

Ok, slightly bigger change but IMO for the better:

We've previously had a "number" translation key when no fallback name or other translation key was specified.
That translation key just refers to the number title translation key in HA:

It gets a bit weird with setting the fallback name and unsetting the translation key (or the other way around, depending on if description is available) for the number platform in recompute_capabilities then. Especially under the assumption we may dynamically call that method later and the ZCL attribute can change, causing both translation key and fallback name to potentially change again.

(Although I did just realize this would require HA changes to work as well, as we persist the fallback name to _attr_name there once it was available once: homeassistant/components/zha/entity.py#L73)

For the sensor platform, we simply don't provide a translation key by default. So, if there's no translation key value in HA and also no fallback name, it defaults to the _ device class, which points to the title (so exact same translation key):

Basically, the number platform is a bit more like the sensor platform now. There should be no changes on the HA side due to us removing the default "number" translation key, as it'll still default to "Number" for non-primary number entities with no specified translation key and no fallback name.

So, the fallback name is now only set to the description attribute in AnalogInputSensor.recompute_capabilities(), AnalogOutputNumber.recompute_capabilities() and BinaryInputWithDescription.recompute_capabilities in the same fashion. No special treatment with unsetting the translation key for number anymore.

Also updated the PR description. Let me know what you think.
Can split up the description removal from the number translation key removal into a separate PR if you prefer.

@TheJulianJES TheJulianJES changed the title Refactor getting ZCL description attribute in sensors Remove redundant "number" translation key and description May 2, 2025
@TheJulianJES TheJulianJES marked this pull request as ready for review May 2, 2025 23:29
@puddly puddly merged commit ecb2b9b into zigpy:dev May 3, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0