|
12 | 12 |
|
13 | 13 | from tests.common import ClusterListener, wait_for_zigpy_tasks
|
14 | 14 | import zhaquirks
|
| 15 | +from zhaquirks.tuya import TUYA_QUERY_DATA |
15 | 16 | from zhaquirks.tuya.builder import (
|
16 | 17 | TuyaIasContact,
|
17 | 18 | TuyaIasFire,
|
@@ -187,3 +188,73 @@ class TestEnum(t.enum8):
|
187 | 188 |
|
188 | 189 | assert tuya_listener.attribute_updates[0][0] == 0xEF0A
|
189 | 190 | assert tuya_listener.attribute_updates[0][1] == TestEnum.B
|
| 191 | + |
| 192 | + |
| 193 | +@pytest.mark.parametrize( |
| 194 | + "read_attr_spell,data_query_spell", |
| 195 | + [ |
| 196 | + (True, False), |
| 197 | + (False, True), |
| 198 | + (True, True), |
| 199 | + (False, False), |
| 200 | + ], |
| 201 | +) |
| 202 | +async def test_tuya_spell(device_mock, read_attr_spell, data_query_spell): |
| 203 | + """Test that enchanted Tuya devices have their spells applied during configuration.""" |
| 204 | + registry = DeviceRegistry() |
| 205 | + |
| 206 | + entry = ( |
| 207 | + TuyaQuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry) |
| 208 | + .tuya_battery(dp_id=1) |
| 209 | + .tuya_onoff(dp_id=3) |
| 210 | + .tuya_enchantment( |
| 211 | + read_attr_spell=read_attr_spell, data_query_spell=data_query_spell |
| 212 | + ) |
| 213 | + .skip_configuration() |
| 214 | + .add_to_registry() |
| 215 | + ) |
| 216 | + |
| 217 | + # coverage for overridden __eq__ method |
| 218 | + assert entry.adds_metadata[0] != entry.adds_metadata[1] |
| 219 | + assert entry.adds_metadata[0] != entry |
| 220 | + |
| 221 | + quirked = registry.get_device(device_mock) |
| 222 | + |
| 223 | + assert isinstance(quirked, CustomDeviceV2) |
| 224 | + assert quirked in registry |
| 225 | + |
| 226 | + request_patch = mock.patch("zigpy.zcl.Cluster.request", mock.AsyncMock()) |
| 227 | + with request_patch as request_mock: |
| 228 | + request_mock.return_value = (foundation.Status.SUCCESS, "done") |
| 229 | + |
| 230 | + # call apply_custom_configuration() on each EnchantedDevice |
| 231 | + # ZHA does this during device configuration normally |
| 232 | + await quirked.apply_custom_configuration() |
| 233 | + |
| 234 | + # the number of Tuya spells that are allowed to be cast, so the sum of enabled Tuya spells |
| 235 | + enabled_tuya_spells_num = ( |
| 236 | + quirked.tuya_spell_read_attributes + quirked.tuya_spell_data_query |
| 237 | + ) |
| 238 | + |
| 239 | + # verify request was called the correct number of times |
| 240 | + assert request_mock.call_count == enabled_tuya_spells_num |
| 241 | + |
| 242 | + # used to check list of mock calls below |
| 243 | + messages = 0 |
| 244 | + |
| 245 | + # check 'attribute read spell' was cast correctly (if enabled) |
| 246 | + if quirked.tuya_spell_read_attributes: |
| 247 | + assert ( |
| 248 | + request_mock.mock_calls[messages][1][1] |
| 249 | + == foundation.GeneralCommand.Read_Attributes |
| 250 | + ) |
| 251 | + assert request_mock.mock_calls[messages][1][3] == [4, 0, 1, 5, 7, 65534] |
| 252 | + messages += 1 |
| 253 | + |
| 254 | + # check 'query data spell' was cast correctly (if enabled) |
| 255 | + if quirked.tuya_spell_data_query: |
| 256 | + assert not request_mock.mock_calls[messages][1][0] |
| 257 | + assert request_mock.mock_calls[messages][1][1] == TUYA_QUERY_DATA |
| 258 | + messages += 1 |
| 259 | + |
| 260 | + request_mock.reset_mock() |
0 commit comments