-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShuttersControl.pm
2069 lines (1820 loc) · 73.5 KB
/
ShuttersControl.pm
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
###############################################################################
#
# Developed with Kate
#
# (c) 2018-2021 Copyright: Marko Oldenburg ([email protected])
# All rights reserved
#
# Special thanks goes to:
# - Bernd (Cluni) this module is based on the logic of his script "Rollladensteuerung für HM/ROLLO inkl. Abschattung und Komfortfunktionen in Perl" (https://forum.fhem.de/index.php/topic,73964.0.html)
# - Beta-User for many tests, many suggestions and good discussions
# - pc1246 write english commandref
# - FunkOdyssey commandref style
# - sledge fix many typo in commandref
# - many User that use with modul and report bugs
# - Christoph (christoph.kaiser.in) Patch that expand RegEx for Window Events
# - Julian (Loredo) expand Residents Events for new Residents functions
# - Christoph (Christoph Morrison) for fix Commandref, many suggestions and good discussions
#
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License,or
# any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# $Id: ShuttersControl.pm 25115 2021-10-24 16:45:38Z CoolTux $
#
###############################################################################
### Notizen
# !!!!! - Innerhalb einer Shutterschleife kein CommandAttr verwenden. Bring Fehler!!! Kommen Raumnamen in die Shutterliste !!!!!!
#
package main;
use strict;
use warnings;
use utf8;
sub ascAPIget {
my ( $getCommand, $shutterDev, $value ) = @_;
return ShuttersControl_ascAPIget( $getCommand, $shutterDev, $value );
}
sub ascAPIset {
my ( $setCommand, $shutterDev, $value ) = @_;
return ShuttersControl_ascAPIset( $setCommand, $shutterDev, $value );
}
## unserer packagename
package FHEM::Automation::ShuttersControl;
use strict;
use warnings;
use POSIX qw(strftime);
use utf8;
use Encode;
use FHEM::Meta;
use GPUtils qw(GP_Import GP_Export);
use Data::Dumper; #only for Debugging #Beta-User: still needed?!?
use Date::Parse;
use FHEM::Automation::ShuttersControl::Shutters;
use FHEM::Automation::ShuttersControl::Dev;
use FHEM::Automation::ShuttersControl::Shading qw (CheckASC_ConditionsForShadingFn);
use FHEM::Automation::ShuttersControl::EventProcessingFunctions qw (:ALL);
use FHEM::Automation::ShuttersControl::Helper qw (:ALL);
# try to use JSON::MaybeXS wrapper
# for chance of better performance + open code
eval {
require JSON::MaybeXS;
import JSON::MaybeXS qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# try to use JSON wrapper
# for chance of better performance
eval {
# JSON preference order
local $ENV{PERL_JSON_BACKEND} =
'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP'
if ( !defined( $ENV{PERL_JSON_BACKEND} ) );
require JSON;
import JSON qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# In rare cases, Cpanel::JSON::XS may
# be installed but JSON|JSON::MaybeXS not ...
eval {
require Cpanel::JSON::XS;
import Cpanel::JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# In rare cases, JSON::XS may
# be installed but JSON not ...
eval {
require JSON::XS;
import JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to built-in JSON which SHOULD
# be available since 5.014 ...
eval {
require JSON::PP;
import JSON::PP qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to JSON::backportPP in really rare cases
require JSON::backportPP;
import JSON::backportPP qw(decode_json encode_json);
1;
}
}
}
}
}
## Import der FHEM Funktionen
#-- Run before package compilation
BEGIN {
# Import from main context
GP_Import(
qw(
devspec2array
readingsSingleUpdate
readingsBulkUpdate
readingsBulkUpdateIfChanged
readingsBeginUpdate
readingsEndUpdate
defs
modules
Log3
CommandAttr
attr
CommandDeleteAttr
CommandDeleteReading
CommandSet
readingFnAttributes
AttrVal
ReadingsVal
IsDisabled
deviceEvents
init_done
addToDevAttrList
addToAttrList
delFromDevAttrList
delFromAttrList
gettimeofday
InternalTimer
RemoveInternalTimer
computeAlignTime
ReplaceEventMap
EvalSpecials
AnalyzeCommandChain
)
);
#-- Export to main context with different name
GP_Export(
qw(
ascAPIget
ascAPIset
DevStateIcon
)
);
}
## Die Attributsliste welche an die Rolläden verteilt wird. Zusammen mit Default Werten
our %userAttrList = (
'ASC_Mode_Up:absent,always,off,home' => '-',
'ASC_Mode_Down:absent,always,off,home' => '-',
'ASC_Up:time,astro,brightness,roommate' => '-',
'ASC_Down:time,astro,brightness,roommate' => '-',
'ASC_AutoAstroModeMorning:REAL,CIVIL,NAUTIC,ASTRONOMIC,HORIZON' => '-',
'ASC_AutoAstroModeMorningHorizon:-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9'
=> '-',
'ASC_AutoAstroModeEvening:REAL,CIVIL,NAUTIC,ASTRONOMIC,HORIZON' => '-',
'ASC_AutoAstroModeEveningHorizon:-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9'
=> '-',
'ASC_Open_Pos:0,10,20,30,40,50,60,70,80,90,100' => [ '', 0, 100 ],
'ASC_Closed_Pos:0,10,20,30,40,50,60,70,80,90,100' => [ '', 100, 0 ],
'ASC_Sleep_Pos:0,10,20,30,40,50,60,70,80,90,100' => [ '', -1, -1 ],
'ASC_Pos_Reading' => [ '', 'position', 'pct' ],
'ASC_Time_Up_Early' => '-',
'ASC_Time_Up_Late' => '-',
'ASC_Time_Up_WE_Holiday' => '-',
'ASC_Time_Down_Early' => '-',
'ASC_Time_Down_Late' => '-',
'ASC_PrivacyUpValue_beforeDayOpen' => '-',
'ASC_PrivacyDownValue_beforeNightClose' => '-',
'ASC_PrivacyUp_Pos' => [ '', 50, 50 ],
'ASC_PrivacyDown_Pos' => [ '', 50, 50 ],
'ASC_TempSensor' => '-',
'ASC_Ventilate_Window_Open:on,off' => '-',
'ASC_LockOut:soft,hard,off' => '-',
'ASC_LockOut_Cmd:inhibit,blocked,protection' => '-',
'ASC_BlockingTime_afterManual' => '-',
'ASC_BlockingTime_beforNightClose' => '-',
'ASC_BlockingTime_beforDayOpen' => '-',
'ASC_BrightnessSensor' => '-',
'ASC_Shading_Pos:10,20,30,40,50,60,70,80,90,100' => [ '', 80, 20 ],
'ASC_Shading_Mode:absent,always,off,home' => '-',
'ASC_Shading_InOutAzimuth' => '-',
'ASC_Shading_StateChange_SunnyCloudy' => '-',
'ASC_Shading_MinMax_Elevation' => '-',
'ASC_Shading_Min_OutsideTemperature' => '-',
'ASC_Shading_WaitingPeriod' => '-',
'ASC_Shading_BetweenTheTime' => '-',
'ASC_Drive_Delay' => '-',
'ASC_Drive_DelayStart' => '-',
'ASC_Shutter_IdleDetection' => '-',
'ASC_WindowRec' => '-',
'ASC_WindowRec_subType:twostate,threestate' => '-',
'ASC_WindowRec_PosAfterDayClosed:open,lastManual' => '-',
'ASC_ShuttersPlace:window,terrace,awning,EG_window' => '-',
'ASC_Ventilate_Pos:10,20,30,40,50,60,70,80,90,100' => [ '', 70, 30 ],
'ASC_ComfortOpen_Pos:0,10,20,30,40,50,60,70,80,90,100' => [ '', 20, 80 ],
'ASC_GuestRoom:on,off' => '-',
'ASC_Antifreeze:off,soft,hard,am,pm' => '-',
'ASC_Antifreeze_Pos:5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100'
=> [ '', 85, 15 ],
'ASC_Partymode:on,off' => '-',
'ASC_Roommate_Device' => '-',
'ASC_Roommate_Reading' => '-',
'ASC_Self_Defense_Mode:absent,gone,off' => '-',
'ASC_Self_Defense_AbsentDelay' => '-',
'ASC_WiggleValue' => '-',
'ASC_WindParameters' => '-',
'ASC_DriveUpMaxDuration' => '-',
'ASC_WindProtection:on,off' => '-',
'ASC_RainProtection:on,off' => '-',
'ASC_ExternalTrigger' => '-',
'ASC_Adv:on,off' => '-',
'ASC_SlatPosCmd_SlatDevice' => '-',
'ASC_commandTemplate' => '-',
);
my %posSetCmds = (
ZWave => 'dim',
Siro => 'pct',
CUL_HM => 'pct',
ROLLO => 'pct',
Shelly => 'pct',
HMCCUDEV => 'pct',
SOMFY => 'position',
tahoma => 'dim',
KLF200Node => 'pct',
DUOFERN => 'position',
HM485 => 'level',
SELVECommeo => 'position',
SELVE => 'position',
EnOcean => 'position',
);
## 2 Objekte werden erstellt
our $shutters = FHEM::Automation::ShuttersControl::Shutters->new();
our $ascDev = FHEM::Automation::ShuttersControl::Dev->new();
sub ascAPIget {
my $getCommand = shift;
my $shutterDev = shift;
my $value = shift;
my $getter = 'get' . $getCommand;
if ( defined($value) && $value ) {
$shutters->setShuttersDev($shutterDev);
return $shutters->$getter($value);
}
elsif ( defined($shutterDev) && $shutterDev ) {
$shutters->setShuttersDev($shutterDev);
return $shutters->$getter;
}
else {
return $ascDev->$getter;
}
return;
}
sub ascAPIset {
my $setCommand = shift;
my $shutterDev = shift;
my $value = shift;
my $setter = 'set' . $setCommand;
if ( defined($shutterDev)
&& $shutterDev
&& defined($value) )
{
$shutters->setShuttersDev($shutterDev);
$shutters->$setter($value);
}
return;
}
sub Define {
my $hash = shift // return;
my $aArg = shift // return;
return $@ if ( !FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
return 'only one AutoShuttersControl instance allowed'
if ( devspec2array('TYPE=AutoShuttersControl') > 1 )
; # es wird geprüft ob bereits eine Instanz unseres Modules existiert,wenn ja wird abgebrochen
return 'too few parameters: define <name> ShuttersControl'
if ( scalar( @{$aArg} ) != 2 );
my $name = shift @$aArg;
$hash->{MID} = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
; # eine Ein Eindeutige ID für interne FHEM Belange / nicht weiter wichtig
$hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{NOTIFYDEV} = 'global,'
. $name; # Liste aller Devices auf deren Events gehört werden sollen
#$hash->{shutters} = $shutters;
#$hash->{ascDev} = $ascDev;
$ascDev->setName($name);
readingsSingleUpdate(
$hash,
'state',
'please set attribute ASC with value 1 or 2 in all auto controlled shutter devices and then execute \'set DEVICENAME scanForShutters\'',
1
);
CommandAttr( undef, $name . ' room ASC' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
CommandAttr( undef, $name . ' icon fts_shutter_automatic' )
if ( AttrVal( $name, 'icon', 'none' ) eq 'none' );
CommandAttr( undef,
$name . ' devStateIcon { ShuttersControl_DevStateIcon($name) }' )
if ( AttrVal( $name, 'devStateIcon', 'none' ) eq 'none' );
addToAttrList('ASC:0,1,2','AutoShuttersControl');
Log3( $name, 3, "AutoShuttersControl ($name) - defined" );
$modules{AutoShuttersControl}{defptr}{ $hash->{MID} } = $hash;
return;
}
sub Undef {
my $hash = shift;
my $name = shift;
UserAttributs_Readings_ForShutters( $hash, 'del' )
; # es sollen alle Attribute und Readings in den Rolläden Devices gelöscht werden welche vom Modul angelegt wurden
delFromAttrList('ASC:0,1,2');
delete( $modules{AutoShuttersControl}{defptr}{ $hash->{MID} } );
Log3( $name, 3, "AutoShuttersControl ($name) - delete device $name" );
return;
}
sub Delete {
my $hash = shift;
my $name = shift;
RemoveShuttersTimer($hash);
return;
}
sub Shutdown {
my $hash = shift;
RemoveShuttersTimer($hash);
return;
}
sub Notify {
my $hash = shift // return;
my $dev = shift // return;
my $name = $hash->{NAME};
my $devname = $dev->{NAME};
my $devtype = $dev->{TYPE};
my $events = deviceEvents( $dev, 1 );
return if ( !$events );
Log3( $name, 4,
"AutoShuttersControl ($name) - Devname: "
. $devname
. " Name: "
. $name
. " Notify: "
. Dumper $events); # mit Dumper
if (
(
grep m{^DEFINED.$name$}xms,
@{$events} && $devname eq 'global' && $init_done
)
|| (
grep m{^INITIALIZED$}xms,
@{$events} or grep m{^REREADCFG$}xms,
@{$events} or grep m{^MODIFIED.$name$}xms,
@{$events}
)
&& $devname eq 'global'
)
{
readingsSingleUpdate( $hash, 'partyMode', 'off', 0 )
if ( $ascDev->getPartyMode eq 'none' );
readingsSingleUpdate( $hash, 'hardLockOut', 'off', 0 )
if ( $ascDev->getHardLockOut eq 'none' );
readingsSingleUpdate( $hash, 'sunriseTimeWeHoliday', 'off', 0 )
if ( $ascDev->getSunriseTimeWeHoliday eq 'none' );
readingsSingleUpdate( $hash, 'selfDefense', 'off', 0 )
if ( $ascDev->getSelfDefense eq 'none' );
readingsSingleUpdate( $hash, 'controlShading', 'off', 0 )
if ( $ascDev->getAutoShuttersControlShading eq 'none' );
readingsSingleUpdate( $hash, 'ascEnable', 'on', 0 )
if ( $ascDev->getASCenable eq 'none' );
CommandAttr( undef,
$name . ' devStateIcon { ShuttersControl_DevStateIcon($name) }' )
if (
AttrVal(
$name, 'devStateIcon',
'{ ShuttersControl_DevStateIcon($name) }'
) ne '{ ShuttersControl_DevStateIcon($name) }'
);
CommandDeleteAttr( undef, $name . ' event-on-change-reading' )
if ( AttrVal( $name, 'event-on-change-reading', 'none' ) ne 'none' );
CommandDeleteAttr( undef, $name . ' event-on-update-reading' )
if ( AttrVal( $name, 'event-on-update-reading', 'none' ) ne 'none' );
# Ist der Event ein globaler und passt zum Rest der Abfrage oben wird nach neuen Rolläden Devices gescannt und eine Liste im Rolladenmodul sortiert nach Raum generiert
ShuttersDeviceScan($hash)
if ( ReadingsVal( $name, 'userAttrList', 'none' ) ne 'none' );
}
return
if ( ref( $hash->{helper}{shuttersList} ) ne 'ARRAY'
|| scalar( @{ $hash->{helper}{shuttersList} } ) == 0 );
my $posReading = $shutters->getPosCmd;
if ( $devname eq $name ) {
if ( grep m{^userAttrList:.rolled.out$}xms, @{$events} ) {
if ( scalar( @{ $hash->{helper}{shuttersList} } ) > 0 ) {
WriteReadingsShuttersList($hash);
UserAttributs_Readings_ForShutters( $hash, 'add' );
InternalTimer(
gettimeofday() + 3,
'FHEM::Automation::ShuttersControl::RenewSunRiseSetShuttersTimer',
$hash
);
InternalTimer(
gettimeofday() + 5,
'FHEM::Automation::ShuttersControl::AutoSearchTwilightDev',
$hash
);
InternalTimer(
gettimeofday() + 5,
sub() { CommandSet( undef, $name . ' controlShading on' ) },
$hash
)
if ( ReadingsVal( $name, 'controlShading', 'off' ) ne 'off' );
}
}
elsif ( grep m{^partyMode:.off$}xms, @{$events} ) {
EventProcessingPartyMode($hash);
}
elsif ( grep m{^sunriseTimeWeHoliday:.(on|off)$}xms, @{$events} ) {
RenewSunRiseSetShuttersTimer($hash);
}
}
elsif ( $devname eq "global" )
{ # Kommt ein globales Event und beinhaltet folgende Syntax wird die Funktion zur Verarbeitung aufgerufen
if (
grep
m{^(ATTR|DELETEATTR)\s(.*ASC_Time_Up_WE_Holiday|.*ASC_Up|.*ASC_Down|.*ASC_AutoAstroModeMorning|.*ASC_AutoAstroModeMorningHorizon|.*ASC_AutoAstroModeEvening|.*ASC_AutoAstroModeEveningHorizon|.*ASC_Time_Up_Early|.*ASC_Time_Up_Late|.*ASC_Time_Down_Early|.*ASC_Time_Down_Late|.*ASC_autoAstroModeMorning|.*ASC_autoAstroModeMorningHorizon|.*ASC_PrivacyDownValue_beforeNightClose|.*ASC_PrivacyUpValue_beforeDayOpen|.*ASC_autoAstroModeEvening|.*ASC_autoAstroModeEveningHorizon|.*ASC_Roommate_Device|.*ASC_WindowRec|.*ASC_residentsDev|.*ASC_rainSensor|.*ASC_windSensor|.*ASC_tempSensor|.*ASC_BrightnessSensor|.*ASC_twilightDevice|.*ASC_ExternalTrigger|.*ASC_Shading_StateChange_SunnyCloudy|.*ASC_TempSensor|.*ASC_Shading_Mode)(\s.*|$)}xms,
@{$events}
)
{
EventProcessingGeneral( $hash, undef, join( ' ', @{$events} ) );
}
}
elsif ( grep m{^($posReading):\s\d{1,3}(\.\d{1,3})?$}xms, @{$events} ) {
ASC_Debug( 'Notify: '
. ' ASC_Pos_Reading Event vom Rollo '
. $devname
. ' wurde erkannt '
. ' - RECEIVED EVENT: '
. Dumper $events);
EventProcessingShutters( $hash, $devname, join( ' ', @{$events} ) );
}
else {
EventProcessingGeneral( $hash, $devname, join( ' ', @{$events} ) )
; # bei allen anderen Events wird die entsprechende Funktion zur Verarbeitung aufgerufen
}
return;
}
sub Set {
my $hash = shift // return;
my $aArg = shift // return;
my $name = shift @$aArg;
my $cmd = shift @$aArg
// return qq{"set $name" needs at least one argument};
if ( lc $cmd eq 'renewalltimer' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
RenewSunRiseSetShuttersTimer($hash);
}
elsif ( lc $cmd eq 'renewtimer' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
CreateSunRiseSetShuttersTimer( $hash, $aArg->[0] );
}
elsif ( lc $cmd eq 'scanforshutters' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
ShuttersDeviceScan($hash);
}
elsif ( lc $cmd eq 'createnewnotifydev' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
CreateNewNotifyDev($hash);
}
elsif ( lc $cmd eq 'partymode' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate( $hash, $cmd, $aArg->[0], 1 )
if ( $aArg->[0] ne ReadingsVal( $name, 'partyMode', 0 ) );
}
elsif ( lc $cmd eq 'hardlockout' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate( $hash, $cmd, $aArg->[0], 1 );
HardewareBlockForShutters( $hash, $aArg->[0] );
}
elsif ( lc $cmd eq 'sunrisetimeweholiday' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate( $hash, $cmd, $aArg->[0], 1 );
}
elsif ( lc $cmd eq 'controlshading' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
my $response = CheckASC_ConditionsForShadingFn($hash,$aArg->[0]);
readingsSingleUpdate(
$hash, $cmd,
(
$aArg->[0] eq 'off' ? $aArg->[0]
: (
$response eq 'none' ? $aArg->[0]
: $response
)
),
1
);
}
elsif ( lc $cmd eq 'selfdefense' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate( $hash, $cmd, $aArg->[0], 1 );
}
elsif ( lc $cmd eq 'ascenable' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate( $hash, $cmd, $aArg->[0], 1 );
}
elsif ( lc $cmd eq 'advdrivedown' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
EventProcessingAdvShuttersClose($hash);
}
elsif ( lc $cmd eq 'shutterascenabletoggle' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
readingsSingleUpdate(
$defs{ $aArg->[0] },
'ASC_Enable',
(
ReadingsVal( $aArg->[0], 'ASC_Enable', 'off' ) eq 'on'
? 'off'
: 'on'
),
1
);
}
elsif ( lc $cmd eq 'wiggle' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) > 1 );
(
$aArg->[0] eq 'all'
? wiggleAll($hash)
: wiggle( $hash, $aArg->[0] )
);
}
else {
my $list = 'scanForShutters:noArg';
$list .=
' renewAllTimer:noArg advDriveDown:noArg partyMode:on,off hardLockOut:on,off sunriseTimeWeHoliday:on,off controlShading:on,off selfDefense:on,off ascEnable:on,off wiggle:all,'
. join( ',', @{ $hash->{helper}{shuttersList} } )
. ' shutterASCenableToggle:'
. join( ',', @{ $hash->{helper}{shuttersList} } )
. ' renewTimer:'
. join( ',', @{ $hash->{helper}{shuttersList} } )
if ( ReadingsVal( $name, 'userAttrList', 'none' ) eq 'rolled out'
&& defined( $hash->{helper}{shuttersList} )
&& scalar( @{ $hash->{helper}{shuttersList} } ) > 0 );
$list .= ' createNewNotifyDev:noArg'
if ( ReadingsVal( $name, 'userAttrList', 'none' ) eq 'rolled out'
&& AttrVal( $name, 'ASC_expert', 0 ) == 1 );
return "Unknown argument $cmd,choose one of $list";
}
return;
}
sub Get {
my $hash = shift // return;
my $aArg = shift // return;
my $name = shift @$aArg // return;
my $cmd = shift @$aArg
// return qq{"get $name" needs at least one argument};
if ( lc $cmd eq 'shownotifydevsinformations' ) {
return "usage: $cmd" if ( scalar( @{$aArg} ) != 0 );
my $ret = GetMonitoredDevs($hash);
return $ret;
}
else {
my $list = "";
$list .= " showNotifyDevsInformations:noArg"
if ( ReadingsVal( $name, 'userAttrList', 'none' ) eq 'rolled out'
&& AttrVal( $name, 'ASC_expert', 0 ) == 1 );
return "Unknown argument $cmd,choose one of $list";
}
}
sub ShuttersDeviceScan {
my $hash = shift;
my $name = $hash->{NAME};
delete $hash->{helper}{shuttersList};
my @list;
@list = devspec2array('ASC=[1-2]');
CommandDeleteReading( undef, $name . ' .*_nextAstroTimeEvent' );
if ( scalar(@list) == 0 ) {
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, 'userAttrList', 'none' );
readingsBulkUpdate( $hash, 'state', 'no shutters found' );
readingsEndUpdate( $hash, 1 );
return;
}
my $shuttersList = '';
for my $shuttersDev (@list) {
push( @{ $hash->{helper}{shuttersList} }, $shuttersDev )
; ## einem Hash wird ein Array zugewiesen welches die Liste der erkannten Rollos beinhaltet
$shutters->setShuttersDev($shuttersDev);
#### Ab hier können temporäre Änderungen der Attribute gesetzt werden
#### Gleichlautende Attribute wo lediglich die Parameter geändert werden sollen müssen hier gelöscht und die Parameter in der Funktion renewSetSunriseSunsetTimer gesetzt werden,
#### vorher empfiehlt es sich die dort vergebenen Parameter aus zu lesen um sie dann hier wieder neu zu setzen. Dazu wird das shutters Objekt um einen Eintrag
#### 'AttrUpdateChanges' erweitert
if (
ReadingsVal(
$shuttersDev, '.ASC_AttrUpdateChanges_' . $hash->{VERSION},
0
) == 0
)
{
$shutters->setAttrUpdateChanges( 'ASC_ShuttersPlace',
AttrVal( $shuttersDev, 'ASC_ShuttersPlace', 'none' ) );
delFromDevAttrList( $shuttersDev, 'ASC_ShuttersPlace' );
# $shutters->setAttrUpdateChanges( 'ASC_Down',
# AttrVal( $shuttersDev, 'ASC_Down', 'none' ) );
# delFromDevAttrList( $shuttersDev, 'ASC_Down' );
# $shutters->setAttrUpdateChanges( 'ASC_Self_Defense_Mode',
# AttrVal( $shuttersDev, 'ASC_Self_Defense_Mode', 'none' ) );
# delFromDevAttrList( $shuttersDev, 'ASC_Self_Defense_Mode' );
# $shutters->setAttrUpdateChanges( 'ASC_Self_Defense_Exclude',
# AttrVal( $shuttersDev, 'ASC_Self_Defense_Exclude', 'none' ) );
# delFromDevAttrList( $shuttersDev, 'ASC_Self_Defense_Exclude' );
}
####
####
$shuttersList = $shuttersList . ',' . $shuttersDev;
$shutters->setLastManPos( $shutters->getStatus );
$shutters->setLastPos( $shutters->getStatus );
$shutters->setDelayCmd('none');
$shutters->setNoDelay(0);
$shutters->setSelfDefenseAbsent( 0, 0 );
$shutters->setPosSetCmd( $posSetCmds{ $defs{$shuttersDev}->{TYPE} } );
$shutters->setShadingStatus(
( $shutters->getStatus != $shutters->getShadingPos ? 'out' : 'in' )
);
# $shutters->setShadingLastStatus(
# ( $shutters->getStatus != $shutters->getShadingPos ? 'in' : 'out' )
# );
$shutters->setPushBrightnessInArray( $shutters->getBrightness );
readingsSingleUpdate( $defs{$shuttersDev}, 'ASC_Enable', 'on', 0 )
if ( ReadingsVal( $shuttersDev, 'ASC_Enable', 'none' ) eq 'none' );
if ( $shutters->getIsDay ) {
$shutters->setSunrise(1);
$shutters->setSunset(0);
}
else {
$shutters->setSunrise(0);
$shutters->setSunset(1);
}
}
$hash->{NOTIFYDEV} = "global," . $name . $shuttersList;
if ( $ascDev->getMonitoredDevs ne 'none' ) {
$hash->{monitoredDevs} =
eval { decode_json( $ascDev->getMonitoredDevs ) };
my $notifyDevString = $hash->{NOTIFYDEV};
while ( my $shuttersDev = each %{ $hash->{monitoredDevs} } ) {
$notifyDevString .= ',' . $shuttersDev;
}
$hash->{NOTIFYDEV} = $notifyDevString;
}
readingsSingleUpdate( $hash, 'userAttrList', 'rolled out', 1 );
return;
}
## Die Funktion schreibt in das Moduldevice Readings welche Rolläden in welchen Räumen erfasst wurden.
sub WriteReadingsShuttersList {
my $hash = shift;
my $name = $hash->{NAME};
CommandDeleteReading( undef, $name . ' room_.*' );
readingsBeginUpdate($hash);
for my $shuttersDev ( @{ $hash->{helper}{shuttersList} } ) {
readingsBulkUpdate(
$hash,
'room_'
. makeReadingName( AttrVal( $shuttersDev, 'room', 'unsorted' ) ),
ReadingsVal(
$name,
'room_'
. makeReadingName(
AttrVal( $shuttersDev, 'room', 'unsorted' )
),
''
)
. ','
. $shuttersDev
)
if (
ReadingsVal(
$name,
'room_'
. makeReadingName(
AttrVal( $shuttersDev, 'room', 'unsorted' )
),
'none'
) ne 'none'
);
readingsBulkUpdate(
$hash,
'room_'
. makeReadingName( AttrVal( $shuttersDev, 'room', 'unsorted' ) ),
$shuttersDev
)
if (
ReadingsVal(
$name,
'room_'
. makeReadingName(
AttrVal( $shuttersDev, 'room', 'unsorted' )
),
'none'
) eq 'none'
);
}
readingsBulkUpdate( $hash, 'state', 'active' );
readingsEndUpdate( $hash, 0 );
return;
}
sub UserAttributs_Readings_ForShutters {
my $hash = shift;
my $cmd = shift;
my $name = $hash->{NAME};
while ( my ( $attrib, $attribValue ) = each %{userAttrList} ) {
for my $shuttersDev ( @{ $hash->{helper}{shuttersList} } ) {
addToDevAttrList( $shuttersDev, $attrib, 'AutoShuttersControl' )
; ## fhem.pl bietet eine Funktion um ein userAttr Attribut zu befüllen. Wir schreiben also in den Attribut userAttr alle unsere Attribute rein. Pro Rolladen immer ein Attribut pro Durchlauf
## Danach werden die Attribute die im userAttr stehen gesetzt und mit default Werten befüllt
## CommandAttr hat nicht funktioniert. Führte zu Problemen
## https://github.com/LeonGaultier/fhem-AutoShuttersControl/commit/e33d3cc7815031b087736c1054b98c57817e7083
if ( $cmd eq 'add' ) {
if ( ref($attribValue) ne 'ARRAY' ) {
$attr{$shuttersDev}{ ( split( ':', $attrib ) )[0] } =
$attribValue
if (
!defined(
$attr{$shuttersDev}{ ( split( ':', $attrib ) )[0] }
)
&& $attribValue ne '-'
);
}
else {
$attr{$shuttersDev}{ ( split( ':', $attrib ) )[0] } =
$attribValue->[ AttrVal( $shuttersDev, 'ASC', 2 ) ]
if (
!defined(
$attr{$shuttersDev}{ ( split( ':', $attrib ) )[0] }
)
&& $attrib eq 'ASC_Pos_Reading'
);
}
### associatedWith damit man sieht das der Rollladen mit einem ASC Device verbunden ist
my $associatedString =
ReadingsVal( $shuttersDev, 'associatedWith', 'none' );
if ( $associatedString ne 'none' ) {
my %hash;
%hash = map { ( $_ => 1 ) }
split( ',', "$associatedString,$name" );
readingsSingleUpdate( $defs{$shuttersDev},
'associatedWith', join( ',', sort keys %hash ), 0 );
}
else {
readingsSingleUpdate( $defs{$shuttersDev},
'associatedWith', $name, 0 );
}
#######################################
}
## Oder das Attribut wird wieder gelöscht.
elsif ( $cmd eq 'del' ) {
$shutters->setShuttersDev($shuttersDev);
RemoveInternalTimer( $shutters->getInTimerFuncHash );
CommandDeleteReading( undef, $shuttersDev . ' .?(ASC)_.*' );
CommandDeleteAttr( undef, $shuttersDev . ' ASC' );
delFromDevAttrList( $shuttersDev, $attrib );
### associatedWith wird wieder entfernt
my $associatedString =
ReadingsVal( $shuttersDev, 'associatedWith', 'none' );
my %hash;
%hash = map { ( $_ => 1 ) }
grep { " $name " !~ m{ $shuttersDev }xms }
split( ',', "$associatedString,$name" );
if ( keys %hash > 1 ) {
readingsSingleUpdate( $defs{$shuttersDev},
'associatedWith', join( ',', sort keys %hash ), 0 );
}
else {
CommandDeleteReading( undef,
$shuttersDev . ' associatedWith' );
}
###################################
}
}
}
return;
}
## Fügt dem NOTIFYDEV Hash weitere Devices hinzu
sub AddNotifyDev {
### Beispielaufruf: AddNotifyDev( $hash, $3, $1, $2 ) if ( $3 ne 'none' );
my ( $hash, $attrVal, $shuttersDev, $shuttersAttr ) = @_;
$attrVal = ( split( ':', $attrVal ) )[0];
my ( $key, $value ) = split( ':', ( split( ' ', $attrVal ) )[0], 2 )
; ## Wir versuchen die Device Attribute anders zu setzen. device=DEVICE reading=READING
$attrVal = $key;
my $name = $hash->{NAME};
my $notifyDev = $hash->{NOTIFYDEV};
$notifyDev = '' if ( !$notifyDev );
my %hash;
%hash = map { ( $_ => 1 ) }
split( ',', "$notifyDev,$attrVal" );
$hash->{NOTIFYDEV} = join( ',', sort keys %hash );
my @devs = split( ',', $attrVal );
for my $dev (@devs) {
$hash->{monitoredDevs}{$dev}{$shuttersDev} = $shuttersAttr;
}
readingsSingleUpdate( $hash, '.monitoredDevs',
eval { encode_json( $hash->{monitoredDevs} ) }, 0 );
return;
}
## entfernt aus dem NOTIFYDEV Hash Devices welche als Wert in Attributen steckten
sub DeleteNotifyDev {
my $hash = shift;
my $shuttersDev = shift;
my $shuttersAttr = shift;
my $name = $hash->{NAME};
my $notifyDevs =
ExtractNotifyDevFromEvent( $hash, $shuttersDev, $shuttersAttr );
for my $notifyDev ( keys( %{$notifyDevs} ) ) {
Log3( $name, 4,
"AutoShuttersControl ($name) - DeleteNotifyDev - NotifyDev: "
. $notifyDev );
delete $hash->{monitoredDevs}{$notifyDev}{$shuttersDev};
if ( !keys %{ $hash->{monitoredDevs}{$notifyDev} } ) {
delete $hash->{monitoredDevs}{$notifyDev};
my $notifyDevString = $hash->{NOTIFYDEV};
$notifyDevString = '' if ( !$notifyDevString );
my %hash;
%hash = map { ( $_ => 1 ) }
grep { " $notifyDev " !~ m{ $_ }xms }
split( ',', "$notifyDevString,$notifyDev" );
$hash->{NOTIFYDEV} = join( ',', sort keys %hash );
}
}
readingsSingleUpdate( $hash, '.monitoredDevs',
eval { encode_json( $hash->{monitoredDevs} ) }, 0 );
return;
}
# Sub für das Zusammensetzen der Rolläden Steuerbefehle
sub ShuttersCommandSet {
my $hash = shift;
my $shuttersDev = shift;
my $posValue = shift;
my $name = $hash->{NAME};
$shutters->setShuttersDev($shuttersDev);
if (
(
CheckIfShuttersWindowRecOpen($shuttersDev) == 2
&& $shutters->getShuttersPlace eq 'terrace'
&& !$shutters->getQueryShuttersPos($posValue)
)
|| (
(
$shutters->getPartyMode eq 'on'
&& $ascDev->getPartyMode eq 'on'
)