Skip to content
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

Fix ZHA handling of power factor ElectricalMeasurement attribute sensor #107641

Merged

Conversation

jeverley
Copy link
Contributor

@jeverley jeverley commented Jan 9, 2024

Proposed change

The Zigbee Cluster Library defines PowerFactor as an int8 with value supported from -100 to 100.

Currently the zha sensor handler defaults to applying the ac_power_divisor and ac_power_multiplier formatters against the power_factor attribute value as _div_mul_prefix is not set for the sensor.

The Cluster Library spec outlines that this should not be the case, the native Zigbee attribute value provides with a % value out of 100.
This change updates the formatter method for sensor class ElectricalMeasurement and ElectricalMeasurementPowerFactor sensor handler to address this.

https://zigbeealliance.org/wp-content/uploads/2021/10/07-5123-08-Zigbee-Cluster-Library.pdf
See: 4.9.2.2.6, 4.9.2.2.6.15, 4.9.2.2.7.5

image image image

The impact of the existing implementation is that quirks not using the default power divisor/multiplier values of 1 are having to multiply power factor prior to updating the cluster attribute, i.e. if the ac_power_divisor is 10, the quirk would multiply the power factor of 100 to 1000.
This results in an out of spec power_factor value.

Reviewing zha-cluster-handlers only one quirk currently use this workaround so impact of this fix should be minimal.

  • ts0601_rcbo.py (currently multiplies power_factor by an additional 10, giving 1000 for 100%).

zha-quirks had one quirk updated, so this change is no longer breaking.
PR:

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

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

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Jan 9, 2024

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

Code owner commands

Code owners of zha can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign zha Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

jeverley added a commit to jeverley/zha-device-handlers that referenced this pull request Jan 9, 2024
Amend power_factor calculation to respect Zigbee cluster specification - required for release of zha fix:

home-assistant/core#107641
@jeverley jeverley force-pushed the zha_metering_power_factor_standards branch from d357eca to 081abba Compare January 10, 2024 10:15
@MartinHjelmare MartinHjelmare changed the title Correct handling of power_factor ElectricalMeasurement attribute sensor Correct ZHA handling of power_factor ElectricalMeasurement attribute sensor Jan 10, 2024
jeverley added a commit to jeverley/home-assistant-core that referenced this pull request Jan 10, 2024
@jeverley jeverley requested a review from elupus January 10, 2024 17:03
@dmulcahey
Copy link
Contributor

@jeverley there are lint errors here

jeverley added a commit to jeverley/zha-device-handlers that referenced this pull request Jan 17, 2024
Amend power_factor calculation to respect Zigbee cluster specification - required for release of zha fix:

home-assistant/core#107641
jeverley added a commit to jeverley/home-assistant-core that referenced this pull request Jan 17, 2024
@jeverley jeverley force-pushed the zha_metering_power_factor_standards branch from 71d49e0 to b5d98f8 Compare January 17, 2024 10:12
@jeverley
Copy link
Contributor Author

@jeverley there are lint errors here

Resolved now 👍

jeverley added a commit to jeverley/zha-device-handlers that referenced this pull request Jan 22, 2024
Amend power_factor calculation to respect Zigbee cluster specification - required for release of zha fix:

home-assistant/core#107641
@jeverley jeverley force-pushed the zha_metering_power_factor_standards branch from b5d98f8 to ded3366 Compare January 22, 2024 10:41
jeverley added a commit to jeverley/home-assistant-core that referenced this pull request Jan 22, 2024
The Zigbee Cluster Library defines PowerFactor as an int8 with value supported from -100 to 100.

Currently the zha sensor handler attempts to apply the ac_power_divisor and ac_power_multiplier formatters against the attribute value, the spec outlines that this should not be the case.

The impact of the current code is that quirks not using the default values of 1 are multiplying/dividing
power and power factor values prior to updating the cluster attribute.

This results in either a non-conformant power_factor e.g. the value was multiplied by 10 so that an ac_power_divider of 10 could be used, or the power readings sacrificing a point of measurement for lower readings.

Two quirks currently use this workaround:
 * ts0601_din_power.py
 * ts0601_rcbo.py
@jeverley jeverley force-pushed the zha_metering_power_factor_standards branch from 085828a to 2f253d0 Compare January 24, 2024 10:29
@dmulcahey
Copy link
Contributor

@jeverley you have a test conflict from your other PR I think

@jeverley
Copy link
Contributor Author

@dmulcahey thanks for the heads up, I've resolved the conflict 👍

@TheJulianJES
Copy link
Member

Currently marked as a "breaking change", but if quirks is updated, this shouldn't be breaking, right?

@jeverley
Copy link
Contributor Author

jeverley commented Jan 24, 2024

Currently marked as a "breaking change", but if quirks is updated, this shouldn't be breaking, right?

Correct, it's only breaking if we don't release the quirk update in parallel (assume we'd want a note in the release notes for anyone using custom quirks).

@TheJulianJES
Copy link
Member

TheJulianJES commented Jan 24, 2024

was automatically closed because the quirks PR included "fix #thisPr" 😅

We should probably wait with merging this one until the quirks bump has been made, otherwise the nightly builds would show the incorrect value for the Tuya RCBO devices. The PR already looks good though.

@TheJulianJES TheJulianJES mentioned this pull request Jan 30, 2024
20 tasks
@TheJulianJES TheJulianJES changed the title Correct ZHA handling of power_factor ElectricalMeasurement attribute sensor Fix ZHA handling of power factor ElectricalMeasurement attribute sensor Jan 30, 2024
@TheJulianJES
Copy link
Member

I've removed the breaking change section and label to clarify that this PR is not a breaking change, but rather just a bug-fix, since the merged quirks bump includes your quirks PR change. So, there's no "breaking change" in that case.

(assume we'd want a note in the release notes for anyone using custom quirks).

Custom quirks are unsupported, but I did have a quick look in the issues section and I didn't notice any custom quirks that even update the power factor. So, there should be no issues there either.

Copy link
Member

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

Changes LGTM. Thanks!

@TheJulianJES TheJulianJES merged commit 2909e1c into home-assistant:dev Jan 30, 2024
43 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 31, 2024
lgraf pushed a commit to lgraf/zha-device-handlers that referenced this pull request May 6, 2024
…re#107641 (zigpy#2896)

Amend power_factor calculation to respect Zigbee cluster specification - required for release of ZHA fix:
home-assistant/core#107641
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants