2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 19:36:02 +00:00

36_Shelly.pm: Neue Version mit Fixes

git-svn-id: https://svn.fhem.de/fhem/trunk@17392 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
phenning 2018-09-23 11:15:14 +00:00
parent 8c8172b705
commit 485aec5008

View File

@ -38,7 +38,7 @@ use vars qw{%attr %defs};
sub Log($$); sub Log($$);
#-- globals on start #-- globals on start
my $version = "1.11"; my $version = "1.12";
#-- these we may get on request #-- these we may get on request
my %gets = ( my %gets = (
@ -132,7 +132,8 @@ sub Shelly_Define($$) {
}; };
$hash->{DURATION} = 0; $hash->{DURATION} = 0;
$hash->{BLOCKED} = 0; $hash->{MOVING} = 0;
delete $hash->{BLOCKED};
$hash->{INTERVAL} = 60; $hash->{INTERVAL} = 60;
$modules{Shelly}{defptr}{$a[0]} = $hash; $modules{Shelly}{defptr}{$a[0]} = $hash;
@ -141,10 +142,16 @@ sub Shelly_Define($$) {
my $oid = $init_done; my $oid = $init_done;
$init_done = 1; $init_done = 1;
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
my $err = Shelly_status($hash);
if( !defined($err) ){
readingsBulkUpdate($hash,"state","initialized"); readingsBulkUpdate($hash,"state","initialized");
readingsBulkUpdate($hash,"network","connected");
}else{
readingsBulkUpdate($hash,"state",$err);
readingsBulkUpdate($hash,"network","not connected");
}
readingsEndUpdate($hash,1); readingsEndUpdate($hash,1);
InternalTimer(gettimeofday() + 5, "Shelly_status", $hash,1);
$init_done = $oid; $init_done = $oid;
return undef; return undef;
@ -180,6 +187,9 @@ sub Shelly_Attr(@) {
my $model = AttrVal($name,"model","shelly2"); my $model = AttrVal($name,"model","shelly2");
my $mode = AttrVal($name,"mode","relay"); my $mode = AttrVal($name,"mode","relay");
#-- temporary code
delete $hash->{BLOCKED};
#--------------------------------------- #---------------------------------------
if ( ($cmd eq "set") && ($attrName =~ /model/) ) { if ( ($cmd eq "set") && ($attrName =~ /model/) ) {
my $regex = "((".join(")|(",(keys %shelly_models))."))"; my $regex = "((".join(")|(",(keys %shelly_models))."))";
@ -201,6 +211,8 @@ sub Shelly_Attr(@) {
fhem("deletereading ".$name." stop_reason.*"); fhem("deletereading ".$name." stop_reason.*");
fhem("deletereading ".$name." last_dir.*"); fhem("deletereading ".$name." last_dir.*");
fhem("deletereading ".$name." pct.*"); fhem("deletereading ".$name." pct.*");
delete $hash->{MOVING};
delete $hash->{DURATION};
} }
#-- always clear readings for meters #-- always clear readings for meters
@ -354,7 +366,8 @@ sub Shelly_Set ($@) {
if( $cmd =~ /^((on)|(off)).*/ ){ if( $cmd =~ /^((on)|(off)).*/ ){
my $channel = $value; my $channel = $value;
if( ($channel !~ /[0123]/) || $channel >= $shelly_models{$model}[0] ){
if( !defined($channel) || ($channel !~ /[0123]/) || $channel >= $shelly_models{$model}[0] ){
if( !defined($channel) ){ if( !defined($channel) ){
$channel = AttrVal($name,"defchannel",undef); $channel = AttrVal($name,"defchannel",undef);
if( !defined($channel) ){ if( !defined($channel) ){
@ -393,7 +406,7 @@ sub Shelly_Set ($@) {
return "[Shelly_Set] Unknown argument " . $cmd . ", choose one of ".$newkeys; return "[Shelly_Set] Unknown argument " . $cmd . ", choose one of ".$newkeys;
} }
if( $hash->{BLOCKED} ){ if( $hash->{MOVING} ){
$msg = "Error: roller blind still moving, wait for some time"; $msg = "Error: roller blind still moving, wait for some time";
Log3 $name,1,"[Shelly_Set] ".$msg; Log3 $name,1,"[Shelly_Set] ".$msg;
return $msg return $msg
@ -436,7 +449,7 @@ sub Shelly_Set ($@) {
$cmd = "?go=close&duration=".$time; $cmd = "?go=close&duration=".$time;
} }
} }
$hash->{BLOCKED} = 1; $hash->{MOVING} = 1;
$hash->{DURATION} = $time; $hash->{DURATION} = $time;
Shelly_updown($hash,$cmd); Shelly_updown($hash,$cmd);
} }
@ -483,10 +496,13 @@ sub Shelly_Set ($@) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $url; my $url;
my $state = $hash->{READINGS}{state}{VAL}; my $state = $hash->{READINGS}{state}{VAL};
my $net = $hash->{READINGS}{network}{VAL};
return
if( $net ne "connected" );
my $model = AttrVal($name,"model",""); my $model = AttrVal($name,"model","");
if ( $hash && !$data){ if ( $hash && !$err && !$data ){
$url = "http://".$hash->{TCPIP}."/".$cmd; $url = "http://".$hash->{TCPIP}."/".$cmd;
Log3 $name, 1,"[Shelly_configure] called with only hash => Issue a non-blocking call to $url"; Log3 $name, 1,"[Shelly_configure] called with only hash => Issue a non-blocking call to $url";
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
@ -533,11 +549,7 @@ sub Shelly_Set ($@) {
my $url; my $url;
my $state = $hash->{READINGS}{state}{VAL}; my $state = $hash->{READINGS}{state}{VAL};
#-- reset blocking due to existing movement if ( $hash && !$err && !$data ){
$hash->{BLOCKED} = 0;
$hash->{DURATION} = 0;
if ( $hash && !$data){
$url = "http://".$hash->{TCPIP}."/status"; $url = "http://".$hash->{TCPIP}."/status";
Log3 $name, 5,"[Shelly_status] called with only hash => Issue a non-blocking call to $url"; Log3 $name, 5,"[Shelly_status] called with only hash => Issue a non-blocking call to $url";
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
@ -548,8 +560,10 @@ sub Shelly_Set ($@) {
}elsif ( $hash && $err ){ }elsif ( $hash && $err ){
Log3 $name, 1,"[Shelly_status] has error $err"; Log3 $name, 1,"[Shelly_status] has error $err";
readingsSingleUpdate($hash,"state","Error",1); readingsSingleUpdate($hash,"state","Error",1);
return; readingsSingleUpdate($hash,"network","not connected",1);
return $err;
} }
Log3 $name, 5,"[Shelly_status] has obtained data $data"; Log3 $name, 5,"[Shelly_status] has obtained data $data";
my $json = JSON->new->utf8; my $json = JSON->new->utf8;
@ -569,7 +583,8 @@ sub Shelly_Set ($@) {
my ($subs,$ison,$overpower,$power,$rstate,$rpower,$rstopreason,$rlastdir); my ($subs,$ison,$overpower,$power,$rstate,$rpower,$rstopreason,$rlastdir);
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate($hash,"state","OK"); readingsBulkUpdateIfChanged($hash,"state","OK");
readingsBulkUpdateIfChanged($hash,"network","connected",1);
#-- we have a Shelly 1, Shelly 4, Shelly 2 or ShellyPlug switch type device #-- we have a Shelly 1, Shelly 4, Shelly 2 or ShellyPlug switch type device
if( ($model eq "shelly1") || ($model eq "shellyplug") || ($model eq "shelly4") || (($model eq "shelly2") && ($mode eq "relay")) ){ if( ($model eq "shelly1") || ($model eq "shellyplug") || ($model eq "shelly4") || (($model eq "shelly2") && ($mode eq "relay")) ){
@ -591,6 +606,9 @@ sub Shelly_Set ($@) {
#-- we have a Shelly 2 roller type device #-- we have a Shelly 2 roller type device
}elsif( ($model eq "shelly2") && ($mode eq "roller") ){ }elsif( ($model eq "shelly2") && ($mode eq "roller") ){
#-- reset blocking due to existing movement
$hash->{MOVING} = 0;
$hash->{DURATION} = 0;
for( my $i=0;$i<$rollers;$i++){ for( my $i=0;$i<$rollers;$i++){
$subs = ($rollers == 1) ? "" : "_".$i; $subs = ($rollers == 1) ? "" : "_".$i;
$rstate = $jhash->{'rollers'}[$i]{'state'}; $rstate = $jhash->{'rollers'}[$i]{'state'};
@ -671,13 +689,17 @@ sub Shelly_Set ($@) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $url; my $url;
my $state = $hash->{READINGS}{state}{VAL}; my $state = $hash->{READINGS}{state}{VAL};
my $net = $hash->{READINGS}{network}{VAL};
return
if( $net ne "connected" );
my $model = AttrVal($name,"model",""); my $model = AttrVal($name,"model","");
#-- empty cmd parameter #-- empty cmd parameter
$cmd = "" $cmd = ""
if( !defined($cmd) ); if( !defined($cmd) );
if ( $hash && !$data){ if ( $hash && !$err && !$data ){
$url = "http://".$hash->{TCPIP}."/roller/0".$cmd; $url = "http://".$hash->{TCPIP}."/roller/0".$cmd;
Log3 $name, 5,"[Shelly_updown] called with only hash => Issue a non-blocking call to $url"; Log3 $name, 5,"[Shelly_updown] called with only hash => Issue a non-blocking call to $url";
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
@ -774,10 +796,13 @@ sub Shelly_Set ($@) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $url; my $url;
my $state = $hash->{READINGS}{state}{VAL}; my $state = $hash->{READINGS}{state}{VAL};
my $net = $hash->{READINGS}{network}{VAL};
return
if( $net ne "connected" );
my $model = AttrVal($name,"model",""); my $model = AttrVal($name,"model","");
if ( $hash && !$data){ if ( $hash && !$err && !$data ){
$url = "http://".$hash->{TCPIP}."/relay/".$channel.$cmd; $url = "http://".$hash->{TCPIP}."/relay/".$channel.$cmd;
Log3 $name, 5,"[Shelly_onoff] called with only hash => Issue a non-blocking call to $url"; Log3 $name, 5,"[Shelly_onoff] called with only hash => Issue a non-blocking call to $url";
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({
@ -860,10 +885,13 @@ sub Shelly_Set ($@) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $url; my $url;
my $state = $hash->{READINGS}{state}{VAL}; my $state = $hash->{READINGS}{state}{VAL};
my $net = $hash->{READINGS}{network}{VAL};
return
if( $net ne "connected" );
my $model = AttrVal($name,"model",""); my $model = AttrVal($name,"model","");
if ( $hash && !$data){ if ( $hash && !$err && !$data ){
$url = "http://".$hash->{TCPIP}."/meter/".$channel; $url = "http://".$hash->{TCPIP}."/meter/".$channel;
Log3 $name, 5,"[Shelly_meter] called with only hash => Issue a non-blocking call to $url"; Log3 $name, 5,"[Shelly_meter] called with only hash => Issue a non-blocking call to $url";
HttpUtils_NonblockingGet({ HttpUtils_NonblockingGet({