diff --git a/FHEM/00_FBAHAHTTP.pm b/FHEM/00_FBAHAHTTP.pm index 32e9381e9..16375fc07 100644 --- a/FHEM/00_FBAHAHTTP.pm +++ b/FHEM/00_FBAHAHTTP.pm @@ -216,20 +216,59 @@ FBAHAHTTP_Set($@) setKeyValue("FBAHAHTTP_PASSWORD_$name", $a[0]); delete($hash->{".SID"}); FBAHAHTTP_Poll($hash); - return; - } - if($type eq "refreshstate") { + + } elsif($type eq "refreshstate") { FBAHAHTTP_Poll($hash); - return; - } - if($type eq "template") { - FBAHAHTTP_Write($hash, $a[0], "applytemplate"); - return; + + } elsif($type eq "template") { + my $cl = $hash->{CL}; + my $doRet = sub($) + { + if($cl) { + asyncOutput($cl, $_[0]); + } else { + Log3 $hash, 4, "$_"; + } + }; + FBAHAHTTP_GetTemplateList($hash, sub($$){ + my ($err, $r) = @_; + return $doRet->($err) if($err); + return $doRet->("Unknown template $a[0]") if(!defined($r->{$a[0]})); + FBAHAHTTP_Write($hash, $r->{$a[0]}, "applytemplate"); + }); } return undef; } +sub +FBAHAHTTP_GetTemplateList($$) +{ + my ($hash, $callbackFn) = @_; + + my $host = ($hash->{DEF} =~ m/^http/i ? $hash->{DEF}:"http://$hash->{DEF}"); + my $sid = $hash->{".SID"}; + my $name = $hash->{NAME}; + return "No SID found" if(!$sid); + HttpUtils_NonblockingGet({ + url=>"$host/webservices/homeautoswitch.lua?". + "sid=$sid&switchcmd=gettemplatelistinfos", + loglevel => AttrVal($name, "verbose", 4), + timeout => AttrVal($name, "fbTimeout", 4), + callback => sub { + if($_[1]) { + delete $hash->{".SID"}; + return $callbackFn->("$name: $_[1]"); + } + my $ret = (defined($_[2]) ? $_[2] : "") ; + my %r; + $ret =~ s: