diff --git a/73_AutoShuttersControl.pm b/73_AutoShuttersControl.pm index 3003ef8..23b6ae4 100644 --- a/73_AutoShuttersControl.pm +++ b/73_AutoShuttersControl.pm @@ -41,7 +41,7 @@ use warnings; -my $version = "0.0.12"; +my $version = "0.0.16"; sub AutoShuttersControl_Initialize($) { @@ -53,8 +53,8 @@ sub AutoShuttersControl_Initialize($) { $hash->{NotifyFn} = "AutoShuttersControl::Notify"; $hash->{UndefFn} = "AutoShuttersControl::Undef"; $hash->{AttrFn} = "AutoShuttersControl::Attr"; - $hash->{AttrList} = "disable:1 ". - "disabledForIntervals ". + $hash->{AttrList} = #"disable:1 ". + #"disabledForIntervals ". "guestPresence:on,off ". "temperatureSensor ". "temperatureReading ". @@ -113,6 +113,7 @@ BEGIN { modules Log3 CommandAttr + CommandDeleteAttr CommandDeleteReading AttrVal ReadingsVal @@ -212,6 +213,8 @@ sub Undef($$) { my $name = $hash->{NAME}; + UserAttributsForShutters($hash,'del'); + delete($modules{AutoShuttersControl}{defptr}{$hash->{MID}}); Log3 $name, 3, "AutoShuttersControl ($name) - delete device $name"; @@ -277,22 +280,52 @@ sub Notify($$) { and $devname eq 'global') { ShuttersDeviceScan($hash); - WriteReadingsShuttersList($hash) unless( scalar(@{$hash->{helper}{shuttersList}} ) == 0 ); + WriteReadingsShuttersList($hash) + unless( scalar(@{$hash->{helper}{shuttersList}} ) == 0 ); } elsif( grep /^userAttrList:.rolled.out$/,@{$events} and $devname eq $name) { - CreateUserAttributsForShutters($hash) unless( scalar(@{$hash->{helper}{shuttersList}} ) == 0 ); + UserAttributsForShutters($hash,'add') + unless( scalar(@{$hash->{helper}{shuttersList}} ) == 0 ); + + } elsif( $devname eq "global" ) { + if (grep /^(ATTR|DELETEATTR).+AutoShuttersControl_Roommate_Device/,@{$events}) { + EventProcessing($hash,join(' ',@{$events})); + + } + + + } -# } elsif( ) { -# -# -# -# } return; } +sub EventProcessing($$) { + + my ($hash,$events) = @_; + my $name = $hash->{NAME}; + + + if( $events =~ m#^ATTR.(.*).(AutoShuttersControl_Roommate_Device).(.*)$# ) { + return if( $hash->{NOTIFYDEV} =~ m#$3# ); + + AddNotifyDev($hash,$3); + + if( ReadingsVal($name,'.monitoredDevs','none') ne 'none' ) { + readingsSingleUpdate($hash,'.monitoredDevs',ReadingsVal($name,'.monitoredDevs','none') . ',' . $1 . ':' . $2 . ':' . $3,0); + } else { + readingsSingleUpdate($hash,'.monitoredDevs',$1 . ':' . $2 . ':' . $3,0); + } + + } elsif($events =~ m#^DELETEATTR.+AutoShuttersControl_Roommate_Device.(.*)$# ) { + return unless( $hash->{NOTIFYDEV} =~ m#$1# ); + + DeleteNotifyDev($hash,$1); + } +} + sub Set($$@) { my ($hash, $name, @aa) = @_; @@ -300,10 +333,9 @@ sub Set($$@) { my ($cmd, @args) = @aa; - if( lc $cmd eq 'rolloutattr' ) { + if( lc $cmd eq 'deletenotifydev' ) { return "usage: $cmd" if( @args != 0 ); - - CreateUserAttributsForShutters($hash); + } elsif( lc $cmd eq 'shutterslist' ) { return "usage: $cmd" if( @args != 0 ); @@ -326,7 +358,6 @@ sub ShuttersDeviceScan($) { my @list; - my @notifyDev = split(',',$hash->{NOTIFYDEV}); @list = devspec2array('(Roll.*|Shutter.*):FILTER=TYPE!=AutoShuttersControl'); @list = split( "[ \t][ \t]*", $hash->{DEF} ) if($hash->{DETECTDEV} eq 'manual'); @@ -335,13 +366,27 @@ sub ShuttersDeviceScan($) { return unless( scalar(@list) > 0 ); + foreach(@list) { push (@{$hash->{helper}{shuttersList}},$_); - push (@notifyDev,$_); + AddNotifyDev($hash,$_); Log3 $name, 4, "AutoShuttersControl ($name) - ShuttersList: " . $_; } - $hash->{NOTIFYDEV} = join(',',@notifyDev); + + if( ReadingsVal($name,'.monitoredDevs','none') ne 'none' ) { + + my $notifyDevString = ''; + my @notifyDev = split(',',ReadingsVal($name,'.monitoredDevs','none')); + + foreach my $notifyDev (@notifyDev) { + $notifyDevString .= ',' . (split(':',$notifyDev))[2]; + } + + $hash->{NOTIFYDEV} = $hash->{NOTIFYDEV} . $notifyDevString; + } + + readingsSingleUpdate($hash,'userAttrList','rolled out',1); } @@ -366,21 +411,54 @@ sub WriteReadingsShuttersList($) { readingsEndUpdate($hash,0); } -sub CreateUserAttributsForShutters($) { +sub UserAttributsForShutters($$) { - my $hash = shift; - my $name = $hash->{NAME}; + my ($hash,$cmd) = @_; + my $name = $hash->{NAME}; while( my ($attrib,$attribValue) = each %{userAttrList} ) { foreach (@{$hash->{helper}{shuttersList}}) { addToDevAttrList($_,$attrib); - CommandAttr(undef,$_ . ' ' . (split(':',$attrib))[0] . ' ' . $attribValue) if( defined($attribValue) and $attribValue and AttrVal($_,(split(':',$attrib))[0],'none') eq 'none' ); + + if( $cmd eq 'add' ) { + CommandAttr(undef,$_ . ' ' . (split(':',$attrib))[0] . ' ' . $attribValue) if( defined($attribValue) and $attribValue and AttrVal($_,(split(':',$attrib))[0],'none') eq 'none' ); + + } elsif( $cmd eq 'del' ) { + CommandDeleteAttr(undef,$_ . ' ' . (split(':',$attrib))[0]); + } } } } +sub AddNotifyDev($$) { + + my ($hash,$dev) = @_; + + + my @notifyDev = split(',',$hash->{NOTIFYDEV}); + + push (@notifyDev,$dev); + $hash->{NOTIFYDEV} = join(',',@notifyDev); +} + +sub DeleteNotifyDev($$) { + + my ($hash,$dev) = @_; + + + my @notifyDev = split(',',$hash->{NOTIFYDEV}); + + @notifyDev = grep {$_ ne $dev} @notifyDev; + $hash->{NOTIFYDEV} = join(',',@notifyDev); + + @notifyDev = split(',',ReadingsVal($hash->{NAME},'.monitoredDevs','none')); + + @notifyDev = grep {$_ ne $dev} @notifyDev; + readingsSingleUpdate($hash,'.monitoredDevs',join(',',@notifyDev),0); +} + @@ -412,60 +490,7 @@ sub CreateUserAttributsForShutters($) { =end html_DE