add Readings support, add set commands
This commit is contained in:
parent
313ad5fcb4
commit
0082d45247
@ -64,7 +64,7 @@ eval "use JSON;1" or $missingModul .= "JSON ";
|
||||
###todo Hier fehlt noch Modulabfrage für ssl
|
||||
|
||||
|
||||
my $version = "0.0.10";
|
||||
my $version = "0.0.22";
|
||||
|
||||
|
||||
|
||||
@ -108,6 +108,7 @@ sub GardenaSmartBridge_Initialize($) {
|
||||
$hash->{AttrFn} = "GardenaSmartBridge_Attr";
|
||||
$hash->{AttrList} = "debugJSON:0,1 ".
|
||||
"disable:1 ".
|
||||
"interval ".
|
||||
$readingFnAttributes;
|
||||
|
||||
foreach my $d(sort keys %{$modules{GardenaSmartBridge}{defptr}}) {
|
||||
@ -240,12 +241,14 @@ sub GardenaSmartBridge_Set($@) {
|
||||
|
||||
GardenaSmartBridge_getDevices($hash);
|
||||
|
||||
} elsif( lc $cmd eq 'dummy2' ) {
|
||||
} elsif( lc $cmd eq 'gettoken' ) {
|
||||
|
||||
|
||||
return "token is up to date" if( defined($hash->{helper}{session_id}) );
|
||||
GardenaSmartBridge_getToken($hash);
|
||||
|
||||
} else {
|
||||
my $list = "getDevicesState:noArg";
|
||||
|
||||
my $list = "getDevicesState:noArg getToken:noArg";
|
||||
return "Unknown argument $cmd, choose one of $list";
|
||||
}
|
||||
|
||||
@ -281,22 +284,25 @@ sub GardenaSmartBridge_Write($@) {
|
||||
my $header = "Content-Type: application/json";
|
||||
my $uri = '';
|
||||
my $method = 'POST';
|
||||
$header .= "\r\nX-Session: $session_id" if( defined($hash->{helper}{session_id}) );
|
||||
$payload = '{' . $payload . '}' if( defined($payload) );
|
||||
$payload = '{}' if( not defined($payload) );
|
||||
$header .= "\r\nX-Session: $session_id" if( defined($hash->{helper}{session_id}) );
|
||||
$payload = '{' . $payload . '}' if( defined($payload) );
|
||||
$payload = '{}' if( not defined($payload) );
|
||||
|
||||
|
||||
if( $payload eq '{}' ) {
|
||||
$method = 'GET';
|
||||
$uri .= '/locations/?user_id=' . $hash->{helper}{user_id} if( not defined($hash->{helper}{locations_id}) );
|
||||
readingsSingleUpdate($hash,'state','fetch locationId',1) if( not defined($hash->{helper}{locations_id}) );
|
||||
$uri .= '/sessions' if( not defined($hash->{helper}{session_id}));
|
||||
$uri .= '/devices' if( not defined($model) and defined($hash->{helper}{locations_id}) );
|
||||
$uri .= '/locations/?user_id=' . $hash->{helper}{user_id} if( not defined($hash->{helper}{locations_id}) );
|
||||
readingsSingleUpdate($hash,'state','fetch locationId',1) if( not defined($hash->{helper}{locations_id}) );
|
||||
$uri .= '/sessions' if( not defined($hash->{helper}{session_id}));
|
||||
$uri .= '/devices' if( not defined($model) and defined($hash->{helper}{locations_id}) );
|
||||
}
|
||||
|
||||
$uri .= '/sessions' if( not defined($hash->{helper}{session_id}));
|
||||
$uri .= '/devices/' . $deviceId . '/abilities/' . $model . '/command' if( defined($model) and defined($payload) );
|
||||
$uri .= '?locationId=' . $hash->{helper}{locations_id} if( defined($hash->{helper}{locations_id}) );
|
||||
$uri .= '/sessions' if( not defined($hash->{helper}{session_id}));
|
||||
|
||||
if( defined($hash->{helper}{locations_id}) ) {
|
||||
$uri .= '/devices/' . $deviceId . '/abilities/' . $model . '/command' if( defined($model) and defined($payload) );
|
||||
$uri .= '?locationId=' . $hash->{helper}{locations_id};
|
||||
}
|
||||
|
||||
|
||||
HttpUtils_NonblockingGet(
|
||||
@ -472,6 +478,9 @@ sub GardenaSmartBridge_getToken($) {
|
||||
GardenaSmartBridge_Write($hash,'"sessions": {"email": "'.GardenaSmartBridge_decrypt($hash->{helper}{username}).'","password": "'.GardenaSmartBridge_decrypt($hash->{helper}{password}).'"}',undef,undef);
|
||||
|
||||
Log3 $name, 3, "GardenaSmartBridge ($name) - send credentials to fetch Token and locationId";
|
||||
|
||||
RemoveInternalTimer($hash);
|
||||
InternalTimer( gettimeofday()+$hash->{INTERVAL},"GardenaSmartBridge_InternalTimerGetDeviceData", $hash, 1 );
|
||||
}
|
||||
|
||||
sub GardenaSmartBridge_encrypt($) {
|
||||
|
@ -62,7 +62,7 @@ eval "use Encode qw(encode encode_utf8 decode_utf8);1" or $missingModul .= "Enco
|
||||
eval "use JSON;1" or $missingModul .= "JSON ";
|
||||
|
||||
|
||||
my $version = "0.0.10";
|
||||
my $version = "0.0.22";
|
||||
|
||||
|
||||
|
||||
@ -148,18 +148,11 @@ sub GardenaSmartDevice_Define($$) {
|
||||
|
||||
return "GardenaSmartDevice device $name on GardenaSmartBridge $iodev already defined."
|
||||
if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name );
|
||||
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - defined with DEVICEID: $deviceId";
|
||||
|
||||
|
||||
|
||||
|
||||
$attr{$name}{room} = "GardenaSmart" if( not defined( $attr{$name}{room} ) );
|
||||
$attr{$name}{model} = $category if( not defined( $attr{$name}{model} ) );
|
||||
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - defined GardenaSmartDevice";
|
||||
|
||||
#GardenaSmartDevice_Open( $hash );
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - defined GardenaSmartDevice with DEVICEID: $deviceId";
|
||||
|
||||
$modules{GardenaSmartDevice}{defptr}{$deviceId} = $hash;
|
||||
|
||||
@ -182,10 +175,8 @@ sub GardenaSmartDevice_Attr(@) {
|
||||
|
||||
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
||||
my $hash = $defs{$name};
|
||||
|
||||
my $orig = $attrVal;
|
||||
|
||||
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
@ -214,12 +205,14 @@ sub GardenaSmartDevice_Set($@) {
|
||||
my $duration = join( " ", @args );
|
||||
$payload = '"name":"start_override_timer","parameters":{"duration":' . $duration . '}';
|
||||
|
||||
} elsif( lc $cmd eq 'statusrequest' ) {
|
||||
} elsif( lc $cmd eq 'manualoverride' ) {
|
||||
|
||||
#$payload = '"name":"device_info"';
|
||||
$payload = '"name":"measure_ambient_temperature"';
|
||||
my $duration = join( " ", @args );
|
||||
$payload = '"name":"manual_override","parameters":{"duration":' . $duration . '}';
|
||||
|
||||
} elsif( lc $cmd eq '' ) {
|
||||
} elsif( lc $cmd eq 'canceloverride' ) {
|
||||
|
||||
$payload = '"name":"cancel_override"';
|
||||
|
||||
} elsif( lc $cmd eq '' ) {
|
||||
|
||||
@ -240,14 +233,16 @@ sub GardenaSmartDevice_Set($@) {
|
||||
|
||||
} else {
|
||||
|
||||
my $list = 'statusRequest:noArg ';
|
||||
$list .= 'parkUntilFurtherNotice:noArg parkUntilNextTimer:noArg startResumeSchedule:noArg startOverrideTimer:slider,0,1,1440' if( AttrVal($name,'model','unknown') eq 'mower' );
|
||||
my $list = '';
|
||||
$list .= 'parkUntilFurtherNotice:noArg parkUntilNextTimer:noArg startResumeSchedule:noArg startOverrideTimer:slider,0,60,1440' if( AttrVal($name,'model','unknown') eq 'mower' );
|
||||
$list .= 'manualOverride:slider,0,10,240 cancelOverride:noArg' if( AttrVal($name,'model','unknown') eq 'watering_computer' );
|
||||
$list .= 'refresh:Temperature,Light,Humidity' if( AttrVal($name,'model','unknown') eq 'sensor' );
|
||||
|
||||
return "Unknown argument $cmd, choose one of $list";
|
||||
}
|
||||
|
||||
IOWrite($hash,$payload,$hash->{DEVICEID},AttrVal($name,'model','unknown'));
|
||||
Log3 $name, 3, "GardenaSmartBridge ($name) - IOWrite: $payload $hash->{DEVICEID} " . AttrVal($name,'model','unknown') . " IODevHash=$hash->{IODev}";
|
||||
Log3 $name, 4, "GardenaSmartBridge ($name) - IOWrite: $payload $hash->{DEVICEID} " . AttrVal($name,'model','unknown') . " IODevHash=$hash->{IODev}";
|
||||
|
||||
return undef;
|
||||
}
|
||||
@ -266,7 +261,7 @@ sub GardenaSmartDevice_Parse($$) {
|
||||
}
|
||||
|
||||
Log3 $name, 4, "GardenaSmartDevice ($name) - ParseFn was called";
|
||||
Log3 $name, 4, "GardenaSmartDevice ($name) - JSON: $json";
|
||||
Log3 $name, 5, "GardenaSmartDevice ($name) - JSON: $json";
|
||||
|
||||
|
||||
if( defined($decode_json->{id}) ) {
|
||||
@ -277,13 +272,13 @@ sub GardenaSmartDevice_Parse($$) {
|
||||
my $name = $hash->{NAME};
|
||||
|
||||
GardenaSmartDevice_WriteReadings($hash,$decode_json);
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - find logical device: $hash->{NAME}";
|
||||
Log3 $name, 4, "GardenaSmartDevice ($name) - find logical device: $hash->{NAME}";
|
||||
|
||||
return $hash->{NAME};
|
||||
|
||||
} else {
|
||||
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - Wird angelegt NAME: $decode_json->{name} ID: $decode_json->{id} CATEGORY: $decode_json->{category} IODev=$name";
|
||||
Log3 $name, 3, "GardenaSmartDevice ($name) - autocreate new device $decode_json->{name} with deviceId $decode_json->{id}, model $decode_json->{category} and IODev IODev=$name";
|
||||
return "UNDEFINED $decode_json->{name} GardenaSmartDevice $decode_json->{id} $decode_json->{category} IODev=$name";
|
||||
}
|
||||
}
|
||||
@ -297,20 +292,21 @@ sub GardenaSmartDevice_WriteReadings($$) {
|
||||
my $abilities = scalar (@{$decode_json->{abilities}});
|
||||
|
||||
|
||||
readingsBeginUpdate($hash);
|
||||
|
||||
do {
|
||||
|
||||
if( ref($decode_json->{abilities}[$abilities]{properties}) eq "ARRAY" and scalar(@{$decode_json->{abilities}[$abilities]{properties}}) > 0 ) {;
|
||||
|
||||
foreach my $propertie (@{$decode_json->{abilities}[$abilities]{properties}}) {
|
||||
readingsBeginUpdate($hash);
|
||||
readingsBulkUpdateIfChanged($hash,$decode_json->{abilities}[$abilities]{name}.'-'.$propertie->{name},$propertie->{value});
|
||||
readingsEndUpdate( $hash, 1 );
|
||||
readingsBulkUpdateIfChanged($hash,$decode_json->{abilities}[$abilities]{name}.'-'.$propertie->{name},$propertie->{value}) if( defined($propertie->{value}) );
|
||||
}
|
||||
}
|
||||
|
||||
$abilities--;
|
||||
} while ($abilities >= 0);
|
||||
|
||||
readingsEndUpdate( $hash, 1 );
|
||||
|
||||
Log3 $name, 4, "GardenaSmartDevice ($name) - readings was written}";
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user