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 support for _TZE200_yi4jtqq1 illuminance sensor. #1795

Merged
merged 8 commits into from
Oct 22, 2022

Conversation

gmsoft-tuxicoman
Copy link
Contributor

This is a simple illuminance sensort with a maximum of 1000 lumen. So it's only really suited for inside, not outside.

The quirk provide both the raw lumen value as well as the low/medium/high brightness level.

@coveralls
Copy link

coveralls commented Sep 29, 2022

Pull Request Test Coverage Report for Build 3291717546

  • 25 of 25 (100.0%) changed or added relevant lines in 1 file are covered.
  • 27 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.5%) to 80.509%

Files with Coverage Reduction New Missed Lines %
zhaquirks/xiaomi/init.py 27 83.79%
Totals Coverage Status
Change from base Build 3212112661: 0.5%
Covered Lines: 6043
Relevant Lines: 7506

💛 - Coveralls

@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2022

Codecov Report

Base: 80.00% // Head: 80.50% // Increases project coverage by +0.50% 🎉

Coverage data is based on head (69aec27) compared to base (2292c4e).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1795      +/-   ##
==========================================
+ Coverage   80.00%   80.50%   +0.50%     
==========================================
  Files         239      241       +2     
  Lines        7430     7506      +76     
==========================================
+ Hits         5944     6043      +99     
+ Misses       1486     1463      -23     
Impacted Files Coverage Δ
zhaquirks/tuya/ts0601_illuminance.py 100.00% <100.00%> (ø)
zhaquirks/tuya/ts001x.py 100.00% <0.00%> (ø)
zhaquirks/lidl/TS0501A.py 100.00% <0.00%> (ø)
zhaquirks/lixee/zlinky.py 100.00% <0.00%> (ø)
zhaquirks/lixee/__init__.py 100.00% <0.00%> (ø)
zhaquirks/inovelli/VZM31SN.py 100.00% <0.00%> (ø)
zhaquirks/tuya/air/ts0601_air_quality.py 100.00% <0.00%> (ø)
zhaquirks/xiaomi/aqara/plug_mmeu01.py
zhaquirks/gledopto/glc009.py 100.00% <0.00%> (ø)
zhaquirks/xiaomi/aqara/plug_eu.py 100.00% <0.00%> (ø)
... and 1 more

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.

@dmulcahey
Copy link
Collaborator

Please rebase this and install / run the precommit checks

@dmulcahey
Copy link
Collaborator

@javicalle lmk what you think when you have a moment please. Thanks!

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.

These would be the minimum requirements to review.

Comment on lines 22 to 23
TUYA_BRIGHTNESS_LEVEL_ATTR = 0x01 # 0-2 "Low, Medium, High"
TUYA_ILLUMINANCE_ATTR = 0x02 # [0, 0, 3, 232] illuminance
Copy link
Collaborator

Choose a reason for hiding this comment

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

No problem with setting as constants, but I would prefer not to use the _ATTR sufix because it can be confusing (they are not clusters attributes).
Replace all around with a DataPoint nomenclature:

TUYA_BRIGHTNESS_LEVEL_DP = 0x01  # 0-2 "Low, Medium, High"
TUYA_ILLUMINANCE_DP = 0x02  # [0, 0, 3, 232] illuminance

