2016-09-27 11:37:19 +00:00
|
|
|
|
###############################################################################
|
2020-01-09 21:02:41 +00:00
|
|
|
|
#
|
2016-09-27 11:37:19 +00:00
|
|
|
|
# Developed with Kate
|
|
|
|
|
#
|
2020-01-09 14:44:41 +00:00
|
|
|
|
# (c) 2016-2020 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
|
2016-09-27 11:37:19 +00:00
|
|
|
|
# All rights reserved
|
|
|
|
|
#
|
|
|
|
|
# This script is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# any later version.
|
|
|
|
|
#
|
|
|
|
|
# The GNU General Public License can be found at
|
|
|
|
|
# http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
|
# A copy is found in the textfile GPL.txt and important notices to the license
|
|
|
|
|
# from the author is found in LICENSE.txt distributed with these scripts.
|
|
|
|
|
#
|
|
|
|
|
# This script is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# $Id$
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
package main;
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
2020-01-11 18:00:05 +00:00
|
|
|
|
use FHEM::Meta;
|
|
|
|
|
|
|
|
|
|
main::LoadModule('NUKIBridge');
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
# 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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-10 20:02:39 +00:00
|
|
|
|
|
2017-01-17 05:18:25 +00:00
|
|
|
|
|
|
|
|
|
# Declare functions
|
|
|
|
|
sub NUKIDevice_Initialize($);
|
|
|
|
|
sub NUKIDevice_Define($$);
|
|
|
|
|
sub NUKIDevice_Undef($$);
|
|
|
|
|
sub NUKIDevice_Attr(@);
|
|
|
|
|
sub NUKIDevice_Set($$@);
|
|
|
|
|
sub NUKIDevice_GetUpdate($);
|
|
|
|
|
sub NUKIDevice_Parse($$);
|
|
|
|
|
sub NUKIDevice_WriteReadings($$);
|
|
|
|
|
|
2020-01-08 12:26:22 +00:00
|
|
|
|
my %deviceTypes = (
|
|
|
|
|
0 => 'smartlock',
|
|
|
|
|
2 => 'opener'
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-10 10:47:49 +00:00
|
|
|
|
my %modes = (
|
|
|
|
|
2 => {
|
|
|
|
|
0 => 'door mode',
|
|
|
|
|
2 => 'door mode'
|
|
|
|
|
},
|
|
|
|
|
3 => {
|
|
|
|
|
0 => '-',
|
|
|
|
|
2 => ' continuous mode'
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
my %lockStates = (
|
|
|
|
|
0 => {
|
|
|
|
|
0 => 'uncalibrated',
|
|
|
|
|
2 => 'untrained'
|
|
|
|
|
},
|
|
|
|
|
1 => {
|
|
|
|
|
0 => 'locked',
|
|
|
|
|
2 => 'online'
|
|
|
|
|
},
|
|
|
|
|
2 => {
|
|
|
|
|
0 => 'unlocking',
|
|
|
|
|
2 => '-'
|
|
|
|
|
},
|
|
|
|
|
3 => {
|
|
|
|
|
0 => 'unlocked',
|
|
|
|
|
2 => 'rto active'
|
|
|
|
|
},
|
|
|
|
|
4 => {
|
|
|
|
|
0 => 'locking',
|
|
|
|
|
2 => '-'
|
|
|
|
|
},
|
|
|
|
|
5 => {
|
|
|
|
|
0 => 'unlatched',
|
|
|
|
|
2 => 'open'
|
|
|
|
|
},
|
|
|
|
|
6 => {
|
|
|
|
|
0 => 'unlocked (lock ‘n’ go)',
|
|
|
|
|
2 => '-'
|
|
|
|
|
},
|
|
|
|
|
7 => {
|
|
|
|
|
0 => 'unlatching',
|
|
|
|
|
2 => 'opening'
|
|
|
|
|
},
|
|
|
|
|
253 => {
|
|
|
|
|
0 => '-',
|
|
|
|
|
2 => 'boot run'
|
|
|
|
|
},
|
|
|
|
|
254 => {
|
|
|
|
|
0 => 'motor blocked',
|
|
|
|
|
2 => '-'
|
|
|
|
|
},
|
|
|
|
|
255 => {
|
|
|
|
|
0 => 'undefined',
|
|
|
|
|
2 => 'undefined'
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-08 12:26:22 +00:00
|
|
|
|
my %deviceTypeIds = reverse(%deviceTypes);
|
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
sub NUKIDevice_Initialize($) {
|
|
|
|
|
my ($hash) = @_;
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2020-01-08 15:30:42 +00:00
|
|
|
|
$hash->{Match} = '^{.*}$';
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$hash->{SetFn} = 'NUKIDevice_Set';
|
|
|
|
|
$hash->{DefFn} = 'NUKIDevice_Define';
|
|
|
|
|
$hash->{UndefFn} = 'NUKIDevice_Undef';
|
|
|
|
|
$hash->{AttrFn} = 'NUKIDevice_Attr';
|
|
|
|
|
$hash->{ParseFn} = 'NUKIDevice_Parse';
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$hash->{AttrList} =
|
|
|
|
|
'IODev '
|
|
|
|
|
. 'model:opener,smartlock '
|
|
|
|
|
. 'disable:1 '
|
|
|
|
|
. $readingFnAttributes;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-11 18:00:05 +00:00
|
|
|
|
return FHEM::Meta::InitMod( __FILE__, $hash );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NUKIDevice_Define($$) {
|
|
|
|
|
my ( $hash, $def ) = @_;
|
2020-01-08 12:26:22 +00:00
|
|
|
|
my @a = split( '[ \t][ \t]*', $def );
|
2020-01-11 18:00:05 +00:00
|
|
|
|
|
|
|
|
|
return $@ unless ( FHEM::Meta::SetInternals($hash) );
|
|
|
|
|
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>'
|
|
|
|
|
if ( @a != 4 );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my $name = $a[0];
|
|
|
|
|
my $nukiId = $a[2];
|
|
|
|
|
my $deviceType = ( defined $a[3] ) ? $a[3] : 0;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2016-12-17 19:47:58 +00:00
|
|
|
|
$hash->{NUKIID} = $nukiId;
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$hash->{DEVICETYPE} = ( defined $deviceType ) ? $deviceType : 0;
|
2020-01-11 18:00:05 +00:00
|
|
|
|
$hash->{VERSION} = version->parse($VERSION)->normal;
|
2016-09-27 22:21:44 +00:00
|
|
|
|
$hash->{STATE} = 'Initialized';
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
|
|
|
|
my $iodev = AttrVal( $name, 'IODev', 'none' );
|
|
|
|
|
|
|
|
|
|
AssignIoPort( $hash, $iodev ) if ( !$hash->{IODev} );
|
|
|
|
|
|
|
|
|
|
if ( defined( $hash->{IODev}->{NAME} ) ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3,
|
|
|
|
|
"NUKIDevice ($name) - I/O device is " . $hash->{IODev}->{NAME} );
|
2020-01-08 12:26:22 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
}
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
$iodev = $hash->{IODev}->{NAME};
|
2020-01-11 18:00:05 +00:00
|
|
|
|
|
|
|
|
|
$hash->{BRIDGEAPI} = $defs{$iodev}->{BRIDGEAPI};
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-08 12:26:22 +00:00
|
|
|
|
my $d = $modules{NUKIDevice}{defptr}{$nukiId};
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-08 12:26:22 +00:00
|
|
|
|
return
|
2020-01-09 21:02:41 +00:00
|
|
|
|
'NUKIDevice device '
|
|
|
|
|
. $name
|
|
|
|
|
. ' on NUKIBridge '
|
|
|
|
|
. $iodev
|
|
|
|
|
. ' already defined.'
|
2020-01-08 12:26:22 +00:00
|
|
|
|
if ( defined($d)
|
|
|
|
|
and $d->{IODev} == $hash->{IODev}
|
|
|
|
|
and $d->{NAME} ne $name );
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - defined with NukiId: $nukiId" );
|
|
|
|
|
|
|
|
|
|
CommandAttr( undef, $name . ' room NUKI' )
|
|
|
|
|
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
|
|
|
|
|
CommandAttr( undef, $name . ' model ' . $deviceTypes{$deviceType} )
|
|
|
|
|
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 );
|
2016-12-10 17:47:56 +00:00
|
|
|
|
}
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2020-01-08 12:26:22 +00:00
|
|
|
|
$modules{NUKIDevice}{defptr}{$nukiId} = $hash;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NUKIDevice_Undef($$) {
|
|
|
|
|
my ( $hash, $arg ) = @_;
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
my $nukiId = $hash->{NUKIID};
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
RemoveInternalTimer($hash);
|
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId" );
|
|
|
|
|
delete( $modules{NUKIDevice}{defptr}{$nukiId} );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NUKIDevice_Attr(@) {
|
|
|
|
|
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my $hash = $defs{$name};
|
2016-12-12 13:23:51 +00:00
|
|
|
|
my $token = $hash->{IODev}->{TOKEN};
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( $attrName eq 'disable' ) {
|
|
|
|
|
if ( $cmd eq 'set' and $attrVal == 1 ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
|
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - disabled" );
|
2016-12-17 19:47:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
elsif ( $cmd eq 'del' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
readingsSingleUpdate( $hash, 'state', 'active', 1 );
|
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - enabled" );
|
2016-12-17 19:47:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 16:20:13 +00:00
|
|
|
|
elsif ( $attrName eq 'disabledForIntervals' ) {
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( $cmd eq 'set' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3,
|
|
|
|
|
"NUKIDevice ($name) - enable disabledForIntervals" );
|
|
|
|
|
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
|
2016-12-17 19:47:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
elsif ( $cmd eq 'del' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
readingsSingleUpdate( $hash, 'state', 'active', 1 );
|
|
|
|
|
Log3( $name, 3,
|
|
|
|
|
"NUKIDevice ($name) - delete disabledForIntervals" );
|
2016-09-27 22:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 16:20:13 +00:00
|
|
|
|
elsif ( $attrName eq 'model' ) {
|
|
|
|
|
if ( $cmd eq 'set' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - change model" );
|
2020-01-09 16:20:13 +00:00
|
|
|
|
$hash->{DEVICETYPE} = $deviceTypeIds{$attrVal};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
return undef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NUKIDevice_Set($$@) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my ( $hash, $name, @aa ) = @_;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my ( $cmd, @args ) = @aa;
|
2016-09-27 22:21:44 +00:00
|
|
|
|
my $lockAction;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( lc($cmd) eq 'statusrequest' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
return ('usage: statusRequest') if ( @args != 0 );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
NUKIDevice_GetUpdate($hash);
|
|
|
|
|
return undef;
|
2020-01-09 21:02:41 +00:00
|
|
|
|
}
|
|
|
|
|
elsif ($cmd eq 'lock'
|
|
|
|
|
or lc($cmd) eq 'deactivaterto'
|
|
|
|
|
or $cmd eq 'unlock'
|
|
|
|
|
or lc($cmd) eq 'activaterto'
|
|
|
|
|
or $cmd eq 'unlatch'
|
|
|
|
|
or lc($cmd) eq 'electricstrikeactuation'
|
|
|
|
|
or lc($cmd) eq 'lockngo'
|
|
|
|
|
or lc($cmd) eq 'activatecontinuousmode'
|
|
|
|
|
or lc($cmd) eq 'lockngowithunlatch'
|
2020-01-11 11:08:35 +00:00
|
|
|
|
or lc($cmd) eq 'deactivatecontinuousmode'
|
|
|
|
|
or $cmd eq 'unpair' )
|
2020-01-09 21:02:41 +00:00
|
|
|
|
{
|
|
|
|
|
return ( 'usage: ' . $cmd )
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( @args != 0 );
|
|
|
|
|
$lockAction = $cmd;
|
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-12-21 20:52:18 +00:00
|
|
|
|
my $list = '';
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$list =
|
|
|
|
|
'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( $hash->{DEVICETYPE} == 0 );
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$list =
|
|
|
|
|
'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg'
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( $hash->{DEVICETYPE} == 2 );
|
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
}
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2016-09-28 20:01:55 +00:00
|
|
|
|
$hash->{helper}{lockAction} = $lockAction;
|
2020-01-11 11:08:35 +00:00
|
|
|
|
|
2020-01-11 13:26:00 +00:00
|
|
|
|
IOWrite( $hash, 'lockAction', $lockAction, $hash->{NUKIID},
|
2020-01-09 21:02:41 +00:00
|
|
|
|
$hash->{DEVICETYPE} );
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2016-12-10 17:47:56 +00:00
|
|
|
|
return undef;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
sub NUKIDevice_GetUpdate($) {
|
2020-01-09 14:44:41 +00:00
|
|
|
|
my $hash = shift;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
my $name = $hash->{NAME};
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2016-12-12 13:23:51 +00:00
|
|
|
|
RemoveInternalTimer($hash);
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} )
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( !IsDisabled($name) );
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call IOWrite" )
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( !IsDisabled($name) );
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
return undef;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:33:03 +00:00
|
|
|
|
sub NUKIDevice_Parse($$) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my ( $hash, $json ) = @_;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 5, "NUKIDevice ($name) - Parse with result: $json" );
|
2016-09-28 20:01:55 +00:00
|
|
|
|
#########################################
|
|
|
|
|
####### Errorhandling #############
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( $json !~ m/^[\[{].*[}\]]$/ ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - invalid json detected: $json" );
|
2020-01-09 14:44:41 +00:00
|
|
|
|
return "NUKIDevice ($name) - invalid json detected: $json";
|
2017-11-21 17:15:15 +00:00
|
|
|
|
}
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2016-09-28 20:01:55 +00:00
|
|
|
|
#########################################
|
|
|
|
|
#### verarbeiten des JSON Strings #######
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my $decode_json = eval { decode_json($json) };
|
|
|
|
|
if ($@) {
|
|
|
|
|
Log3( $name, 3, "NUKIDevice ($name) - JSON error while request: $@" );
|
2018-02-27 10:25:20 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( ref($decode_json) ne 'HASH' ) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 2,
|
|
|
|
|
"NUKIDevice ($name) - got wrong status message for $name: $decode_json"
|
|
|
|
|
);
|
2016-09-27 11:37:19 +00:00
|
|
|
|
return undef;
|
|
|
|
|
}
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
my $nukiId = $decode_json->{nukiId};
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
if ( my $hash = $modules{NUKIDevice}{defptr}{$nukiId} ) {
|
|
|
|
|
my $name = $hash->{NAME};
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
NUKIDevice_WriteReadings( $hash, $decode_json );
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 4,
|
|
|
|
|
"NUKIDevice ($name) - find logical device: $hash->{NAME}" );
|
2020-01-08 12:26:22 +00:00
|
|
|
|
|
2020-01-09 16:20:13 +00:00
|
|
|
|
##################
|
|
|
|
|
## Zwischenlösung so für die Umstellung, kann später gelöscht werden
|
2020-01-09 21:02:41 +00:00
|
|
|
|
if ( AttrVal( $name, 'model', '' ) eq '' ) {
|
|
|
|
|
CommandDefMod( undef,
|
|
|
|
|
$name
|
|
|
|
|
. ' NUKIDevice '
|
|
|
|
|
. $hash->{NUKIID} . ' '
|
|
|
|
|
. $decode_json->{deviceType} );
|
|
|
|
|
CommandAttr( undef,
|
|
|
|
|
$name
|
|
|
|
|
. ' model '
|
|
|
|
|
. $deviceTypes{ $decode_json->{deviceType} } );
|
|
|
|
|
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
|
2020-01-09 16:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 14:44:41 +00:00
|
|
|
|
return $hash->{NAME};
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 3,
|
|
|
|
|
"NUKIDevice ($name) - autocreate new device "
|
|
|
|
|
. makeDeviceName( $decode_json->{name} )
|
|
|
|
|
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
|
|
|
|
|
);
|
2020-01-09 14:44:41 +00:00
|
|
|
|
return
|
|
|
|
|
'UNDEFINED '
|
2020-01-09 21:02:41 +00:00
|
|
|
|
. makeDeviceName( $decode_json->{name} )
|
|
|
|
|
. " NUKIDevice $decode_json->{nukiId} $decode_json->{deviceType}";
|
2020-01-08 12:26:22 +00:00
|
|
|
|
}
|
2016-09-27 11:37:19 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
Log3( $name, 5, "NUKIDevice ($name) - parse status message for $name" );
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
NUKIDevice_WriteReadings( $hash, $decode_json );
|
2016-11-16 10:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NUKIDevice_WriteReadings($$) {
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my ( $hash, $decode_json ) = @_;
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
my $name = $hash->{NAME};
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
############################
|
2016-09-28 20:01:55 +00:00
|
|
|
|
#### Status des Smartlock
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
|
|
|
|
if ( defined( $hash->{helper}{lockAction} ) ) {
|
2020-01-10 11:11:08 +00:00
|
|
|
|
my $state;
|
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
if (
|
|
|
|
|
defined( $decode_json->{success} )
|
|
|
|
|
and ( $decode_json->{success} eq 'true'
|
|
|
|
|
or $decode_json->{success} == 1 )
|
2020-01-09 14:44:41 +00:00
|
|
|
|
)
|
|
|
|
|
{
|
2020-01-10 10:47:49 +00:00
|
|
|
|
$state = $hash->{helper}{lockAction};
|
|
|
|
|
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID} )
|
2020-01-09 21:02:41 +00:00
|
|
|
|
if (
|
|
|
|
|
ReadingsVal( $hash->{IODev}->{NAME}, 'bridgeType', 'Software' )
|
|
|
|
|
eq 'Software' );
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2020-01-09 21:02:41 +00:00
|
|
|
|
}
|
|
|
|
|
elsif (
|
|
|
|
|
defined( $decode_json->{success} )
|
|
|
|
|
and ( $decode_json->{success} eq 'false'
|
|
|
|
|
or $decode_json->{success} == 0 )
|
|
|
|
|
)
|
2020-01-09 14:44:41 +00:00
|
|
|
|
{
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
|
|
|
|
$state = $deviceTypes{ $hash->{DEVICETYPE} } . ' response error';
|
|
|
|
|
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID},
|
|
|
|
|
$hash->{DEVICETYPE} );
|
2017-01-07 08:31:09 +00:00
|
|
|
|
}
|
2017-01-07 20:38:50 +00:00
|
|
|
|
|
2020-01-10 11:11:08 +00:00
|
|
|
|
$decode_json->{'state'} = $state;
|
2016-09-28 20:01:55 +00:00
|
|
|
|
delete $hash->{helper}{lockAction};
|
2020-01-10 10:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readingsBeginUpdate($hash);
|
|
|
|
|
|
|
|
|
|
my $t;
|
|
|
|
|
my $v;
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2020-01-10 10:47:49 +00:00
|
|
|
|
if ( defined( $decode_json->{lastKnownState} )
|
|
|
|
|
and ref( $decode_json->{lastKnownState} ) eq 'HASH' )
|
|
|
|
|
{
|
|
|
|
|
while ( ( $t, $v ) = each %{ $decode_json->{lastKnownState} } ) {
|
|
|
|
|
$decode_json->{$t} = $v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete $decode_json->{lastKnownState};
|
2020-01-09 21:02:41 +00:00
|
|
|
|
}
|
2020-01-10 10:47:49 +00:00
|
|
|
|
|
|
|
|
|
while ( ( $t, $v ) = each %{$decode_json} ) {
|
|
|
|
|
readingsBulkUpdate( $hash, $t, $v )
|
|
|
|
|
unless ( $t eq 'state'
|
|
|
|
|
or $t eq 'mode'
|
|
|
|
|
or $t eq 'deviceType'
|
|
|
|
|
or $t eq 'paired'
|
2020-01-10 11:22:33 +00:00
|
|
|
|
or $t eq 'batteryCritical'
|
|
|
|
|
or $t eq 'timestamp' );
|
2020-01-11 11:08:35 +00:00
|
|
|
|
readingsBulkUpdate( $hash, $t,
|
|
|
|
|
( $v =~ m/^[0-9]$/ ? $lockStates{$v}{ $hash->{DEVICETYPE} } : $v ) )
|
2020-01-10 10:47:49 +00:00
|
|
|
|
if ( $t eq 'state' );
|
|
|
|
|
readingsBulkUpdate( $hash, $t, $modes{$v}{ $hash->{DEVICETYPE} } )
|
|
|
|
|
if ( $t eq 'mode' );
|
|
|
|
|
readingsBulkUpdate( $hash, $t, $deviceTypes{$v} )
|
|
|
|
|
if ( $t eq 'deviceType' );
|
|
|
|
|
readingsBulkUpdate( $hash, $t, ( $v == 1 ? 'true' : 'false' ) )
|
|
|
|
|
if ( $t eq 'paired' );
|
|
|
|
|
readingsBulkUpdate( $hash, 'batteryState',
|
|
|
|
|
( ( $v eq 'true' or $v == 1 ) ? 'low' : 'ok' ) )
|
|
|
|
|
if ( $t eq 'batteryCritical' );
|
2016-09-28 20:01:55 +00:00
|
|
|
|
}
|
2020-01-09 21:02:41 +00:00
|
|
|
|
|
2016-09-28 20:01:55 +00:00
|
|
|
|
readingsEndUpdate( $hash, 1 );
|
2020-01-09 14:44:41 +00:00
|
|
|
|
|
2020-01-10 10:47:49 +00:00
|
|
|
|
Log3( $name, 5, "NUKIDevice ($name) - lockAction readings set for $name" );
|
|
|
|
|
|
2016-09-28 20:01:55 +00:00
|
|
|
|
return undef;
|
2016-09-27 11:37:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
|
|
=pod
|
2016-10-10 10:14:16 +00:00
|
|
|
|
=item device
|
2016-10-22 15:00:29 +00:00
|
|
|
|
=item summary Modul to control the Nuki Smartlock's
|
2016-10-10 10:14:16 +00:00
|
|
|
|
=item summary_DE Modul zur Steuerung des Nuki Smartlocks.
|
|
|
|
|
|
2016-09-27 11:37:19 +00:00
|
|
|
|
=begin html
|
|
|
|
|
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<a name="NUKIDevice"></a>
|
2016-09-27 11:37:19 +00:00
|
|
|
|
<h3>NUKIDevice</h3>
|
|
|
|
|
<ul>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
<u><b>NUKIDevice - Controls the Nuki Smartlock</b></u>
|
|
|
|
|
<br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
The Nuki module connects FHEM over the Nuki Bridge with a Nuki Smartlock or Nuki Opener. After that, it´s possible to lock and unlock the Smartlock.<br>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
Normally the Nuki devices are automatically created by the bridge module.
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDevicedefine"></a>
|
|
|
|
|
<b>Define</b>
|
|
|
|
|
<ul><br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
<code>define <name> NUKIDevice <Nuki-Id> <IODev-Device> <Device-Type></code>
|
|
|
|
|
<br><br>
|
|
|
|
|
Device-Type is 0 for the Smartlock and 2 for the Opener.
|
2016-10-11 07:11:43 +00:00
|
|
|
|
<br><br>
|
|
|
|
|
Example:
|
|
|
|
|
<ul><br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
<code>define Frontdoor NUKIDevice 1 NBridge1 0</code><br>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
</ul>
|
|
|
|
|
<br>
|
|
|
|
|
This statement creates a NUKIDevice with the name Frontdoor, the NukiId 1 and the IODev device NBridge1.<br>
|
|
|
|
|
After the device has been created, the current state of the Smartlock is automatically read from the bridge.
|
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDevicereadings"></a>
|
|
|
|
|
<b>Readings</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>state - Status of the Smartlock or error message if any error.</li>
|
|
|
|
|
<li>lockState - current lock status uncalibrated, locked, unlocked, unlocked (lock ‘n’ go), unlatched, locking, unlocking, unlatching, motor blocked, undefined.</li>
|
2017-01-19 20:07:38 +00:00
|
|
|
|
<li>name - name of the device</li>
|
|
|
|
|
<li>paired - paired information false/true</li>
|
|
|
|
|
<li>rssi - value of rssi</li>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
<li>succes - true, false Returns the status of the last closing command. Ok or not Ok.</li>
|
|
|
|
|
<li>batteryCritical - Is the battery in a critical state? True, false</li>
|
2018-06-01 06:45:26 +00:00
|
|
|
|
<li>batteryState - battery status, ok / low</li>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDeviceset"></a>
|
|
|
|
|
<b>Set</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>statusRequest - retrieves the current state of the smartlock from the bridge.</li>
|
|
|
|
|
<li>lock - lock</li>
|
|
|
|
|
<li>unlock - unlock</li>
|
|
|
|
|
<li>unlatch - unlock / open Door</li>
|
2017-01-19 20:07:38 +00:00
|
|
|
|
<li>unpair - Removes the pairing with a given Smart Lock</li>
|
2016-10-11 07:11:43 +00:00
|
|
|
|
<li>locknGo - lock when gone</li>
|
|
|
|
|
<li>locknGoWithUnlatch - lock after the door has been opened</li>
|
|
|
|
|
<br>
|
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDeviceattribut"></a>
|
|
|
|
|
<b>Attributes</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>disable - disables the Nuki device</li>
|
|
|
|
|
<br>
|
|
|
|
|
</ul>
|
2016-09-27 11:37:19 +00:00
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
=end html
|
|
|
|
|
=begin html_DE
|
|
|
|
|
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<a name="NUKIDevice"></a>
|
2016-09-27 11:37:19 +00:00
|
|
|
|
<h3>NUKIDevice</h3>
|
|
|
|
|
<ul>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<u><b>NUKIDevice - Steuert das Nuki Smartlock</b></u>
|
|
|
|
|
<br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
Das Nuki Modul verbindet FHEM über die Nuki Bridge mit einem Nuki Smartlock oder Nuki Opener. Es ist dann möglich das Schloss zu ver- und entriegeln.<br>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
In der Regel werden die Nuki Devices automatisch durch das Bridgemodul angelegt.
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDevicedefine"></a>
|
|
|
|
|
<b>Define</b>
|
|
|
|
|
<ul><br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
<code>define <name> NUKIDevice <Nuki-Id> <IODev-Device> <Device-Type></code>
|
|
|
|
|
<br><br>
|
|
|
|
|
Device-Type ist 0 für das Smartlock und 2 f&üuml;r den Opener.
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<br><br>
|
|
|
|
|
Beispiel:
|
|
|
|
|
<ul><br>
|
2019-12-21 20:52:18 +00:00
|
|
|
|
<code>define Haustür NUKIDevice 1 NBridge1 0</code><br>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
</ul>
|
|
|
|
|
<br>
|
|
|
|
|
Diese Anweisung erstellt ein NUKIDevice mit Namen Haustür, der NukiId 1 sowie dem IODev Device NBridge1.<br>
|
|
|
|
|
Nach dem anlegen des Devices wird automatisch der aktuelle Zustand des Smartlocks aus der Bridge gelesen.
|
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDevicereadings"></a>
|
|
|
|
|
<b>Readings</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>state - Status des Smartlock bzw. Fehlermeldung von Fehler vorhanden.</li>
|
|
|
|
|
<li>lockState - aktueller Schließstatus uncalibrated, locked, unlocked, unlocked (lock ‘n’ go), unlatched, locking, unlocking, unlatching, motor blocked, undefined.</li>
|
2017-01-19 20:07:38 +00:00
|
|
|
|
<li>name - Name des Smart Locks</li>
|
|
|
|
|
<li>paired - pairing Status des Smart Locks</li>
|
|
|
|
|
<li>rssi - rssi Wert des Smart Locks</li>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<li>succes - true, false Gibt des Status des letzten Schließbefehles wieder. Geklappt oder nicht geklappt.</li>
|
|
|
|
|
<li>batteryCritical - Ist die Batterie in einem kritischen Zustand? true, false</li>
|
2018-06-01 06:45:26 +00:00
|
|
|
|
<li>batteryState - Status der Batterie, ok/low</li>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDeviceset"></a>
|
|
|
|
|
<b>Set</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>statusRequest - ruft den aktuellen Status des Smartlocks von der Bridge ab.</li>
|
|
|
|
|
<li>lock - verschließen</li>
|
|
|
|
|
<li>unlock - aufschließen</li>
|
|
|
|
|
<li>unlatch - entriegeln/Falle öffnen.</li>
|
2017-01-19 20:07:38 +00:00
|
|
|
|
<li>unpair - entfernt das pairing mit dem Smart Lock</li>
|
2016-10-10 10:14:16 +00:00
|
|
|
|
<li>locknGo - verschließen wenn gegangen</li>
|
|
|
|
|
<li>locknGoWithUnlatch - verschließen nach dem die Falle geöffnet wurde.</li>
|
|
|
|
|
<br>
|
|
|
|
|
</ul>
|
|
|
|
|
<br><br>
|
|
|
|
|
<a name="NUKIDeviceattribut"></a>
|
|
|
|
|
<b>Attribute</b>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>disable - deaktiviert das Nuki Device</li>
|
|
|
|
|
<br>
|
|
|
|
|
</ul>
|
2016-09-27 11:37:19 +00:00
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
=end html_DE
|
2020-01-11 18:00:05 +00:00
|
|
|
|
|
|
|
|
|
=for :application/json;q=META.json 74_NUKIDevice.pm
|
|
|
|
|
{
|
|
|
|
|
"abstract": "Modul to control the Nuki Smartlock's over the Nuki Bridge",
|
|
|
|
|
"x_lang": {
|
|
|
|
|
"de": {
|
|
|
|
|
"abstract": "Modul to control the Nuki Smartlock's over the Nuki Bridge"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"keywords": [
|
|
|
|
|
"fhem-mod-device",
|
|
|
|
|
"fhem-core",
|
|
|
|
|
"Smartlock",
|
|
|
|
|
"Nuki",
|
|
|
|
|
"Control"
|
|
|
|
|
],
|
|
|
|
|
"release_status": "under develop",
|
|
|
|
|
"license": "GPL_2",
|
|
|
|
|
"version": "v1.9.1",
|
|
|
|
|
"author": [
|
|
|
|
|
"Marko Oldenburg <leongaultier@gmail.com>"
|
|
|
|
|
],
|
|
|
|
|
"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
|
|
|
|
|
|
2016-12-10 17:47:56 +00:00
|
|
|
|
=cut
|