From 9879af040afab4af2d789bb87eb34532c55df29a Mon Sep 17 00:00:00 2001 From: jpawlowski Date: Sat, 1 Dec 2018 09:08:59 +0000 Subject: [PATCH] Unit.pm: allow using formatValue without device name, determine unit by reading name git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@17874 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/Unit.pm | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/FHEM/Unit.pm b/FHEM/Unit.pm index 4f10d4fc6..c2154d51d 100644 --- a/FHEM/Unit.pm +++ b/FHEM/Unit.pm @@ -2878,6 +2878,9 @@ sub rname2rtype ($$@) { my $guess; my %return; + # reading name exactly matches rtype + return $reading if ( $rtypes->{$reading} && !wantarray ); + # remove some prefix or other values to # flatten reading name $r =~ s/^fc\d+_//i; @@ -2902,7 +2905,7 @@ sub rname2rtype ($$@) { $rt = ( $readingsDB->{global}{$dr}{rtype} ? $readingsDB->{global}{$dr}{rtype} - : "-" + : undef ); } @@ -2913,7 +2916,7 @@ sub rname2rtype ($$@) { $rt = ( $readingsDB->{global}{$r}{rtype} ? $readingsDB->{global}{$r}{rtype} - : "-" + : undef ); } @@ -2923,7 +2926,14 @@ sub rname2rtype ($$@) { $rt = $1; } + if (wantarray) { + return ( $reading, $return{aliasname}, $return{shortname}, + $return{guess} ) + if ( $rtypes->{$reading} ); + return ( $rt, $return{aliasname}, $return{shortname}, $return{guess} ); + } return $rt if ( $rt && $rtypes->{$rt} ); + return undef; } ###################################### @@ -3944,10 +3954,17 @@ sub formatValue($$$;$$$$) { # find desc for device:reading sub readingsDesc($;$) { my ( $device, $reading ) = @_; + $device = "" unless ( defined($device) ); my $desc = getCombinedKeyValAttr( $device, "readingsDesc", $reading ); my $rtype; - $rtype = $desc->{rtype} if ( $desc->{rtype} ); + if ( $desc->{rtype} ) { + $rtype = $desc->{rtype}; + } + else { + $rtype = rname2rtype( $device, $reading ); + $desc->{rtype} = $rtype; + } if ( $rtype && defined( $rtypes->{$rtype} ) ) { @@ -4178,14 +4195,18 @@ sub makeSTATE($;$$) { # get combined hash for settings from module, device, global and device attributes sub getCombinedKeyValAttr($;$$) { my ( $name, $attribute, $reading ) = @_; - my $d = $defs{$name} if ( $defs{$name} ); - my $m = $modules{ $d->{TYPE} } if ( $d && $d->{TYPE} ); + my $d = $defs{$name} if ( $name && $defs{$name} ); + my $m = $modules{ $d->{TYPE} } if ( $d && $d->{TYPE} ); my $g = $defs{"global"}; # join hashes until 3rd level my $desc; - if ( $m && $m->{$attribute} && ref( $m->{$attribute} ) eq "HASH" ) { + if ( $m + && $attribute + && $m->{$attribute} + && ref( $m->{$attribute} ) eq "HASH" ) + { Log3( $name, 5, "getCombinedKeyValAttr $name $reading: including HASH from module X_Initialize() function" ); @@ -4214,7 +4235,11 @@ sub getCombinedKeyValAttr($;$$) { } } - if ( $g && $g->{$attribute} && ref( $g->{$attribute} ) eq "HASH" ) { + if ( $g + && $attribute + && $g->{$attribute} + && ref( $g->{$attribute} ) eq "HASH" ) + { Log3( $name, 5, "getCombinedKeyValAttr $name $reading: including HASH from global attribute $attribute" ); @@ -4243,7 +4268,11 @@ sub getCombinedKeyValAttr($;$$) { } } - if ( $d && $d->{$attribute} && ref( $d->{$attribute} ) eq "HASH" ) { + if ( $d + && $attribute + && $d->{$attribute} + && ref( $d->{$attribute} ) eq "HASH" ) + { Log3( $name, 5, "getCombinedKeyValAttr $name $reading: including HASH from device attribute $attribute" );