-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy path__init__.py
949 lines (803 loc) · 33 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
"""Tuya devices."""
import datetime
import logging
from typing import Any, List, Optional, Tuple, Union
from zigpy.quirks import CustomCluster, CustomDevice
import zigpy.types as t
from zigpy.zcl import foundation
from zigpy.zcl.clusters.closures import WindowCovering
from zigpy.zcl.clusters.general import LevelControl, OnOff, PowerConfiguration
from zigpy.zcl.clusters.hvac import Thermostat, UserInterface
from zhaquirks import Bus, EventableCluster, LocalDataCluster
from zhaquirks.const import DOUBLE_PRESS, LONG_PRESS, SHORT_PRESS, ZHA_SEND_EVENT
# ---------------------------------------------------------
# Tuya Custom Cluster ID
# ---------------------------------------------------------
TUYA_CLUSTER_ID = 0xEF00
# ---------------------------------------------------------
# Tuya Cluster Commands
# ---------------------------------------------------------
TUYA_SET_DATA = 0x0000
TUYA_GET_DATA = 0x0001
TUYA_SET_DATA_RESPONSE = 0x0002
TUYA_SET_TIME = 0x0024
TUYA_LEVEL_COMMAND = 514
COVER_EVENT = "cover_event"
LEVEL_EVENT = "level_event"
# ---------------------------------------------------------
# Value for dp_type
# ---------------------------------------------------------
# ID Name Description
# ---------------------------------------------------------
# 0x00 DP_TYPE_RAW ?
# 0x01 DP_TYPE_BOOL ?
# 0x02 DP_TYPE_VALUE 4 byte unsigned integer
# 0x03 DP_TYPE_STRING variable length string
# 0x04 DP_TYPE_ENUM 1 byte enum
# 0x05 DP_TYPE_FAULT 1 byte bitmap (didn't test yet)
TUYA_DP_TYPE_RAW = 0x0000
TUYA_DP_TYPE_BOOL = 0x0100
TUYA_DP_TYPE_VALUE = 0x0200
TUYA_DP_TYPE_STRING = 0x0300
TUYA_DP_TYPE_ENUM = 0x0400
TUYA_DP_TYPE_FAULT = 0x0500
# ---------------------------------------------------------
# Value for dp_identifier (These are device specific)
# ---------------------------------------------------------
# ID Name Type Description
# ---------------------------------------------------------
# 0x01 control enum open, stop, close, continue
# 0x02 percent_control value 0-100% control
# 0x03 percent_state value Report from motor about current percentage
# 0x04 control_back enum Configures motor direction (untested)
# 0x05 work_state enum Motor Direction Setting
# 0x06 situation_set enum Configures if 100% equals to fully closed or fully open (untested)
# 0x07 fault bitmap Anything but 0 means something went wrong (untested)
TUYA_DP_ID_CONTROL = 0x01
TUYA_DP_ID_PERCENT_CONTROL = 0x02
TUYA_DP_ID_PERCENT_STATE = 0x03
TUYA_DP_ID_DIRECTION_CHANGE = 0x05
TUYA_DP_ID_COVER_INVERTED = 0x06
# ---------------------------------------------------------
# Window Cover Server Commands
# ---------------------------------------------------------
WINDOW_COVER_COMMAND_UPOPEN = 0x0000
WINDOW_COVER_COMMAND_DOWNCLOSE = 0x0001
WINDOW_COVER_COMMAND_STOP = 0x0002
WINDOW_COVER_COMMAND_LIFTPERCENT = 0x0005
WINDOW_COVER_COMMAND_CUSTOM = 0x0006
# ---------------------------------------------------------
# TUYA Cover Custom Values
# ---------------------------------------------------------
COVER_EVENT = "cover_event"
ATTR_COVER_POSITION = 0x0008
ATTR_COVER_DIRECTION = 0x8001
ATTR_COVER_INVERTED = 0x8002
# For most tuya devices 0 = Up/Open, 1 = Stop, 2 = Down/Close
TUYA_COVER_COMMAND = {
"_TZE200_zah67ekd": {0x0000: 0x0000, 0x0001: 0x0002, 0x0002: 0x0001},
"_TZE200_fzo2pocs": {0x0000: 0x0000, 0x0001: 0x0002, 0x0002: 0x0001},
"_TZE200_xuzcvlku": {0x0000: 0x0000, 0x0001: 0x0002, 0x0002: 0x0001},
"_TZE200_rddyvrci": {0x0000: 0x0002, 0x0001: 0x0001, 0x0002: 0x0000},
}
# ---------------------------------------------------------
# TUYA Switch Custom Values
# ---------------------------------------------------------
SWITCH_EVENT = "switch_event"
ATTR_ON_OFF = 0x0000
ATTR_COVER_POSITION = 0x0008
TUYA_CMD_BASE = 0x0100
# ---------------------------------------------------------
# DP Value meanings in Status Report
# ---------------------------------------------------------
# Type ID IntDP Description
# ---------------------------------------------------------
# 0x04 0x01 1025 Confirm opening/closing/stopping (triggered from Zigbee)
# 0x02 0x02 514 Started moving to position (triggered from Zigbee)
# 0x04 0x07 1031 Started moving (triggered by transmitter order pulling on curtain)
# 0x02 0x03 515 Arrived at position
# 0x01 0x05 261 Returned by configuration set; ignore
# 0x02 0x69 617 Not sure what this is
# 0x04 0x05 1029 Changed the Motor Direction
# 0x04 0x65 1125 Change of tilt/lift mode 1 = lift 0=tilt
# ---------------------------------------------------------
_LOGGER = logging.getLogger(__name__)
class BigEndianInt16(int):
"""Helper class to represent big endian 16 bit value."""
def serialize(self) -> bytes:
"""Value serialisation."""
try:
return self.to_bytes(2, "big", signed=False)
except OverflowError as e:
# OverflowError is not a subclass of ValueError, making it annoying to catch
raise ValueError(str(e)) from e
@classmethod
def deserialize(cls, data: bytes) -> Tuple["BigEndianInt16", bytes]:
"""Value deserialisation."""
if len(data) < 2:
raise ValueError(f"Data is too short to contain {cls._size} bytes")
r = cls.from_bytes(data[:2], "big", signed=False)
data = data[2:]
return r, data
class TuyaTimePayload(t.LVList, item_type=t.uint8_t, length_type=BigEndianInt16):
"""Tuya set time payload definition."""
pass
class Data(t.List, item_type=t.uint8_t):
"""list of uint8_t."""
@classmethod
def from_value(cls, value):
"""Convert from a zigpy typed value to a tuya data payload."""
# serialized in little-endian by zigpy
data = cls(value.serialize())
# we want big-endian, with length prepended
data.append(len(data))
data.reverse()
return data
def to_value(self, ztype):
"""Convert from a tuya data payload to a zigpy typed value."""
# first uint8_t is the length of the remaining data
# tuya data is in big endian whereas ztypes use little endian
value, _ = ztype.deserialize(bytes(reversed(self[1:])))
return value
class TuyaManufCluster(CustomCluster):
"""Tuya manufacturer specific cluster."""
name = "Tuya Manufacturer Specicific"
cluster_id = TUYA_CLUSTER_ID
ep_attribute = "tuya_manufacturer"
set_time_offset = 0
class Command(t.Struct):
"""Tuya manufacturer cluster command."""
status: t.uint8_t
tsn: t.uint8_t
command_id: t.uint16_t
function: t.uint8_t
data: Data
""" Time sync command (It's transparent between MCU and server)
Time request device -> server
payloadSize = 0
Set time, server -> device
payloadSize, should be always 8
payload[0-3] - UTC timestamp (big endian)
payload[4-7] - Local timestamp (big endian)
Zigbee payload is very similar to the UART payload which is described here: https://developer.tuya.com/en/docs/iot/device-development/access-mode-mcu/zigbee-general-solution/tuya-zigbee-module-uart-communication-protocol/tuya-zigbee-module-uart-communication-protocol?id=K9ear5khsqoty#title-10-Time%20synchronization
Some devices need the timestamp in seconds from 1/1/1970 and others in seconds from 1/1/2000.
NOTE: You need to wait for time request before setting it. You can't set time without request."""
manufacturer_server_commands = {
0x0000: ("set_data", (Command,), False),
0x0024: ("set_time", (TuyaTimePayload,), False),
}
manufacturer_client_commands = {
0x0001: ("get_data", (Command,), True),
0x0002: ("set_data_response", (Command,), True),
0x0024: ("set_time_request", (t.data16,), True),
}
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: Tuple,
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
) -> None:
"""Handle time request."""
if hdr.command_id != 0x0024 or self.set_time_offset == 0:
return super().handle_cluster_request(
hdr, args, dst_addressing=dst_addressing
)
# Send default response because the MCU expects it
if not hdr.frame_control.disable_default_response:
self.send_default_rsp(hdr, status=foundation.Status.SUCCESS)
_LOGGER.debug(
"[0x%04x:%s:0x%04x] Got set time request (command 0x%04x)",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
hdr.command_id,
)
payload = TuyaTimePayload()
utc_timestamp = int(
(
datetime.datetime.utcnow()
- datetime.datetime(self.set_time_offset, 1, 1)
).total_seconds()
)
local_timestamp = int(
(
datetime.datetime.now() - datetime.datetime(self.set_time_offset, 1, 1)
).total_seconds()
)
payload.extend(utc_timestamp.to_bytes(4, "big", signed=False))
payload.extend(local_timestamp.to_bytes(4, "big", signed=False))
self.create_catching_task(
super().command(TUYA_SET_TIME, payload, expect_reply=False)
)
class TuyaManufClusterAttributes(TuyaManufCluster):
"""Manufacturer specific cluster for Tuya converting attributes <-> commands."""
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: Tuple,
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
) -> None:
"""Handle cluster request."""
if hdr.command_id not in (0x0001, 0x0002):
return super().handle_cluster_request(
hdr, args, dst_addressing=dst_addressing
)
# Send default response because the MCU expects it
if not hdr.frame_control.disable_default_response:
self.send_default_rsp(hdr, status=foundation.Status.SUCCESS)
tuya_cmd = args[0].command_id
tuya_data = args[0].data
_LOGGER.debug(
"[0x%04x:%s:0x%04x] Received value %s "
"for attribute 0x%04x (command 0x%04x)",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
repr(tuya_data[1:]),
tuya_cmd,
hdr.command_id,
)
if tuya_cmd not in self.attributes:
return
ztype = self.attributes[tuya_cmd][1]
zvalue = tuya_data.to_value(ztype)
self._update_attribute(tuya_cmd, zvalue)
def read_attributes(
self, attributes, allow_cache=False, only_cache=False, manufacturer=None
):
"""Ignore remote reads as the "get_data" command doesn't seem to do anything."""
return super().read_attributes(
attributes, allow_cache=True, only_cache=True, manufacturer=manufacturer
)
async def write_attributes(self, attributes, manufacturer=None):
"""Defer attributes writing to the set_data tuya command."""
records = self._write_attr_records(attributes)
for record in records:
cmd_payload = TuyaManufCluster.Command()
cmd_payload.status = 0
cmd_payload.tsn = self.endpoint.device.application.get_sequence()
cmd_payload.command_id = record.attrid
cmd_payload.function = 0
cmd_payload.data = Data.from_value(record.value.value)
await super().command(
TUYA_SET_DATA,
cmd_payload,
manufacturer=manufacturer,
expect_reply=False,
tsn=cmd_payload.tsn,
)
return (foundation.Status.SUCCESS,)
class TuyaOnOff(CustomCluster, OnOff):
"""Tuya On/Off cluster for On/Off device."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.switch_bus.add_listener(self)
def switch_event(self, channel, state):
"""Switch event."""
_LOGGER.debug(
"%s - Received switch event message, channel: %d, state: %d",
self.endpoint.device.ieee,
channel,
state,
)
self._update_attribute(ATTR_ON_OFF, state)
def command(
self,
command_id: Union[foundation.Command, int, t.uint8_t],
*args,
manufacturer: Optional[Union[int, t.uint16_t]] = None,
expect_reply: bool = True,
tsn: Optional[Union[int, t.uint8_t]] = None,
):
"""Override the default Cluster command."""
if command_id in (0x0000, 0x0001):
cmd_payload = TuyaManufCluster.Command()
cmd_payload.status = 0
cmd_payload.tsn = 0
cmd_payload.command_id = TUYA_CMD_BASE + self.endpoint.endpoint_id
cmd_payload.function = 0
cmd_payload.data = [1, command_id]
return self.endpoint.tuya_manufacturer.command(
TUYA_SET_DATA, cmd_payload, expect_reply=True
)
return foundation.Status.UNSUP_CLUSTER_COMMAND
class TuyaManufacturerClusterOnOff(TuyaManufCluster):
"""Manufacturer Specific Cluster of On/Off device."""
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: Tuple[TuyaManufCluster.Command],
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
) -> None:
"""Handle cluster request."""
tuya_payload = args[0]
if hdr.command_id in (0x0002, 0x0001):
self.endpoint.device.switch_bus.listener_event(
SWITCH_EVENT,
tuya_payload.command_id - TUYA_CMD_BASE,
tuya_payload.data[1],
)
class TuyaSwitch(CustomDevice):
"""Tuya switch device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.switch_bus = Bus()
super().__init__(*args, **kwargs)
class TuyaDimmerSwitch(TuyaSwitch):
"""Tuya dimmer switch device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.dimmer_bus = Bus()
super().__init__(*args, **kwargs)
class TuyaThermostatCluster(LocalDataCluster, Thermostat):
"""Thermostat cluster for Tuya thermostats."""
_CONSTANT_ATTRIBUTES = {0x001B: Thermostat.ControlSequenceOfOperation.Heating_Only}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.thermostat_bus.add_listener(self)
def temperature_change(self, attr, value):
"""Local or target temperature change from device."""
self._update_attribute(self.attridx[attr], value)
def state_change(self, value):
"""State update from device."""
if value == 0:
mode = self.RunningMode.Off
state = self.RunningState.Idle
else:
mode = self.RunningMode.Heat
state = self.RunningState.Heat_State_On
self._update_attribute(self.attridx["running_mode"], mode)
self._update_attribute(self.attridx["running_state"], state)
# pylint: disable=R0201
def map_attribute(self, attribute, value):
"""Map standardized attribute value to dict of manufacturer values."""
return {}
async def write_attributes(self, attributes, manufacturer=None):
"""Implement writeable attributes."""
records = self._write_attr_records(attributes)
if not records:
return (foundation.Status.SUCCESS,)
manufacturer_attrs = {}
for record in records:
attr_name = self.attributes[record.attrid][0]
new_attrs = self.map_attribute(attr_name, record.value.value)
_LOGGER.debug(
"[0x%04x:%s:0x%04x] Mapping standard %s (0x%04x) "
"with value %s to custom %s",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
attr_name,
record.attrid,
repr(record.value.value),
repr(new_attrs),
)
manufacturer_attrs.update(new_attrs)
if not manufacturer_attrs:
return (foundation.Status.FAILURE,)
await self.endpoint.tuya_manufacturer.write_attributes(
manufacturer_attrs, manufacturer=manufacturer
)
return (foundation.Status.SUCCESS,)
# pylint: disable=W0236
async def command(
self,
command_id: Union[foundation.Command, int, t.uint8_t],
*args,
manufacturer: Optional[Union[int, t.uint16_t]] = None,
expect_reply: bool = True,
tsn: Optional[Union[int, t.uint8_t]] = None,
):
"""Implement thermostat commands."""
if command_id != 0x0000:
return foundation.Status.UNSUP_CLUSTER_COMMAND
mode, offset = args
if mode not in (self.SetpointMode.Heat, self.SetpointMode.Both):
return foundation.Status.INVALID_VALUE
attrid = self.attridx["occupied_heating_setpoint"]
success, _ = await self.read_attributes((attrid,), manufacturer=manufacturer)
try:
current = success[attrid]
except KeyError:
return foundation.Status.FAILURE
# offset is given in decidegrees, see Zigbee cluster specification
return await self.write_attributes(
{"occupied_heating_setpoint": current + offset * 10},
manufacturer=manufacturer,
)
class TuyaUserInterfaceCluster(LocalDataCluster, UserInterface):
"""HVAC User interface cluster for tuya thermostats."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.ui_bus.add_listener(self)
def child_lock_change(self, mode):
"""Change of child lock setting."""
if mode == 0:
lockout = self.KeypadLockout.No_lockout
else:
lockout = self.KeypadLockout.Level_1_lockout
self._update_attribute(self.attridx["keypad_lockout"], lockout)
def map_attribute(self, attribute, value):
"""Map standardized attribute value to dict of manufacturer values."""
return {}
async def write_attributes(self, attributes, manufacturer=None):
"""Defer the keypad_lockout attribute to child_lock."""
records = self._write_attr_records(attributes)
manufacturer_attrs = {}
for record in records:
if record.attrid == self.attridx["keypad_lockout"]:
lock = 0 if record.value.value == self.KeypadLockout.No_lockout else 1
new_attrs = {self._CHILD_LOCK_ATTR: lock}
else:
attr_name = self.attributes[record.attrid][0]
new_attrs = self.map_attribute(attr_name, record.value.value)
_LOGGER.debug(
"[0x%04x:%s:0x%04x] Mapping standard %s (0x%04x) "
"with value %s to custom %s",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
attr_name,
record.attrid,
repr(record.value.value),
repr(new_attrs),
)
manufacturer_attrs.update(new_attrs)
if not manufacturer_attrs:
return (foundation.Status.FAILURE,)
await self.endpoint.tuya_manufacturer.write_attributes(
manufacturer_attrs, manufacturer=manufacturer
)
return (foundation.Status.SUCCESS,)
class TuyaPowerConfigurationCluster(LocalDataCluster, PowerConfiguration):
"""PowerConfiguration cluster for battery-operated thermostats."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.battery_bus.add_listener(self)
def battery_change(self, value):
"""Change of reported battery percentage remaining."""
self._update_attribute(self.attridx["battery_percentage_remaining"], value * 2)
class TuyaThermostat(CustomDevice):
"""Generic Tuya thermostat device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.thermostat_bus = Bus()
self.ui_bus = Bus()
self.battery_bus = Bus()
super().__init__(*args, **kwargs)
class TuyaSmartRemoteOnOffCluster(OnOff, EventableCluster):
"""TuyaSmartRemoteOnOffCluster: fire events corresponding to press type."""
press_type = {
0x00: SHORT_PRESS,
0x01: DOUBLE_PRESS,
0x02: LONG_PRESS,
}
name = "TS004X_cluster"
ep_attribute = "TS004X_cluster"
def __init__(self, *args, **kwargs):
"""Init."""
self.last_tsn = -1
super().__init__(*args, **kwargs)
manufacturer_server_commands = {
0xFD: ("press_type", (t.uint8_t,), False),
}
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: List[Any],
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
):
"""Handle press_types command."""
# normally if default response sent, TS004x wouldn't send such repeated zclframe (with same sequence number),
# but for stability reasons (e. g. the case the response doesn't arrive the device), we can simply ignore it
if hdr.tsn == self.last_tsn:
_LOGGER.debug("TS004X: ignoring duplicate frame")
return
# save last sequence number
self.last_tsn = hdr.tsn
# send default response (as soon as possible), so avoid repeated zclframe from device
if not hdr.frame_control.disable_default_response:
self.debug("TS004X: send default response")
self.send_default_rsp(hdr, status=foundation.Status.SUCCESS)
# handle command
if hdr.command_id == 0xFD:
press_type = args[0]
self.listener_event(
ZHA_SEND_EVENT, self.press_type.get(press_type, "unknown"), []
)
# Tuya Window Cover Implementation
class TuyaManufacturerWindowCover(TuyaManufCluster):
"""Manufacturer Specific Cluster for cover device."""
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: Tuple[TuyaManufCluster.Command],
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
) -> None:
"""Handle cluster request."""
"""Tuya Specific Cluster Commands"""
if hdr.command_id == TUYA_SET_DATA_RESPONSE:
tuya_payload = args[0]
_LOGGER.debug(
"%s Received Attribute Report. Command is 0x%04x, Tuya Paylod values"
"[Status : %s, TSN: %s, Command: 0x%04x, Function: 0x%02x, Data: %s]",
self.endpoint.device.ieee,
hdr.command_id,
tuya_payload.status,
tuya_payload.tsn,
tuya_payload.command_id,
tuya_payload.function,
tuya_payload.data,
)
if tuya_payload.command_id == TUYA_DP_TYPE_VALUE + TUYA_DP_ID_PERCENT_STATE:
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_POSITION,
tuya_payload.data[4],
)
elif (
tuya_payload.command_id
== TUYA_DP_TYPE_ENUM + TUYA_DP_ID_DIRECTION_CHANGE
):
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_DIRECTION,
tuya_payload.data[1],
)
elif (
tuya_payload.command_id == TUYA_DP_TYPE_ENUM + TUYA_DP_ID_COVER_INVERTED
):
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_INVERTED,
tuya_payload.data[1], # Check this
)
elif hdr.command_id == 0x0011:
"""Assuming this is the pairing event"""
_LOGGER.debug(
"%s Pairing New Tuya Roller Blind. Self [%s], Header [%s], Tuya Paylod [%s]",
self.endpoint.device.ieee,
self,
hdr,
args,
)
"""set initial attributes"""
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_POSITION,
0,
)
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_DIRECTION,
0,
)
self.endpoint.device.cover_bus.listener_event(
COVER_EVENT,
ATTR_COVER_INVERTED,
0,
)
elif hdr.command_id == TUYA_SET_TIME:
"""Time event call super"""
super().handle_cluster_request(self, hdr, args, dst_addressing)
else:
_LOGGER.debug(
"%s Received Attribute Report - Unknown Command. Self [%s], Header [%s], Tuya Paylod [%s]",
self.endpoint.device.ieee,
self,
hdr,
args,
)
class TuyaWindowCoverControl(LocalDataCluster, WindowCovering):
"""Manufacturer Specific Cluster of Device cover."""
"""Add additional attributes for direction"""
attributes = WindowCovering.attributes.copy()
attributes.update({ATTR_COVER_DIRECTION: ("motor_direction", t.Bool)})
attributes.update({ATTR_COVER_INVERTED: ("cover_inverted", t.Bool)})
def __init__(self, *args, **kwargs):
"""Initialize instance."""
super().__init__(*args, **kwargs)
self.endpoint.device.cover_bus.add_listener(self)
def cover_event(self, attribute, value):
"""Event listener for cover events."""
if attribute == ATTR_COVER_POSITION:
value = (
value if self._attr_cache.get(ATTR_COVER_INVERTED) == 1 else 100 - value
)
self._update_attribute(attribute, value)
_LOGGER.debug(
"%s Tuya Attribute Cache : [%s]",
self.endpoint.device.ieee,
self._attr_cache,
)
def command(
self,
command_id: Union[foundation.Command, int, t.uint8_t],
*args,
manufacturer: Optional[Union[int, t.uint16_t]] = None,
expect_reply: bool = True,
tsn: Optional[Union[int, t.uint8_t]] = None,
):
"""Override the default Cluster command."""
if manufacturer is None:
manufacturer = self.endpoint.device.manufacturer
_LOGGER.debug(
"%s Sending Tuya Cluster Command.. Manufacturer is %s Cluster Command is 0x%04x, Arguments are %s",
self.endpoint.device.ieee,
manufacturer,
command_id,
args,
)
# Open Close or Stop commands
tuya_payload = TuyaManufCluster.Command()
if command_id in (
WINDOW_COVER_COMMAND_UPOPEN,
WINDOW_COVER_COMMAND_DOWNCLOSE,
WINDOW_COVER_COMMAND_STOP,
):
tuya_payload.status = 0
tuya_payload.tsn = tsn if tsn else 0
tuya_payload.command_id = TUYA_DP_TYPE_ENUM + TUYA_DP_ID_CONTROL
tuya_payload.function = 0
tuya_payload.data = [
1,
# need to implement direction change
TUYA_COVER_COMMAND[manufacturer][command_id],
] # remap the command to the Tuya command
# Set Position Command
elif command_id == WINDOW_COVER_COMMAND_LIFTPERCENT:
tuya_payload.status = 0
tuya_payload.tsn = tsn if tsn else 0
tuya_payload.command_id = TUYA_DP_TYPE_VALUE + TUYA_DP_ID_PERCENT_CONTROL
tuya_payload.function = 0
"""Check direction and correct value"""
position = (
args[0]
if self._attr_cache.get(ATTR_COVER_INVERTED) == 1
else 100 - args[0]
)
tuya_payload.data = [
4,
0,
0,
0,
position,
]
# Custom Command
elif command_id == WINDOW_COVER_COMMAND_CUSTOM:
tuya_payload.status = args[0]
tuya_payload.tsn = args[1]
tuya_payload.command_id = args[2]
tuya_payload.function = args[3]
tuya_payload.data = args[4]
else:
tuya_payload = None
# Send the command
if tuya_payload.command_id:
_LOGGER.debug(
"%s Sending Tuya Command. Paylod values [endpoint_id : %s, "
"Status : %s, TSN: %s, Command: 0x%04x, Function: %s, Data: %s]",
self.endpoint.device.ieee,
self.endpoint.endpoint_id,
tuya_payload.status,
tuya_payload.tsn,
tuya_payload.command_id,
tuya_payload.function,
tuya_payload.data,
)
return self.endpoint.tuya_manufacturer.command(
TUYA_SET_DATA, tuya_payload, expect_reply=True
)
else:
_LOGGER.debug("Unrecognised command: %x", command_id)
return foundation.Status.UNSUP_CLUSTER_COMMAND
class TuyaWindowCover(CustomDevice):
"""Tuya switch device."""
def __init__(self, *args, **kwargs):
"""Init device."""
self.cover_bus = Bus()
super().__init__(*args, **kwargs)
class TuyaManufacturerLevelControl(TuyaManufCluster):
"""Manufacturer Specific Cluster for cover device."""
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: Tuple[TuyaManufCluster.Command],
*,
dst_addressing: Optional[
Union[t.Addressing.Group, t.Addressing.IEEE, t.Addressing.NWK]
] = None,
) -> None:
"""Handle cluster request."""
tuya_payload = args[0]
_LOGGER.debug(
"%s Received Attribute Report. Command is %x, Tuya Paylod values"
"[Status : %s, TSN: %s, Command: %s, Function: %s, Data: %s]",
self.endpoint.device.ieee,
hdr.command_id,
tuya_payload.status,
tuya_payload.tsn,
tuya_payload.command_id,
tuya_payload.function,
tuya_payload.data,
)
if hdr.command_id in (0x0002, 0x0001):
if tuya_payload.command_id == TUYA_LEVEL_COMMAND:
self.endpoint.device.dimmer_bus.listener_event(
LEVEL_EVENT,
tuya_payload.command_id,
tuya_payload.data,
)
else:
self.endpoint.device.switch_bus.listener_event(
SWITCH_EVENT,
tuya_payload.command_id - TUYA_CMD_BASE,
tuya_payload.data[1],
)
class TuyaLevelControl(CustomCluster, LevelControl):
"""Tuya Level cluster for dimmable device."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.dimmer_bus.add_listener(self)
def level_event(self, channel, state):
"""Level event."""
level = (((state[3] << 8) + state[4]) * 255) // 1000
_LOGGER.debug(
"%s - Received level event message, channel: %d, level: %d, data: %d",
self.endpoint.device.ieee,
channel,
level,
state,
)
self._update_attribute(self.attridx["current_level"], level)
def command(
self,
command_id: Union[foundation.Command, int, t.uint8_t],
*args,
manufacturer: Optional[Union[int, t.uint16_t]] = None,
expect_reply: bool = True,
tsn: Optional[Union[int, t.uint8_t]] = None,
):
"""Override the default Cluster command."""
_LOGGER.debug(
"%s Sending Tuya Cluster Command.. Cluster Command is %x, Arguments are %s",
self.endpoint.device.ieee,
command_id,
args,
)
# Move to level
# move_to_level_with_on_off
if command_id in (0x0000, 0x0001, 0x0004):
cmd_payload = TuyaManufCluster.Command()
cmd_payload.status = 0
cmd_payload.tsn = 0
cmd_payload.command_id = TUYA_LEVEL_COMMAND
cmd_payload.function = 0
brightness = (args[0] * 1000) // 255
val1 = brightness >> 8
val2 = brightness & 0xFF
cmd_payload.data = [4, 0, 0, val1, val2] # Custom Command
return self.endpoint.tuya_manufacturer.command(
TUYA_SET_DATA, cmd_payload, expect_reply=True
)
return foundation.Status.UNSUP_CLUSTER_COMMAND