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

Split Tuya IAS function into contact & smoke #3608

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions tests/test_tuya_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from tests.common import ClusterListener, wait_for_zigpy_tasks
import zhaquirks
from zhaquirks.tuya.builder import (
TuyaIasContact,
TuyaIasFire,
TuyaPowerConfigurationCluster2AAA,
TuyaQuirkBuilder,
TuyaRelativeHumidity,
Expand Down Expand Up @@ -46,6 +48,41 @@ def real_device(MockAppController):
return device


@pytest.mark.parametrize(
"method_name,attr_name,exp_class",
[
("tuya_battery", "power", TuyaPowerConfigurationCluster2AAA),
("tuya_metering", "smartenergy_metering", TuyaValveWaterConsumed),
("tuya_onoff", "on_off", TuyaOnOffNM),
("tuya_soil_moisture", "soil_moisture", TuyaSoilMoisture),
("tuya_temperature", "temperature", TuyaTemperatureMeasurement),
("tuya_humidity", "humidity", TuyaRelativeHumidity),
("tuya_smoke", "ias_zone", TuyaIasFire),
("tuya_contact", "ias_zone", TuyaIasContact),
],
)
async def test_convenience_methods(device_mock, method_name, attr_name, exp_class):
"""Test TuyaQuirkBuilder convenience methods."""

registry = DeviceRegistry()

entry = TuyaQuirkBuilder(
device_mock.manufacturer, device_mock.model, registry=registry
)
entry = getattr(entry, method_name)(dp_id=1)
entry.skip_configuration().add_to_registry()

quirked = registry.get_device(device_mock)
assert isinstance(quirked, CustomDeviceV2)
assert quirked in registry

ep = quirked.endpoints[1]

ep_attr = getattr(ep, attr_name)
assert ep_attr is not None
assert isinstance(ep_attr, exp_class)


async def test_tuya_quirkbuilder(device_mock):
"""Test adding a v2 Tuya Quirk to the registry and getting back a quirked device."""

Expand All @@ -60,10 +97,7 @@ class TestEnum(t.enum8):
entry = (
TuyaQuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.tuya_battery(dp_id=1)
.tuya_metering(dp_id=2)
.tuya_onoff(dp_id=3)
.tuya_soil_moisture(dp_id=4)
.tuya_temperature(dp_id=5)
.tuya_switch(
dp_id=6,
attribute_name="test_onoff",
Expand Down Expand Up @@ -97,7 +131,7 @@ class TestEnum(t.enum8):
translation_key="test_enum",
fallback_name="Test enum",
)
.tuya_humidity(dp_id=11)
.skip_configuration()
.add_to_registry()
)

Expand Down Expand Up @@ -126,24 +160,6 @@ class TestEnum(t.enum8):
assert tuya_cluster.attributes_by_name["test_sensor"].id == 0xEF09
assert tuya_cluster.attributes_by_name["test_enum"].id == 0xEF0A

assert ep.power is not None
assert isinstance(ep.power, TuyaPowerConfigurationCluster2AAA)

assert ep.smartenergy_metering is not None
assert isinstance(ep.smartenergy_metering, TuyaValveWaterConsumed)

assert ep.on_off is not None
assert isinstance(ep.on_off, TuyaOnOffNM)

assert ep.soil_moisture is not None
assert isinstance(ep.soil_moisture, TuyaSoilMoisture)

assert ep.temperature is not None
assert isinstance(ep.temperature, TuyaTemperatureMeasurement)

assert ep.humidity is not None
assert isinstance(ep.humidity, TuyaRelativeHumidity)

with mock.patch.object(
tuya_cluster.endpoint, "request", return_value=foundation.Status.SUCCESS
) as m1:
Expand Down
57 changes: 42 additions & 15 deletions zhaquirks/tuya/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@
from zhaquirks.tuya.mcu import DPToAttributeMapping, TuyaMCUCluster, TuyaOnOffNM


class TuyaIASFire(IasZone, TuyaLocalCluster):
"""Tuya local IAS cluster."""
class TuyaIasContact(IasZone, TuyaLocalCluster):
"""Tuya local IAS contact cluster."""

_CONSTANT_ATTRIBUTES = {
IasZone.AttributeDefs.zone_type.id: IasZone.ZoneType.Contact_Switch
}


class TuyaIasFire(IasZone, TuyaLocalCluster):
"""Tuya local IAS smoke/fire cluster."""

_CONSTANT_ATTRIBUTES = {
IasZone.AttributeDefs.zone_type.id: IasZone.ZoneType.Fire_Sensor
Expand Down Expand Up @@ -99,35 +107,54 @@ def tuya_battery(
self.adds(power_cfg)
return self

def tuya_metering(
self,
dp_id: int,
metering_cfg: TuyaLocalCluster = TuyaValveWaterConsumed,
) -> QuirkBuilder:
"""Add a Tuya Metering Configuration."""
self.tuya_dp(
dp_id,
metering_cfg.ep_attribute,
"current_summ_delivered",
def tuya_contact(self, dp_id: int):
"""Add a Tuya IAS contact sensor."""
self.tuya_ias(
dp_id=dp_id,
ias_cfg=TuyaIasContact,
converter=lambda x: IasZone.ZoneStatus.Alarm_1 if x != 0 else 0,
)
return self

def tuya_smoke(self, dp_id: int):
"""Add a Tuya IAS smoke/fire sensor."""
self.tuya_ias(
dp_id=dp_id,
ias_cfg=TuyaIasFire,
converter=lambda x: IasZone.ZoneStatus.Alarm_1 if x == 0 else 0,
)
self.adds(metering_cfg)
return self

def tuya_ias(
self,
dp_id: int,
ias_cfg: TuyaLocalCluster = TuyaIASFire,
ias_cfg: TuyaLocalCluster,
converter: Optional[Callable[[Any], Any]] = None,
) -> QuirkBuilder:
"""Add a Tuya IAS Configuration."""
self.tuya_dp(
dp_id,
ias_cfg.ep_attribute,
IasZone.AttributeDefs.zone_status.name,
converter=lambda x: IasZone.ZoneStatus.Alarm_1 if x == 0 else 0,
converter=converter,
)
self.adds(ias_cfg)
return self

def tuya_metering(
self,
dp_id: int,
metering_cfg: TuyaLocalCluster = TuyaValveWaterConsumed,
) -> QuirkBuilder:
"""Add a Tuya Metering Configuration."""
self.tuya_dp(
dp_id,
metering_cfg.ep_attribute,
"current_summ_delivered",
)
self.adds(metering_cfg)
return self

def tuya_onoff(
self,
dp_id: int,
Expand Down
4 changes: 2 additions & 2 deletions zhaquirks/tuya/tuya_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
.applies_to("_TZE204_vawy74yh", "TS0601")
.applies_to("_TZE284_0zaf1cr8", "TS0601")
.applies_to("_TZ3210_up3pngle", "TS0205")
.tuya_ias(dp_id=1)
.tuya_smoke(dp_id=1)
.skip_configuration()
.add_to_registry()
)

(
TuyaQuirkBuilder("_TZE204_ntcy3xu1", "TS0601")
.tuya_ias(dp_id=1)
.tuya_smoke(dp_id=1)
.tuya_dp(
dp_id=14,
ep_attribute=TuyaPowerConfigurationCluster2AAA.ep_attribute,
Expand Down
Loading