-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
Fix parsing of Tuya electricity RAW values #157039
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 parsing of Tuya electricity RAW values #157039
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
|
Hey there @tuya, @zlinoliver, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
|
Can you provide a link to the specifications? |
|
I've marked this PR a draft, awaiting the above question to be answered. Please un-draft it once it is ready for review again by clicking the "Ready for review" button. Thanks! 👍 ../Frenck |
The specifications are not available via a direct public link, but you can view them within the Tuya IoT Platform console. Here are the steps to access the definitions: Log in to Tuya IoT Platform. Navigate to: Product -> Create -> Energy -> Circuit Breaker -> TuyaOS -> Custom Solution. You will see options for v0 and v2 versions. (Note: v1 existed previously but has been superseded by v2). After creating a product, go to Function Definition. Click "Edit" on Phase A, and you will find the detailed definition in the remark field. |
Can you add a screenshot here for reference? |
Sure, here are the screenshots for reference. Note on Language: Please note that even though my platform language is set to English, the raw protocol remarks inside the "Edit" view are hardcoded in Chinese by the platform. |
epenet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all this logic would be better inside a class.
I have opened #157357 as a preliminary PR, and then from_bytes would be adjusted to handle the new structures.
|
@abelyliu I have been playing with it some more, and I have a come up with the attached diff file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @qiaoerliu
It seems you haven't yet signed a CLA. Please do so here.
Once you do that we will be able to review and accept this pull request.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @qiaoerliu
It seems you haven't yet signed a CLA. Please do so here.
Once you do that we will be able to review and accept this pull request.
Thanks!
8b944c6 to
d36339e
Compare
I have applied your raw_data_models.diff to the branch and it looks correct. Also, I noticed my previous commits were using the wrong email address (which caused the CLA failure). I have fixed the author info and force-pushed the updates. The CLA check should be passing now. |
epenet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'd like a second pair of eyes
frenck
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| current: float | ||
| power: float | ||
| voltage: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: in a follow-up PR, maybe you could parse the extra values and make the sensors available
current: float
power: float
voltage: float
reactive_power: float | None = None
apparent_power: float | None = None
power_factor: float | None = None
Proposed change
phase_aRAW payloads, supporting three protocol versions:[0x01][0x0F] + 15 bytes data.[0x02][0x0F] + 15 bytes data + 1 byte sign bitmap(bit0: current negative; bit1: active power negative; bit2: reactive negative; bit3: PF negative).dlq, productcnpkf4xdmd9v49iq) that reportphase_ain v01/v02 formats. For example, a fixture previously showing599.296 A(due to mis-parsing) now shows0.072 Afor the same payload, which matches the vendor protocol.Type of change
Additional information
tuya(categorydlq, product examplecnpkf4xdmd9v49iq).V(2B, 0.1V),I(3B, 0.001A),P(3B, 0.001kW); big-endian.[01][0F] + 15B data(Voltage, Current, Active/Reactive/Apparent Power, PF).[02][0F] + 15B data + 1B sign bitmap(bit0 current, bit1 active power, bit2 reactive, bit3 PF).08800003E8002710→ 217.6 V, 1.000 A, 10.000 kW (10,000 W).010F08800003E8002710000DAC0030D450→ 217.6 V, 1.000 A, 10,000 W.020F08800003E8002710000DAC0030D4500F→ 217.6 V, -1.000 A, -10,000 W (sign bitmap applied).tests/components/tuya/fixtures/dlq_cnpkf4xdmd9v49iq.jsonhasstatus["phase_a"] = "Ag8JJQAASAAACAAAAAAACGME"(v02). New parsing yields0.072 Ainstead of the old599.296 A.This PR fixes or closes issue: (none)
This PR is related to issue: (none)
Link to documentation pull request: (none required; behavior is a correctness fix)
Checklist
ruff format homeassistant tests).If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
requirements_all.txt. (Not applicable)I have reviewed two other open pull requests in this repository: (optional)
Notes for reviewers
homeassistant/components/tuya/sensor.pyand only affect RAWphase_afields.tests/components/tuya/test_sensor_phase_parsing.pyto assert v00/v01/v02 parsing and v02 sign handling).How I tested locally
script/run-in-env.sh pytest tests/components/tuya/test_sensor.py -k test_platform_setup_and_discovery -vvdlq_cnpkf4xdmd9v49iq.jsonare consistent with the vendor protocol.--snapshot-update.script/run-in-env.sh pytest tests/components/tuya -q.script/run-in-env.sh python -m script.hassfestscript/run-in-env.sh pylint homeassistant/components/tuya -j 2