forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.cpp
1321 lines (1191 loc) · 48.8 KB
/
thermostat.cpp
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
/*
* thermostat.cpp
*
* Add Support for Thermostat Cluster 0x0201 and Bitron Thermostat 902010/32
*
* A sensor type ZHAThermostat is created with the following options in state and config:
*
* option | read/write | attribute | description
* -------------------|------------|-----------|---------------------
* state.on | read only | 0x0029 | running state on/off
* state.temperature | read only | 0x0000 | measured temperature
* config.heatsetpoint| read write | 0x0012 | heating setpoint
* config.mode | read write | 0x001C | System mode
* config.scheduleron | read write | 0x0025 | scheduler on/off
* config.offset | read write | 0x0010 | temperature offset
* config.scheduler | read write | (command) | scheduled setpoints
*
*
* Example sensor:
*
* /api/<apikey>/sensors/<id>/
* {
* config: {
* "heatsetpoint": 2200,
* "offset": 0,
* "scheduler": "Monday,Tuesday,Wednesday,Thursday,Friday 05:00 2200 19:00 1800;Saturday,Sunday 06:00 2100 19:00 1800;"
* "scheduleron": true
* },
* state: {
* "on": true,
* "temperature": 2150
* },
* "ep": 1,
* "manufacturername": "Bitron Home",
* "modelid": "902010/32",
* "type": "ZHAThermostat",
* ...
* }
*
* Rest API example commands:
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "heatsetpoint": 1800 }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "scheduleron": true }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "offset": 0 }'
* -X PUT /api/<apikey>/sensors/<id>/config -d '{ "scheduler": "Monday 05:00 2200 19:00 1800;" }'
* -d '{ "scheduler": "" }' (send get scheduler command)
*
*
* Attributes (only a subset):
* ID Type Type Description Default
* 0x0000 0x29 int16s Local Temperature --
* 0x0010 0x28 int8s Local Temperature Calibration --
* 0x0012 0x29 int16s Occupied Heating Setpoint 2000 (20 °C)
* 0x0025 0x18 bit8 Programming Operation Mode 0
* Bit#0 of this attribute controls
* the enabling of the Thermostat Scheduler.
* 0x0029 0x19 bit16 Thermostat Running State 0
* Bit0=Heat State On
* Bit1=Cool State On
*
* Commands Received (Client to Server):
* Command-ID Name
* 0x00 Setpoint Raise/Lower
* 0x01 Set Weekly Schedule
* 0x02 Get Weekly Schedule
* 0x03 Clear Weekly Schedule
*
* Commands Generated (Server to Client):
* Command-ID Name
* 0x00 Current Weekly Schedule
*
* Weekly Schedule format
* Octets 1 1 1 2 2/0 2/0 ... 2 2/0 2/0
* Type enum8 bit8 bit8 int16u int16s int16s
* Name Number Day of Mode Transition Heat Cool
* Transitions Week (1,2) Time 1 Setpoint 1 Setpoint1
*
* Day of Week: bitmap8 [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Away]
* bit 0 1 2 3 4 5 6 7
* Example:
* Monday, Tuesday, Thursday, Friday = 0011 0110 = 0x36
* | | | ^-------^| ||
* | | --------------- ||
* | -----------------------------|
* ---------------------------------------
*
*/
#include <bitset>
#include <QJsonDocument>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
/*! Covert Zigbee weekdays bitmap to ISO or v.v.
*/
static quint8 convertWeekdayBitmap(const quint8 weekdayBitmap)
{
quint8 result = 0;
if (weekdayBitmap & 0b00000001) { result |= 0b00000001; }
if (weekdayBitmap & 0b00000010) { result |= 0b01000000; }
if (weekdayBitmap & 0b00000100) { result |= 0b00100000; }
if (weekdayBitmap & 0b00001000) { result |= 0b00010000; }
if (weekdayBitmap & 0b00010000) { result |= 0b00001000; }
if (weekdayBitmap & 0b00100000) { result |= 0b00000100; }
if (weekdayBitmap & 0b01000000) { result |= 0b00000010; }
return result;
}
/*! Serialise a list of transitions.
* \param transitions the list of transitions
* \param s the serialised transitions
*/
bool DeRestPluginPrivate::serialiseThermostatTransitions(const QVariantList &transitions, QString *s)
{
*s = "";
if (transitions.size() < 1 || transitions.size() > 10)
{
return false;
}
for (const QVariant &entry : transitions)
{
QVariantMap transition = entry.toMap();
for (const QString &key : transition.keys())
{
if (key != QLatin1String("localtime") && key != QLatin1String("heatsetpoint"))
{
return false;
}
}
if (!transition.contains(QLatin1String("localtime")) || !transition.contains(QLatin1String("heatsetpoint")) ||
transition[QLatin1String("localtime")].type() != QVariant::String || transition[QLatin1String("heatsetpoint")].type() != QVariant::Double)
{
return false;
}
bool ok;
int heatsetpoint = transition[QLatin1String("heatsetpoint")].toInt(&ok);
if (!ok || heatsetpoint < 500 || heatsetpoint > 3000)
{
return false;
}
QString localtime = transition[QLatin1String("localtime")].toString();
int hh, mm;
ok = (localtime.size() == 6 && localtime.mid(0, 1) == "T" && localtime.mid(3, 1) == ":");
if (ok)
{
hh = localtime.mid(1, 2).toInt(&ok);
}
if (ok)
{
mm = localtime.mid(4, 2).toInt(&ok);
}
if (!ok)
{
return false;
}
*s += QString("T%1:%2|%3")
.arg(hh, 2, 10, QChar('0'))
.arg(mm, 2, 10, QChar('0'))
.arg(heatsetpoint);
}
return true;
}
/*! Deserialise a list of transitions.
* \param s the serialised transitions
* \param transitions the list of transitions
*/
bool DeRestPluginPrivate::deserialiseThermostatTransitions(const QString &s, QVariantList *transitions)
{
transitions->clear();
QStringList list = s.split("T", QString::SkipEmptyParts);
for (const QString &entry : list)
{
QStringList attributes = entry.split("|");
if (attributes.size() != 2)
{
transitions->clear();
return false;
}
QVariantMap map;
map[QLatin1String("localtime")] = "T" + attributes.at(0);
map[QLatin1String("heatsetpoint")] = attributes.at(1).toInt();
transitions->push_back(map);
}
return true;
}
/*! Serialise a thermostat schedule
* \param schedule the schedule
* \param s the serialised schedule
*/
bool DeRestPluginPrivate::serialiseThermostatSchedule(const QVariantMap &schedule, QString *s)
{
*s = "";
for (const QString &key : schedule.keys())
{
QString transitions;
*s += QString("%1/").arg(key);
if (!serialiseThermostatTransitions(schedule[key].toList(), &transitions))
{
return false;
}
*s += transitions;
}
return true;
}
/*! Deserialise a thermostat schedule
* \param s the serialised schedule
* \param schedule the schedule
*/
bool DeRestPluginPrivate::deserialiseThermostatSchedule(const QString &s, QVariantMap *schedule)
{
schedule->clear();
QStringList list = s.split("W", QString::SkipEmptyParts);
for (const QString &entry : list)
{
QStringList attributes = entry.split("/");
QVariantList list;
if (attributes.size() != 2 || !deserialiseThermostatTransitions(attributes.at(1), &list))
{
schedule->clear();
return false;
}
(*schedule)["W" + attributes.at(0)] = list;
}
return true;
}
/*! Update thermostat schedule with new transitions
* \param sensor the sensorNode
* \param newWeekdays the ISO bitmap of the weekdays
* \param transitions the serialised list of transitions
*/
void DeRestPluginPrivate::updateThermostatSchedule(Sensor *sensor, quint8 newWeekdays, QString &transitions)
{
// Deserialise currently saved schedule, without newWeekdays.
bool ok = true;
ResourceItem *item = sensor->item(RConfigSchedule);
if (!item)
{
return;
}
QMap<quint8, QString> map;
QStringList list = item->toString().split("W", QString::SkipEmptyParts);
for (const QString &entry : list)
{
QStringList attributes = entry.split("/");
quint8 weekdays = attributes.at(0).toUInt(&ok);
if (!ok)
{
break;
}
weekdays &= ~newWeekdays;
if (weekdays != 0)
{
map[weekdays] = attributes.at(1);
}
}
if (!ok)
{
map.clear();
}
// Check if we already have an entry with these transitions.
if (transitions.size() > 0)
{
ok = false;
for (const quint8 weekdays : map.keys())
{
if (map[weekdays] == transitions)
{
// Merge the entries.
map.remove(weekdays);
map[weekdays | newWeekdays] = transitions;
ok = true;
break;
}
}
if (!ok)
{
// Create new entry.
map[newWeekdays] = transitions;
}
}
// Store the updated schedule.
QString s = QString("");
for (const quint8 weekdays : map.keys())
{
s += QString("W%1/").arg(weekdays) + map[weekdays];
}
item->setValue(s);
enqueueEvent(Event(RSensors, RConfigSchedule, sensor->id(), item));
updateEtag(sensor->etag);
updateEtag(gwConfigEtag);
sensor->setNeedSaveDatabase(true);
queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
// static const QStringList weekday({"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Away"});
static TaskItem taskScheduleTimer;
static int dayofweekTimer = 0;
/*! Handle packets related to the ZCL Thermostat cluster.
\param ind the APS level data indication containing the ZCL packet
\param zclFrame the actual ZCL frame which holds the Thermostat cluster command or attribute
*/
void DeRestPluginPrivate::handleThermostatClusterIndication(const deCONZ::ApsDataIndication &ind, deCONZ::ZclFrame &zclFrame)
{
Sensor *sensor = getSensorNodeForAddressAndEndpoint(ind.srcAddress(), ind.srcEndpoint(), QLatin1String("ZHAThermostat"));
if (!sensor)
{
DBG_Printf(DBG_INFO, "No thermostat sensor found for 0x%016llX, endpoint: 0x%08X\n", ind.srcAddress().ext(), ind.srcEndpoint());
return;
}
QDataStream stream(zclFrame.payload());
stream.setByteOrder(QDataStream::LittleEndian);
bool isReadAttr = false;
bool isReporting = false;
bool isClusterCmd = false;
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReadAttributesResponseId)
{
isReadAttr = true;
}
if (zclFrame.isProfileWideCommand() && zclFrame.commandId() == deCONZ::ZclReportAttributesId)
{
isReporting = true;
}
if ((zclFrame.frameControl() & 0x09) == (deCONZ::ZclFCDirectionServerToClient | deCONZ::ZclFCClusterCommand))
{
isClusterCmd = true;
}
// Read ZCL reporting and ZCL Read Attributes Response
if (isReadAttr || isReporting)
{
const NodeValue::UpdateType updateType = isReadAttr ? NodeValue::UpdateByZclRead : NodeValue::UpdateByZclReport;
bool configUpdated = false;
bool stateUpdated = false;
while (!stream.atEnd())
{
quint16 attrId;
quint8 attrTypeId;
stream >> attrId;
if (isReadAttr)
{
quint8 status;
stream >> status; // Read Attribute Response status
if (status != deCONZ::ZclSuccessStatus)
{
continue;
}
}
stream >> attrTypeId;
deCONZ::ZclAttribute attr(attrId, attrTypeId, QLatin1String(""), deCONZ::ZclRead, false);
if (!attr.readFromStream(stream))
{
continue;
}
ResourceItem *item = nullptr;
switch (attrId)
{
case 0x0000: // Local Temperature
{
qint16 temperature = attr.numericValue().s16;
item = sensor->item(RStateTemperature);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item->toNumber() != temperature)
{
item->setValue(temperature);
enqueueEvent(Event(RSensors, RStateTemperature, sensor->id(), item));
stateUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0008: // Pi Heating Demand
{
if (sensor->modelId().startsWith(QLatin1String("SPZB")) || // Eurotronic Spirit
sensor->modelId() == QLatin1String("eTRV0100") || // Danfoss Ally
sensor->modelId() == QLatin1String("TRV001") || // Hive TRV
sensor->modelId() == QLatin1String("Thermostat")) // eCozy
{
quint8 valve = attr.numericValue().u8;
bool on = valve > 3;
item = sensor->item(RStateOn);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item->toBool() != on)
{
item->setValue(on);
enqueueEvent(Event(RSensors, RStateOn, sensor->id(), item));
stateUpdated = true;
}
}
item = sensor->item(RStateValve);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toNumber() != valve)
{
item->setValue(valve);
enqueueEvent(Event(RSensors, RStateValve, sensor->id(), item));
stateUpdated = true;
}
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0010: // Local Temperature Calibration (offset in 0.1 °C steps, from -2,5 °C to +2,5 °C)
{
qint8 config = attr.numericValue().s8 * 10;
item = sensor->item(RConfigOffset);
if (item && item->toNumber() != config)
{
item->setValue(config);
enqueueEvent(Event(RSensors, RConfigOffset, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0011: // Occupied Cooling Setpoint
{
qint16 coolSetpoint = attr.numericValue().s16;
item = sensor->item(RConfigCoolSetpoint);
if (item && item->toNumber() != coolSetpoint)
{
item->setValue(coolSetpoint);
enqueueEvent(Event(RSensors, RConfigCoolSetpoint, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0012: // Occupied Heating Setpoint
{
if (sensor->modelId().startsWith(QLatin1String("SPZB"))) // Eurotronic Spirit
{
// Use 0x4003 instead.
}
else
{
qint16 heatSetpoint = attr.numericValue().s16;
item = sensor->item(RConfigHeatSetpoint);
if (item && item->toNumber() != heatSetpoint)
{
item->setValue(heatSetpoint);
enqueueEvent(Event(RSensors, RConfigHeatSetpoint, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x001C: // System Mode
{
if (sensor->modelId().startsWith(QLatin1String("SLR2")) || // Hive
sensor->modelId().startsWith(QLatin1String("SLR1b")) || // Hive
sensor->modelId().startsWith(QLatin1String("TH112")) || // Sinope
sensor->modelId().startsWith(QLatin1String("Zen-01")) || // Zen
sensor->modelId().startsWith(QLatin1String("AC201"))) // OWON
{
qint8 mode = attr.numericValue().s8;
QString mode_set;
mode_set = QString("off");
if ( mode == 0x01 ) { mode_set = QString("auto"); }
if ( mode == 0x03 ) { mode_set = QString("cool"); }
if ( mode == 0x04 ) { mode_set = QString("heat"); }
if ( mode == 0x05 ) { mode_set = QString("emergency heating"); }
if ( mode == 0x06 ) { mode_set = QString("precooling"); }
if ( mode == 0x07 ) { mode_set = QString("fan only"); }
if ( mode == 0x08 ) { mode_set = QString("dry"); }
if ( mode == 0x09 ) { mode_set = QString("sleep"); }
item = sensor->item(RConfigMode);
if (item && !item->toString().isEmpty() && item->toString() != mode_set)
{
item->setValue(mode_set);
enqueueEvent(Event(RSensors, RConfigMode, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0023: // Temperature Setpoint Hold
{
if (sensor->modelId() == QLatin1String("Thermostat")) // eCozy
{
bool scheduleOn = (attr.numericValue().u8 == 0x00); // setpoint hold off -> schedule enabled
item = sensor->item(RConfigScheduleOn);
if (item && item->toBool() != scheduleOn)
{
item->setValue(scheduleOn);
enqueueEvent(Event(RSensors, RConfigScheduleOn, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0025: // Thermostat Programming Operation Mode, default 0 (bit#0 = disable/enable Scheduler)
{
bool on = attr.bitmap() & 0x01 ? true : false;
item = sensor->item(RConfigScheduleOn);
if (item && item->toBool() != on)
{
item->setValue(on);
enqueueEvent(Event(RSensors, RConfigScheduleOn, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0029: // Thermostat Running State (bit0=Heat State On/Off, bit1=Cool State On/Off)
{
bool on = attr.bitmap() > 0;
item = sensor->item(RStateOn);
if (item && updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toBool() != on)
{
item->setValue(on);
enqueueEvent(Event(RSensors, RStateOn, sensor->id(), item));
stateUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0030: // Setpoint Change Source
{
quint8 source = attr.numericValue().u8;
item = sensor->item(RConfigLastChangeSource);
if (item && item->toNumber() != source && source <= 2)
{
item->setValue(source);
enqueueEvent(Event(RSensors, RConfigLastChangeSource, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0031: // Setpoint Change Amount
{
qint16 amount = attr.numericValue().s16;
item = sensor->item(RConfigLastChangeAmount);
if (item && item->toNumber() != amount && amount > -32768)
{
item->setValue(amount);
enqueueEvent(Event(RSensors, RConfigLastChangeAmount, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0032: // Setpoint Change Timestamp
{
QDateTime time = epoch.addSecs(attr.numericValue().u32 - QDateTime::currentDateTime().offsetFromUtc());
item = sensor->item(RConfigLastChangeTime);
if (item) // && item->toVariant().toDateTime().toMSecsSinceEpoch() != time.toMSecsSinceEpoch())
{
item->setValue(time);
enqueueEvent(Event(RSensors, RConfigLastChangeTime, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0045: // AC Louvers Position
{
qint8 mode = attr.numericValue().s8;
QString modeSet;
modeSet = QLatin1String("fully closed");
if ( mode == 0x01 ) { modeSet = QLatin1String("fully closed"); }
else if ( mode == 0x02 ) { modeSet = QLatin1String("fully open"); }
else if ( mode == 0x03 ) { modeSet = QLatin1String("quarter open"); }
else if ( mode == 0x04 ) { modeSet = QLatin1String("half open"); }
else if ( mode == 0x05 ) { modeSet = QLatin1String("three quarters open"); }
item = sensor->item(RConfigSwingMode);
if (item && !item->toString().isEmpty() && item->toString() != modeSet)
{
item->setValue(modeSet);
enqueueEvent(Event(RSensors, RConfigSwingMode, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0403: // Temperature measurement
{
if (sensor->modelId().startsWith(QLatin1String("Super TR"))) // ELKO
{
quint8 mode = attr.numericValue().u8;
QString mode_set;
if ( mode == 0x00 ) { mode_set = QString("air sensor"); }
if ( mode == 0x01 ) { mode_set = QString("floor sensor"); }
if ( mode == 0x03 ) { mode_set = QString("floor protection"); }
item = sensor->item(RConfigTemperatureMeasurement);
if (item && item->toString() != mode_set)
{
item->setValue(mode_set);
enqueueEvent(Event(RSensors, RConfigTemperatureMeasurement, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0406: // Device on/off
{
if (sensor->modelId() == QLatin1String("Super TR")) // ELKO
{
bool on = attr.numericValue().u8 > 0 ? true : false;
item = sensor->item(RStateOn);
if (item && updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toBool() != on)
{
item->setValue(on);
enqueueEvent(Event(RSensors, RStateOn, sensor->id(), item));
stateUpdated = true;
}
// Set config/mode to have an adequate representation based on this attribute
QString mode_set;
if ( on == false ) { mode_set = QString("off"); }
if ( on == true ) { mode_set = QString("heat"); }
item = sensor->item(RConfigMode);
if (item && !item->toString().isEmpty() && item->toString() != mode_set)
{
item->setValue(mode_set);
enqueueEvent(Event(RSensors, RConfigMode, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
}
break;
case 0x0409: // Floor temperature
{
if (sensor->modelId().startsWith(QLatin1String("Super TR"))) // ELKO
{
qint16 floortemp = attr.numericValue().s16;
item = sensor->item(RStateFloorTemperature);
if (item && updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toNumber() != floortemp)
{
item->setValue(floortemp);
enqueueEvent(Event(RSensors, RStateFloorTemperature, sensor->id(), item));
stateUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x0413: // Child lock
{
if (sensor->modelId() == QLatin1String("Super TR")) // ELKO
{
bool enabled = attr.numericValue().u8 > 0 ? true : false;
item = sensor->item(RConfigLocked);
if (item && item->toBool() != enabled)
{
item->setValue(enabled);
enqueueEvent(Event(RSensors, RConfigLocked, sensor->id(), item));
configUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
}
break;
case 0x0415: // Heating active/inactive
{
if (sensor->modelId() == QLatin1String("Super TR")) // ELKO
{
bool on = attr.numericValue().u8 > 0 ? true : false;
item = sensor->item(RStateHeating);
if (item && updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toBool() != on)
{
item->setValue(on);
enqueueEvent(Event(RSensors, RStateHeating, sensor->id(), item));
stateUpdated = true;
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
}
break;
// manufacturerspecific reported by Eurotronic SPZB0001
// https://eurotronic.org/wp-content/uploads/2019/01/Spirit_ZigBee_BAL_web_DE_view_V9.pdf
case 0x4000: // enum8 (0x30): value 0x02, TRV mode
{
if (zclFrame.manufacturerCode() == VENDOR_JENNIC)
{
}
else if (zclFrame.manufacturerCode() == VENDOR_DANFOSS)
{
quint8 windowmode = attr.numericValue().u8;
QString windowmode_set;
if ( windowmode == 0x01 ) { windowmode_set = QString("Closed"); }
if ( windowmode == 0x02 ) { windowmode_set = QString("Hold"); }
if ( windowmode == 0x03 ) { windowmode_set = QString("Open"); }
if ( windowmode == 0x04 ) { windowmode_set = QString("Open (external), closed (internal)"); }
item = sensor->item(RStateWindowOpen);
if (item && updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item && item->toString() != windowmode_set)
{
item->setValue(windowmode_set);
enqueueEvent(Event(RSensors, RStateWindowOpen, sensor->id(), item));
stateUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x4001: // U8 (0x20): value 0x00, valve position
case 0x4002: // U8 (0x20): value 0x00, errors
{
if (zclFrame.manufacturerCode() == VENDOR_JENNIC)
{
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
break;
case 0x4003: // Current temperature set point
{ // this will be reported when manually changing the temperature
if (zclFrame.manufacturerCode() == VENDOR_JENNIC && sensor->modelId().startsWith(QLatin1String("SPZB"))) // Eurotronic Spirit
{
qint16 heatSetpoint = attr.numericValue().s16;
item = sensor->item(RConfigHeatSetpoint);
if (item)
{
if (updateType == NodeValue::UpdateByZclReport)
{
stateUpdated = true;
}
if (item->toNumber() != heatSetpoint)
{
item->setValue(heatSetpoint);
enqueueEvent(Event(RSensors, RConfigHeatSetpoint, sensor->id(), item));
stateUpdated = true;
}
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x4008: // U24 (0x22): 0x000001, host flags
{
if (zclFrame.manufacturerCode() == VENDOR_JENNIC && sensor->modelId().startsWith(QLatin1String("SPZB"))) // Eurotronic Spirit
{
quint32 hostFlags = attr.numericValue().u32;
bool flipped = hostFlags & 0x000002;
QString mode = hostFlags & 0x000010 ? "off" : hostFlags & 0x000004 ? "heat" : "auto";
bool locked = hostFlags & 0x000080;
item = sensor->item(RConfigHostFlags);
if (item && item->toNumber() != hostFlags)
{
item->setValue(hostFlags);
// Hidden attribute - no event
configUpdated = true; // but do save database
}
item = sensor->item(RConfigDisplayFlipped);
if (item && item->toBool() != flipped)
{
item->setValue(flipped);
enqueueEvent(Event(RSensors, RConfigDisplayFlipped, sensor->id(), item));
configUpdated = true;
}
item = sensor->item(RConfigLocked);
if (item && item->toBool() != locked)
{
item->setValue(locked);
enqueueEvent(Event(RSensors, RConfigLocked, sensor->id(), item));
configUpdated = true;
}
item = sensor->item(RConfigMode);
if (item && item->toString() != mode)
{
item->setValue(mode);
enqueueEvent(Event(RSensors, RConfigMode, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x4012: // Mounting mode active
{
if (zclFrame.manufacturerCode() == VENDOR_DANFOSS && (sensor->modelId() == QLatin1String("eTRV0100") ||
sensor->modelId() == QLatin1String("TRV001")))
{
bool enabled = attr.numericValue().u8 > 0 ? true : false;
item = sensor->item(RStateMountingModeActive);
if (item && item->toBool() != enabled)
{
item->setValue(enabled);
enqueueEvent(Event(RSensors, RStateMountingModeActive, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
case 0x4013: // Mounting mode control
{
if (zclFrame.manufacturerCode() == VENDOR_DANFOSS && (sensor->modelId() == QLatin1String("eTRV0100") ||
sensor->modelId() == QLatin1String("TRV001")))
{
bool enabled = attr.numericValue().u8 > 0 ? true : false;
item = sensor->item(RConfigMountingMode);
if (item && item->toBool() != enabled)
{
item->setValue(enabled);
enqueueEvent(Event(RSensors, RConfigMountingMode, sensor->id(), item));
configUpdated = true;
}
}
sensor->setZclValue(updateType, ind.srcEndpoint(), THERMOSTAT_CLUSTER_ID, attrId, attr.numericValue());
}
break;
default:
break;
}
}
if (stateUpdated)
{
sensor->updateStateTimestamp();
enqueueEvent(Event(RSensors, RStateLastUpdated, sensor->id()));
}
if (configUpdated || stateUpdated)
{
updateEtag(sensor->etag);
updateEtag(gwConfigEtag);
sensor->setNeedSaveDatabase(true);
queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}
}
// Read ZCL Cluster Command Response
if (isClusterCmd && zclFrame.commandId() == 0x00) // Get Weekly Schedule Response
{
// Read command parameters into serialised string.
QString transitions = QString("");
quint8 numberOfTransitions = 0;
quint8 dayOfWeek = 0;
quint8 mode = 0;
stream >> numberOfTransitions;
stream >> dayOfWeek;
stream >> mode;
for (quint8 i = 0; i < numberOfTransitions; i++)
{
quint16 transitionTime;
qint16 heatSetpoint;
qint16 coolSetpoint;
stream >> transitionTime;
if (mode & 0x01) // bit 0: heat setpoint
{
stream >> heatSetpoint;
transitions += QString("T%1:%2|%3")
.arg(transitionTime / 60, 2, 10, QChar('0'))
.arg(transitionTime % 60, 2, 10, QChar('0'))
.arg(heatSetpoint);
}
if (mode & 0x02) // bit 1: cold setpoint
{
stream >> coolSetpoint;
// ignored for now
break;
}
}
if (stream.status() == QDataStream::ReadPastEnd)
{
return;
}
const quint8 newWeekdays = convertWeekdayBitmap(dayOfWeek);
updateThermostatSchedule(sensor, newWeekdays, transitions);
}
}
/*! Adds a thermostat command task to the queue.
\param task - the task item
\param cmdId - 0x00 setpoint raise/lower
0x02 get schedule
0x03 clear schedule
\param setpoint - raise/lower value
\param days - days to return schedule
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskThermostatCmd(TaskItem &task, uint16_t mfrCode, uint8_t cmd, int16_t setpoint, uint8_t daysToReturn)
{
task.taskType = TaskThermostat;
task.req.setClusterId(THERMOSTAT_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(cmd);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer);
if (mfrCode != 0x0000)
{
task.zclFrame.setFrameControl(task.zclFrame.frameControl() | deCONZ::ZclFCManufacturerSpecific);
task.zclFrame.setManufacturerCode(mfrCode);
}
// payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);