-
Notifications
You must be signed in to change notification settings - Fork 20
/
ts0601_trv_moes.py
1535 lines (1370 loc) · 59.8 KB
/
ts0601_trv_moes.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Moes TRV devices support."""
import logging
from typing import Optional, Union
import zigpy.types as t
from zhaquirks import Bus, LocalDataCluster
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya import (
TuyaManufClusterAttributes,
TuyaPowerConfigurationCluster,
TuyaThermostat,
TuyaThermostatCluster,
TuyaUserInterfaceCluster,
)
from zigpy.profiles import zha
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import (
AnalogOutput,
Basic,
Groups,
Identify,
OnOff,
Ota,
Scenes,
Time,
)
from zigpy.zcl.clusters.hvac import Thermostat
_LOGGER = logging.getLogger(__name__)
MOES_TARGET_TEMP_ATTR = 0x0202 # target room temp (decidegree)
MOES_TEMPERATURE_ATTR = 0x0203 # current room temp (decidegree)
MOES_MODE_ATTR = 0x0404 # [0] away [1] scheduled [2] manual [3] comfort [4] eco [5] boost [6] complex
MOES_CHILD_LOCK_ATTR = 0x0107 # [0] unlocked [1] locked
MOES_VALVE_DETECT_ATTR = 0x0114 # [0] do not report [1] report
MOES_TEMP_CALIBRATION_ATTR = 0x022C # temperature calibration (decidegree)
MOES_MIN_TEMPERATURE_ATTR = 0x0266 # minimum limit of temperature setting (decidegree)
MOES_MAX_TEMPERATURE_ATTR = 0x0267 # maximum limit of temperature setting (decidegree)
MOES_WINDOW_DETECT_ATTR = 0x0068 # [0,35,5] on/off, temperature, operating time (min)
MOES_BOOST_TIME_ATTR = 0x0269 # BOOST mode operating time in (sec)
MOES_FORCE_VALVE_ATTR = 0x046A # [0] normal [1] open [2] close
MOES_COMFORT_TEMP_ATTR = 0x026B # comfort mode temperaure (decidegree)
MOES_ECO_TEMP_ATTR = 0x026C # eco mode temperature (decidegree)
MOES_VALVE_STATE_ATTR = 0x026D # opening percentage
MOES_BATTERY_LOW_ATTR = 0x016E # battery low warning
MOES_WEEK_FORMAT_ATTR = 0x046F # [0] 5 days [1] 6 days, [2] 7 days
MOES_AWAY_TEMP_ATTR = 0x0272 # away mode temperature (decidegree)
MOES_AUTO_LOCK_ATTR = 0x0174 # [0] auto [1] manual
MOES_AWAY_DAYS_ATTR = 0x0275 # away mode duration (days)
# schedule [6,0,20,8,0,15,11,30,15,12,30,15,17,30,20,22,0,15]
# 6:00 - 20*, 8:00 - 15*, 11:30 - 15*, 12:30 - 15*, 17:30 - 20*, 22:00 - 15*
# Top bits in hours have special meaning
# 8: ??
# 7: Current schedule indicator
MOES_SCHEDULE_WORKDAY_ATTR = 0x0070
MOES_SCHEDULE_WEEKEND_ATTR = 0x0071
MoesManufClusterSelf = {}
class data144(t.FixedList, item_type=t.uint8_t, length=18):
"""General data, Discrete, 144 bit."""
pass
class CustomTuyaOnOff(LocalDataCluster, OnOff):
"""Custom Tuya OnOff cluster."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.thermostat_onoff_bus.add_listener(self)
# 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.WriteAttributesStatusRecord(foundation.Status.SUCCESS)]]
manufacturer_attrs = {}
for record in records:
attr_name = self.attributes[record.attrid].name
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.WriteAttributesStatusRecord(
foundation.Status.FAILURE, r.attrid
)
for r in records
]
]
await MoesManufClusterSelf[
self.endpoint.device.ieee
].endpoint.tuya_manufacturer.write_attributes(
manufacturer_attrs, manufacturer=manufacturer
)
return [[foundation.WriteAttributesStatusRecord(foundation.Status.SUCCESS)]]
async def command(
self,
command_id: Union[foundation.GeneralCommand, 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, 0x0002):
if command_id == 0x0000:
value = False
elif command_id == 0x0001:
value = True
else:
attrid = self.attributes_by_name["on_off"].id
success, _ = await self.read_attributes(
(attrid,), manufacturer=manufacturer
)
try:
value = success[attrid]
except KeyError:
return foundation.Status.FAILURE
value = not value
(res,) = await self.write_attributes(
{"on_off": value},
manufacturer=manufacturer,
)
return [command_id, res[0].status]
return [command_id, foundation.Status.UNSUP_CLUSTER_COMMAND]
class MoesManufCluster(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of some thermostatic valves."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
global MoesManufClusterSelf
MoesManufClusterSelf[self.endpoint.device.ieee] = self
set_time_offset = 1970
attributes = TuyaManufClusterAttributes.attributes.copy()
attributes.update(
{
MOES_TEMPERATURE_ATTR: ("temperature", t.uint32_t),
MOES_TARGET_TEMP_ATTR: ("target_temperature", t.uint32_t),
MOES_MODE_ATTR: ("mode", t.uint8_t),
MOES_CHILD_LOCK_ATTR: ("child_lock", t.uint8_t),
MOES_VALVE_DETECT_ATTR: ("valve_detect", t.uint8_t),
MOES_TEMP_CALIBRATION_ATTR: ("temperature_calibration", t.int32s),
MOES_MIN_TEMPERATURE_ATTR: ("min_temperature", t.uint32_t),
MOES_MAX_TEMPERATURE_ATTR: ("max_temperature", t.uint32_t),
MOES_WINDOW_DETECT_ATTR: ("window_detection", t.data24),
MOES_BOOST_TIME_ATTR: ("boost_duration_seconds", t.uint32_t),
MOES_FORCE_VALVE_ATTR: ("valve_force_state", t.uint8_t),
MOES_COMFORT_TEMP_ATTR: ("comfort_mode_temperature", t.uint32_t),
MOES_ECO_TEMP_ATTR: ("eco_mode_temperature", t.uint32_t),
MOES_VALVE_STATE_ATTR: ("valve_state", t.uint32_t),
MOES_BATTERY_LOW_ATTR: ("battery_low", t.uint8_t),
MOES_WEEK_FORMAT_ATTR: ("week_format", t.uint8_t),
MOES_AWAY_TEMP_ATTR: ("away_mode_temperature", t.uint32_t),
MOES_AUTO_LOCK_ATTR: ("auto_lock", t.uint8_t),
MOES_AWAY_DAYS_ATTR: ("away_duration_days", t.uint32_t),
MOES_SCHEDULE_WORKDAY_ATTR: ("workday_schedule", data144),
MOES_SCHEDULE_WEEKEND_ATTR: ("weekend_schedule", data144),
}
)
DIRECT_MAPPED_ATTRS = {
MOES_TEMPERATURE_ATTR: ("local_temperature", lambda value: value * 10),
MOES_TARGET_TEMP_ATTR: ("occupied_heating_setpoint", lambda value: value * 10),
MOES_MODE_ATTR: ("operation_preset", None),
MOES_TEMP_CALIBRATION_ATTR: (
"local_temperature_calibration",
lambda value: value * 10,
),
MOES_MIN_TEMPERATURE_ATTR: (
"min_heat_setpoint_limit",
lambda value: value * 100,
),
MOES_MAX_TEMPERATURE_ATTR: (
"max_heat_setpoint_limit",
lambda value: value * 100,
),
MOES_AWAY_TEMP_ATTR: ("unoccupied_heating_setpoint", lambda value: value * 100),
MOES_COMFORT_TEMP_ATTR: ("comfort_heating_setpoint", lambda value: value * 100),
MOES_ECO_TEMP_ATTR: ("eco_heating_setpoint", lambda value: value * 100),
MOES_VALVE_STATE_ATTR: ("valve_open_percentage", None),
MOES_AWAY_DAYS_ATTR: ("unoccupied_duration_days", None),
MOES_BOOST_TIME_ATTR: ("boost_duration_seconds", None),
MOES_WEEK_FORMAT_ATTR: ("work_days", None),
MOES_FORCE_VALVE_ATTR: ("valve_force_state", None),
}
def _update_attribute(self, attrid, value):
"""Override default _update_attribute."""
super()._update_attribute(attrid, value)
if attrid in self.DIRECT_MAPPED_ATTRS:
self.endpoint.device.thermostat_bus.listener_event(
"temperature_change",
self.DIRECT_MAPPED_ATTRS[attrid][0],
value
if self.DIRECT_MAPPED_ATTRS[attrid][1] is None
else self.DIRECT_MAPPED_ATTRS[attrid][1](value),
)
elif attrid in (MOES_SCHEDULE_WORKDAY_ATTR, MOES_SCHEDULE_WEEKEND_ATTR):
self.endpoint.device.thermostat_bus.listener_event(
"schedule_change", attrid, value
)
if attrid == MOES_WINDOW_DETECT_ATTR:
self.endpoint.device.MoesWindowDetection_bus.listener_event(
"window_detect_change", value
)
elif attrid == MOES_CHILD_LOCK_ATTR:
mode = 1 if value else 0
self.endpoint.device.ui_bus.listener_event("child_lock_change", mode)
self.endpoint.device.thermostat_onoff_bus.listener_event(
"child_lock_change", value
)
elif attrid == MOES_MODE_ATTR:
self.endpoint.device.thermostat_bus.listener_event("mode_change", value)
elif attrid == MOES_VALVE_STATE_ATTR:
self.endpoint.device.thermostat_bus.listener_event("state_change", value)
self.endpoint.device.MoesValveState_bus.listener_event("set_value", value)
elif attrid == MOES_AUTO_LOCK_ATTR:
mode = 1 if value else 0
self.endpoint.device.ui_bus.listener_event("autolock_change", mode)
elif attrid == MOES_BATTERY_LOW_ATTR:
self.endpoint.device.battery_bus.listener_event(
"battery_change", 5 if value else 100
)
elif attrid == MOES_TEMP_CALIBRATION_ATTR:
self.endpoint.device.MoesTempCalibration_bus.listener_event(
"set_value",
self.DIRECT_MAPPED_ATTRS[MOES_TEMP_CALIBRATION_ATTR][1](value),
)
elif attrid == MOES_BOOST_TIME_ATTR:
self.endpoint.device.MoesBoostTime_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_VALVE_DETECT_ATTR:
self.endpoint.device.thermostat_onoff_bus.listener_event(
"valve_detect_change", value
)
elif attrid == MOES_MIN_TEMPERATURE_ATTR:
self.endpoint.device.MoesMinTemp_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_MAX_TEMPERATURE_ATTR:
self.endpoint.device.MoesMaxTemp_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_COMFORT_TEMP_ATTR:
self.endpoint.device.MoesComfortTemp_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_ECO_TEMP_ATTR:
self.endpoint.device.MoesEcoTemp_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_AWAY_TEMP_ATTR:
self.endpoint.device.MoesAwayTemp_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_AWAY_DAYS_ATTR:
self.endpoint.device.MoesAwayDays_bus.listener_event(
"set_value",
value,
)
elif attrid == MOES_VALVE_STATE_ATTR:
self.endpoint.device.thermostat_bus.listener_event(
"system_mode_change", value
)
class MoesThermostat(TuyaThermostatCluster):
"""Thermostat cluster for some thermostatic valves."""
class Preset(t.enum8):
"""Working modes of the thermostat."""
Away = 0x00
Schedule = 0x01
Manual = 0x02
Comfort = 0x03
Eco = 0x04
Boost = 0x05
Complex = 0x06
class WorkDays(t.enum8):
"""Workday configuration for scheduler operation mode."""
MonToFri = 0x00
MonToSat = 0x01
MonToSun = 0x02
class ForceValveState(t.enum8):
"""Force valve state option."""
Normal = 0x00
Open = 0x01
Close = 0x02
_CONSTANT_ATTRIBUTES = {
0x001B: Thermostat.ControlSequenceOfOperation.Heating_Only,
0x001C: Thermostat.SystemMode.Heat,
}
attributes = TuyaThermostatCluster.attributes.copy()
attributes.update(
{
0x4000: ("comfort_heating_setpoint", t.int16s),
0x4001: ("eco_heating_setpoint", t.int16s),
0x4002: ("operation_preset", Preset),
0x4003: ("work_days", WorkDays),
0x4004: ("valve_open_percentage", t.uint8_t),
0x4005: ("boost_duration_seconds", t.uint32_t),
0x4006: ("valve_force_state", ForceValveState),
0x4007: ("unoccupied_duration_days", t.uint32_t),
0x4110: ("workday_schedule_1_hour", t.uint8_t),
0x4111: ("workday_schedule_1_minute", t.uint8_t),
0x4112: ("workday_schedule_1_temperature", t.int16s),
0x4120: ("workday_schedule_2_hour", t.uint8_t),
0x4121: ("workday_schedule_2_minute", t.uint8_t),
0x4122: ("workday_schedule_2_temperature", t.int16s),
0x4130: ("workday_schedule_3_hour", t.uint8_t),
0x4131: ("workday_schedule_3_minute", t.uint8_t),
0x4132: ("workday_schedule_3_temperature", t.int16s),
0x4140: ("workday_schedule_4_hour", t.uint8_t),
0x4141: ("workday_schedule_4_minute", t.uint8_t),
0x4142: ("workday_schedule_4_temperature", t.int16s),
0x4150: ("workday_schedule_5_hour", t.uint8_t),
0x4151: ("workday_schedule_5_minute", t.uint8_t),
0x4152: ("workday_schedule_5_temperature", t.int16s),
0x4160: ("workday_schedule_6_hour", t.uint8_t),
0x4161: ("workday_schedule_6_minute", t.uint8_t),
0x4162: ("workday_schedule_6_temperature", t.int16s),
0x4210: ("weekend_schedule_1_hour", t.uint8_t),
0x4211: ("weekend_schedule_1_minute", t.uint8_t),
0x4212: ("weekend_schedule_1_temperature", t.int16s),
0x4220: ("weekend_schedule_2_hour", t.uint8_t),
0x4221: ("weekend_schedule_2_minute", t.uint8_t),
0x4222: ("weekend_schedule_2_temperature", t.int16s),
0x4230: ("weekend_schedule_3_hour", t.uint8_t),
0x4231: ("weekend_schedule_3_minute", t.uint8_t),
0x4232: ("weekend_schedule_3_temperature", t.int16s),
0x4240: ("weekend_schedule_4_hour", t.uint8_t),
0x4241: ("weekend_schedule_4_minute", t.uint8_t),
0x4242: ("weekend_schedule_4_temperature", t.int16s),
0x4250: ("weekend_schedule_5_hour", t.uint8_t),
0x4251: ("weekend_schedule_5_minute", t.uint8_t),
0x4252: ("weekend_schedule_5_temperature", t.int16s),
0x4260: ("weekend_schedule_6_hour", t.uint8_t),
0x4261: ("weekend_schedule_6_minute", t.uint8_t),
0x4262: ("weekend_schedule_6_temperature", t.int16s),
}
)
DIRECT_MAPPING_ATTRS = {
"occupied_heating_setpoint": (
MOES_TARGET_TEMP_ATTR,
lambda value: round(value / 10),
),
"unoccupied_heating_setpoint": (
MOES_AWAY_TEMP_ATTR,
lambda value: round(value / 100),
),
"comfort_heating_setpoint": (
MOES_COMFORT_TEMP_ATTR,
lambda value: round(value / 100),
),
"eco_heating_setpoint": (MOES_ECO_TEMP_ATTR, lambda value: round(value / 100)),
"min_heat_setpoint_limit": (
MOES_MIN_TEMPERATURE_ATTR,
lambda value: round(value / 100),
),
"max_heat_setpoint_limit": (
MOES_MAX_TEMPERATURE_ATTR,
lambda value: round(value / 100),
),
"local_temperature_calibration": (
MOES_TEMP_CALIBRATION_ATTR,
lambda value: round(value / 10),
),
"work_days": (MOES_WEEK_FORMAT_ATTR, None),
"operation_preset": (MOES_MODE_ATTR, None),
"boost_duration_seconds": (MOES_BOOST_TIME_ATTR, None),
"valve_force_state": (MOES_FORCE_VALVE_ATTR, None),
"unoccupied_duration_days": (MOES_AWAY_DAYS_ATTR, None),
}
WORKDAY_SCHEDULE_ATTRS = {
"workday_schedule_6_temperature": 1500,
"workday_schedule_6_minute": 0,
"workday_schedule_6_hour": 22,
"workday_schedule_5_temperature": 2000,
"workday_schedule_5_minute": 30,
"workday_schedule_5_hour": 17,
"workday_schedule_4_temperature": 1500,
"workday_schedule_4_minute": 30,
"workday_schedule_4_hour": 12,
"workday_schedule_3_temperature": 1500,
"workday_schedule_3_minute": 30,
"workday_schedule_3_hour": 11,
"workday_schedule_2_temperature": 1500,
"workday_schedule_2_minute": 0,
"workday_schedule_2_hour": 8,
"workday_schedule_1_temperature": 2000,
"workday_schedule_1_minute": 0,
"workday_schedule_1_hour": 6,
}
WEEKEND_SCHEDULE_ATTRS = {
"weekend_schedule_6_temperature": 1500,
"weekend_schedule_6_minute": 0,
"weekend_schedule_6_hour": 22,
"weekend_schedule_5_temperature": 2000,
"weekend_schedule_5_minute": 30,
"weekend_schedule_5_hour": 17,
"weekend_schedule_4_temperature": 1500,
"weekend_schedule_4_minute": 30,
"weekend_schedule_4_hour": 12,
"weekend_schedule_3_temperature": 1500,
"weekend_schedule_3_minute": 30,
"weekend_schedule_3_hour": 11,
"weekend_schedule_2_temperature": 1500,
"weekend_schedule_2_minute": 0,
"weekend_schedule_2_hour": 8,
"weekend_schedule_1_temperature": 2000,
"weekend_schedule_1_minute": 0,
"weekend_schedule_1_hour": 6,
}
def map_attribute(self, attribute, value):
"""Map standardized attribute value to dict of manufacturer values."""
if attribute in self.DIRECT_MAPPING_ATTRS:
return {
self.DIRECT_MAPPING_ATTRS[attribute][0]: value
if self.DIRECT_MAPPING_ATTRS[attribute][1] is None
else self.DIRECT_MAPPING_ATTRS[attribute][1](value)
}
if attribute in ("programing_oper_mode", "occupancy"):
if attribute == "occupancy":
occupancy = value
oper_mode = self._attr_cache.get(
self.attributes_by_name["programing_oper_mode"].id,
self.ProgrammingOperationMode.Simple,
)
else:
occupancy = self._attr_cache.get(
self.attributes_by_name["occupancy"].id, self.Occupancy.Occupied
)
oper_mode = value
if occupancy == self.Occupancy.Unoccupied:
return {MOES_MODE_ATTR: 0}
if occupancy == self.Occupancy.Occupied:
if oper_mode == self.ProgrammingOperationMode.Schedule_programming_mode:
return {MOES_MODE_ATTR: 1}
if oper_mode == self.ProgrammingOperationMode.Simple:
return {MOES_MODE_ATTR: 2}
if oper_mode == self.ProgrammingOperationMode.Economy_mode:
return {MOES_MODE_ATTR: 4}
self.error("Unsupported value for ProgrammingOperationMode")
else:
self.error("Unsupported value for Occupancy")
if attribute == "system_mode":
if value == self.SystemMode.Off:
mode = 2
if value == self.SystemMode.Heat:
mode = 1
else:
mode = 0
return {MOES_FORCE_VALVE_ATTR: mode}
if attribute in self.WORKDAY_SCHEDULE_ATTRS:
data = data144()
for num, (attr, default) in enumerate(self.WORKDAY_SCHEDULE_ATTRS.items()):
if num % 3 == 0:
if attr == attribute:
val = round(value / 100)
else:
val = round(
self._attr_cache.get(
self.attributes_by_name[attr].id, default
)
/ 100
)
else:
if attr == attribute:
val = value
else:
val = self._attr_cache.get(
self.attributes_by_name[attr].id, default
)
data.append(val)
return {MOES_SCHEDULE_WORKDAY_ATTR: data}
if attribute in self.WEEKEND_SCHEDULE_ATTRS:
data = data144()
for num, (attr, default) in enumerate(self.WEEKEND_SCHEDULE_ATTRS.items()):
if num % 3 == 0:
if attr == attribute:
val = round(value / 100)
else:
val = round(
self._attr_cache.get(
self.attributes_by_name[attr].id, default
)
/ 100
)
else:
if attr == attribute:
val = value
else:
val = self._attr_cache.get(
self.attributes_by_name[attr].id, default
)
data.append(val)
return {MOES_SCHEDULE_WEEKEND_ATTR: data}
def mode_change(self, value):
"""System Mode change."""
if value == 0:
prog_mode = self.ProgrammingOperationMode.Simple
occupancy = self.Occupancy.Unoccupied
elif value == 1:
prog_mode = self.ProgrammingOperationMode.Schedule_programming_mode
occupancy = self.Occupancy.Occupied
elif value == 2:
prog_mode = self.ProgrammingOperationMode.Simple
occupancy = self.Occupancy.Occupied
elif value == 3:
prog_mode = self.ProgrammingOperationMode.Simple
occupancy = self.Occupancy.Occupied
elif value == 4:
prog_mode = self.ProgrammingOperationMode.Economy_mode
occupancy = self.Occupancy.Occupied
elif value == 5:
prog_mode = self.ProgrammingOperationMode.Simple
occupancy = self.Occupancy.Occupied
else:
prog_mode = self.ProgrammingOperationMode.Simple
occupancy = self.Occupancy.Occupied
self._update_attribute(
self.attributes_by_name["programing_oper_mode"].id, prog_mode
)
self._update_attribute(self.attributes_by_name["occupancy"].id, occupancy)
def system_mode_change(self, value):
"""System Mode change."""
if value == 2:
mode = self.SystemMode.Off
if value == 0:
mode = self.SystemMode.Auto
else:
mode = self.SystemMode.Heat
self._update_attribute(self.attributes_by_name["system_mode"].id, mode)
def schedule_change(self, attr, value):
"""Scheduler attribute change."""
if attr == MOES_SCHEDULE_WORKDAY_ATTR:
self._update_attribute(
self.attributes_by_name["workday_schedule_1_hour"].id, value[17] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_1_minute"].id, value[16]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_1_temperature"].id,
value[15] * 100,
)
self._update_attribute(
self.attributes_by_name["workday_schedule_2_hour"].id, value[14] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_2_minute"].id, value[13]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_2_temperature"].id,
value[12] * 100,
)
self._update_attribute(
self.attributes_by_name["workday_schedule_3_hour"].id, value[11] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_3_minute"].id, value[10]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_3_temperature"].id,
value[9] * 100,
)
self._update_attribute(
self.attributes_by_name["workday_schedule_4_hour"].id, value[8] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_4_minute"].id, value[7]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_4_temperature"].id,
value[6] * 100,
)
self._update_attribute(
self.attributes_by_name["workday_schedule_5_hour"].id, value[5] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_5_minute"].id, value[4]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_5_temperature"].id,
value[3] * 100,
)
self._update_attribute(
self.attributes_by_name["workday_schedule_6_hour"].id, value[2] & 0x3F
)
self._update_attribute(
self.attributes_by_name["workday_schedule_6_minute"].id, value[1]
)
self._update_attribute(
self.attributes_by_name["workday_schedule_6_temperature"].id,
value[0] * 100,
)
elif attr == MOES_SCHEDULE_WEEKEND_ATTR:
self._update_attribute(
self.attributes_by_name["weekend_schedule_1_hour"].id, value[17] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_1_minute"].id, value[16]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_1_temperature"].id,
value[15] * 100,
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_2_hour"].id, value[14] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_2_minute"].id, value[13]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_2_temperature"].id,
value[12] * 100,
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_3_hour"].id, value[11] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_3_minute"].id, value[10]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_3_temperature"].id,
value[9] * 100,
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_4_hour"].id, value[8] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_4_minute"].id, value[7]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_4_temperature"].id,
value[6] * 100,
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_5_hour"].id, value[5] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_5_minute"].id, value[4]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_5_temperature"].id,
value[3] * 100,
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_6_hour"].id, value[2] & 0x3F
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_6_minute"].id, value[1]
)
self._update_attribute(
self.attributes_by_name["weekend_schedule_6_temperature"].id,
value[0] * 100,
)
class MoesUserInterface(TuyaUserInterfaceCluster):
"""HVAC User interface cluster for tuya electric heating thermostats."""
_CHILD_LOCK_ATTR = MOES_CHILD_LOCK_ATTR
attributes = TuyaUserInterfaceCluster.attributes.copy()
attributes.update(
{
0x5000: ("auto_lock", t.Bool),
}
)
def autolock_change(self, value):
"""Automatic lock change."""
self._update_attribute(self.attributes_by_name["auto_lock"].id, value)
def map_attribute(self, attribute, value):
"""Map standardized attribute value to dict of manufacturer values."""
if attribute == "auto_lock":
return {MOES_AUTO_LOCK_ATTR: value}
class MoesWindowDetection(LocalDataCluster, OnOff):
"""On/Off cluster for the window detection function of the electric heating thermostats."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.MoesWindowDetection_bus.add_listener(self)
attributes = LocalDataCluster.attributes.copy()
attributes.update(
{
0x6000: ("window_detection_temperature", t.int16s),
0x6001: ("window_detection_timeout_minutes", t.uint8_t),
}
)
def window_detect_change(self, value):
"""Window detection change."""
self._update_attribute(
self.attributes_by_name["window_detection_timeout_minutes"].id, value[0]
)
self._update_attribute(
self.attributes_by_name["window_detection_temperature"].id, value[1] * 100
)
self._update_attribute(self.attributes_by_name["on_off"].id, value[2])
async def write_attributes(self, attributes, manufacturer=None):
"""Defer attributes writing to the set_data tuya command."""
records = self._write_attr_records(attributes)
if not records:
return [[foundation.WriteAttributesStatusRecord(foundation.Status.SUCCESS)]]
has_change = False
data = t.data24()
data.append(
self._attr_cache.get(
self.attributes_by_name["window_detection_timeout_minutes"].id,
5,
)
)
data.append(
round(
self._attr_cache.get(
self.attributes_by_name["window_detection_temperature"].id,
50,
)
/ 100
)
)
data.append(
self._attr_cache.get(
self.attributes_by_name["on_off"].id,
False,
)
)
for record in records:
attr_name = self.attributes[record.attrid].name
if attr_name == "on_off":
data[2] = record.value.value
has_change = True
elif attr_name == "window_detection_temperature":
data[1] = record.value.value / 100
has_change = True
elif attr_name == "window_detection_timeout_minutes":
data[0] = record.value.value
has_change = True
if has_change:
return await self.endpoint.tuya_manufacturer.write_attributes(
{MOES_WINDOW_DETECT_ATTR: data}, manufacturer=manufacturer
)
return [
[
foundation.WriteAttributesStatusRecord(
foundation.Status.FAILURE, r.attrid
)
for r in records
]
]
async def command(
self,
command_id: Union[foundation.GeneralCommand, 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, 0x0002):
if command_id == 0x0000:
value = False
elif command_id == 0x0001:
value = True
else:
attrid = self.attributes_by_name["on_off"].id
success, _ = await self.read_attributes(
(attrid,), manufacturer=manufacturer
)
try:
value = success[attrid]
except KeyError:
return foundation.Status.FAILURE
value = not value
(res,) = await self.write_attributes(
{"on_off": value},
manufacturer=manufacturer,
)
return [command_id, res[0].status]
return [command_id, foundation.Status.UNSUP_CLUSTER_COMMAND]
class MoesChildLock(CustomTuyaOnOff):
"""On/Off cluster for the child lock function of the electric heating thermostats."""
def child_lock_change(self, value):
"""Child lock change."""
self._update_attribute(self.attributes_by_name["on_off"].id, value)
def map_attribute(self, attribute, value):
"""Map standardized attribute value to dict of manufacturer values."""
if attribute == "on_off":
return {MOES_CHILD_LOCK_ATTR: value}
class MoesValveState(LocalDataCluster, AnalogOutput):
"""Analog output for Valve State."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.MoesValveState_bus.add_listener(self)
self._update_attribute(self.attributes_by_name["description"].id, "Valve State")
self._update_attribute(self.attributes_by_name["max_present_value"].id, 100)
self._update_attribute(self.attributes_by_name["min_present_value"].id, 0)
self._update_attribute(self.attributes_by_name["resolution"].id, 1)
self._update_attribute(self.attributes_by_name["application_type"].id, 4 << 16)
self._update_attribute(self.attributes_by_name["engineering_units"].id, 98)
def set_value(self, value):
"""Set value."""
self._update_attribute(self.attributes_by_name["present_value"].id, value)
def get_value(self):
"""Get value."""
return self._attr_cache.get(self.attributes_by_name["present_value"].id)
async def write_attributes(self, attributes, manufacturer=None):
"""Override the default Cluster write_attributes."""
for attrid, value in attributes.items():
if isinstance(attrid, str):
attrid = self.attributes_by_name[attrid].id
if attrid not in self.attributes:
self.error("%d is not a valid attribute id", attrid)
continue
self._update_attribute(attrid, value)
await MoesManufClusterSelf[
self.endpoint.device.ieee
].endpoint.tuya_manufacturer.write_attributes(
{MOES_VALVE_STATE_ATTR: value}, manufacturer=None
)
return ([foundation.WriteAttributesStatusRecord(foundation.Status.SUCCESS)],)
class MoesTempCalibration(LocalDataCluster, AnalogOutput):
"""Analog output for Temp Calibration."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.MoesTempCalibration_bus.add_listener(self)
self._update_attribute(
self.attributes_by_name["description"].id, "Temperature Calibration"
)
self._update_attribute(self.attributes_by_name["max_present_value"].id, 9)
self._update_attribute(self.attributes_by_name["min_present_value"].id, -9)
self._update_attribute(self.attributes_by_name["resolution"].id, 1)
self._update_attribute(self.attributes_by_name["application_type"].id, 13 << 16)
self._update_attribute(self.attributes_by_name["engineering_units"].id, 62)
def set_value(self, value):
"""Set value."""
self._update_attribute(self.attributes_by_name["present_value"].id, value)
def get_value(self):
"""Get value."""
return self._attr_cache.get(self.attributes_by_name["present_value"].id)
async def write_attributes(self, attributes, manufacturer=None):
"""Override the default Cluster write_attributes."""
for attrid, value in attributes.items():
if isinstance(attrid, str):
attrid = self.attributes_by_name[attrid].id
if attrid not in self.attributes:
self.error("%d is not a valid attribute id", attrid)
continue
self._update_attribute(attrid, value)
await MoesManufClusterSelf[
self.endpoint.device.ieee
].endpoint.tuya_manufacturer.write_attributes(
{MOES_TEMP_CALIBRATION_ATTR: value},
manufacturer=None,
)
return ([foundation.WriteAttributesStatusRecord(foundation.Status.SUCCESS)],)
class MoesBoostTime(LocalDataCluster, AnalogOutput):
"""Analog output for Boost Time."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.MoesBoostTime_bus.add_listener(self)
self._update_attribute(self.attributes_by_name["description"].id, "Boost Time")
self._update_attribute(self.attributes_by_name["max_present_value"].id, 9999)
self._update_attribute(self.attributes_by_name["min_present_value"].id, 0)
self._update_attribute(self.attributes_by_name["resolution"].id, 1)
self._update_attribute(self.attributes_by_name["application_type"].id, 14 << 16)
self._update_attribute(self.attributes_by_name["engineering_units"].id, 73)
def set_value(self, value):
"""Set value."""
self._update_attribute(self.attributes_by_name["present_value"].id, value)
def get_value(self):
"""Get value."""
return self._attr_cache.get(self.attributes_by_name["present_value"].id)
async def write_attributes(self, attributes, manufacturer=None):
"""Override the default Cluster write_attributes."""
for attrid, value in attributes.items():