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

Add device support for TS110E Model QS-Zigbee-D02-TRIAC-LN #1422

Merged
merged 7 commits into from
Jul 8, 2022

Conversation

jonnylangefeld
Copy link
Contributor

This device uses a non-default set level command.

This device looks exactly the same as TS110F but seems to have different zigbee command IDs. This is TS110F on blakadder: https://zigbee.blakadder.com/Lonsonho_QS-Zigbee-D02-TRIAC-LN.html

Tested in local docker environment.
Thanks to @javicalle this was possible.
Fix #1415

This device uses a non-default set level command.
Tested in local docker environment.
Thanks to @javicalle this was possible.
Fix zigpy#1415
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.

A little fix for the checks

Scenes.cluster_id,
OnOff.cluster_id,
F000LevelControlCluster,
TuyaZBExternalSwitchTypeCluster.cluster_id,
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove the '.cluster_id' part:

                    TuyaZBExternalSwitchTypeCluster,

Copy link
Contributor

Choose a reason for hiding this comment

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

I think first testing if the cluster / attribute is working on the device or not and is its not working then deleting it in the replacement (its always possible adding it later if its needed) but its very likely not working on this device.

The implementation is here:

# Tuya Zigbee Cluster 0xE001 Implementation
class ExternalSwitchType(t.enum8):
"""Tuya external switch type enum."""
Toggle = 0x00
State = 0x01
Momentary = 0x02
class TuyaZBExternalSwitchTypeCluster(CustomCluster):
"""Tuya External Switch Type Cluster."""
name = "Tuya External Switch Type Cluster"
cluster_id = TUYA_CLUSTER_E001_ID
ep_attribute = "tuya_external_switch_type"
attributes = {0xD030: ("external_switch_type", ExternalSwitchType)}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually noticed that I had to leave in the .cluster_id part, otherwise the quirk wouldn't show up on the device anymore.

Comment on lines +32 to +36
class TuyaLevelPayload(t.Struct):
"""Tuya Level payload."""

level: t.uint16_t
transtime: t.uint16_t
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should probably move to tuya.__init__.py, but let's keep it here for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or first do the implementation in the quirk and getting it merged and its working OK and then making one more PR for moving it to tuya INIT then tuya INIT is little tricky with many addings and can needing rebasing many times and its PITA (i have one PR waiting for being merged to it).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's the idea.
Keep here and merge. When required, move to Tuya INIT. 👍🏻

HALOGEN = 0x02


class F000LevelControlCluster(LevelControl):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This LevelControl part is actually making issues. Because it applies to all dimmers in my house now, even the non-Tuya ones. And those obviously don't need this quirk. I think I somehow need to incorporate CustomCluster somewhere. But just adding class F000LevelControlCluster(CustomCluster, LevelControl): doesn't work.
@javicalle do you know how I can change this quirk to only work with the Custom device DimmerSwitchWithNeutral1Gang below?

Copy link
Collaborator

@javicalle javicalle Jun 12, 2022

Choose a reason for hiding this comment

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

Not sure what you mean. The F000LevelControlCluster cluster definition is only used in the DimmerSwitchWithNeutral1Gang quirk. An this quirk only applies to _TZ3210_ngqk6jia device.

What is exactly the problem? When they started? Have you updated HA?

If you believe that there's a problem with your quirk, try to remove it an check if the problem persist, but I don't know how that quirk can affect other devices than the _TZ3210_ngqk6jia.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the zhaquirks.tuya.ts110e.DimmerSwitchWithNeutral1Gang quirk didn't show up on the new dimmer (non tuya) I bought, but the F000LevelControlCluster showed up on the new dimmer I bought. I found that that is because we just used class F000LevelControlCluster(LevelControl):, without implementing a CustomCluster.
I have experimented some more and found that adding CustomCluster as mentioned in my last comment didn't work, because that sent the manufacturer ID to the device and the device didn't recognize that. I found out by continuously comparing the logs between the setting that worked and the one that didn't work.

Long story short, I had to add NoManufacturerCluster to make it work. That is only in the later versions of zha-device-handlers, so I had to update my entire home-assistance instance. I have done that now and updated my quirk to use class F000LevelControlCluster(NoManufacturerCluster, LevelControl):. And that ended up doing what I wanted which is that the F000LevelControlCluster only applies to the tuya dimmer and not to others.

I will update this PR accordingly as there are some other changes with the latest updates on the dev branch.

@codecov-commenter
Copy link

Codecov Report

Merging #1422 (25ab689) into dev (e3fa034) will decrease coverage by 0.23%.
The diff coverage is 72.50%.

@@            Coverage Diff             @@
##              dev    #1422      +/-   ##
==========================================
- Coverage   80.52%   80.29%   -0.24%     
==========================================
  Files         222      233      +11     
  Lines        6414     7018     +604     