attributes = IlluminanceMeasurement.attributes.copy()
attributes.update(
{
0xFF00: ("brightness_level", t.CharacterString, True),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The illuminanceMeasurement cluster don't have any reference to brightness_level so not sure if that could be the correct attribute name 🤔
We can keep the reference for now but I would replace for manufacturer_brightness_level or manufacturer_measured_value.

TUYA_BRIGHTNESS_LEVEL_ATTR = 0x01 # 0-2 "Low, Medium, High"
TUYA_ILLUMINANCE_ATTR = 0x02 # [0, 0, 3, 232] illuminance

TUYA_BRIGHTNESS_LEVEL_STR = ["Low", "Medium", "High"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about if we use:

class BrightnessLevel(t.enum8):
    """Brightness level enum."""

    LOW = 0x00
    MEDIUM = 0x01
    HIGH = 0x02

That way you can define the attribute as:

            0xFF00: ("brightness_level", BrightnessLevel, True),

And convert as:

    dp_to_attribute: Dict[int, DPToAttributeMapping] = {
        TUYA_BRIGHTNESS_LEVEL_ATTR: DPToAttributeMapping(
            TuyaIlluminanceMeasurement.ep_attribute,
            "brightness_level",
            lambda x: BrightnessLevel(x),
        ),

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.

I intend to migrate the class hierarchy for these types of devices, so I am going to ask you to make the following changes.
These changes are not a prerequisite, but they will help us with future maintenance.

If you're going to tackle the changes, I'd suggest making the other changes first, then attacking these changes.

Comment on lines 39 to 58
class TuyaIlluminanceCluster(TuyaNewManufCluster):
"""Tuya Illuminance cluster."""

dp_to_attribute: Dict[int, DPToAttributeMapping] = {
TUYA_BRIGHTNESS_LEVEL_ATTR: DPToAttributeMapping(
TuyaIlluminanceMeasurement.ep_attribute,
"brightness_level",
lambda x: TUYA_BRIGHTNESS_LEVEL_STR[x],
),
TUYA_ILLUMINANCE_ATTR: DPToAttributeMapping(
TuyaIlluminanceMeasurement.ep_attribute,
"measured_value",
lambda x: (10000.0 * math.log10(x) + 1.0 if x != 0 else 0),
),
}

data_point_handlers = {
TUYA_BRIGHTNESS_LEVEL_ATTR: "_dp_2_attr_update",
TUYA_ILLUMINANCE_ATTR: "_dp_2_attr_update",
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You will need to change the imports statements to the new ones:

from zhaquirks.tuya import TuyaLocalCluster
from zhaquirks.tuya.mcu import (
    DPToAttributeMapping,
    TuyaDPType,
    TuyaMCUCluster,
)

Change your TuyaIlluminanceCluster class declaration to something like:

class TuyaIlluminanceCluster(TuyaMCUCluster):
    """Tuya Illuminance cluster."""

    dp_to_attribute: Dict[int, DPToAttributeMapping] = {
        TUYA_BRIGHTNESS_LEVEL_ATTR: DPToAttributeMapping(
            TuyaIlluminanceMeasurement.ep_attribute,
            "brightness_level",  # or the new name for attribute
            dp_type=TuyaDPType.ENUM,
            converter=lambda x: BrightnessLevel(x),
        ),
        TUYA_ILLUMINANCE_ATTR: DPToAttributeMapping(
            TuyaIlluminanceMeasurement.ep_attribute,
            "measured_value",
            dp_type=TuyaDPType.VALUE,
            converter=lambda x: (10000.0 * math.log10(x) + 1.0 if x != 0 else 0),
        ),
    }

    data_point_handlers = {
        TUYA_BRIGHTNESS_LEVEL_ATTR: "_dp_2_attr_update",
        TUYA_ILLUMINANCE_ATTR: "_dp_2_attr_update",
    }

Thats all.

@javicalle
Copy link
Collaborator

Not happy with another ts0601_whatever.py file, but not sure if there is a better fit. Keep as is for now.

The TuyaIlluminanceMeasurement must be moved to a generic class.
I like the security code check, but not sure if it is correct (I believe that would be something like if x > 1 else 0 or maybe a parenthesis is needed here).
Keep as is and I will move in another PR.

The device doesn't have a battery report?
The new implementation would log any not implemented report (warning level).

@gmsoft-tuxicoman
Copy link
Contributor Author

I've made all the changes.

This device doesn't have a battery sensor as it's USB powered.

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.

Just one left.

attributes = IlluminanceMeasurement.attributes.copy()
attributes.update(
{
0xFF00: ("brightness_level", BrightnessLevel, True),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe manufacturer_brightness_level here?

Must be the same value as in dp_to_attribute mapping.

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

Thanks for your work and patience.

@javicalle javicalle added the needs final review PR is ready for a final review from a maintainer label Oct 20, 2022
@dmulcahey dmulcahey merged commit cb10ea0 into zigpy:dev Oct 22, 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.

5 participants