Skip to content

Commit

Permalink
Add Nous (Tuya) SZ-T04 (#3683)
Browse files Browse the repository at this point in the history
  • Loading branch information
prairiesnpr authored Jan 23, 2025
1 parent 6c5fc22 commit c99f86b
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 126 deletions.
59 changes: 57 additions & 2 deletions tests/test_tuya_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for TuyaQuirkBuilder."""

import datetime
from unittest import mock

import pytest
Expand All @@ -9,10 +10,11 @@
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import Basic, BatterySize

from tests.common import ClusterListener, wait_for_zigpy_tasks
from tests.common import ClusterListener, MockDatetime, wait_for_zigpy_tasks
import zhaquirks
from zhaquirks.tuya import (
TUYA_QUERY_DATA,
TUYA_SET_TIME,
TuyaPowerConfigurationCluster,
TuyaPowerConfigurationCluster2AAA,
)
Expand All @@ -31,6 +33,9 @@
TuyaValveWaterConsumed,
)
from zhaquirks.tuya.mcu import TuyaMCUCluster, TuyaOnOffNM
from zhaquirks.tuya.tuya_sensor import NoManufTimeTuyaMCUCluster

ZCL_TUYA_SET_TIME = b"\x09\x12\x24\x0d\x00"

zhaquirks.setup()

Expand Down Expand Up @@ -140,6 +145,9 @@ class TestEnum(t.enum8):
A = 0x00
B = 0x01

class ModTuyaMCUCluster(TuyaMCUCluster):
"""Modified Cluster."""

entry = (
TuyaQuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.tuya_battery(dp_id=1)
Expand Down Expand Up @@ -178,7 +186,7 @@ class TestEnum(t.enum8):
fallback_name="Test enum",
)
.skip_configuration()
.add_to_registry()
.add_to_registry(replacement_cluster=ModTuyaMCUCluster)
)

# coverage for overridden __eq__ method
Expand All @@ -195,6 +203,7 @@ class TestEnum(t.enum8):
assert isinstance(ep.basic, Basic)

assert ep.tuya_manufacturer is not None
assert isinstance(ep.tuya_manufacturer, ModTuyaMCUCluster)
assert isinstance(ep.tuya_manufacturer, TuyaMCUCluster)

tuya_cluster = ep.tuya_manufacturer
Expand Down Expand Up @@ -303,3 +312,49 @@ async def test_tuya_spell(device_mock, read_attr_spell, data_query_spell):
messages += 1

request_mock.reset_mock()


async def test_tuya_mcu_set_time(device_mock):
"""Test TuyaQuirkBuilder replacement cluster, set_time requests (0x24) messages for MCU devices."""

registry = DeviceRegistry()

(
TuyaQuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.tuya_battery(dp_id=1)
.skip_configuration()
.add_to_registry(replacement_cluster=NoManufTimeTuyaMCUCluster)
)

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

ep = quirked.endpoints[1]

assert not ep.tuya_manufacturer._is_manuf_specific
assert not ep.tuya_manufacturer.server_commands[
TUYA_SET_TIME
].is_manufacturer_specific

# Mock datetime
origdatetime = datetime.datetime
datetime.datetime = MockDatetime

# simulate a SET_TIME message
hdr, args = ep.tuya_manufacturer.deserialize(ZCL_TUYA_SET_TIME)
assert hdr.command_id == TUYA_SET_TIME

with mock.patch.object(
ep.tuya_manufacturer._endpoint,
"request",
return_value=foundation.Status.SUCCESS,
) as m1:
ep.tuya_manufacturer.handle_message(hdr, args)
await wait_for_zigpy_tasks()

res_hdr = foundation.ZCLHeader.deserialize(m1.await_args[1]["data"])
assert not res_hdr[0].manufacturer
assert not res_hdr[0].frame_control.is_manufacturer_specific

datetime.datetime = origdatetime # restore datetime
8 changes: 8 additions & 0 deletions tests/test_tuya_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
("_TZE200_bq5c8xfe", "TS0601", 100, 10, True),
("_TZE200_vs0skpuc", "TS0601", 100, 10, True),
("_TZE200_44af8vyi", "TS0601", 100, 10, True),
("_TZE200_lve3dvpy", "TS0601", 100, 10, False), # TH01Z - Temp & humid w/ clock
("_TZE200_c7emyjom", "TS0601", 100, 10, False),
("_TZE200_locansqn", "TS0601", 100, 10, False),
("_TZE200_qrztc3ev", "TS0601", 100, 10, False),
("_TZE200_snloy4rw", "TS0601", 100, 10, False),
("_TZE200_eanjj2pa", "TS0601", 100, 10, False),
("_TZE200_ydrdfkim", "TS0601", 100, 10, False),
("_TZE284_locansqn", "TS0601", 100, 10, False),
],
)
async def test_handle_get_data(
Expand Down
124 changes: 0 additions & 124 deletions zhaquirks/tuya/ts0601_sensor.py

This file was deleted.

Loading

0 comments on commit c99f86b

Please sign in to comment.