From 78cdb9c7a8f181c57c67b48d032df99366878a78 Mon Sep 17 00:00:00 2001 From: LeonGaultier Date: Fri, 5 Jun 2020 19:36:06 +0000 Subject: [PATCH] 46_TeslaPowerwall2AC: New API Implementation git-svn-id: https://svn.fhem.de/fhem/trunk@22121 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/46_TeslaPowerwall2AC.pm | 990 ++++++++++++++++++++---------- 2 files changed, 672 insertions(+), 319 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 205bc0f95..23e9ae42b 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. + - changed: 46_TeslaPowerwall2AC: New API Implementation - changed: 76_SMAPortal: module refactored, attributes cookielifetime, getDataRetries, timeout, maxCallCycle are deleted ! - bugfix: 73_AutoShuttersControl: fix shading drive out of closed Pos diff --git a/fhem/FHEM/46_TeslaPowerwall2AC.pm b/fhem/FHEM/46_TeslaPowerwall2AC.pm index d256fe52c..4c9b98517 100644 --- a/fhem/FHEM/46_TeslaPowerwall2AC.pm +++ b/fhem/FHEM/46_TeslaPowerwall2AC.pm @@ -1,8 +1,8 @@ ############################################################################### -# +# # Developed with Kate # -# (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com) +# (c) 2017-2020 Copyright: Marko Oldenburg (leongaultier at gmail dot com) # All rights reserved # # This script is free software; you can redistribute it and/or modify @@ -31,7 +31,7 @@ # # if($@){ # Log3($SELF, 2, "$TYPE ($SELF) - error while request: $@"); -# +# # readingsSingleUpdate($hash, "state", "error", 1); # # return; @@ -40,484 +40,788 @@ ####### ####### # URLs zum Abrufen diverser Daten -# http:///api/system_status/soe -# http:///api/meters/aggregates -# http:///api/site_info -# http:///api/sitemaster -# http:///api/powerwalls -# http:///api/networks -# http:///api/system/networks -# http:///api/operation +# https:///api/system_status/soe +# https:///api/meters/aggregates +# https:///api/site_info +# https:///api/sitemaster +# https:///api/powerwalls +# https:///api/networks +# https:///api/system/networks +# https:///api/operation # ## ## - - -package main; - - -my $missingModul = ""; +package FHEM::TeslaPowerwall2AC; use strict; use warnings; - +use FHEM::Meta; +use GPUtils qw(GP_Import GP_Export); use HttpUtils; -eval "use JSON;1" or $missingModul .= "JSON "; +use Data::Dumper; +# try to use JSON::MaybeXS wrapper +# for chance of better performance + open code +eval { + require JSON::MaybeXS; + import JSON::MaybeXS qw( decode_json encode_json ); + 1; +}; -my $version = "0.4.0"; +if ($@) { + $@ = undef; + # try to use JSON wrapper + # for chance of better performance + eval { + # JSON preference order + local $ENV{PERL_JSON_BACKEND} = + 'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP' + unless ( defined( $ENV{PERL_JSON_BACKEND} ) ); + require JSON; + import JSON qw( decode_json encode_json ); + 1; + }; -# Declare functions -sub TeslaPowerwall2AC_Attr(@); -sub TeslaPowerwall2AC_Define($$); -sub TeslaPowerwall2AC_Initialize($); -sub TeslaPowerwall2AC_Get($@); -sub TeslaPowerwall2AC_Notify($$); -sub TeslaPowerwall2AC_GetData($); -sub TeslaPowerwall2AC_Undef($$); -sub TeslaPowerwall2AC_ResponseProcessing($$$); -sub TeslaPowerwall2AC_ReadingsProcessing_Aggregates($$); -sub TeslaPowerwall2AC_ReadingsProcessing_Powerwalls($$); -sub TeslaPowerwall2AC_ErrorHandling($$$); -sub TeslaPowerwall2AC_WriteReadings($$$); -sub TeslaPowerwall2AC_Timer_GetData($); + if ($@) { + $@ = undef; + # In rare cases, Cpanel::JSON::XS may + # be installed but JSON|JSON::MaybeXS not ... + eval { + require Cpanel::JSON::XS; + import Cpanel::JSON::XS qw(decode_json encode_json); + 1; + }; + if ($@) { + $@ = undef; + # In rare cases, JSON::XS may + # be installed but JSON not ... + eval { + require JSON::XS; + import JSON::XS qw(decode_json encode_json); + 1; + }; -my %paths = ( 'statussoe' => 'system_status/soe', - 'aggregates' => 'meters/aggregates', - 'siteinfo' => 'site_info', - 'sitemaster' => 'sitemaster', - 'powerwalls' => 'powerwalls', - 'registration' => 'customer/registration', - 'status' => 'status' + if ($@) { + $@ = undef; + + # Fallback to built-in JSON which SHOULD + # be available since 5.014 ... + eval { + require JSON::PP; + import JSON::PP qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # Fallback to JSON::backportPP in really rare cases + require JSON::backportPP; + import JSON::backportPP qw(decode_json encode_json); + 1; + } + } + } + } +} + +## Import der FHEM Funktionen +#-- Run before package compilation +BEGIN { + + # Import from main context + GP_Import( + qw( + readingsSingleUpdate + readingsBulkUpdate + readingsBulkUpdateIfChanged + readingsBeginUpdate + readingsEndUpdate + CommandAttr + defs + Log3 + readingFnAttributes + HttpUtils_NonblockingGet + AttrVal + ReadingsVal + IsDisabled + deviceEvents + init_done + gettimeofday + InternalTimer + RemoveInternalTimer) + ); +} + +#-- Export to main context with different name +GP_Export( + qw( + Initialize + Timer_GetData + Write + ) ); +my %paths = ( + 'statussoe' => 'system_status/soe', + 'aggregates' => 'meters/aggregates', + 'meterssite' => 'meters/site', + 'meterssolar' => 'meters/solar', + 'siteinfo' => 'site_info', + 'sitename' => 'site_info/site_name', + 'sitemaster' => 'sitemaster', + 'powerwalls' => 'powerwalls', + 'registration' => 'customer/registration', + 'status' => 'status', + 'login' => 'login/Basic', + 'gridstatus' => 'system_status/grid_status', +); -sub TeslaPowerwall2AC_Initialize($) { +my %cmdPaths = ( + 'powerwallsstop' => 'sitemaster/stop', + 'powerwallsrun' => 'sitemaster/run', +); + +sub Initialize($) { my ($hash) = @_; - + # Consumer - $hash->{GetFn} = "TeslaPowerwall2AC_Get"; - $hash->{DefFn} = "TeslaPowerwall2AC_Define"; - $hash->{UndefFn} = "TeslaPowerwall2AC_Undef"; - $hash->{NotifyFn} = "TeslaPowerwall2AC_Notify"; - - $hash->{AttrFn} = "TeslaPowerwall2AC_Attr"; - $hash->{AttrList} = "interval ". - "disable:1 ". - $readingFnAttributes; - - foreach my $d(sort keys %{$modules{TeslaPowerwall2AC}{defptr}}) { - - my $hash = $modules{TeslaPowerwall2AC}{defptr}{$d}; - $hash->{VERSION} = $version; - } + $hash->{GetFn} = 'FHEM::TeslaPowerwall2AC::Get'; + $hash->{SetFn} = 'FHEM::TeslaPowerwall2AC::Set'; + $hash->{DefFn} = 'FHEM::TeslaPowerwall2AC::Define'; + $hash->{UndefFn} = 'FHEM::TeslaPowerwall2AC::Undef'; + $hash->{NotifyFn} = 'FHEM::TeslaPowerwall2AC::Notify'; + + $hash->{AttrFn} = 'FHEM::TeslaPowerwall2AC::Attr'; + $hash->{AttrList} = + 'interval ' . 'disable:1 ' . 'devel:1 ' . $readingFnAttributes; + + return FHEM::Meta::InitMod( __FILE__, $hash ); } -sub TeslaPowerwall2AC_Define($$) { - +sub Define($$) { my ( $hash, $def ) = @_; - - my @a = split( "[ \t][ \t]*", $def ); + my @a = split( '[ \t][ \t]*', $def ); - - return "too few parameters: define TeslaPowerwall2AC " if( @a != 3); - return "Cannot define a TeslaPowerwall2AC device. Perl modul $missingModul is missing." if ( $missingModul ); - - my $name = $a[0]; - - my $host = $a[2]; - $hash->{HOST} = $host; - $hash->{INTERVAL} = 300; - $hash->{PORT} = 80; - $hash->{VERSION} = $version; - $hash->{NOTIFYDEV} = "global"; - $hash->{actionQueue} = []; + return $@ unless ( FHEM::Meta::SetInternals($hash) ); + use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' ); + return 'too few parameters: define TeslaPowerwall2AC ' + if ( @a != 3 ); - $attr{$name}{room} = "Tesla" if( !defined( $attr{$name}{room} ) ); - - Log3 $name, 3, "TeslaPowerwall2AC ($name) - defined TeslaPowerwall2AC Device with Host $host, Port $hash->{PORT} and Interval $hash->{INTERVAL}"; - - $modules{TeslaPowerwall2AC}{defptr}{HOST} = $hash; + my $name = $a[0]; + + my $host = $a[2]; + $hash->{HOST} = $host; + $hash->{INTERVAL} = 300; + $hash->{VERSION} = version->parse($VERSION)->normal; + $hash->{NOTIFYDEV} = "global,$name"; + $hash->{actionQueue} = []; + + CommandAttr( undef, $name . ' room Tesla' ) + if ( AttrVal( $name, 'room', 'none' ) eq 'none' ); + Log3 $name, 3, +"TeslaPowerwall2AC ($name) - defined TeslaPowerwall2AC Device with Host $host and Interval $hash->{INTERVAL}"; return undef; } -sub TeslaPowerwall2AC_Undef($$) { - - my ( $hash, $arg ) = @_; - - my $name = $hash->{NAME}; - +sub Undef($$) { + my ( $hash, $arg ) = @_; + my $name = $hash->{NAME}; + RemoveInternalTimer($hash); Log3 $name, 3, "TeslaPowerwall2AC ($name) - Device $name deleted"; - delete $modules{TeslaPowerwall2AC}{defptr}{HOST} if( defined($modules{TeslaPowerwall2AC}{defptr}{HOST}) and $hash->{HOST} ); return undef; } -sub TeslaPowerwall2AC_Attr(@) { - +sub Attr(@) { my ( $cmd, $name, $attrName, $attrVal ) = @_; - my $hash = $defs{$name}; + my $hash = $defs{$name}; - - if( $attrName eq "disable" ) { - if( $cmd eq "set" and $attrVal eq "1" ) { + if ( $attrName eq 'disable' ) { + if ( $cmd eq 'set' and $attrVal eq '1' ) { RemoveInternalTimer($hash); - readingsSingleUpdate ( $hash, "state", "disabled", 1 ); + readingsSingleUpdate( $hash, 'state', 'disabled', 1 ); Log3 $name, 3, "TeslaPowerwall2AC ($name) - disabled"; - - } elsif( $cmd eq "del" ) { + + } + elsif ( $cmd eq 'del' ) { Log3 $name, 3, "TeslaPowerwall2AC ($name) - enabled"; } } - - if( $attrName eq "disabledForIntervals" ) { - if( $cmd eq "set" ) { - return "check disabledForIntervals Syntax HH:MM-HH:MM or 'HH:MM-HH:MM HH:MM-HH:MM ...'" - unless($attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/); + + if ( $attrName eq 'disabledForIntervals' ) { + if ( $cmd eq 'set' ) { + return +'check disabledForIntervals Syntax HH:MM-HH:MM or \'HH:MM-HH:MM HH:MM-HH:MM ...\'' + unless ( $attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/ ); Log3 $name, 3, "TeslaPowerwall2AC ($name) - disabledForIntervals"; - readingsSingleUpdate ( $hash, "state", "disabled", 1 ); - - } elsif( $cmd eq "del" ) { + readingsSingleUpdate( $hash, 'state', 'disabled', 1 ); + + } + elsif ( $cmd eq 'del' ) { Log3 $name, 3, "TeslaPowerwall2AC ($name) - enabled"; - readingsSingleUpdate( $hash, "state", "active", 1 ); + readingsSingleUpdate( $hash, 'state', 'active', 1 ); } } - - if( $attrName eq "interval" ) { - if( $cmd eq "set" ) { - if( $attrVal < 30 ) { - Log3 $name, 3, "TeslaPowerwall2AC ($name) - interval too small, please use something >= 30 (sec), default is 300 (sec)"; - return "interval too small, please use something >= 30 (sec), default is 300 (sec)"; - - } else { + + if ( $attrName eq 'interval' ) { + if ( $cmd eq 'set' ) { + if ( $attrVal < 60 ) { + Log3 $name, 3, +"TeslaPowerwall2AC ($name) - interval too small, please use something >= 60 (sec), default is 300 (sec)"; + return +'interval too small, please use something >= 60 (sec), default is 300 (sec)'; + + } + else { RemoveInternalTimer($hash); $hash->{INTERVAL} = $attrVal; - Log3 $name, 3, "TeslaPowerwall2AC ($name) - set interval to $attrVal"; - TeslaPowerwall2AC_Timer_GetData($hash); + Log3 $name, 3, + "TeslaPowerwall2AC ($name) - set interval to $attrVal"; + Timer_GetData($hash); } - } elsif( $cmd eq "del" ) { + } + elsif ( $cmd eq 'del' ) { RemoveInternalTimer($hash); $hash->{INTERVAL} = 300; - Log3 $name, 3, "TeslaPowerwall2AC ($name) - set interval to default"; - TeslaPowerwall2AC_Timer_GetData($hash); + Log3 $name, 3, + "TeslaPowerwall2AC ($name) - set interval to default"; + Timer_GetData($hash); } } - + return undef; } -sub TeslaPowerwall2AC_Notify($$) { - - my ($hash,$dev) = @_; +sub Notify($$) { + my ( $hash, $dev ) = @_; my $name = $hash->{NAME}; - return if (IsDisabled($name)); - + return if ( IsDisabled($name) ); + my $devname = $dev->{NAME}; my $devtype = $dev->{TYPE}; - my $events = deviceEvents($dev,1); - return if (!$events); + my $events = deviceEvents( $dev, 1 ); + return if ( !$events ); - - TeslaPowerwall2AC_Timer_GetData($hash) if( grep /^INITIALIZED$/,@{$events} - or grep /^DELETEATTR.$name.disable$/,@{$events} - or grep /^DELETEATTR.$name.interval$/,@{$events} - or (grep /^DEFINED.$name$/,@{$events} and $init_done) ); + Timer_GetData($hash) + if ( + grep /^INITIALIZED$/, + @{$events} or grep /^DELETEATTR.$name.disable$/, + @{$events} or grep /^DELETEATTR.$name.interval$/, + @{$events} or ( grep /^DEFINED.$name$/, @{$events} and $init_done ) + ); return; } -sub TeslaPowerwall2AC_Get($@) { - - my ($hash, $name, $cmd) = @_; +sub Get($@) { + my ( $hash, $name, $cmd ) = @_; my $arg; + if ( $cmd eq 'statusSOE' ) { - if( $cmd eq 'statusSOE' ) { + $arg = lc($cmd); - $arg = lc($cmd); - - } elsif( $cmd eq 'aggregates' ) { - - $arg = lc($cmd); - - } elsif( $cmd eq 'siteinfo' ) { - - $arg = lc($cmd); - - } elsif( $cmd eq 'powerwalls' ) { - - $arg = lc($cmd); - - } elsif( $cmd eq 'sitemaster' ) { - - $arg = lc($cmd); - - } elsif( $cmd eq 'registration' ) { - - $arg = lc($cmd); - - } elsif( $cmd eq 'status' ) { - - $arg = lc($cmd); - - } else { - - my $list = 'statusSOE:noArg aggregates:noArg siteinfo:noArg sitemaster:noArg powerwalls:noArg registration:noArg status:noArg'; - - return "Unknown argument $cmd, choose one of $list"; } - + elsif ( $cmd eq 'aggregates' ) { + + $arg = lc($cmd); + + } + elsif ( $cmd eq 'siteinfo' ) { + + $arg = lc($cmd); + + } + elsif ( $cmd eq 'powerwalls' ) { + + $arg = lc($cmd); + + } + elsif ( $cmd eq 'sitemaster' ) { + + $arg = lc($cmd); + + } + elsif ( $cmd eq 'registration' ) { + + $arg = lc($cmd); + + } + elsif ( $cmd eq 'status' ) { + + $arg = lc($cmd); + + } + else { + + my $list = +'statusSOE:noArg aggregates:noArg siteinfo:noArg sitemaster:noArg powerwalls:noArg registration:noArg status:noArg'; + + return 'Unknown argument ' . $cmd . ', choose one of ' . $list; + } + return 'There are still path commands in the action queue' - if( defined($hash->{actionQueue}) and scalar(@{$hash->{actionQueue}}) > 0 ); - - unshift( @{$hash->{actionQueue}}, $arg ); - TeslaPowerwall2AC_GetData($hash); + if ( defined( $hash->{actionQueue} ) + and scalar( @{ $hash->{actionQueue} } ) > 0 ); + + unshift( @{ $hash->{actionQueue} }, $arg ); + Write($hash); return undef; } -sub TeslaPowerwall2AC_Timer_GetData($) { +sub Set($@) { + my ( $hash, $name, $cmd, @args ) = @_; + my $arg; - my $hash = shift; - my $name = $hash->{NAME}; - - - if( defined($hash->{actionQueue}) and scalar(@{$hash->{actionQueue}}) == 0 ) { - if( not IsDisabled($name) ) { - while( my $obj = each %paths ) { - unshift( @{$hash->{actionQueue}}, $obj ); - } - - TeslaPowerwall2AC_GetData($hash); - - } else { - readingsSingleUpdate($hash,'state','disabled',1); - } + if ( $cmd eq 'powerwalls' ) { + $arg = lc( $cmd . $args[0] ); } - - InternalTimer( gettimeofday()+$hash->{INTERVAL}, 'TeslaPowerwall2AC_Timer_GetData', $hash ); - Log3 $name, 4, "TeslaPowerwall2AC ($name) - Call InternalTimer TeslaPowerwall2AC_Timer_GetData"; + else { + + my $list = ''; + $list .= 'powerwalls:run,stop' + if ( AttrVal( $name, 'devel', 0 ) == 1 ); + + return 'Unknown argument ' . $cmd . ', choose one of ' . $list; + } + + unshift( @{ $hash->{actionQueue} }, $arg ); + Write($hash); + + return undef; } -sub TeslaPowerwall2AC_GetData($) { +sub Timer_GetData($) { + my $hash = shift; + my $name = $hash->{NAME}; - my ($hash) = @_; - - my $name = $hash->{NAME}; - my $host = $hash->{HOST}; - my $port = $hash->{PORT}; - my $path = pop( @{$hash->{actionQueue}} ); - my $uri = $host . ':' . $port . '/api/' . $paths{$path}; + RemoveInternalTimer($hash); + if ( defined( $hash->{actionQueue} ) + and scalar( @{ $hash->{actionQueue} } ) == 0 ) + { + if ( not IsDisabled($name) ) { + while ( my $obj = each %paths ) { + unshift( @{ $hash->{actionQueue} }, $obj ); + } - readingsSingleUpdate($hash,'state','fetch data - ' . scalar(@{$hash->{actionQueue}}) . ' entries in the Queue',1); + Write($hash); + + } + else { + readingsSingleUpdate( $hash, 'state', 'disabled', 1 ); + } + } + + InternalTimer( gettimeofday() + $hash->{INTERVAL}, + 'TeslaPowerwall2AC_Timer_GetData', $hash ); + Log3 $name, 4, + "TeslaPowerwall2AC ($name) - Call InternalTimer Timer_GetData"; +} + +sub Write($) { + my ($hash) = @_; + my $name = $hash->{NAME}; + + my ( $uri, $method, $header, $data, $path ) = + CreateUri( $hash, pop( @{ $hash->{actionQueue} } ) ); + + readingsSingleUpdate( + $hash, + 'state', + 'fetch data - ' + . scalar( @{ $hash->{actionQueue} } ) + . ' entries in the Queue', + 1 + ); HttpUtils_NonblockingGet( { - url => "http://" . $uri, - timeout => 5, - method => 'GET', - hash => $hash, - setCmd => $path, - doTrigger => 1, - callback => \&TeslaPowerwall2AC_ErrorHandling, + url => 'https://' . $uri, + timeout => 5, + method => $method, + data => $data, + header => $header, + hash => $hash, + setCmd => $path, + doTrigger => 1, + callback => \&ErrorHandling, } ); - - Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: http://$uri"; + + Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: https://$uri"; } -sub TeslaPowerwall2AC_ErrorHandling($$$) { - - my ($param,$err,$data) = @_; - - my $hash = $param->{hash}; - my $name = $hash->{NAME}; - +sub ErrorHandling($$$) { + my ( $param, $err, $data ) = @_; + my $hash = $param->{hash}; + my $name = $hash->{NAME}; ### Begin Error Handling - - if( defined( $err ) ) { - if( $err ne "" ) { - - readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, 'state', $err, 1); + + if ( defined($err) ) { + if ( $err ne '' ) { + + readingsBeginUpdate($hash); + readingsBulkUpdate( $hash, 'state', $err, 1 ); readingsBulkUpdate( $hash, 'lastRequestError', $err, 1 ); readingsEndUpdate( $hash, 1 ); - + Log3 $name, 3, "TeslaPowerwall2AC ($name) - RequestERROR: $err"; - + $hash->{actionQueue} = []; return; } } - if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { - - readingsBeginUpdate( $hash ); + if ( $data eq '' and exists( $param->{code} ) && $param->{code} != 200 ) { + + readingsBeginUpdate($hash); readingsBulkUpdate( $hash, 'state', $param->{code}, 1 ); readingsBulkUpdate( $hash, 'lastRequestError', $param->{code}, 1 ); - Log3 $name, 3, "TeslaPowerwall2AC ($name) - RequestERROR: ".$param->{code}; + Log3 $name, 3, + "TeslaPowerwall2AC ($name) - RequestERROR: " . $param->{code}; readingsEndUpdate( $hash, 1 ); - - Log3 $name, 5, "TeslaPowerwall2AC ($name) - RequestERROR: received http code ".$param->{code}." without any data after requesting"; + + Log3 $name, 5, + "TeslaPowerwall2AC ($name) - RequestERROR: received http code " + . $param->{code} + . " without any data after requesting"; $hash->{actionQueue} = []; return; } - if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { - - readingsBeginUpdate( $hash ); - - readingsBulkUpdate( $hash, 'state', $param->{code}, 1 ); - readingsBulkUpdate( $hash, "lastRequestError", $param->{code}, 1 ); + if ( $data =~ m#{"code":(\d+),"error":"(.+)","message":"(.+)"}$# ) { + + readingsBeginUpdate($hash); + + readingsBulkUpdate( $hash, 'state', $1, 1 ); + readingsBulkUpdate( + $hash, + 'lastRequestError', + 'Path: ' + . $param->{setCmd} . ' ' + . $1 + . ' - Error: ' + . $2 + . ' Messages: ' + . $3, + 1 + ); readingsEndUpdate( $hash, 1 ); - - Log3 $name, 3, "TeslaPowerwall2AC ($name) - statusRequestERROR: http error ".$param->{code}; - - $hash->{actionQueue} = []; - return; - ### End Error Handling } - - TeslaPowerwall2AC_GetData($hash) - if( defined($hash->{actionQueue}) and scalar(@{$hash->{actionQueue}}) > 0 ); - + #### End Error Handling + + InternalTimer( gettimeofday() + 3, 'TeslaPowerwall2AC_Write', $hash ) + if ( defined( $hash->{actionQueue} ) + and scalar( @{ $hash->{actionQueue} } ) > 0 ); + Log3 $name, 4, "TeslaPowerwall2AC ($name) - Recieve JSON data: $data"; - - TeslaPowerwall2AC_ResponseProcessing($hash,$param->{setCmd},$data); + + ResponseProcessing( $hash, $param->{setCmd}, $data ); } -sub TeslaPowerwall2AC_ResponseProcessing($$$) { - - my ($hash,$path,$json) = @_; - - my $name = $hash->{NAME}; +sub ResponseProcessing($$$) { + my ( $hash, $path, $json ) = @_; + my $name = $hash->{NAME}; my $decode_json; my $readings; - - $decode_json = eval{decode_json($json)}; - if($@){ + $decode_json = eval { decode_json($json) }; + if ($@) { Log3 $name, 4, "TeslaPowerwall2AC ($name) - error while request: $@"; readingsBeginUpdate($hash); - readingsBulkUpdate($hash, 'JSON Error', $@); - readingsBulkUpdate($hash, 'state', 'JSON error'); - readingsEndUpdate($hash,1); + readingsBulkUpdate( $hash, 'JSON Error', $@ ); + readingsBulkUpdate( $hash, 'state', 'JSON error' ); + readingsEndUpdate( $hash, 1 ); return; } - + + return + if ( ref($decode_json) eq 'HASH' + and defined( $decode_json->{error} ) + and $decode_json->{error} + and defined( $decode_json->{code} ) + and $decode_json->{code} ); + #### Verarbeitung der Readings zum passenden Path - - if( $path eq 'aggregates') { - $readings = TeslaPowerwall2AC_ReadingsProcessing_Aggregates($hash,$decode_json); - - } elsif( $path eq 'powerwalls') { - $readings = TeslaPowerwall2AC_ReadingsProcessing_Powerwalls($hash,$decode_json); - - } else { + + if ( $path eq 'aggregates' ) { + $readings = ReadingsProcessing_Aggregates( $hash, $decode_json ); + } + elsif ( $path eq 'powerwalls' ) { + $readings = ReadingsProcessing_Powerwalls( $hash, $decode_json ); + } + elsif ( $path eq 'login' ) { + return $hash->{TOKEN} = $decode_json->{token}; + } + elsif ( $path eq 'meterssite' ) { + $readings = ReadingsProcessing_Meters_Site( $hash, $decode_json ); + } + elsif ( $path eq 'meterssolar' ) { + $readings = ReadingsProcessing_Meters_Solar( $hash, $decode_json ); + } + else { $readings = $decode_json; } - - TeslaPowerwall2AC_WriteReadings($hash,$path,$readings); + + WriteReadings( $hash, $path, $readings ); } -sub TeslaPowerwall2AC_WriteReadings($$$) { +sub WriteReadings($$$) { + my ( $hash, $path, $readings ) = @_; + my $name = $hash->{NAME}; - my ($hash,$path,$readings) = @_; - - my $name = $hash->{NAME}; - - Log3 $name, 4, "TeslaPowerwall2AC ($name) - Write Readings"; - - + readingsBeginUpdate($hash); - while( my ($r,$v) = each %{$readings} ) { - readingsBulkUpdate($hash,$path.'-'.$r,$v); + while ( my ( $r, $v ) = each %{$readings} ) { + readingsBulkUpdate( $hash, $path . '-' . $r, $v ); } - - readingsBulkUpdate($hash,'batteryLevel',sprintf("%.1f",$readings->{percentage})) if( defined($readings->{percentage}) ); - readingsBulkUpdate($hash,'batteryPower',sprintf("%.1f",(ReadingsVal($name,'siteinfo-nominal_system_energy_kWh',0)/100) * ReadingsVal($name,'statussoe-percentage',0) ) ); - readingsBulkUpdateIfChanged($hash,'actionQueue',scalar(@{$hash->{actionQueue}}) . ' entries in the Queue'); - readingsBulkUpdateIfChanged($hash,'state',(defined($hash->{actionQueue}) and scalar(@{$hash->{actionQueue}}) == 0 ? 'ready' : 'fetch data - ' . scalar(@{$hash->{actionQueue}}) . ' paths in actionQueue')); - readingsEndUpdate($hash,1); + + readingsBulkUpdate( $hash, 'batteryLevel', + sprintf( "%.1f", $readings->{percentage} ) ) + if ( defined( $readings->{percentage} ) ); + readingsBulkUpdate( + $hash, + 'batteryPower', + sprintf( + "%.1f", + ( + ReadingsVal( $name, 'siteinfo-nominal_system_energy_kWh', 0 ) / + 100 + ) * ReadingsVal( $name, 'statussoe-percentage', 0 ) + ) + ); + readingsBulkUpdateIfChanged( $hash, 'actionQueue', + scalar( @{ $hash->{actionQueue} } ) . ' entries in the Queue' ); + readingsBulkUpdateIfChanged( + $hash, 'state', + ( + defined( $hash->{actionQueue} ) + and scalar( @{ $hash->{actionQueue} } ) == 0 + ? 'ready' + : 'fetch data - ' + . scalar( @{ $hash->{actionQueue} } ) + . ' paths in actionQueue' + ) + ); + readingsEndUpdate( $hash, 1 ); } -sub TeslaPowerwall2AC_ReadingsProcessing_Aggregates($$) { - - my ($hash,$decode_json) = @_; - - my $name = $hash->{NAME}; +sub ReadingsProcessing_Aggregates($$) { + my ( $hash, $decode_json ) = @_; + my $name = $hash->{NAME}; my %readings; - - - if( ref($decode_json) eq "HASH" ) { - while( my $obj = each %{$decode_json} ) { - while( my ($r,$v) = each %{$decode_json->{$obj}} ) { - $readings{$obj.'-'.$r} = $v; + + if ( ref($decode_json) eq 'HASH' ) { + while ( my $obj = each %{$decode_json} ) { + while ( my ( $r, $v ) = each %{ $decode_json->{$obj} } ) { + $readings{ $obj . '-' . $r } = $v; } } - - } else { + } + else { $readings{'error'} = 'aggregates response is not a Hash'; } - + return \%readings; } -sub TeslaPowerwall2AC_ReadingsProcessing_Powerwalls($$) { - - my ($hash,$decode_json) = @_; - - my $name = $hash->{NAME}; +sub ReadingsProcessing_Powerwalls($$) { + my ( $hash, $decode_json ) = @_; + my $name = $hash->{NAME}; my %readings; - - - if( ref($decode_json->{powerwalls}) eq "ARRAY" and scalar(@{$decode_json->{powerwalls}}) > 0 ) { + + if ( ref( $decode_json->{powerwalls} ) eq 'ARRAY' + and scalar( @{ $decode_json->{powerwalls} } ) > 0 ) + { my $i = 0; - - foreach my $powerwall (@{$decode_json->{powerwalls}}) { - if( ref($powerwall) eq "HASH" ) { - - while( my ($r,$v) = each %{$powerwall} ) { - $readings{'wall_'.$i.'_'.$r} = $v; + foreach my $powerwall ( @{ $decode_json->{powerwalls} } ) { + if ( ref($powerwall) eq 'HASH' ) { + + while ( my ( $r, $v ) = each %{$powerwall} ) { + $readings{ 'wall_' . $i . '_' . $r } = $v; } - + $i++; } } - - $readings{'numberOfWalls'} = $i; - } else { + $readings{'numberOfWalls'} = $i; + } + else { $readings{'error'} = 'aggregates response is not a Array'; } - + return \%readings; } +sub ReadingsProcessing_Site_Info($$) { + my ( $hash, $decode_json ) = @_; + my $name = $hash->{NAME}; + my %readings; + if ( ref($decode_json) eq 'HASH' ) { + while ( my $obj = each %{$decode_json} ) { + while ( my ( $r, $v ) = each %{ $decode_json->{$obj} } ) { + $readings{ $obj . '-' . $r } = $v; + } + } + } + else { + $readings{'error'} = 'siteinfo response is not a Hash'; + } + return \%readings; +} + +sub ReadingsProcessing_Meters_Site($$) { + my ( $hash, $decode_json ) = @_; + my $name = $hash->{NAME}; + my %readings; + + if ( ref($decode_json) eq 'ARRAY' + and scalar( @{$decode_json} ) > 0 ) + { + if ( ref( $decode_json->[0] ) eq 'HASH' ) { + while ( my $obj = each %{ $decode_json->[0] } ) { + if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' + or ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) + { + if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) { + while ( my ( $r, $v ) = + each %{ $decode_json->[0]->{$obj} } ) + { + if ( ref($v) ne 'HASH' ) { + $readings{ $obj . '-' . $r } = $v; + } + else { + while ( my ( $r2, $v2 ) = + each %{ $decode_json->[0]->{$obj}->{$r} } ) + { + $readings{ $obj . '-' . $r . '-' . $r2 } = + $v2; + } + } + } + } + elsif ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' ) { + + } + } + else { + $readings{$obj} = $decode_json->[0]->{$obj}; + } + } + } + } + else { + $readings{'error'} = 'metes site response is not a Array'; + } + + return \%readings; +} + +sub ReadingsProcessing_Meters_Solar($$) { + my ( $hash, $decode_json ) = @_; + my $name = $hash->{NAME}; + my %readings; + + if ( ref($decode_json) eq 'ARRAY' + and scalar( @{$decode_json} ) > 0 ) + { + if ( ref( $decode_json->[0] ) eq 'HASH' ) { + while ( my $obj = each %{ $decode_json->[0] } ) { + if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' + or ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) + { + if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) { + while ( my ( $r, $v ) = + each %{ $decode_json->[0]->{$obj} } ) + { + if ( ref($v) ne 'HASH' ) { + $readings{ $obj . '-' . $r } = $v; + } + else { + while ( my ( $r2, $v2 ) = + each %{ $decode_json->[0]->{$obj}->{$r} } ) + { + $readings{ $obj . '-' . $r . '-' . $r2 } = + $v2; + } + } + } + } + elsif ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' ) { + + } + } + else { + $readings{$obj} = $decode_json->[0]->{$obj}; + } + } + } + } + else { + $readings{'error'} = 'metes solar response is not a Array'; + } + + return \%readings; +} + +sub CreateUri($$) { + my ( $hash, $path ) = @_; + my $host = $hash->{HOST}; + my $method = 'GET'; + my $uri; + my $header; + my $data; + + if ( $path eq 'powerwallsstop' + or $path eq 'powerwallsruns' ) + { + $uri = $host . '/api/' . $cmdPaths{$path}; + } + else { + $uri = $host . '/api/' . $paths{$path}; + } + + if ( $path eq 'sitemasterrun' ) { + $header = 'Authorization: Bearer' . $hash->{TOKEN}; + + } + elsif ( $path eq 'login' ) { + $method = 'POST'; + $header = 'Content-Type: application/json'; + $data = '{"username":"","password":"S' + . ReadingsVal( $hash->{NAME}, + 'powerwalls-wall_0_PackageSerialNumber', 0 ) + . '","force_sm_off":false}'; + } + + return ( $uri, $method, $header, $data, $path ); +} 1; - =pod =item device @@ -587,4 +891,52 @@ sub TeslaPowerwall2AC_ReadingsProcessing_Powerwalls($$) {

Tesla Powerwall 2 AC

=end html_DE + +=for :application/json;q=META.json 46_TeslaPowerwall2AC.pm +{ + "abstract": "Modul to retrieves data from a Tesla Powerwall 2AC", + "x_lang": { + "de": { + "abstract": "" + } + }, + "keywords": [ + "fhem-mod-device", + "fhem-core", + "Power", + "Tesla", + "AC", + "Powerwall", + "Control" + ], + "release_status": "under develop", + "license": "GPL_2", + "version": "v0.8.0", + "author": [ + "Marko Oldenburg " + ], + "x_fhem_maintainer": [ + "CoolTux" + ], + "x_fhem_maintainer_github": [ + "LeonGaultier" + ], + "prereqs": { + "runtime": { + "requires": { + "FHEM": 5.00918799, + "perl": 5.016, + "Meta": 0, + "JSON": 0, + "Date::Parse": 0 + }, + "recommends": { + }, + "suggests": { + } + } + } +} +=end :application/json;q=META.json + =cut