Skip to content

Conversation

@abelyliu
Copy link
Contributor

@abelyliu abelyliu commented Jan 5, 2026

Breaking change

None

Proposed change

This PR adds support for the report_type attribute from the Tuya API to properly handle different energy reporting mechanisms.

Background: Some Tuya devices report energy consumption as incremental deltas (sum type) rather than cumulative totals (minux type). Without proper handling, these delta values would be displayed directly, resulting in incorrect energy readings (e.g., showing 0.1 kWh repeatedly instead of accumulating to the actual total).

Implementation:

  • Detect report_type from Tuya API (sum, minux, un_known)
  • For sum (delta) type sensors: accumulate incremental values locally and persist across restarts using RestoreSensor
  • For minux (cumulative) type sensors: use the value directly as TOTAL_INCREASING
  • Skip duplicate updates using dp_timestamps to prevent double-counting
  • Feature is enabled only for specific device categories via REPORT_TYPE_ENABLED_CATEGORIES

Supported device categories: CZ (Socket), XNYJCN (Solar Inverter), ZNDB (Circuit Breaker), DLQ (Leakage Protector), TDQ (Switch with Metering), KG (Switch), PC (Power Strip), SFKZQ (Third Party Gateway)

Type of change

  • New feature (which adds functionality to an existing integration)

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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][dev-checklist]
  • I have followed the [perfect PR recommendations][perfect-pr]
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

  • The [manifest file][manifest-docs] 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.

Dependency update: tuya-device-sharing-sdk 0.2.7 → 0.2.8 (adds report_type attribute to DeviceStatusRange)

To help with the load of incoming pull requests:

  • I have reviewed two other [open pull requests][prs] in this repository.

@home-assistant
Copy link

home-assistant bot commented Jan 5, 2026

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

Code owner commands

Code owners of tuya 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 tuya 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.

@home-assistant
Copy link

home-assistant bot commented Jan 5, 2026

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft January 5, 2026 07:39
@abelyliu abelyliu requested a review from epenet January 5, 2026 08:17
@epenet
Copy link
Contributor

epenet commented Jan 5, 2026

Please rebase after the merge of the dependency bump

@abelyliu
Copy link
Contributor Author

abelyliu commented Jan 5, 2026

Please rebase after the merge of the dependency bump

Updated as requested.

Please rebase after the merge of the dependency bump

I have rebased onto the latest dev branch.

@epenet
Copy link
Contributor

epenet commented Jan 5, 2026

Please rebase after the merge of the dependency bump

Updated as requested.

Please rebase after the merge of the dependency bump

I have rebased onto the latest dev branch.

I don't think you rebased properly - as you have picked up some unlinked changes.
Please ensure the branch is clean after rebase

@abelyliu abelyliu force-pushed the tuya-sensor-report-type branch from c68fb71 to 014d825 Compare January 5, 2026 11:57
@abelyliu abelyliu mentioned this pull request Jan 5, 2026
11 tasks
- Support report_type attribute from Tuya API (sum/minux/un_known)
- Implement delta value accumulation for 'sum' report type sensors
- Use RestoreSensor to persist accumulated values across restarts
- Add REPORT_TYPE_ENABLED_CATEGORIES for controlled feature rollout
- Include report_type in diagnostics output
- Bump tuya-device-sharing-sdk to 0.2.8

Supported device categories: CZ, XNYJCN, ZNDB, DLQ, TDQ, KG, PC, SFKZQ
@abelyliu abelyliu force-pushed the tuya-sensor-report-type branch from c84fb48 to 5993b68 Compare January 7, 2026 01:49
@abelyliu abelyliu marked this pull request as ready for review January 7, 2026 02:08
@home-assistant home-assistant bot requested a review from epenet January 7, 2026 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants