From 77603c7c04adf8c3f60adb81365f13d7d09114aa Mon Sep 17 00:00:00 2001 From: justme-1968 Date: Mon, 11 Nov 2019 09:45:09 +0000 Subject: [PATCH] 31_HUEDevice.pm: handle IODev rename git-svn-id: https://svn.fhem.de/fhem/trunk@20492 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/30_HUEBridge.pm | 13 +++++++++ fhem/FHEM/30_LIGHTIFY.pm | 17 ++++++++++-- fhem/FHEM/30_tradfri.pm | 14 +++++++++- fhem/FHEM/31_HUEDevice.pm | 57 ++++++++++++++++++++++++++++++++------- 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 8c8ee94dd..63d6cb5a2 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 31_HUEDevice: handle IODev rename - bugfix: 73_PRESENCE: add missing attributes retryCount and retryInterval to AttrList - change: 93_DbRep: commandref revised diff --git a/fhem/FHEM/30_HUEBridge.pm b/fhem/FHEM/30_HUEBridge.pm index 9a983f16e..2a8badc84 100644 --- a/fhem/FHEM/30_HUEBridge.pm +++ b/fhem/FHEM/30_HUEBridge.pm @@ -31,6 +31,7 @@ sub HUEBridge_Initialize($) #Consumer $hash->{DefFn} = "HUEBridge_Define"; + $hash->{RenameFn} = "HUEBridge_Rename"; $hash->{NotifyFn} = "HUEBridge_Notify"; $hash->{SetFn} = "HUEBridge_Set"; $hash->{GetFn} = "HUEBridge_Get"; @@ -257,6 +258,18 @@ HUEBridge_Define($$) return undef; } sub +HUEBridge_Rename($$$) +{ + my ($new,$old) = @_; + + foreach my $chash ( values %{$modules{HUEDevice}{defptr}} ) { + next if( !$chash->{IODev} ); + next if( $chash->{IODev}{NAME} ne $new ); + + HUEDevice_IODevChanged($chash, $old, $new); + } +} +sub HUEBridge_Notify($$) { my ($hash,$dev) = @_; diff --git a/fhem/FHEM/30_LIGHTIFY.pm b/fhem/FHEM/30_LIGHTIFY.pm index d2a89df2d..7f5998547 100644 --- a/fhem/FHEM/30_LIGHTIFY.pm +++ b/fhem/FHEM/30_LIGHTIFY.pm @@ -51,7 +51,7 @@ LIGHTIFY_Initialize($) $hash->{Clients} = ":HUEDevice:"; $hash->{DefFn} = "LIGHTIFY_Define"; - $hash->{NOTIFYDEV} = "global"; + $hash->{RenameFn} = "LIGHTIFY_Rename"; $hash->{NotifyFn} = "LIGHTIFY_Notify"; $hash->{UndefFn} = "LIGHTIFY_Undefine"; $hash->{SetFn} = "LIGHTIFY_Set"; @@ -80,6 +80,8 @@ LIGHTIFY_Define($$) $hash->{INTERVAL} = 60; + $hash->{NOTIFYDEV} = "global"; + if( $init_done ) { LIGHTIFY_Disconnect($hash); LIGHTIFY_Connect($hash); @@ -91,7 +93,18 @@ LIGHTIFY_Define($$) return undef; } - +sub +LIGHTIFY_Rename($$$) +{ + my ($new,$old) = @_; + + foreach my $chash ( values %{$modules{HUEDevice}{defptr}} ) { + next if( !$chash->{IODev} ); + next if( $chash->{IODev}{NAME} ne $new ); + + HUEDevice_IODevChanged($chash, $old, $new); + } +} sub LIGHTIFY_Notify($$) { diff --git a/fhem/FHEM/30_tradfri.pm b/fhem/FHEM/30_tradfri.pm index 83374c85e..fc28d978e 100644 --- a/fhem/FHEM/30_tradfri.pm +++ b/fhem/FHEM/30_tradfri.pm @@ -35,6 +35,7 @@ tradfri_Initialize($) $hash->{WriteFn} = "tradfri_Write"; $hash->{DefFn} = "tradfri_Define"; + $hash->{RenameFn} = "tradfri_Rename"; $hash->{NotifyFn} = "tradfri_Notify"; $hash->{UndefFn} = "tradfri_Undefine"; $hash->{DelayedShutdownFn} = "tradfri_DelayedShutdown"; @@ -106,7 +107,18 @@ tradfri_Define($$) return undef; } - +sub +tradfri_Rename($$$) +{ + my ($new,$old) = @_; + + foreach my $chash ( values %{$modules{HUEDevice}{defptr}} ) { + next if( !$chash->{IODev} ); + next if( $chash->{IODev}{NAME} ne $new ); + + HUEDevice_IODevChanged($chash, $old, $new); + } +} sub tradfri_Notify($$) { diff --git a/fhem/FHEM/31_HUEDevice.pm b/fhem/FHEM/31_HUEDevice.pm index 2074b6b81..717ae4618 100644 --- a/fhem/FHEM/31_HUEDevice.pm +++ b/fhem/FHEM/31_HUEDevice.pm @@ -267,6 +267,51 @@ HUEDevice_summaryFn($$$$) return HUEDevice_devStateIcon($hash); } +sub +HUEDevice_IODevChanged($$$) +{ + my ($hash,$old,$new) = @_; + $hash = $defs{$hash} if( ref($hash) ne 'HASH' ); + my $name = $hash->{NAME}; + + if( $hash->{TYPE} ne 'HUEDevice' ) { + Log3 $name, 1, "$name: can't change IODev for TYPE $hash->{TYPE}"; + return undef; + } + + $old = AttrVal($name, "IODev", undef) if( !$old ); + + my $code = $hash->{ID}; + $code = $old ."-". $code if( $old ); + + delete $modules{HUEDevice}{defptr}{$code}; + + AssignIoPort($hash,$new); + if( defined($hash->{IODev}) ) { + Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME}; + } else { + Log3 $name, 1, "$name: no I/O device"; + } + $new = $hash->{IODev}->{NAME} if( defined($hash->{IODev}) ); + + $code = $hash->{ID}; + $code = $new ."-". $code if( $new ); + $modules{HUEDevice}{defptr}{$code} = $hash; + + if( $old ) { + if( $new ) { + $hash->{DEF} =~ s/IODev=$old/IODev=$new/; + } else { + $hash->{DEF} =~ s/IODev=$old//; + } + } elsif( $new ) { + $hash->{DEF} .= " IODev=$new" + } + $hash->{DEF} =~ s/ / /g; + + return $new; +} + sub HUEDevice_Define($$) { my ($hash, $def) = @_; @@ -305,13 +350,7 @@ sub HUEDevice_Define($$) $hash->{ID} = $hash->{helper}->{devtype}.$id; - AssignIoPort($hash,$iodev) if( !$hash->{IODev} ); - if(defined($hash->{IODev})) { - Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME}; - } else { - Log3 $name, 1, "$name: no I/O device"; - } - $iodev = $hash->{IODev}->{NAME} if( defined($hash->{IODev}) ); + $iodev = HUEDevice_IODevChanged( $hash, undef, $iodev ) if( !$hash->{IODev} ); my $code = $hash->{ID}; $code = $iodev ."-". $code if( defined($iodev) ); @@ -403,7 +442,7 @@ sub HUEDevice_Undefine($$) RemoveInternalTimer($hash); my $code = $hash->{ID}; - $code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}->{NAME}) ); + $code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}) ); delete($modules{HUEDevice}{defptr}{$code}); @@ -443,7 +482,7 @@ HUEDevice_SetParam($$@) } elsif($cmd eq 'down' ) { $cmd = 'pct'; $value = 0; - + } if($cmd eq "pct" && $value == 0 && $subtype ne 'blind' ) { $cmd = "off"; $value = $value2;