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 quirk for the TS0207 rain solar sensor #3475

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

aarongorka
Copy link

Proposed change

Adds a quirk that fixes this device by changing the "moving" and "closed" sensors to the rain and illuminance sensors that the device actually has.

image

Additional information

Most of the credit for this code goes to @hadeshimself from this comment.

Fixes #3249.

Checklist

  • The changes are tested and work correctly
  • pre-commit checks pass / the code has been formatted using Black
  • Tests have been added to verify that the new code works

Copy link

codecov bot commented Oct 30, 2024

Codecov Report

Attention: Patch coverage is 87.23404% with 6 lines in your changes missing coverage. Please review.

Project coverage is 89.43%. Comparing base (728ee42) to head (e1fa887).
Report is 34 commits behind head on dev.

Files with missing lines Patch % Lines
zhaquirks/tuya/ts0207.py 87.23% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #3475      +/-   ##
==========================================
- Coverage   89.44%   89.43%   -0.02%     
==========================================
  Files         311      312       +1     
  Lines       10033    10080      +47     
==========================================
+ Hits         8974     9015      +41     
- Misses       1059     1065       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Adds a quirk that fixes this device by changing the "moving" and
"closed" sensors to the rain and illuminance sensors that the device
actually has.

Most of the credit for this code goes to @hadeshimself from
[this](zigpy#3249 (comment))
comment.

Fixes zigpy#3249.
@aarongorka
Copy link
Author

FWIW I did briefly look at adding a test at least for the illuminance measurement getting copied in to a new cluster but I couldn't really get my head around how the existing tests are working.

@Fredyy90
Copy link

would be great is someone with the knowledeg, how the test should look like, could chip into this pr, currently my sensor is useless in homeassitant/zah until this quirk is live

@TheJulianJES TheJulianJES added the Tuya Request/PR regarding a Tuya device label Nov 24, 2024
Copy link
Collaborator

@TheJulianJES TheJulianJES left a comment

Choose a reason for hiding this comment

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

We might be able to do this cleaner with the new TuyaQuirkBuilder for v2 quirks.
Some WIP docs are here (from #3480). You can also search for TuyaQuirkBuilder in the repo to see existing quirks use this.

@TheJulianJES TheJulianJES added the v2 quirk candidate This PR is a good fit to be converted to a v2 quirk. label Nov 24, 2024
@prairiesnpr
Copy link
Contributor

prairiesnpr commented Nov 25, 2024

Just to give you something to start with, a v2 version might look something like this. Untested, I don't have the device.

"""Quirk for TS0207 rain sensors."""

from zigpy.quirks.v2.homeassistant import LIGHT_LUX, EntityType
from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass
import zigpy.types as t
from zigpy.zcl.clusters.measurement import IlluminanceMeasurement
from zigpy.zcl.clusters.security import IasZone

from zhaquirks.tuya import TuyaLocalCluster
from zhaquirks.tuya.builder import TuyaQuirkBuilder


class TuyaIasZone(IasZone, TuyaLocalCluster):
    """IAS Zone for rain sensors."""

    _CONSTANT_ATTRIBUTES = {
        IasZone.AttributeDefs.zone_type: IasZone.ZoneType.Water_Sensor
    }


class TuyaIlluminanceCluster(IlluminanceMeasurement, TuyaLocalCluster):
    """Tuya Illuminance cluster."""


(
    TuyaQuirkBuilder("_TZ3210_tgvtvdoc", "TS0207")
    .tuya_dp(
        dp_id=101,
        ep_attribute=TuyaIlluminanceCluster,
        attribute_name=TuyaIlluminanceCluster.AttributeDefs.measured_value.name,
        converter=lambda x: x * 7,
    )
    .adds(TuyaIlluminanceCluster)
    .tuya_sensor(
        dp_id=102,
        attribute_name="average_light_intensity_20mins",
        type=t.uint32_t,
        state_class=SensorStateClass.MEASUREMENT,
        device_class=SensorDeviceClass.DURATION,
        unit=LIGHT_LUX,
        entity_type=EntityType.DIAGNOSTIC,
        translation_key="average_light_intensity_20mins",
        fallback_name="Average light intensity last 20 min",
    )
    .tuya_sensor(
        dp_id=103,
        attribute_name="todays_max_light_intensity",
        type=t.uint32_t,
        state_class=SensorStateClass.MEASUREMENT,
        device_class=SensorDeviceClass.DURATION,
        unit=LIGHT_LUX,
        entity_type=EntityType.DIAGNOSTIC,
        translation_key="todays_max_light_intensity",
        fallback_name="Todays max light intensity",
    )
    .tuya_binary_sensor(
        dp_id=104,
        attribute_name="cleaning_reminder",
        translation_key="cleaning_reminder",
        fallback_name="Cleaning reminder",
    )
    .tuya_dp(
        dp_id=105,
        ep_attribute=TuyaIasZone.ep_attribute,
        attribute_name=TuyaIasZone.AttributeDefs.zone_state.name,
    )
    .adds(TuyaIasZone)
    .skip_configuration()
    .add_to_registry()
)

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 v2 quirk candidate This PR is a good fit to be converted to a v2 quirk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Device Support Request] Tuya rainsensor TS0207_TZ3210_tgvtvdoc
4 participants