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

Set TuyaData.dp_type automatically #2043

Merged
merged 3 commits into from
Feb 4, 2023
Merged

Conversation

Shulyaka
Copy link
Contributor

This is also a part separated from #2017

This PR adds the tuya.TuyaData.__init__() constructor that automatically maps TuyaData.dp_type from zigpy type. That allows to remove the dp_type field from the tuya.mcu.DPToAttributeMapping mapping which may simplify the things.

This is up to the developers to decide whether this is needed or not.

@codecov-commenter
Copy link

codecov-commenter commented Dec 30, 2022

Codecov Report

Base: 83.69% // Head: 83.69% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (1d0387e) compared to base (11d693e).
Patch coverage: 91.66% of modified lines in pull request are covered.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #2043   +/-   ##
=======================================
  Coverage   83.69%   83.69%           
=======================================
  Files         257      257           
  Lines        8225     8238   +13     
=======================================
+ Hits         6884     6895   +11     
- Misses       1341     1343    +2     
Impacted Files Coverage Δ
zhaquirks/tuya/ts0601_rcbo.py 98.59% <ø> (ø)
zhaquirks/tuya/ts0601_siren.py 93.96% <ø> (ø)
zhaquirks/tuya/ts0601_valve.py 92.30% <ø> (ø)
zhaquirks/tuya/__init__.py 75.33% <89.47%> (+0.45%) ⬆️
zhaquirks/tuya/mcu/__init__.py 99.50% <100.00%> (-0.02%) ⬇️
zhaquirks/tuya/ts0601_garage.py 100.00% <100.00%> (ø)
zhaquirks/tuya/ts0601_illuminance.py 100.00% <100.00%> (ø)
zhaquirks/tuya/ts0601_sensor.py 100.00% <100.00%> (ø)

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.

@coveralls
Copy link

coveralls commented Dec 30, 2022

Pull Request Test Coverage Report for Build 4091436317

  • 22 of 24 (91.67%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 83.697%

Changes Missing Coverage Covered Lines Changed/Added Lines %
zhaquirks/tuya/init.py 17 19 89.47%
Totals Coverage Status
Change from base Build 4079750249: 0.001%
Covered Lines: 6895
Relevant Lines: 8238

💛 - Coveralls

@javicalle
Copy link
Collaborator

javicalle commented Dec 30, 2022

I like the idea very much, but I'm not sure if will work.
I thought I had done tests to validate the TuyaData.dp_type, but I see that there aren't any, so not sure.

My concern is that the implementation is based on the type of the data:

isinstance(value, whatever)

And the instances of TuyaDatapointData will be created here:

Upper in code, the method will be called from here:

And data will come in a TuyaClusterData object that is defined as:

Do you see my point? (attr_value: int)

I'm pretty sure that must be a way to avoid the type instantiation as you want but I haven't found it.
In origin, we know the cluster attribute to update and its value, but not sure if a direct mapping Zigbee attribute type <--> MCU DP type will be possible.

@Shulyaka
Copy link
Contributor Author

Fixed annotation for TuyaClusterData.attr_value.

The type is set when we assign the attribute, here is an example for bool:

attr_value=bool(command_id),

@Shulyaka
Copy link
Contributor Author

Hm, that fails for python 3.8 and 3.9, checking.

@javicalle
Copy link
Collaborator

Ok, I see...
What about this?

cluster_data = TuyaClusterData(

The method is invoked when you update one cluster attribute from the 'manage cluster' view (and maybe other places). 🤔

@Shulyaka
Copy link
Contributor Author

Shulyaka commented Dec 30, 2022

What about this?

In this case we are using the type of the cluster attribute.
See https://github.com/zigpy/zigpy/blob/master/zigpy/zcl/__init__.py#L547
I believe they should also match the tuya type in most cases.

@Shulyaka
Copy link
Contributor Author

Hm, that fails for python 3.8 and 3.9, checking.

Ok, I removed the annotation fix because I couldn't easily make it work for python below 3.10. zigpy.types.Struct doesn't want to accept typing.Union.

Regardless, it looks like TuyaClusterData.attr_value can have types other than int, I don't know if it is a zigpy bug or not.

@dmulcahey
Copy link
Collaborator

Are we good with this?

@dmulcahey
Copy link
Collaborator

@Shulyaka @javicalle please let me know when this is mergable

@Shulyaka
Copy link
Contributor Author

From my perspective, it is ready. @javicalle just needs to decide if the idea worth it.

@dmulcahey
Copy link
Collaborator

@javicalle thoughts?

@javicalle
Copy link
Collaborator

I meant to do some testing and validate this change but all my installation is broken and I'm having trouble spending time to fix it.
Please let me check it out over the weekend.

@javicalle
Copy link
Collaborator

First of all, I apologize to @Shulyaka for not having been able to review the topic before 🙇🏻‍♂️

Apart from a minor comment, let's go for it!
@dmulcahey would it be possible to not include the change until the 01/25 beta release?
@Shulyaka anything I can help you with, just tell me.

Comment on lines 238 to 252
if value is None:
return
elif isinstance(value, (t.bitmap8, t.bitmap16, t.bitmap32)):
self.dp_type = TuyaDPType.BITMAP
elif isinstance(value, (bool, t.Bool)):
self.dp_type = TuyaDPType.BOOL
elif isinstance(value, enum.Enum):
self.dp_type = TuyaDPType.ENUM
elif isinstance(value, int):
self.dp_type = TuyaDPType.VALUE
elif isinstance(value, str):
self.dp_type = TuyaDPType.STRING
elif isinstance(value, t.Struct):
self.dp_type = TuyaDPType.RAW

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe a safe check here to be sure to assign the dp_type?

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 have a different idea, to set it default to RAW

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@TheJulianJES TheJulianJES added the Tuya Request/PR regarding a Tuya device label Jan 16, 2023
@dmulcahey
Copy link
Collaborator

@Shulyaka can you rebase this and see if any of the recently merged tuya quirks need changes? Once that’s done I’ll merge this and get a release produced for the beta.

@TheJulianJES TheJulianJES added the waiting for changes Waiting for changes to the PR label Jan 31, 2023
@Shulyaka
Copy link
Contributor Author

Shulyaka commented Feb 4, 2023

Done.
Sorry for the delay, I had a flu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tuya Request/PR regarding a Tuya device waiting for changes Waiting for changes to the PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants