-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.php
More file actions
565 lines (528 loc) · 21.8 KB
/
module.php
File metadata and controls
565 lines (528 loc) · 21.8 KB
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
<?php
/** @noinspection SpellCheckingInspection */
/** @noinspection DuplicatedCode */
/** @noinspection PhpUnused */
declare(strict_types=1);
include_once __DIR__ . '/../libs/constants.php';
class TadoHeating extends IPSModuleStrict
{
public function Create(): void
{
//Never delete this line!
parent::Create();
$this->RegisterProperties();
$this->CreateProfiles();
$this->RegisterVariables();
$this->RegisterTimers();
}
public function Destroy(): void
{
//Never delete this line!
parent::Destroy();
$this->DeleteProfiles();
}
public function ApplyChanges(): void
{
//Wait until IP-Symcon is started
$this->RegisterMessage(0, IPS_KERNELSTARTED);
//Register FM Connect message
$this->RegisterMessage($this->InstanceID, FM_CONNECT);
//Never delete this line!
parent::ApplyChanges();
//Check kernel runlevel
if (IPS_GetKernelRunlevel() != KR_READY) {
return;
}
//Set timer
$milliseconds = $this->ReadPropertyInteger('UpdateInterval') * 1000;
$this->SetTimerInterval('UpdateHeatingState', $milliseconds);
//Update state
if ($this->HasActiveParent()) {
$this->UpdateHeatingZoneState();
}
}
public function GetCompatibleParents(): string
{
//Connect to a new or existing tado° Splitter instance
return json_encode([
'type' => 'connect',
'moduleIDs' => [
TADO_SPLITTER_GUID
]
]);
}
public function MessageSink($TimeStamp, $SenderID, $Message, $Data): void
{
$this->SendDebug('MessageSink', 'SenderID: ' . $SenderID . ', Message: ' . $Message, 0);
switch ($Message) {
case IPS_KERNELSTARTED:
$this->KernelReady();
break;
case FM_CONNECT:
$this->UpdateHeatingZoneState();
break;
}
}
public function GetConfigurationForm(): string
{
$formData = json_decode(file_get_contents(__DIR__ . '/form.json'), true);
return json_encode($formData);
}
public function ReceiveData($JSONString): string
{
//Receive data from splitter, not used at the moment
$this->SendDebug(__FUNCTION__, 'Incoming data: ' . $JSONString, 0);
$data = json_decode($JSONString);
$this->SendDebug(__FUNCTION__, 'Buffer data: ' . json_encode($data->Buffer), 0);
return '';
}
#################### Request action
public function RequestAction($Ident, $Value): void
{
switch ($Ident) {
case 'Mode':
$this->ToggleHeatingMode($Value);
break;
case 'SetpointTemperature':
$this->SetHeatingTemperature($Value);
break;
case 'HeatingTimer':
$this->SetHeatingTimer($Value);
break;
}
}
public function ToggleHeatingMode(bool $Mode): void
{
$this->SendDebug(__FUNCTION__, 'The method was executed with parameter $Mode: ' . json_encode($Mode) . ' (' . microtime(true) . ')', 0);
//Check parent
if (!$this->CheckParent()) {
return;
}
//Check IDs
if (!$this->CheckHomeID()) {
return;
}
if (!$this->CheckZoneID()) {
return;
}
$this->SetValue('Mode', $Mode);
//Manual mode
if (!$Mode) {
$this->SendDebug(__FUNCTION__, 'Mode: Manual', 0);
$this->SetHeating();
}
//Automatic mode
if ($Mode) {
$this->SendDebug(__FUNCTION__, 'Mode: Automatic', 0);
$homeID = intval($this->ReadPropertyString('HomeID'));
$zoneID = intval($this->ReadPropertyString('ZoneID'));
$data = [];
$buffer = [];
$data['DataID'] = TADO_SPLITTER_DATA_GUID;
$buffer['Command'] = 'StopManualMode';
$buffer['Params'] = ['homeID' => $homeID, 'zoneID' => $zoneID];
$data['Buffer'] = $buffer;
$data = json_encode($data);
json_decode($this->SendDataToParent($data), true);
}
$this->UpdateHeatingZoneState();
}
public function SetHeatingTemperature(float $Temperature): void
{
$this->SendDebug(__FUNCTION__, 'The method was executed with parameter $Temperatur: ' . json_encode($Temperature) . ' (' . microtime(true) . ')', 0);
//Check parent
if (!$this->CheckParent()) {
return;
}
//Check IDs
if (!$this->CheckHomeID()) {
return;
}
if (!$this->CheckZoneID()) {
return;
}
$this->SetValue('Mode', false);
$this->SetValue('SetpointTemperature', $Temperature);
$this->SetHeating();
$this->UpdateHeatingZoneState();
}
public function SetHeatingTimer(int $Duration): void
{
$this->SendDebug(__FUNCTION__, 'The method was executed with parameter $Duration: ' . json_encode($Duration) . ' (' . microtime(true) . ')', 0);
//Check parent
if (!$this->CheckParent()) {
return;
}
//Check IDs
if (!$this->CheckHomeID()) {
return;
}
if (!$this->CheckZoneID()) {
return;
}
$this->SetValue('Mode', false);
$this->SetValue('HeatingTimer', $Duration);
$this->SetHeating();
$this->UpdateHeatingZoneState();
}
public function UpdateHeatingZoneState(): void
{
if (!$this->CheckParent()) {
return;
}
//Check IDs
if (!$this->CheckHomeID()) {
return;
}
if (!$this->CheckZoneID()) {
return;
}
$homeID = $this->ReadPropertyString('HomeID');
$zoneID = $this->ReadPropertyString('ZoneID');
$data = [];
$buffer = [];
$data['DataID'] = TADO_SPLITTER_DATA_GUID;
$buffer['Command'] = 'GetZoneState';
$buffer['Params'] = ['homeID' => (int) $homeID, 'zoneID' => (int) $zoneID];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = json_decode($this->SendDataToParent($data), true);
$this->SendDebug(__FUNCTION__, json_encode($result), 0);
if (!empty($result)) {
// Mode
$mode = 1; //automatic
if (array_key_exists('overlayType', $result)) {
if ($result['overlayType'] == 'MANUAL') {
$mode = 0;
}
}
$this->SetValue('Mode', $mode);
//Setpoint temperature
if (array_key_exists('setting', $result)) {
$this->SendDebug(__FUNCTION__, 'array key setting exits', 0);
if (array_key_exists('temperature', $result['setting'])) {
$temperatureSettings = $result['setting']['temperature'];
if (is_array($temperatureSettings)) {
if (array_key_exists('celsius', $temperatureSettings)) {
$temperature = floatval($temperatureSettings['celsius']);
$this->SetValue('SetpointTemperature', $temperature);
}
}
}
}
//Timer
if (array_key_exists('overlay', $result)) {
$heatingTimer = 0;
$this->SendDebug(__FUNCTION__, 'array key overlay exits', 0);
$overlay = $result['overlay'];
if (is_array($overlay)) {
if (array_key_exists('termination', $overlay)) {
$this->SendDebug(__FUNCTION__, 'array key termination exits', 0);
$termination = $overlay['termination'];
if (is_array($termination)) {
if (array_key_exists('typeSkillBasedApp', $termination)) {
$this->SendDebug(__FUNCTION__, 'array key typeSkillBasedApp exits', 0);
$type = $termination['typeSkillBasedApp'];
$this->SendDebug(__FUNCTION__, 'Timer type: ' . $type, 0);
if ($type == 'TIMER' || $type == 'NEXT_TIME_BLOCK') {
if (array_key_exists('remainingTimeInSeconds', $termination)) {
$heatingTimer = $termination['remainingTimeInSeconds'];
}
}
}
}
}
}
$this->SetValue('HeatingTimer', $heatingTimer);
}
//Open window
$windowState = false; #closed
if (array_key_exists('openWindow', $result)) {
$openWindow = $result['openWindow'];
if (is_array($openWindow)) {
$windowState = true; #open
}
}
$this->SetValue('WindowStatus', $windowState);
//Sensor
if (array_key_exists('sensorDataPoints', $result)) {
//Inside temperature
if (array_key_exists('insideTemperature', $result['sensorDataPoints'])) {
$insideTemperature = $result['sensorDataPoints']['insideTemperature']['celsius'];
$this->SetValue('RoomTemperature', (float) $insideTemperature);
}
//Humidity
if (array_key_exists('humidity', $result['sensorDataPoints'])) {
$humidity = $result['sensorDataPoints']['humidity']['percentage'];
$this->SetValue('AirHumidity', (float) $humidity);
}
}
//Heating power
$heatingPower = 0;
if (array_key_exists('activityDataPoints', $result)) {
$activityDataPoints = $result['activityDataPoints'];
if (is_array($activityDataPoints)) {
if (array_key_exists('heatingPower', $activityDataPoints)) {
$heatingPowerData = $activityDataPoints['heatingPower'];
if (is_array($heatingPowerData)) {
if (array_key_exists('percentage', $heatingPowerData)) {
$heatingPower = $heatingPowerData['percentage'];
}
}
}
}
}
$this->SetValue('HeatingPower', $heatingPower);
//Geofencing status
if (array_key_exists('tadoMode', $result)) {
$tadoMode = $result['tadoMode'];
$state = false;
if ($tadoMode == 'AWAY') {
$state = true;
}
$this->SetValue('GeofencingStatus', $state);
}
}
}
#################### Private
private function KernelReady(): void
{
$this->ApplyChanges();
}
private function RegisterProperties(): void
{
$this->RegisterPropertyString('HomeID', '');
$this->RegisterPropertyString('HomeName', '');
$this->RegisterPropertyString('ZoneID', '');
$this->RegisterPropertyString('ZoneName', '');
$this->RegisterPropertyString('ZoneType', '');
$this->RegisterPropertyInteger('UpdateInterval', 0);
}
private function CreateProfiles(): void
{
//Mode
$profile = 'TADO.' . $this->InstanceID . '.Mode';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 0);
}
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Manual'), 'Execute', -1);
IPS_SetVariableProfileAssociation($profile, 1, $this->Translate('Automatic'), 'Calendar', 0x00FF00);
//Setpoint temperature
$profile = 'TADO.' . $this->InstanceID . '.SetpointTemperature';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 2);
}
IPS_SetVariableProfileIcon($profile, 'Temperature');
IPS_SetVariableProfileValues($profile, 0, 25, 0);
IPS_SetVariableProfileDigits($profile, 1);
$color = -1;
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Off'), '', 0xA9B8C4);
IPS_SetVariableProfileAssociation($profile, 0, 'Aus', '', 0xA9B8C4);
for ($i = 5; $i <= 25; $i += 0.5) {
if ($i >= 5 && $i < 7) {
$color = 0x76A595;
}
if ($i >= 7 && $i < 9) {
$color = 0x78AD98;
}
if ($i >= 9 && $i < 11) {
$color = 0x7AB299;
}
if ($i >= 11 && $i < 13) {
$color = 0x7BB798;
}
if ($i >= 13 && $i < 15) {
$color = 0x7EBC98;
}
if ($i >= 15 && $i < 17) {
$color = 0x80C397;
}
if ($i >= 17 && $i < 19) {
$color = 0x82C695;
}
if ($i >= 19 && $i < 21) {
$color = 0xF8D56A;
}
if ($i >= 21 && $i < 23) {
$color = 0xF6C365;
}
if ($i >= 23 && $i < 25) {
$color = 0xF4B160;
}
if ($i >= 25) {
$color = 0xF09035;
}
IPS_SetVariableProfileAssociation($profile, $i, $i . ' °', '', $color);
}
//Heating timer
$profile = 'TADO.' . $this->InstanceID . '.HeatingTimer';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 1);
}
IPS_SetVariableProfileIcon($profile, 'Clock');
IPS_SetVariableProfileValues($profile, 0, 12, 0);
IPS_SetVariableProfileDigits($profile, 2);
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Off'), '', 0xA9B8C4);
IPS_SetVariableProfileAssociation($profile, 1, $this->Translate('Till next time block'), '', 0xA9B8C4);
for ($i = 5; $i <= 45; $i += 5) {
$seconds = $i * 60;
$j = sprintf('%02d', $i);
IPS_SetVariableProfileAssociation($profile, $seconds, '00:' . $j . ':00', '', -1);
}
IPS_SetVariableProfileAssociation($profile, 3600, '01:00:00', '', -1);
for ($i = 15; $i <= 45; $i += 15) {
$seconds = 3600 + ($i * 60);
IPS_SetVariableProfileAssociation($profile, $seconds, '01:' . $i . ':00', '', -1);
}
IPS_SetVariableProfileAssociation($profile, 7200, '02:00:00', '', -1);
for ($i = 15; $i <= 45; $i += 15) {
$seconds = 7200 + ($i * 60);
IPS_SetVariableProfileAssociation($profile, $seconds, '02:' . $i . ':00', '', -1);
}
IPS_SetVariableProfileAssociation($profile, 10800, '03:00:00', '', -1);
IPS_SetVariableProfileAssociation($profile, 10800 + 1800, '03:30:00', '', -1);
IPS_SetVariableProfileAssociation($profile, 14400, '04:00:00', '', -1);
IPS_SetVariableProfileAssociation($profile, 14400 + 1800, '04:30:00', '', -1);
for ($i = 5; $i <= 12; $i++) {
$seconds = $i * 3600;
$j = sprintf('%02d', $i);
IPS_SetVariableProfileAssociation($profile, $seconds, $j . ':00:00', '', -1);
}
//Heating power
$profile = 'TADO.' . $this->InstanceID . '.HeatingPower';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 1);
}
IPS_SetVariableProfileIcon($profile, 'Radiator');
IPS_SetVariableProfileText($profile, '', ' %');
IPS_SetVariableProfileValues($profile, 0, 100, 1);
//Geofencing status
$profile = 'TADO.' . $this->InstanceID . '.GeofencingStatus';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 0);
}
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Home'), 'Presence', 0x00FF00);
IPS_SetVariableProfileAssociation($profile, 1, $this->Translate('Away'), 'Motion', 0x0000FF);
//Window status
$profile = 'TADO.' . $this->InstanceID . '.WindowStatus';
if (!IPS_VariableProfileExists($profile)) {
IPS_CreateVariableProfile($profile, 0);
}
IPS_SetVariableProfileIcon($profile, 'Window');
IPS_SetVariableProfileAssociation($profile, 0, $this->Translate('Closed'), '', 0x00FF00);
IPS_SetVariableProfileAssociation($profile, 1, $this->Translate('Open'), '', 0x0000FF);
}
private function DeleteProfiles(): void
{
$profiles = ['Mode', 'SetpointTemperature', 'HeatingTimer', 'HeatingPower', 'GeofencingStatus'];
foreach ($profiles as $profile) {
$profileName = 'TADO.' . $this->InstanceID . '.' . $profile;
if (@IPS_VariableProfileExists($profileName)) {
IPS_DeleteVariableProfile($profileName);
}
}
}
private function RegisterVariables(): void
{
//Mode
$profile = 'TADO.' . $this->InstanceID . '.Mode';
$this->RegisterVariableBoolean('Mode', $this->Translate('Automatic'), $profile, 10);
$this->EnableAction('Mode');
//Set point temperature
$profile = 'TADO.' . $this->InstanceID . '.SetpointTemperature';
$this->RegisterVariableFloat('SetpointTemperature', $this->Translate('Setpoint temperature'), $profile, 20);
$this->EnableAction('SetpointTemperature');
//Heating timer
$profile = 'TADO.' . $this->InstanceID . '.HeatingTimer';
$this->RegisterVariableInteger('HeatingTimer', 'Timer', $profile, 30);
$this->EnableAction('HeatingTimer');
//Room temperature
$this->RegisterVariableFloat('RoomTemperature', $this->Translate('Room temperature'), '~Temperature', 40);
//Humidity
$this->RegisterVariableFloat('AirHumidity', $this->Translate('Air humidity'), '~Humidity.F', 50);
//Heating power
$profile = 'TADO.' . $this->InstanceID . '.HeatingPower';
$this->RegisterVariableInteger('HeatingPower', $this->Translate('Heating Power'), $profile, 60);
//Geofencing status
$profile = 'TADO.' . $this->InstanceID . '.GeofencingStatus';
$this->RegisterVariableBoolean('GeofencingStatus', 'Geofencing Status', $profile, 70);
//Window status
$profile = 'TADO.' . $this->InstanceID . '.WindowStatus';
$this->RegisterVariableBoolean('WindowStatus', $this->Translate('Window'), $profile, 80);
}
private function RegisterTimers(): void
{
$this->RegisterTimer('UpdateHeatingState', 0, 'TADO_UpdateHeatingZoneState(' . $this->InstanceID . ');');
}
private function CheckParent(): bool
{
$result = true;
if (!$this->HasActiveParent()) {
$this->SendDebug(__FUNCTION__, 'Parent splitter instance is inactive!', 0);
$result = false;
}
return $result;
}
private function CheckHomeID(): bool
{
$result = true;
if (empty($this->ReadPropertyString('HomeID'))) {
$this->SendDebug(__FUNCTION__, 'No HomeID assigned in the properties!', 0);
$result = false;
}
return $result;
}
private function CheckZoneID(): bool
{
$result = true;
if (empty($this->ReadPropertyString('ZoneID'))) {
$this->SendDebug(__FUNCTION__, 'No ZoneID assigned in the properties!', 0);
$result = false;
}
return $result;
}
private function SetHeating(): void
{
$homeID = intval($this->ReadPropertyString('HomeID'));
$zoneID = intval($this->ReadPropertyString('ZoneID'));
$data = [];
$buffer = [];
$data['DataID'] = TADO_SPLITTER_DATA_GUID;
$power = 'ON';
$temperature = $this->GetValue('SetpointTemperature');
if ($temperature == 0) {
$power = 'OFF';
}
$heatingTimer = $this->GetValue('HeatingTimer');
//No Timer
if ($heatingTimer == 0) {
$this->SendDebug(__FUNCTION__, 'Do not use timer, set temperature for unlimited time.', 0);
$buffer['Command'] = 'SetHeatingZoneTemperature';
$buffer['Params'] = ['homeID' => $homeID, 'zoneID' => $zoneID, 'power' => $power, 'temperature' => $temperature];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = json_decode($this->SendDataToParent($data), true);
$this->SendDebug(__FUNCTION__, json_encode($result), 0);
}
//Timer till next time block
if ($heatingTimer == 1) {
$this->SendDebug(__FUNCTION__, 'Use heating timer, set temperature till next time block.', 0);
$buffer['Command'] = 'SetHeatingZoneTemperatureTimerNextTimeBlock';
$buffer['Params'] = ['homeID' => $homeID, 'zoneID' => $zoneID, 'power' => $power, 'temperature' => $temperature];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = json_decode($this->SendDataToParent($data), true);
$this->SendDebug(__FUNCTION__, json_encode($result), 0);
}
//Timer
if ($heatingTimer >= 300) {
$this->SendDebug(__FUNCTION__, 'Use heating timer, set temperature for ' . $heatingTimer . 'seconds.', 0);
$buffer['Command'] = 'SetHeatingZoneTemperatureTimer';
$buffer['Params'] = ['homeID' => $homeID, 'zoneID' => $zoneID, 'power' => $power, 'temperature' => $temperature, 'durationInSeconds' => $heatingTimer];
$data['Buffer'] = $buffer;
$data = json_encode($data);
$result = json_decode($this->SendDataToParent($data), true);
$this->SendDebug(__FUNCTION__, json_encode($result), 0);
}
}
}