2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 06:39:11 +00:00

fhem.pl: attr: add -a and -r Options (Forum #83414)

git-svn-id: https://svn.fhem.de/fhem/trunk@16017 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2018-01-27 22:06:08 +00:00
parent b2043fa3fc
commit 0bf933410d
3 changed files with 70 additions and 40 deletions

View File

@ -675,7 +675,7 @@ The following local attributes are used by a wider range of devices:
<a name="attr"></a>
<h3>attr</h3>
<ul>
<code>attr &lt;devspec&gt; &lt;attrname&gt; [&lt;value&gt;] </code><br>
<code>attr [-a|-r] &lt;devspec&gt; &lt;attrname&gt; [&lt;value&gt;]</code><br>
<br>Set an attribute for a device defined by <a href="#define">define</a>.
You can define your own <a href="#attributes">attributes</a> too to use them
@ -685,19 +685,27 @@ The following local attributes are used by a wider range of devices:
See the <a href="#devspec">Device specification</a> section for details on
&lt;devspec&gt;.
After setting the attribute, the global event "ATTR" will be generated.
<br><br>
<br>
If the option -a is specified, append the given value to the currently
existing value. Note: if the value does not start with a comma (,), then a
space will be added automatically to the old value before appending the
new.<br>
With the -r option one can remove a part of an attribute value.<br>
<br>
Examples:
<ul>
<code>attr global verbose 3</code><br>
<code>attr lamp room kitchen</code><br>
<code>attr lamp group lights</code><br>
<code>attr lamp loglevel 6</code><br>
<code>attr weatherstation event-on-update-reading wind,temperature,humidity</code><br>
<code>attr weatherstation event-on-change-reading israining</code><br>
<code>attr weatherstation event-on-change-reading israining,state</code><br>
<code>attr heating stateFormat Temp:measured-temp, Valve:actuator</code><br>
</ul>
<ul><code>
attr global verbose 3<br>
attr lamp room kitchen<br>
attr lamp group lights<br>
attr lamp loglevel 6<br>
attr weatherstation event-on-update-reading wind,temperature,humidity<br>
attr weatherstation event-on-change-reading israining<br>
attr weatherstation event-on-change-reading israining,state<br>
attr heating stateFormat Temp:measured-temp, Valve:actuator<br>
attr -a TYPE=SVG room ,SvgRoom</br>
attr -r TYPE=SVG room ,SvgRoom</br>
</code></ul>
<br>
Notes:<br>

View File

@ -685,7 +685,7 @@ Die folgenden lokalen Attribute werden von mehreren Ger&auml;ten verwendet:
<a name="attr"></a>
<h3>attr</h3>
<ul>
<code>attr &lt;devspec&gt; &lt;attrname&gt; [&lt;value&gt;] </code><br>
<code>attr [-a|-r] &lt;devspec&gt; &lt;attrname&gt; [&lt;value&gt;]</code><br>
<br>
Dieser Befehl setzt ein Attribut f&uuml;r ein Ger&auml;t welches mit <a
@ -702,17 +702,26 @@ Die folgenden lokalen Attribute werden von mehreren Ger&auml;ten verwendet:
Nach der Durchf&uuml;hrung das globale Ereignis "ATTR" wird generiert.
<br>
Falls die Option -a spezifiziert ist, dann wird value zum aktuellen Wert
hinzugef&uuml;gt. Achtung: falls value nicht mit einem Komma (,)
anf&auml;ngt, dann wird es mit einem Leerzeichen angeh&auml;ngt.
<br>
Mit der -r Option kann man Teile eines Attributes wieder entfernen.<br>
<br>
Beispiele:
<ul>
<code>attr global verbose 3</code><br>
<code>attr lamp room kitchen</code><br>
<code>attr lamp group lights</code><br>
<code>attr lamp loglevel 6</code><br>
<code>attr weatherstation event-on-update-reading wind,temperature,humidity</code><br>
<code>attr weatherstation event-on-change-reading israining</code><br>
<code>attr weatherstation event-on-change-reading israining,state</code><br>
<code>attr heating stateFormat Temp:measured-temp, Valve:actuator</code><br>
</ul>
<ul><code>
attr global verbose 3<br>
attr lamp room kitchen<br>
attr lamp group lights<br>
attr lamp loglevel 6<br>
attr weatherstation event-on-update-reading wind,temperature,humidity<br>
attr weatherstation event-on-change-reading israining<br>
attr weatherstation event-on-change-reading israining,state<br>
attr heating stateFormat Temp:measured-temp, Valve:actuator<br>
attr -a TYPE=SVG room ,SvgRoom</br>
attr -r TYPE=SVG room ,SvgRoom</br>
</code></ul>
<br>
Bemerkungen:<br>

View File

@ -2668,11 +2668,13 @@ sub
CommandAttr($$)
{
my ($cl, $param) = @_;
my ($ret, @a);
my ($ret, $append, $remove, @a);
$append = ($param =~ s/^-a //);
$remove = ($param =~ s/^-r //);
@a = split(" ", $param, 3) if($param);
return "Usage: attr <name> <attrname> [<attrvalue>]\n$namedef"
return "Usage: attr [-a|-r] <name> <attrname> [<attrvalue>]\n$namedef"
if(@a && @a < 2);
my @rets;
@ -2680,6 +2682,7 @@ CommandAttr($$)
my $hash = $defs{$sdev};
my $attrName = $a[1];
my $attrVal = $a[2];
if(!defined($hash)) {
push @rets, "Please define $sdev first" if($init_done);#define -ignoreErr
next;
@ -2710,15 +2713,25 @@ CommandAttr($$)
}
}
if($attrName eq 'disable' and $a[2] && $a[2] eq 'toggle') {
$a[2] = IsDisabled($sdev) ? 0 : 1;
if($append && $attrVal && $attr{$sdev} && $attr{$sdev}{$attrName}) {
$attrVal = $attr{$sdev}{$attrName} .
($attrVal =~ m/^,/ ? $attrVal : " $attrVal");
}
if($remove && $attrVal && $attr{$sdev} && $attr{$sdev}{$attrName}) {
my $v = $attr{$sdev}{$attrName};
$v =~ s/\s*$attrVal\s*//;
$attrVal = $v;
}
if($attrName eq 'disable' and $attrVal && $attrVal eq 'toggle') {
$attrVal = IsDisabled($sdev) ? 0 : 1;
}
if($attrName eq "userReadings") {
my @userReadings;
# myReading1[:trigger1] [modifier1] { codecodecode1 }, ...
my $arg= $a[2];
my $arg= $attrVal;
# matches myReading1[:trigger2] { codecode1 }
my $regexi= '\s*([\w.-]+)(:\S*)?\s+((\w+)\s+)?({.*?})\s*';
@ -2755,7 +2768,7 @@ CommandAttr($$)
if($attrName eq "eventMap") {
delete $hash->{".eventMapHash"};
delete $hash->{".eventMapCmd"};
$attr{$sdev}{eventMap} = (defined $a[2] ? $a[2] : 1);
$attr{$sdev}{eventMap} = (defined $attrVal ? $attrVal : 1);
my $r = ReplaceEventMap($sdev, "test", 1); # refresh eventMapCmd
if($r =~ m/^ERROR in eventMap for /) {
delete($attr{$sdev}{eventMap});
@ -2773,11 +2786,11 @@ CommandAttr($$)
pv=>{"%name"=>1, "%state"=>1, "%type"=>1} },
);
if(defined($a[2]) && $ra{$attrName} && $init_done) {
my ($lval,$rp) = ($a[2], $ra{$attrName}{p});
if(defined($attrVal) && $ra{$attrName} && $init_done) {
my ($lval,$rp) = ($attrVal, $ra{$attrName}{p});
if($rp && $lval =~ m/$rp/) {
my $err = perlSyntaxCheck($a[2], %{$ra{$attrName}{pv}});
my $err = perlSyntaxCheck($attrVal, %{$ra{$attrName}{pv}});
return "attr $sdev $a[1]: $err" if($err);
} else {
@ -2792,8 +2805,8 @@ CommandAttr($$)
}
if($fhemdebug && $sdev eq "global") {
$a[2] = "-" if($attrName eq "logfile");
$a[2] = 5 if($attrName eq "verbose");
$attrVal = "-" if($attrName eq "logfile");
$attrVal = 5 if($attrName eq "verbose");
}
$ret = CallFn($sdev, "AttrFn", "set", @a);
if($ret) {
@ -2801,27 +2814,27 @@ CommandAttr($$)
next;
}
my $val = $a[2];
my $val = $attrVal;
$val = 1 if(!defined($val));
$attr{$sdev}{$attrName} = $val;
if($attrName eq "IODev") {
if(!$a[2] || !defined($defs{$a[2]})) {
if(!$attrVal || !defined($defs{$attrVal})) {
if($init_done) {
push @rets,"$sdev: unknown IODev $a[2] specified";
push @rets,"$sdev: unknown IODev $attrVal specified";
} else {
$hash->{IODevMissing} = 1;
$hash->{IODevName} = $a[2];
$hash->{IODevName} = $attrVal;
}
next;
}
my $ioname = $a[2];
my $ioname = $attrVal;
$hash->{IODev} = $defs{$ioname};
delete($defs{$ioname}{".clientArray"}); # Force a recompute
}
if($attrName eq "stateFormat" && $init_done) {
my $err = perlSyntaxCheck($a[2], ("%name"=>""));
my $err = perlSyntaxCheck($attrVal, ("%name"=>""));
return $err if($err);
evalStateFormat($hash);
}