fix getDeviceList bug

This commit is contained in:
Marko Oldenburg 2020-01-09 22:02:41 +01:00
parent 6f71931524
commit 2b9989d491
2 changed files with 634 additions and 538 deletions

View File

@ -34,10 +34,8 @@
##
#
################################
package main;
use strict;
@ -115,11 +113,9 @@ if ($@) {
}
}
my $version = '0.7.11';
my $version = '0.7.15';
my $bridgeapi = '1.9';
my %lockActionsSmartLock = (
'unlock' => 1,
'lock' => 2,
@ -136,7 +132,6 @@ my %lockActionsOpener = (
'deactivateContinuousMode' => 5
);
# Declare functions
sub NUKIBridge_Initialize ($);
sub NUKIBridge_Define ($$);
@ -159,8 +154,6 @@ sub NUKIBridge_getLogfile($);
sub NUKIBridge_getCallbackList($);
sub NUKIBridge_CallBlocking($@);
sub NUKIBridge_Initialize($) {
my ($hash) = @_;
@ -169,7 +162,8 @@ sub NUKIBridge_Initialize($) {
$hash->{Clients} = ':NUKIDevice:';
$hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' };
my $webhookFWinstance = join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
my $webhookFWinstance =
join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
# Consumer
$hash->{SetFn} = 'NUKIBridge_Set';
@ -177,10 +171,12 @@ sub NUKIBridge_Initialize($) {
$hash->{DefFn} = 'NUKIBridge_Define';
$hash->{UndefFn} = 'NUKIBridge_Undef';
$hash->{AttrFn} = 'NUKIBridge_Attr';
$hash->{AttrList} = 'disable:1 '.
'webhookFWinstance:' . $webhookFWinstance . ' '.
'webhookHttpHostname '.
$readingFnAttributes;
$hash->{AttrList} =
'disable:1 '
. 'webhookFWinstance:'
. $webhookFWinstance . ' '
. 'webhookHttpHostname '
. $readingFnAttributes;
foreach my $d ( sort keys %{ $modules{NUKIBridge}{defptr} } ) {
my $hash = $modules{NUKIBridge}{defptr}{$d};
@ -196,7 +192,6 @@ sub NUKIBridge_Define($$) {
return ('too few parameters: define <name> NUKIBridge <HOST> <TOKEN>')
if ( @a != 4 );
my $name = $a[0];
my $host = $a[2];
my $token = $a[3];
@ -211,12 +206,19 @@ sub NUKIBridge_Define($$) {
$hash->{helper}->{iowrite} = 0;
my $infix = 'NUKIBridge';
Log3($name, 3, "NUKIBridge ($name) - defined with host $host on port $port, Token $token");
Log3( $name, 3,
"NUKIBridge ($name) - defined with host $host on port $port, Token $token"
);
CommandAttr( undef, $name . ' room NUKI' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
if ( NUKIBridge_addExtension( $name, 'NUKIBridge_CGI', $infix . "-" . $host ) ) {
if (
NUKIBridge_addExtension(
$name, 'NUKIBridge_CGI', $infix . "-" . $host
)
)
{
$hash->{fhem}{infix} = $infix;
}
@ -227,9 +229,12 @@ sub NUKIBridge_Define($$) {
RemoveInternalTimer($hash);
if ($init_done) {
NUKIBridge_firstRun($hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) );
} else {
InternalTimer( gettimeofday()+15, 'NUKIBridge_firstRun', $hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) );
NUKIBridge_firstRun($hash)
if ( ( $hash->{HOST} ) and ( $hash->{TOKEN} ) );
}
else {
InternalTimer( gettimeofday() + 15, 'NUKIBridge_firstRun', $hash )
if ( ( $hash->{HOST} ) and ( $hash->{TOKEN} ) );
}
$modules{NUKIBridge}{defptr}{ $hash->{HOST} } = $hash;
@ -272,43 +277,76 @@ sub NUKIBridge_Attr(@) {
if ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIBridge ($name) - enable disabledForIntervals");
Log3( $name, 3,
"NUKIBridge ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIBridge ($name) - delete disabledForIntervals");
Log3( $name, 3,
"NUKIBridge ($name) - delete disabledForIntervals" );
}
}
######################
#### webhook #########
return("Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address")
if ( $attrVal and $attrName eq 'webhookHttpHostname' and $attrVal !~ /^([A-Za-z_.0-9]+\.[A-Za-z_.0-9]+)|[0-9:]+$/ );
return (
"Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address"
)
if ( $attrVal
and $attrName eq 'webhookHttpHostname'
and $attrVal !~ /^([A-Za-z_.0-9]+\.[A-Za-z_.0-9]+)|[0-9:]+$/ );
return("Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing")
if ( $attrVal and $attrName eq 'webhookFWinstance' and ( !defined( $defs{$attrVal} ) or $defs{$attrVal}{TYPE} ne 'FHEMWEB' ) );
return (
"Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing"
)
if ( $attrVal
and $attrName eq 'webhookFWinstance'
and
( !defined( $defs{$attrVal} ) or $defs{$attrVal}{TYPE} ne 'FHEMWEB' ) );
return("Invalid value for attribute $attrName: needs to be an integer value")
return (
"Invalid value for attribute $attrName: needs to be an integer value")
if ( $attrVal and $attrName eq 'webhookPort' and $attrVal !~ /^\d+$/ );
if ( $attrName =~ /^webhook.*/ ) {
my $webhookHttpHostname = ( $attrName eq 'webhookHttpHostname' ? $attrVal : AttrVal( $name, 'webhookHttpHostname', '' ) );
my $webhookFWinstance = ( $attrName eq 'webhookFWinstance' ? $attrVal : AttrVal( $name, 'webhookFWinstance', '' ) );
my $webhookHttpHostname =
( $attrName eq 'webhookHttpHostname'
? $attrVal
: AttrVal( $name, 'webhookHttpHostname', '' ) );
my $webhookFWinstance =
( $attrName eq 'webhookFWinstance'
? $attrVal
: AttrVal( $name, 'webhookFWinstance', '' ) );
$hash->{WEBHOOK_URI} = '/' . AttrVal( $webhookFWinstance, 'webname', 'fhem' ) . '/NUKIBridge' . '-' . $hash->{HOST};
$hash->{WEBHOOK_PORT} = ( $attrName eq 'webhookPort' ? $attrVal : AttrVal( $name, 'webhookPort', InternalVal( $webhookFWinstance, 'PORT', '' )) );
$hash->{WEBHOOK_URI} = '/'
. AttrVal( $webhookFWinstance, 'webname', 'fhem' )
. '/NUKIBridge' . '-'
. $hash->{HOST};
$hash->{WEBHOOK_PORT} = (
$attrName eq 'webhookPort' ? $attrVal : AttrVal(
$name, 'webhookPort',
InternalVal( $webhookFWinstance, 'PORT', '' )
)
);
$hash->{WEBHOOK_URL} = '';
$hash->{WEBHOOK_COUNTER} = 0;
if ( $webhookHttpHostname ne '' and $hash->{WEBHOOK_PORT} ne '' ) {
$hash->{WEBHOOK_URL} = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI};
my $url = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI};
$hash->{WEBHOOK_URL} =
'http://'
. $webhookHttpHostname . ':'
. $hash->{WEBHOOK_PORT}
. $hash->{WEBHOOK_URI};
my $url =
'http://'
. $webhookHttpHostname . ':'
. $hash->{WEBHOOK_PORT}
. $hash->{WEBHOOK_URI};
Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" );
NUKIBridge_Write( $hash, 'callback/add', $url, undef, undef )
@ -328,7 +366,9 @@ sub NUKIBridge_addExtension($$$) {
my $url = '/' . $link;
Log3($name, 2, "NUKIBridge ($name) - Registering NUKIBridge for webhook URI $url ...");
Log3( $name, 2,
"NUKIBridge ($name) - Registering NUKIBridge for webhook URI $url ..."
);
$data{FWEXT}{$url}{deviceName} = $name;
$data{FWEXT}{$url}{FUNC} = $func;
@ -343,7 +383,9 @@ sub NUKIBridge_removeExtension($) {
my $url = '/' . $link;
my $name = $data{FWEXT}{$url}{deviceName};
Log3($name, 2, "NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url...");
Log3( $name, 2,
"NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url..."
);
delete $data{FWEXT}{$url};
}
@ -352,39 +394,45 @@ sub NUKIBridge_Set($@) {
my ( $arg, @params ) = @args;
if ($cmd eq 'getdevicelist') {
if ( lc($cmd) eq 'getdevicelist' ) {
return 'usage: getDeviceList' if ( @args != 0 );
NUKIBridge_Write($hash,"list",undef,undef,undef) if ( !IsDisabled($name) );
NUKIBridge_Write( $hash, "list", undef, undef, undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ( $cmd eq 'info' ) {
return 'usage: statusRequest' if ( @args != 0 );
NUKIBridge_Write($hash,"info",undef,undef,undef) if ( !IsDisabled($name) );
NUKIBridge_Write( $hash, "info", undef, undef, undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ( lc($cmd) eq 'fwupdate' ) {
return 'usage: fwUpdate' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"fwupdate",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "fwupdate", undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ( $cmd eq 'reboot' ) {
return 'usage: reboot' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"reboot",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "reboot", undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ( lc($cmd) eq 'clearlog' ) {
return 'usage: clearLog' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"clearlog",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "clearlog", undef )
if ( !IsDisabled($name) );
}
elsif ( lc($cmd) eq 'factoryreset' ) {
return 'usage: clearLog' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"factoryReset",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "factoryReset", undef )
if ( !IsDisabled($name) );
}
elsif ( lc($cmd) eq 'callbackremove' ) {
return 'usage: callbackRemove' if ( @args > 1 );
@ -393,8 +441,11 @@ sub NUKIBridge_Set($@) {
my $resp = NUKIBridge_CallBlocking( $hash, 'callback/remove', $id )
if ( !IsDisabled($name) );
if ( ( $resp->{success} eq 'true'
or $resp->{success} == 1)
if (
(
$resp->{success} eq 'true'
or $resp->{success} == 1
)
and !IsDisabled($name)
)
{
@ -455,9 +506,12 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
Log3( $name, 4, "NUKIBridge ($name) - run NUKIBridge_Write" );
}
InternalTimer( gettimeofday()+15+int(rand(15)), 'NUKIBridge_GetCheckBridgeAlive', $hash);
InternalTimer( gettimeofday() + 15 + int( rand(15) ),
'NUKIBridge_GetCheckBridgeAlive', $hash );
Log3($name, 4, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive");
Log3( $name, 4,
"NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive"
);
}
sub NUKIBridge_firstRun($) {
@ -466,8 +520,10 @@ sub NUKIBridge_firstRun($) {
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
NUKIBridge_Write($hash,'list',undef,undef,undef) if ( !IsDisabled($name) );
InternalTimer( gettimeofday()+15, 'NUKIBridge_GetCheckBridgeAlive', $hash);
NUKIBridge_Write( $hash, 'list', undef, undef, undef )
if ( !IsDisabled($name) );
InternalTimer( gettimeofday() + 15,
'NUKIBridge_GetCheckBridgeAlive', $hash );
return undef;
}
@ -550,26 +606,39 @@ sub NUKIBridge_Distribution($$$) {
}
readingsBulkUpdate( $hash, 'lastError', $err )
if ( ReadingsVal($name,'state','not connected') eq 'not connected' );
if ( ReadingsVal( $name, 'state', 'not connected' ) eq
'not connected' );
Log3($name, 4, "NUKIBridge ($name) - error while requesting: $err");
Log3( $name, 4,
"NUKIBridge ($name) - error while requesting: $err" );
readingsEndUpdate( $hash, 1 );
return $err;
}
}
if ( ($json eq '' or $json =~ /Unavailable/i) and exists( $param->{code} ) and $param->{code} != 200 ) {
if ( ( $json eq '' or $json =~ /Unavailable/i )
and exists( $param->{code} )
and $param->{code} != 200 )
{
if ( $param->{code} == 503 and $json eq 'HTTP 503 Unavailable' ) {
Log3($name, 4, "NUKIBridge ($name) - Response from Bridge: $param->{code}, $json");
Log3( $name, 4,
"NUKIBridge ($name) - Response from Bridge: $param->{code}, $json"
);
readingsEndUpdate( $hash, 1 );
return;
}
readingsBulkUpdate( $hash, 'lastError', 'Internal error, ' .$param->{code} );
Log3($name, 4, "NUKIBridge ($name) - received http code " .$param->{code}." without any data after requesting");
readingsBulkUpdate( $hash, 'lastError',
'Internal error, ' . $param->{code} );
Log3( $name, 4,
"NUKIBridge ($name) - received http code "
. $param->{code}
. " without any data after requesting" );
readingsEndUpdate( $hash, 1 );
return('received http code ' .$param->{code} . ' without any data after requesting');
return ('received http code '
. $param->{code}
. ' without any data after requesting' );
}
if ( ( $json =~ /Error/i ) and exists( $param->{code} ) ) {
@ -598,7 +667,8 @@ sub NUKIBridge_Distribution($$$) {
else {
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@");
Log3( $name, 3,
"NUKIBridge ($name) - JSON error while request: $@" );
return;
}
@ -622,10 +692,12 @@ sub NUKIBridge_ResponseProcessing($$$) {
if ( !$json ) {
Log3( $name, 3, "NUKIBridge ($name) - empty answer received" );
return undef;
} elsif ( $json =~ m'HTTP/1.1 200 OK' ) {
}
elsif ( $json =~ m'HTTP/1.1 200 OK' ) {
Log3( $name, 4, "NUKIBridge ($name) - empty answer received" );
return undef;
} elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
}
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3( $name, 3, "NUKIBridge ($name) - invalid json detected: $json" );
return ("NUKIBridge ($name) - invalid json detected: $json");
}
@ -636,9 +708,15 @@ sub NUKIBridge_ResponseProcessing($$$) {
return;
}
if ( (ref($decode_json) eq 'ARRAY'
if ( $path eq 'list'
or $path eq 'info' )
{
if (
(
ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0
and $path eq 'list')
and $path eq 'list'
)
or ( ref( $decode_json->{scanResults} ) eq 'ARRAY'
and scalar( @{ $decode_json->{scanResults} } ) > 0
and $path eq 'info' )
@ -653,7 +731,6 @@ sub NUKIBridge_ResponseProcessing($$$) {
my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] );
while ($json) {
Log3( $name, 5,
"NUKIBridge ($name) - Decoding JSON message. Length: "
. length($json)
@ -693,9 +770,12 @@ sub NUKIBridge_ResponseProcessing($$$) {
NUKIBridge_InfoProcessing( $hash, $decode_json );
}
}
else {
Log3($name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt:
$json");
Log3(
$name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt:
$json"
);
return;
}
@ -715,7 +795,6 @@ sub NUKIBridge_CGI() {
return ('NUKIBridge WEBHOOK - No IODev found')
unless ( defined($hash) and defined($name) );
my $json = ( split( '&', $request, 2 ) )[1];
if ( !$json ) {
@ -727,18 +806,22 @@ sub NUKIBridge_CGI() {
return undef;
}
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - invalid json detected: $json");
Log3( $name, 3,
"NUKIBridge WEBHOOK ($name) - invalid json detected: $json" );
return ("NUKIBridge WEBHOOK ($name) - invalid json detected: $json");
}
Log3($name, 5, "NUKIBridge WEBHOOK ($name) - Webhook received with JSON: $json");
Log3( $name, 5,
"NUKIBridge WEBHOOK ($name) - Webhook received with JSON: $json" );
if ( $json =~ m/^\{.*\}$/ ) {
$hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow();
Log3($name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for
matching NukiId at device $name");
Log3(
$name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for
matching NukiId at device $name"
);
Dispatch( $hash, $json, undef );
@ -747,7 +830,8 @@ matching NukiId at device $name");
# no data received
else {
Log3($name, 4, "NUKIBridge WEBHOOK - received malformed request\n$request");
Log3( $name, 4,
"NUKIBridge WEBHOOK - received malformed request\n$request" );
}
return ( 'text/plain; charset=utf-8', 'Call failure: ' . $request );
@ -770,15 +854,20 @@ sub NUKIBridge_InfoProcessing($$) {
);
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,'appVersion',$decode_json->{versions}->{appVersion});
readingsBulkUpdate($hash,'firmwareVersion',$decode_json->{versions}->{firmwareVersion});
readingsBulkUpdate($hash,'wifiFirmwareVersion',$decode_json->{versions}->{wifiFirmwareVersion});
readingsBulkUpdate($hash,'bridgeType',$bridgeType{$decode_json->{bridgeType}});
readingsBulkUpdate( $hash, 'appVersion',
$decode_json->{versions}->{appVersion} );
readingsBulkUpdate( $hash, 'firmwareVersion',
$decode_json->{versions}->{firmwareVersion} );
readingsBulkUpdate( $hash, 'wifiFirmwareVersion',
$decode_json->{versions}->{wifiFirmwareVersion} );
readingsBulkUpdate( $hash, 'bridgeType',
$bridgeType{ $decode_json->{bridgeType} } );
readingsBulkUpdate( $hash, 'hardwareId', $decode_json->{ids}{hardwareId} );
readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} );
readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} );
readingsBulkUpdate( $hash, 'currentTime', $decode_json->{currentTime} );
readingsBulkUpdate($hash,'serverConnected',$decode_json->{serverConnected});
readingsBulkUpdate( $hash, 'serverConnected',
$decode_json->{serverConnected} );
readingsEndUpdate( $hash, 1 );
}
@ -788,7 +877,8 @@ sub NUKIBridge_getLogfile($) {
my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking( $hash, 'log', undef );
Log3($name, 4, "NUKIBridge ($name) - Log data are collected and processed");
Log3( $name, 4,
"NUKIBridge ($name) - Log data are collected and processed" );
if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) {
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
@ -839,11 +929,14 @@ sub NUKIBridge_getCallbackList($) {
return
unless ( ref($decode_json) eq 'HASH' );
Log3($name, 4, "NUKIBridge ($name) - Callback data is collected and
processed");
Log3(
$name, 4, "NUKIBridge ($name) - Callback data is collected and
processed"
);
if ( ref($decode_json->{callbacks}) eq 'ARRAY' and
scalar(@{$decode_json->{callbacks}}) > 0 ) {
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY'
and scalar( @{ $decode_json->{callbacks} } ) > 0 )
{
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
my $ret = '<html><table width=100%><tr><td>';
@ -880,7 +973,6 @@ sub NUKIBridge_CallBlocking($@) {
my $port = $hash->{PORT};
my $token = $hash->{TOKEN};
my $url = 'http://' . $hash->{HOST} . ':' . $port;
$url .= '/' . $path
if ( defined $path );
@ -889,13 +981,14 @@ sub NUKIBridge_CallBlocking($@) {
$url .= '&' . $obj
if ( defined($obj) );
my($err,$data) = HttpUtils_BlockingGet({
my ( $err, $data ) = HttpUtils_BlockingGet(
{
url => $url,
timeout => 3,
method => "GET",
header => "Content-Type: application/json",
});
}
);
if ( !$data ) {
Log3( $name, 3, "NUKIDevice ($name) - empty answer received for $url" );
@ -906,7 +999,8 @@ sub NUKIBridge_CallBlocking($@) {
return undef;
}
elsif ( $data !~ m/^[\[{].*[}\]]$/ and $path ne "log" ) {
Log3($name, 3, "NUKIDevice ($name) - invalid json detected for $url: $data");
Log3( $name, 3,
"NUKIDevice ($name) - invalid json detected for $url: $data" );
return ("NUKIDevice ($name) - invalid json detected for $url: $data");
}
@ -970,20 +1064,12 @@ sub NUKIBridge_ParseJSON($$) {
}
}
Log3($name, 5,
"NUKIBridge ($name) - return msg: $msg and tail: $tail");
Log3( $name, 5, "NUKIBridge ($name) - return msg: $msg and tail: $tail" );
return ( $msg, $tail );
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's over the Nuki Bridge.

View File

@ -25,7 +25,6 @@
#
###############################################################################
package main;
use strict;
@ -102,11 +101,7 @@ if ($@) {
}
}
my $version = '0.7.11';
my $version = '0.7.15';
# Declare functions
sub NUKIDevice_Initialize($);
@ -118,7 +113,6 @@ sub NUKIDevice_GetUpdate($);
sub NUKIDevice_Parse($$);
sub NUKIDevice_WriteReadings($$);
my %deviceTypes = (
0 => 'smartlock',
2 => 'opener'
@ -126,10 +120,6 @@ my %deviceTypes = (
my %deviceTypeIds = reverse(%deviceTypes);
sub NUKIDevice_Initialize($) {
my ($hash) = @_;
@ -141,11 +131,11 @@ sub NUKIDevice_Initialize($) {
$hash->{AttrFn} = 'NUKIDevice_Attr';
$hash->{ParseFn} = 'NUKIDevice_Parse';
$hash->{AttrList} = 'IODev '.
'model:opener,smartlock '.
'disable:1 '.
$readingFnAttributes;
$hash->{AttrList} =
'IODev '
. 'model:opener,smartlock '
. 'disable:1 '
. $readingFnAttributes;
foreach my $d ( sort keys %{ $modules{NUKIDevice}{defptr} } ) {
my $hash = $modules{NUKIDevice}{defptr}{$d};
@ -157,8 +147,8 @@ sub NUKIDevice_Define($$) {
my ( $hash, $def ) = @_;
my @a = split( '[ \t][ \t]*', $def );
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>' if ( @a != 4 );
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>'
if ( @a != 4 );
my $name = $a[0];
my $nukiId = $a[2];
@ -169,14 +159,13 @@ sub NUKIDevice_Define($$) {
$hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized';
my $iodev = AttrVal( $name, 'IODev', 'none' );
AssignIoPort( $hash, $iodev ) if ( !$hash->{IODev} );
if ( defined( $hash->{IODev}->{NAME} ) ) {
Log3($name, 3, "NUKIDevice ($name) - I/O device is "
. $hash->{IODev}->{NAME});
Log3( $name, 3,
"NUKIDevice ($name) - I/O device is " . $hash->{IODev}->{NAME} );
}
else {
Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
@ -187,12 +176,15 @@ sub NUKIDevice_Define($$) {
my $d = $modules{NUKIDevice}{defptr}{$nukiId};
return
'NUKIDevice device ' . $name . ' on NUKIBridge ' . $iodev . ' already defined.'
'NUKIDevice device '
. $name
. ' on NUKIBridge '
. $iodev
. ' already defined.'
if ( defined($d)
and $d->{IODev} == $hash->{IODev}
and $d->{NAME} ne $name );
Log3( $name, 3, "NUKIDevice ($name) - defined with NukiId: $nukiId" );
CommandAttr( undef, $name . ' room NUKI' )
@ -201,9 +193,12 @@ sub NUKIDevice_Define($$) {
if ( AttrVal( $name, 'model', 'none' ) eq 'none' );
if ($init_done) {
InternalTimer( gettimeofday()+int(rand(10)), "NUKIDevice_GetUpdate", $hash);
} else {
InternalTimer( gettimeofday()+15+int(rand(5)), "NUKIDevice_GetUpdate", $hash);
InternalTimer( gettimeofday() + int( rand(10) ),
"NUKIDevice_GetUpdate", $hash );
}
else {
InternalTimer( gettimeofday() + 15 + int( rand(5) ),
"NUKIDevice_GetUpdate", $hash );
}
$modules{NUKIDevice}{defptr}{$nukiId} = $hash;
@ -244,13 +239,15 @@ sub NUKIDevice_Attr(@) {
}
elsif ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIDevice ($name) - enable disabledForIntervals");
Log3( $name, 3,
"NUKIDevice ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIDevice ($name) - delete disabledForIntervals");
Log3( $name, 3,
"NUKIDevice ($name) - delete disabledForIntervals" );
}
}
elsif ( $attrName eq 'model' ) {
@ -275,7 +272,8 @@ sub NUKIDevice_Set($$@) {
NUKIDevice_GetUpdate($hash);
return undef;
} elsif ( $cmd eq 'unpair' ) {
}
elsif ( $cmd eq 'unpair' ) {
return ('usage: unpair') if ( @args != 0 );
if ( !IsDisabled($name) ) {
@ -285,7 +283,8 @@ sub NUKIDevice_Set($$@) {
}
return undef;
} elsif ( $cmd eq 'lock'
}
elsif ($cmd eq 'lock'
or lc($cmd) eq 'deactivaterto'
or $cmd eq 'unlock'
or lc($cmd) eq 'activaterto'
@ -294,18 +293,20 @@ sub NUKIDevice_Set($$@) {
or lc($cmd) eq 'lockngo'
or lc($cmd) eq 'activatecontinuousmode'
or lc($cmd) eq 'lockngowithunlatch'
or lc($cmd) eq 'deactivatecontinuousmode'
)
or lc($cmd) eq 'deactivatecontinuousmode' )
{
return ( 'usage: ' . $cmd )
if ( @args != 0 );
$lockAction = $cmd;
} else {
}
else {
my $list = '';
$list= 'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
$list =
'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
if ( $hash->{DEVICETYPE} == 0 );
$list= 'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg'
$list =
'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg'
if ( $hash->{DEVICETYPE} == 2 );
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
@ -314,7 +315,8 @@ sub NUKIDevice_Set($$@) {
$hash->{helper}{lockAction} = $lockAction;
$hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite($hash,"lockAction",$lockAction,$hash->{NUKIID},$hash->{DEVICETYPE});
IOWrite( $hash, "lockAction", $lockAction, $hash->{NUKIID},
$hash->{DEVICETYPE} );
return undef;
}
@ -348,7 +350,6 @@ sub NUKIDevice_Parse($$) {
return "NUKIDevice ($name) - invalid json detected: $json";
}
#########################################
#### verarbeiten des JSON Strings #######
my $decode_json = eval { decode_json($json) };
@ -357,9 +358,10 @@ sub NUKIDevice_Parse($$) {
return;
}
if ( ref($decode_json) ne 'HASH' ) {
Log3($name, 2, "NUKIDevice ($name) - got wrong status message for $name: $decode_json");
Log3( $name, 2,
"NUKIDevice ($name) - got wrong status message for $name: $decode_json"
);
return undef;
}
@ -375,8 +377,15 @@ sub NUKIDevice_Parse($$) {
##################
## Zwischenlösung so für die Umstellung, kann später gelöscht werden
if ( AttrVal( $name, 'model', '' ) eq '' ) {
CommandDefMod(undef,$name . ' NUKIDevice ' . $hash->{NUKIID} . ' ' . $decode_json->{deviceType});
CommandAttr(undef,$name . ' model ' . $deviceTypes{$decode_json->{deviceType}});
CommandDefMod( undef,
$name
. ' NUKIDevice '
. $hash->{NUKIID} . ' '
. $decode_json->{deviceType} );
CommandAttr( undef,
$name
. ' model '
. $deviceTypes{ $decode_json->{deviceType} } );
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
}
@ -386,7 +395,8 @@ sub NUKIDevice_Parse($$) {
Log3( $name, 3,
"NUKIDevice ($name) - autocreate new device "
. makeDeviceName( $decode_json->{name} )
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}");
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
);
return
'UNDEFINED '
. makeDeviceName( $decode_json->{name} )
@ -403,7 +413,6 @@ sub NUKIDevice_WriteReadings($$) {
my $name = $hash->{NAME};
############################
#### Status des Smartlock
@ -414,7 +423,8 @@ sub NUKIDevice_WriteReadings($$) {
{
$battery = 'ok';
} elsif ( $decode_json->{batteryCritical} eq 'true'
}
elsif ($decode_json->{batteryCritical} eq 'true'
or $decode_json->{batteryCritical} == 1 )
{
$battery = "low";
@ -426,7 +436,8 @@ sub NUKIDevice_WriteReadings($$) {
if ( defined( $hash->{helper}{lockAction} ) ) {
my ( $state, $lockState );
if ( defined($decode_json->{success})
if (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'true'
or $decode_json->{success} == 1 )
)
@ -435,16 +446,21 @@ sub NUKIDevice_WriteReadings($$) {
$lockState = $hash->{helper}{lockAction};
IOWrite( $hash, "lockState", undef, $hash->{NUKIID} )
if ( ReadingsVal($hash->{IODev}->{NAME},'bridgeType','Software') eq 'Software' );
if (
ReadingsVal( $hash->{IODev}->{NAME}, 'bridgeType', 'Software' )
eq 'Software' );
} elsif ( defined($decode_json->{success})
}
elsif (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'false'
or $decode_json->{success} == 0 )
)
{
$state = $deviceTypes{ $hash->{DEVICETYPE} } . ' response error';
IOWrite($hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} );
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID},
$hash->{DEVICETYPE} );
}
readingsBulkUpdate( $hash, 'state', $state );
@ -452,10 +468,13 @@ sub NUKIDevice_WriteReadings($$) {
readingsBulkUpdate( $hash, 'success', $decode_json->{success} );
delete $hash->{helper}{lockAction};
Log3($name, 5, "NUKIDevice ($name) - lockAction readings set for $name");
Log3( $name, 5,
"NUKIDevice ($name) - lockAction readings set for $name" );
} else {
readingsBulkUpdate( $hash, 'batteryCritical', $decode_json->{batteryCritical} );
}
else {
readingsBulkUpdate( $hash, 'batteryCritical',
$decode_json->{batteryCritical} );
readingsBulkUpdate( $hash, 'lockState', $decode_json->{stateName} );
readingsBulkUpdate( $hash, 'state', $decode_json->{stateName} );
readingsBulkUpdate( $hash, 'batteryState', $battery );
@ -473,17 +492,8 @@ sub NUKIDevice_WriteReadings($$) {
return undef;
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's