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

Tuya TS0601 _TZE200_hkdl5fmv RCBO #1991

Merged
merged 6 commits into from
Dec 12, 2022
Merged

Tuya TS0601 _TZE200_hkdl5fmv RCBO #1991

merged 6 commits into from
Dec 12, 2022

Conversation

Shulyaka
Copy link
Contributor

@Shulyaka Shulyaka commented Dec 7, 2022

Add support for Tuya ts0601 _TZE200_hkdl5fmv circuit breaker (https://aliexpress.com/item/1005004749980758.html)

Special thanks to @danieledwardgeorgehitchcock for Koenkk/zigbee-herdsman-converters#3197

image

@coveralls
Copy link

coveralls commented Dec 7, 2022

Pull Request Test Coverage Report for Build 3658331273

  • 143 of 145 (98.62%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.4%) to 83.51%

Changes Missing Coverage Covered Lines Changed/Added Lines %
zhaquirks/tuya/ts0601_rcbo.py 143 145 98.62%
Totals Coverage Status
Change from base Build 3638608563: 0.4%
Covered Lines: 6685
Relevant Lines: 8005

💛 - Coveralls

@dmulcahey
Copy link
Collaborator

Are apparent power, instantaneous demand and power factor actually supported?

@codecov-commenter
Copy link

codecov-commenter commented Dec 7, 2022

Codecov Report

Base: 83.09% // Head: 83.51% // Increases project coverage by +0.41% 🎉

Coverage data is based on head (c7df85f) compared to base (792d7e2).
Patch coverage: 98.62% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1991      +/-   ##
==========================================
+ Coverage   83.09%   83.51%   +0.41%     
==========================================
  Files         248      249       +1     
  Lines        7860     8005     +145     
==========================================
+ Hits         6531     6685     +154     
+ Misses       1329     1320       -9     
Impacted Files Coverage Δ
zhaquirks/tuya/ts0601_rcbo.py 98.62% <98.62%> (ø)
zhaquirks/tuya/__init__.py 72.94% <0.00%> (+1.04%) ⬆️
zhaquirks/tuya/mcu/__init__.py 99.50% <0.00%> (+2.46%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

They are not provided by the device, but we can calculate the apparent power in the quirk by multiplying the voltage by the current, and then we can calculate the power factor by dividing the active power by the apparent power. Should I do it?
And I have no idea what instantaneous demand is, let me recearch...

@dmulcahey
Copy link
Collaborator

ID is the current power draw from what is connected. (How much power is being used in near real time). I am more interested in why they show up if they don’t have values. It means we have something screwy somewhere either in quirks or in ZHA itself.

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

I fail to see the difference:

4.9.2.2.6.10 ActivePower
Represents the single phase or Phase A, current demand of active power delivered or received at the premises, in Watts (W). Positive values indicate power delivered to the premises where negative values indicate power received from the premises.

10.4.2.2.5.1 InstantaneousDemand Attribute
InstantaneousDemand represents the current Demand of Energy, Gas, or Water delivered or received at the premises. Positive values indicate demand delivered to the premises where negative values indicate demand received from the premises. InstantaneousDemand is updated continuously as new measurements are made.

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

I think that's something in ZHA, like if certain cluster is present, the sensors are added regardless if the respective attributes are set.

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

I've just discovered the tuya.PowerOnState class, give me a moment to rewrite the quirk to use it instead of a new class,,,

@Shulyaka Shulyaka marked this pull request as draft December 7, 2022 17:35
@Shulyaka Shulyaka marked this pull request as draft December 7, 2022 17:35
Copy link
Collaborator

@javicalle javicalle left a comment

Choose a reason for hiding this comment

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

Use the same cluster reference in all the mappings.

zhaquirks/tuya/ts0601_rcbo.py Outdated Show resolved Hide resolved
zhaquirks/tuya/ts0601_rcbo.py Outdated Show resolved Hide resolved
@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

Don't merge yet, I want to get the GUI "power on state" selector working (I just found out that it exists!)

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 7, 2022

Ah, there is a hard coded white list in ZHA code for that

@CONFIG_DIAGNOSTIC_MATCH(
    channel_names=CHANNEL_ON_OFF,
    models={"TS011F", "TS0121", "TS0001", "TS0002", "TS0003", "TS0004"},
)
@CONFIG_DIAGNOSTIC_MATCH(
    channel_names="tuya_manufacturer",
    manufacturers={
        "_TZE200_7tdtqgwv",
        "_TZE200_amp6tsvy",
        "_TZE200_oisqyl4o",
        "_TZE200_vhy3iakz",
        "_TZ3000_uim07oem",
        "_TZE200_wfxuhoea",
        "_TZE200_tviaymwx",
        "_TZE200_g1ib5ldv",
        "_TZE200_wunufsil",
        "_TZE200_7deq70b8",
        "_TZE200_tz32mtza",
        "_TZE200_2hf7x9n3",
        "_TZE200_aqnazj70",
        "_TZE200_1ozguk6x",
        "_TZE200_k6jhsr0q",
        "_TZE200_9mahtqtg",
    },
)
class TuyaPowerOnStateSelectEntity(ZCLEnumSelectEntity, id_suffix="power_on_state"):
    """Representation of a ZHA power on state select entity."""

    _select_attr = "power_on_state"
    _enum = TuyaPowerOnState
    _attr_name = "Power on state"

Ok, will raise a separate request to ZHA. This PR is ready from my side.

@Shulyaka Shulyaka marked this pull request as ready for review December 7, 2022 22:53
@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 9, 2022

@dmulcahey Shall I include apparent power and power factor calculation into the quirk?

изображение

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 9, 2022

I decided to include it, because it's just cool:)

@dmulcahey dmulcahey merged commit d6a3fb9 into zigpy:dev Dec 12, 2022
@dmulcahey
Copy link
Collaborator

@Shulyaka I merged this into the current release and all the tests are now failing... Can you take a peek?

@Shulyaka
Copy link
Contributor Author

Strange. Let me check...

@dmulcahey
Copy link
Collaborator

FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre0-\x01\x02\x00\x00\x01\t\x02\x00\x04X\x02\x00\x00-on_off-attributes0] - AssertionError: expected call not found.
[1543](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1544)
Expected: request(61184, 2, b'\x01\x02\x00\x00\x01\t\x02\x00\x04X\x02\x00\x00', expect_reply=False, command_id=0)
[1544](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1545)
Actual: request(61184, 2, b'\x01\x02\x00\x00\x01\t\x02\x00\x04\x00\x00\x02X', expect_reply=False, command_id=0)
[1545](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1546)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre4-\x01\x06\x00\x00\x03p\x00\x00\x03Z\x00\x00-device_temperature-attributes4] - AssertionError: expected call not found.
[1546](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1547)
Expected: request(61184, 6, b'\x01\x06\x00\x00\x03p\x00\x00\x03Z\x00\x00', expect_reply=False, command_id=0)
[1547](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1548)
Actual: request(61184, 6, b'\x01\x06\x00\x00\x03p\x00\x00\x03\x00\x00Z', expect_reply=False, command_id=0)
[1548](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1549)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre5-\x01\x0e\x00\x00\x07m\x00\x00\x08\x01\x00\x00\x01,\x00\x00\x00-electrical_measurement-attributes5] - AssertionError: expected call not found.
[1549](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1550)
Expected: request(61184, 14, b'\x01\x0e\x00\x00\x07m\x00\x00\x08\x01\x00\x00\x01,\x00\x00\x00', expect_reply=False, command_id=0)
[1550](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1551)
Actual: request(61184, 14, b'\x01\x0e\x00\x00\x07m\x00\x00\x08\x00\x00\x00,\x01\x00\x00\x01', expect_reply=False, command_id=0)
[1551](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1552)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre6-\x01\n\x00\x00\x06n\x00\x00\x08\x0b"\x00\x00\x01\xf4\x00\x00-electrical_measurement-attributes6] - AssertionError: expected call not found.
[1552](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1553)
Expected: request(61184, 10, b'\x01\n\x00\x00\x06n\x00\x00\x08\x0b"\x00\x00\x01\xf4\x00\x00', expect_reply=False, command_id=0)
[1553](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1554)
Actual: request(61184, 10, b'\x01\n\x00\x00\x06n\x00\x00\x08\x00\x00\xf4\x01\x00\x00"\x0b', expect_reply=False, command_id=0)
[1554](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1555)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre7-\x01\x08\x00\x00\x04o\x00\x00\x05\x01_\x90\x01\x01-electrical_measurement-attributes7] - AssertionError: expected call not found.
[1555](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1556)
Expected: request(61184, 8, b'\x01\x08\x00\x00\x04o\x00\x00\x05\x01_\x90\x01\x01', expect_reply=False, command_id=0)
[1556](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1557)
Actual: request(61184, 8, b'\x01\x08\x00\x00\x04o\x00\x00\x05\x01\x01\x90_\x01', expect_reply=False, command_id=0)
[1557](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1558)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre8-\x01\x02\x00\x00\x01o\x00\x00\x05\x01\x86\xa0\x00\x01-electrical_measurement-attributes8] - AssertionError: expected call not found.
[1558](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1559)
Expected: request(61184, 2, b'\x01\x02\x00\x00\x01o\x00\x00\x05\x01\x86\xa0\x00\x01', expect_reply=False, command_id=0)
[1559](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1560)
Actual: request(61184, 2, b'\x01\x02\x00\x00\x01o\x00\x00\x05\x01\x00\xa0\x86\x01', expect_reply=False, command_id=0)
[1560](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1561)
FAILED tests/test_tuya_rcbo.py::test_write_attr_rcbo[frames_pre9-\x01\x04\x00\x00\x02l\x00\x00\x03\x14\xb4\x01-smartenergy_metering-attributes9] - AssertionError: expected call not found.
[1561](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1562)
Expected: request(61184, 4, b'\x01\x04\x00\x00\x02l\x00\x00\x03\x14\xb4\x01', expect_reply=False, command_id=0)
[1562](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1563)
Actual: request(61184, 4, b'\x01\x04\x00\x00\x02l\x00\x00\x03\x01\xb4\x14', expect_reply=False, command_id=0)
[1563](https://github.com/zigpy/zha-device-handlers/actions/runs/3677444435/jobs/6219418961#step:8:1564)
Error: Process completed with exit code 1.

@Shulyaka
Copy link
Contributor Author

Something to do with the byte order

@dmulcahey
Copy link
Collaborator

@dmulcahey
Copy link
Collaborator

image

@dmulcahey
Copy link
Collaborator

Will this break your prior PR too?

@javicalle
Copy link
Collaborator

I have reverted a PR that mess with the TuyaDPType.VALUE, maybe related?

@Shulyaka
Copy link
Contributor Author

Yes, they are failing because of #1999, which was not merged when I submitted this PR.
I can create a workaround, then after we reapply #1984 we can revert it as well.

@javicalle
Copy link
Collaborator

Can I help in some way?

@dmulcahey
Copy link
Collaborator

Yes, they are failing because of #1999, which was not merged when I submitted this PR.
I can create a workaround, then after we reapply #1984 we can revert it as well.

do I need to kill the HA PR w/ the release and do another?

@Shulyaka
Copy link
Contributor Author

It's up to you. But only this device is affected.

@dmulcahey
Copy link
Collaborator

ok then I'll just bump again once it's fixed

@Shulyaka
Copy link
Contributor Author

Do you plan to reapply #1984 some day when the side effects are sorted out?

@Shulyaka
Copy link
Contributor Author

My understanding is the following:

  1. Tuya uses big-endian int types.
  2. Because zigbee types are little-endian, some tuya quirks made a workaround to reverse byte order. But not all of them.
  3. @javicalle made a fix (Fix TuyaDPType.VALUE payload #1984) to reverse the byte order for all quirks, but that broke those quirks with the workaround, because the values became double-reversed.
  4. The fix was reverted (Revert PR #1984 #1999) which in turn broke this PR.

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 12, 2022

By the way there are now big endian types in zigpy: zigpy/zigpy#1124

@dmulcahey
Copy link
Collaborator

Do you plan to reapply #1984 some day when the side effects are sorted out?

This is a question for @javicalle I try to stay out of the Tuya stuff...

@javicalle
Copy link
Collaborator

  1. Tuya uses big-endian int types.
  2. Because zigbee types are little-endian, some tuya quirks made a workaround to reverse byte order. But not all of them.

I have to admit that I'm not familar with all that endians, but my understanding is that all the quirks using the TuyaNewManufCluster (and descendants) made the reverse byte order for the TuyaDPType.VALUE (and the BOOL, but that doesn't matter)

  1. @javicalle made a fix (Fix TuyaDPType.VALUE payload #1984) to reverse the byte order for all quirks, but that broke those quirks with the workaround, because the values became double-reversed.

I was trying to figure out what the problem was with #1554 and in my analysis I have detected a problem with the reverse of TuyaDPType.VALUE working only in one direction:

  1. this code deserialize the Tuya message and reverse all TuyaDPType except the TuyaDPType.VALUE:

    res.raw, data = t.LVBytes.deserialize(data)
    if res.dp_type not in (
    TuyaDPType.BITMAP,
    TuyaDPType.STRING,
    TuyaDPType.ENUM,
    TuyaDPType.RAW,
    ):
    res.raw = res.raw[::-1]

  2. at this part, the TuyaData generation inverts all the bytes for every TuyaDPType (included VALUE):

    tuya_data.raw = t.LVBytes.deserialize(val)[0]

I have to clarify that not all the quirks use these classes and not all the ones that use the classes have the problem.

  1. The fix was reverted (Revert PR #1984 #1999) which in turn broke this PR.

That's correct.
Your PR was already affected for #1984 and you have to fix your code because of that. I can't find it but I remember causing the problem.

I believe that most of the test fails because the expected value on test, but I try to review your code again to search for problems in the DPType.

@Shulyaka
Copy link
Contributor Author

@javicalle please check #2009

@javicalle
Copy link
Collaborator

Do you plan to reapply #1984 some day when the side effects are sorted out?

I have a new version, but I haven't found any user able to test.
But once your PR is applied that would be my problem 😉

@javicalle please check #2009

Already doing.

@Shulyaka
Copy link
Contributor Author

I have a new version, but I haven't found any user able to test.

Please tag me when it is ready, I would like to check it as well if you don't mind. By the way we also reverse the endianess in Data.from_value().

@javicalle
Copy link
Collaborator

By the way we also reverse the endianess in Data.from_value().

I had missed that call. Thanks for pointing me.
The truth is that the whole implementation of the from_cluster_data is quite disastrous (my fault) 😞

@Shulyaka Shulyaka mentioned this pull request Dec 18, 2022
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.

5 participants