==========================================
+ Hits         5165     5635     +470     
- Misses       1249     1383     +134     
Impacted Files Coverage Δ
zhaquirks/tuya/ts110e.py 72.50% <72.50%> (ø)
zhaquirks/tuya/ts004f.py 80.64% <0.00%> (-19.36%) ⬇️
zhaquirks/tuya/ts0601_siren.py 93.96% <0.00%> (-6.04%) ⬇️
zhaquirks/xiaomi/aqara/tvoc.py 73.80% <0.00%> (-2.67%) ⬇️
zhaquirks/xiaomi/aqara/roller_curtain_e1.py 59.70% <0.00%> (-2.57%) ⬇️
zhaquirks/__init__.py 77.89% <0.00%> (-1.48%) ⬇️
zhaquirks/tuya/mcu/__init__.py 98.77% <0.00%> (-1.23%) ⬇️
zhaquirks/xiaomi/aqara/plug_mmeu01.py 78.12% <0.00%> (-0.83%) ⬇️
zhaquirks/xiaomi/__init__.py 82.37% <0.00%> (-0.81%) ⬇️
zhaquirks/xiaomi/mija/smoke.py 89.47% <0.00%> (-0.53%) ⬇️
... and 62 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3fa034...25ab689. Read the comment docs.

@coveralls
Copy link

coveralls commented Jun 13, 2022

Pull Request Test Coverage Report for Build 2489301802

  • 29 of 40 (72.5%) changed or added relevant lines in 1 file are covered.
  • 604 unchanged lines in 29 files lost coverage.
  • Overall coverage decreased (-0.2%) to 80.294%

Changes Missing Coverage Covered Lines Changed/Added Lines %
zhaquirks/tuya/ts110e.py 29 40 72.5%
Files with Coverage Reduction New Missed Lines %
zhaquirks/tuya/mcu/init.py 2 98.17%
zhaquirks/xiaomi/aqara/ctrl_ln.py 2 88.89%
zhaquirks/xiaomi/mija/smoke.py 2 89.47%
zhaquirks/aurora/aurora_dimmer.py 3 82.61%
zhaquirks/tuya/ts0601_electric_heating.py 3 57.58%
zhaquirks/danfoss/thermostat.py 4 82.76%
zhaquirks/tuya/ts130f.py 4 83.02%
zhaquirks/xiaomi/aqara/opple_remote.py 5 88.89%
zhaquirks/konke/init.py 6 75.68%
zhaquirks/tuya/ts004f.py 6 80.65%
Totals Coverage Status
Change from base Build 1947732782: -0.2%
Covered Lines: 5635
Relevant Lines: 7018

💛 - Coveralls

@jonnylangefeld
Copy link
Contributor Author

After lots of testing I'm pretty confident this is working code now. All the checks are succeeding too. I've also updated it to work with the new attributes and server_commands (vs the old manufacturer_server_attributes and manufacturer_attributes). @javicalle should we merge it?

}
)

# 0xF000 reported values are 10-1000, convert to 0-254
Copy link
Collaborator

Choose a reason for hiding this comment

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

The conversion is 1-255 (which I think is the range that HA handles).

And the values 10 & 1000 I think correspond to the manufacturer_min_level and manufacturer_max_level values.
If someone modifies the values that conversion can do strange things.
The truth is that I don't know what could be the best way to handle this situation, but it is probably something quite common in dimmers that would be addressed in a generic way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've experimented with this quite a while and this is what worked best for me. Would you agree to merging this as is (because it seems to work with the default manufacturer_min_level and manufacturer_max_level) and if someone in the future comes in and finds a better approach to calculate the values we just update the formula? At least this PR supports the device in the first place 😊

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree. We can go ahead with the version as is.

Comment on lines +112 to +118
return await super().command(
TUYA_CUSTOM_LEVEL_COMMAND,
TuyaLevelPayload(level=brightness, transtime=0),
manufacturer=manufacturer,
expect_reply=expect_reply,
tsn=tsn,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine to me, but probably you can get the same result without extending the NoManufacturerCluster and putting here:

manufacturer=foundation.ZCLHeader.NO_MANUFACTURER_ID,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm that doesn't work because then this Class would also show up as cluster in the dimmers of other devices. F000LevelControlCluster has to inherit in some way from CustomCluster and NoManufacturerCluster does that for us (including setting manufacturer=foundation.ZCLHeader.NO_MANUFACTURER_ID,).

Copy link
Collaborator

Choose a reason for hiding this comment

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

this Class would also show up as cluster in the dimmers of other devices

That sentence not make sense to me. A quirk shouldn't modify other devices that don't use the quirk.
Trying to figure out how can be done I believe that some code like this could make the described behaviour:

    # attributes = LevelControl.attributes.copy()
    attributes.update(
        {
            # 0xF000
            TUYA_LEVEL_ATTRIBUTE: ("manufacturer_current_level", t.uint16_t),
        }
    )

If not making a copy of parent class attributes, maybe the code is modifying the parent attributes and changes for any device that uses the parent class 🤔

In any case, your proposed code is fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah I don’t know, but it did show up as level control cluster on this new dimmer I bought…

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.

LGTM

@javicalle javicalle added the needs final review PR is ready for a final review from a maintainer label Jun 15, 2022
@dmulcahey dmulcahey merged commit 5bbe4e0 into zigpy:dev Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs final review PR is ready for a final review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Device Support Request] Device support for Tuya TS110E
6 participants