ready for testing

This commit is contained in:
Marko Oldenburg 2021-11-26 19:38:49 +01:00
parent 1bc18d3263
commit 4694030cd5
5 changed files with 1019 additions and 986 deletions

View File

@ -1,800 +0,0 @@
###############################################################################
#
# Developed with Kate
#
# (c) 2016-2021 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
# 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;
package FHEM::NUKIDevice;
use strict;
use warnings;
use FHEM::Meta;
use GPUtils qw(GP_Import GP_Export);
main::LoadModule('NUKIBridge');
# 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;
}
}
}
}
}
## Import der FHEM Funktionen
#-- Run before package compilation
BEGIN {
# Import from main context
GP_Import(
qw(
readingsSingleUpdate
readingsBulkUpdate
readingsBeginUpdate
readingsEndUpdate
readingFnAttributes
makeDeviceName
defs
modules
Log3
CommandAttr
AttrVal
IsDisabled
deviceEvents
init_done
InternalVal
ReadingsVal
AssignIoPort
IOWrite
data)
);
}
#-- Export to main context with different name
GP_Export(
qw(
Initialize
)
);
my %deviceTypes = (
0 => 'smartlock',
2 => 'opener',
4 => 'smartlockNG'
);
my %modes = (
2 => {
0 => 'door mode',
2 => 'door mode'
},
3 => {
0 => '-',
2 => ' continuous mode'
}
);
my %lockStates = (
0 => {
0 => 'uncalibrated',
2 => 'untrained',
4 => 'uncalibrated'
},
1 => {
0 => 'locked',
2 => 'online',
4 => 'locked'
},
2 => {
0 => 'unlocking',
2 => '-',
4 => 'unlocking'
},
3 => {
0 => 'unlocked',
2 => 'rto active',
4 => 'unlocked'
},
4 => {
0 => 'locking',
2 => '-',
4 => 'locking'
},
5 => {
0 => 'unlatched',
2 => 'open',
4 => 'unlatched'
},
6 => {
0 => 'unlocked (lock n go)',
2 => '-',
4 => 'unlocked (lock n go)'
},
7 => {
0 => 'unlatching',
2 => 'opening',
4 => 'unlatching'
},
253 => {
0 => '-',
2 => 'boot run',
4 => '-'
},
254 => {
0 => 'motor blocked',
2 => '-',
4 => 'motor blocked'
},
255 => {
0 => 'undefined',
2 => 'undefined',
4 => 'undefined'
}
);
my %deviceTypeIds = reverse(%deviceTypes);
sub Initialize($) {
my ($hash) = @_;
$hash->{Match} = '^{.*}$';
$hash->{SetFn} = 'FHEM::NUKIDevice::Set';
$hash->{DefFn} = 'FHEM::NUKIDevice::Define';
$hash->{UndefFn} = 'FHEM::NUKIDevice::Undef';
$hash->{NotifyFn} = 'FHEM::NUKIDevice::Notify';
$hash->{AttrFn} = 'FHEM::NUKIDevice::Attr';
$hash->{ParseFn} = 'FHEM::NUKIDevice::Parse';
$hash->{AttrList} =
'IODev '
. 'model:opener,smartlock '
. 'disable:1 '
. $readingFnAttributes;
return FHEM::Meta::InitMod( __FILE__, $hash );
}
sub Define($$) {
my ( $hash, $def ) = @_;
my @a = split( '[ \t][ \t]*', $def );
return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>'
if ( @a != 4 );
my $name = $a[0];
my $nukiId = $a[2];
my $deviceType = ( defined $a[3] ) ? $a[3] : 0;
$hash->{NUKIID} = $nukiId;
$hash->{DEVICETYPE} = ( defined $deviceType ) ? $deviceType : 0;
$hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{STATE} = 'Initialized';
$hash->{NOTIFYDEV} = 'global,autocreate,' . $name;
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} );
}
else {
Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
}
$iodev = $hash->{IODev}->{NAME};
$hash->{BRIDGEAPI} = $defs{$iodev}->{BRIDGEAPI};
my $d = $modules{NUKIDevice}{defptr}{$nukiId};
return
'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' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
CommandAttr( undef, $name . ' model ' . $deviceTypes{$deviceType} )
if ( AttrVal( $name, 'model', 'none' ) eq 'none' );
$modules{NUKIDevice}{defptr}{$nukiId} = $hash;
GetUpdate($hash)
if ( ReadingsVal( $name, 'success', 'none' ) eq 'none'
and $init_done );
return undef;
}
sub Undef($$) {
my ( $hash, $arg ) = @_;
my $nukiId = $hash->{NUKIID};
my $name = $hash->{NAME};
Log3( $name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId" );
delete( $modules{NUKIDevice}{defptr}{$nukiId} );
return undef;
}
sub Attr(@) {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $token = $hash->{IODev}->{TOKEN};
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal == 1 ) {
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3( $name, 3, "NUKIDevice ($name) - disabled" );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3, "NUKIDevice ($name) - enabled" );
}
}
elsif ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
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" );
}
}
elsif ( $attrName eq 'model' ) {
if ( $cmd eq 'set' ) {
Log3( $name, 3, "NUKIDevice ($name) - change model" );
$hash->{DEVICETYPE} = $deviceTypeIds{$attrVal};
}
}
return undef;
}
sub Notify($$) {
my ( $hash, $dev ) = @_;
my $name = $hash->{NAME};
return if ( IsDisabled($name) );
my $devname = $dev->{NAME};
my $devtype = $dev->{TYPE};
my $events = deviceEvents( $dev, 1 );
return if ( !$events );
GetUpdate($hash)
if (
(
grep /^INITIALIZED$/,
@{$events}
or grep /^REREADCFG$/,
@{$events}
or grep /^MODIFIED.$name$/,
@{$events}
or grep /^DEFINED.$name$/,
@{$events}
)
and $devname eq 'global'
and $init_done
);
return;
}
sub Set($$@) {
my ( $hash, $name, @aa ) = @_;
my ( $cmd, @args ) = @aa;
my $lockAction;
if ( lc($cmd) eq 'statusrequest' ) {
return ('usage: statusRequest') if ( @args != 0 );
GetUpdate($hash);
return undef;
}
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'
or lc($cmd) eq 'deactivatecontinuousmode'
or $cmd eq 'unpair' )
{
return ( 'usage: ' . $cmd )
if ( @args != 0 );
$lockAction = $cmd;
}
else {
my $list = '';
$list =
'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
if ( $hash->{DEVICETYPE} == 0
|| $hash->{DEVICETYPE} == 4 );
$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 );
}
$hash->{helper}{lockAction} = $lockAction;
# IOWrite( $hash, 'lockAction', $lockAction, $hash->{NUKIID},
# $hash->{DEVICETYPE} );
IOWrite( $hash, 'lockAction', '{"param":"' . $lockAction
. '","nukiId":' . $hash->{NUKIID} . ',"deviceType":' . $hash->{DEVICETYPE} . '}' );
return undef;
}
sub GetUpdate($) {
my $hash = shift;
my $name = $hash->{NAME};
if ( !IsDisabled($name) ) {
# IOWrite( $hash, 'lockState', undef, $hash->{NUKIID},
# $hash->{DEVICETYPE} );
IOWrite( $hash, 'lockState', '{"nukiId":' . $hash->{NUKIID}
. ',"deviceType":' . $hash->{DEVICETYPE} . '}' );
Log3( $name, 2, "NUKIDevice ($name) - GetUpdate Call IOWrite" );
}
return undef;
}
sub Parse($$) {
my ( $hash, $json ) = @_;
my $name = $hash->{NAME};
Log3( $name, 5, "NUKIDevice ($name) - Parse with result: $json" );
#########################################
####### Errorhandling #############
if ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3( $name, 3, "NUKIDevice ($name) - invalid json detected: $json" );
return "NUKIDevice ($name) - invalid json detected: $json";
}
#########################################
#### verarbeiten des JSON Strings #######
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 3, "NUKIDevice ($name) - JSON error while request: $@" );
return;
}
if ( ref($decode_json) ne 'HASH' ) {
Log3( $name, 2,
"NUKIDevice ($name) - got wrong status message for $name: $decode_json"
);
return undef;
}
my $nukiId = $decode_json->{nukiId};
if ( my $hash = $modules{NUKIDevice}{defptr}{$nukiId} ) {
my $name = $hash->{NAME};
WriteReadings( $hash, $decode_json );
Log3( $name, 4,
"NUKIDevice ($name) - find logical device: $hash->{NAME}" );
##################
## 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} } );
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
}
return $hash->{NAME};
}
else {
Log3( $name, 4,
"NUKIDevice ($name) - autocreate new device "
. makeDeviceName( $decode_json->{name} )
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
);
return
'UNDEFINED '
. makeDeviceName( $decode_json->{name} )
. " NUKIDevice $decode_json->{nukiId} $decode_json->{deviceType}";
}
Log3( $name, 5, "NUKIDevice ($name) - parse status message for $name" );
WriteReadings( $hash, $decode_json );
}
sub WriteReadings($$) {
my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME};
############################
#### Status des Smartlock
if ( defined( $hash->{helper}{lockAction} ) ) {
my $state;
if (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'true'
or $decode_json->{success} == 1 )
)
{
$state = $hash->{helper}{lockAction};
# IOWrite( $hash, 'lockState', undef, $hash->{NUKIID} )
IOWrite( $hash, 'lockState', '{"nukiId":' . $hash->{NUKIID}
. ',"deviceType":' . $hash->{DEVICETYPE} . '}' )
if (
ReadingsVal( $hash->{IODev}->{NAME}, 'bridgeType', 'Software' )
eq 'Software' );
}
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', '{"nukiId":' . $hash->{NUKIID}
. ',"deviceType":' . $hash->{DEVICETYPE} . '}' );
}
$decode_json->{'state'} = $state;
delete $hash->{helper}{lockAction};
}
readingsBeginUpdate($hash);
my $t;
my $v;
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};
}
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'
or $t eq 'batteryCritical'
or $t eq 'timestamp' );
readingsBulkUpdate( $hash, $t,
( $v =~ m/^[0-9]$/ ? $lockStates{$v}{ $hash->{DEVICETYPE} } : $v ) )
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' );
}
readingsEndUpdate( $hash, 1 );
Log3( $name, 5, "NUKIDevice ($name) - lockAction readings set for $name" );
return undef;
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's
=item summary_DE Modul zur Steuerung des Nuki Smartlocks.
=begin html
<a name="NUKIDevice"></a>
<h3>NUKIDevice</h3>
<ul>
<u><b>NUKIDevice - Controls the Nuki Smartlock</b></u>
<br>
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>
Normally the Nuki devices are automatically created by the bridge module.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type is 0 for the Smartlock and 2 for the Opener.
<br><br>
Example:
<ul><br>
<code>define Frontdoor NUKIDevice 1 NBridge1 0</code><br>
</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>
<li>name - name of the device</li>
<li>paired - paired information false/true</li>
<li>rssi - value of rssi</li>
<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>
<li>batteryState - battery status, ok / low</li>
</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>
<li>unpair - Removes the pairing with a given Smart Lock</li>
<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>
</ul>
=end html
=begin html_DE
<a name="NUKIDevice"></a>
<h3>NUKIDevice</h3>
<ul>
<u><b>NUKIDevice - Steuert das Nuki Smartlock</b></u>
<br>
Das Nuki Modul verbindet FHEM über die Nuki Bridge mit einem Nuki Smartlock oder Nuki Opener. Es ist dann m&ouml;glich das Schloss zu ver- und entriegeln.<br>
In der Regel werden die Nuki Devices automatisch durch das Bridgemodul angelegt.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type ist 0 f&uuml;r das Smartlock und 2 f&üuml;r den Opener.
<br><br>
Beispiel:
<ul><br>
<code>define Haust&uuml;r NUKIDevice 1 NBridge1 0</code><br>
</ul>
<br>
Diese Anweisung erstellt ein NUKIDevice mit Namen Haust&uuml;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&szlig;status uncalibrated, locked, unlocked, unlocked (lock n go), unlatched, locking, unlocking, unlatching, motor blocked, undefined.</li>
<li>name - Name des Smart Locks</li>
<li>paired - pairing Status des Smart Locks</li>
<li>rssi - rssi Wert des Smart Locks</li>
<li>succes - true, false Gibt des Status des letzten Schlie&szlig;befehles wieder. Geklappt oder nicht geklappt.</li>
<li>batteryCritical - Ist die Batterie in einem kritischen Zustand? true, false</li>
<li>batteryState - Status der Batterie, ok/low</li>
</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&szlig;en</li>
<li>unlock - aufschlie&szlig;en</li>
<li>unlatch - entriegeln/Falle &ouml;ffnen.</li>
<li>unpair - entfernt das pairing mit dem Smart Lock</li>
<li>locknGo - verschlie&szlig;en wenn gegangen</li>
<li>locknGoWithUnlatch - verschlie&szlig;en nach dem die Falle ge&ouml;ffnet wurde.</li>
<br>
</ul>
<br><br>
<a name="NUKIDeviceattribut"></a>
<b>Attribute</b>
<ul>
<li>disable - deaktiviert das Nuki Device</li>
<br>
</ul>
</ul>
=end html_DE
=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": "stable",
"license": "GPL_2",
"version": "v1.9.50",
"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
=cut

10
FHEM/73_NUKIBridge.pm Executable file → Normal file
View File

@ -43,14 +43,13 @@ use warnings;
use FHEM::Meta; use FHEM::Meta;
require FHEM::Devices::Nuki::Bridge; require FHEM::Devices::Nuki::Bridge;
sub ::NUKIBridge_Initialize { goto &Initialize } sub ::NUKIBridge_Initialize { goto &Initialize }
sub Initialize { sub Initialize {
my ($hash) = @_; my ($hash) = @_;
# Provider # Provider
$hash->{WriteFn} = \&Write; $hash->{WriteFn} = \&FHEM::Devices::Nuki::Bridge::Write;
$hash->{Clients} = ':NUKIDevice:'; $hash->{Clients} = ':NUKIDevice:';
$hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' }; $hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' };
@ -74,9 +73,6 @@ sub Initialize {
return FHEM::Meta::InitMod( __FILE__, $hash ); return FHEM::Meta::InitMod( __FILE__, $hash );
} }
1; 1;
=pod =pod
@ -245,8 +241,8 @@ sub Initialize {
], ],
"release_status": "stable", "release_status": "stable",
"license": "GPL_2", "license": "GPL_2",
"version": "v1.9.30", "version": "v2.0.0",
"x_apiversion": "1.9", "x_apiversion": "1.12.3",
"author": [ "author": [
"Marko Oldenburg <leongaultier@gmail.com>" "Marko Oldenburg <leongaultier@gmail.com>"
], ],

235
FHEM/74_NUKIDevice.pm Normal file
View File

@ -0,0 +1,235 @@
###############################################################################
#
# Developed with Kate
#
# (c) 2016-2021 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
# 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 FHEM::NUKIDevice;
use strict;
use warnings;
use FHEM::Meta;
require FHEM::Devices::Nuki::Device;
main::LoadModule('NUKIBridge');
sub ::NUKIDevice_Initialize { goto &Initialize }
sub Initialize($) {
my ($hash) = @_;
$hash->{Match} = '^{.*}$';
$hash->{SetFn} = \&FHEM::Devices::Nuki::Device::Set;
$hash->{DefFn} = \&FHEM::Devices::Nuki::Device::Define;
$hash->{UndefFn} = \&FHEM::Devices::Nuki::Device::Undef;
$hash->{NotifyFn} = \&FHEM::Devices::Nuki::Device::Notify;
$hash->{AttrFn} = \&FHEM::Devices::Nuki::Device::Attr;
$hash->{ParseFn} = \&FHEM::Devices::Nuki::Device::Parse;
$hash->{AttrList} =
'IODev '
. 'model:opener,smartlock '
. 'disable:1 '
. $::readingFnAttributes;
return FHEM::Meta::InitMod( __FILE__, $hash );
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's
=item summary_DE Modul zur Steuerung des Nuki Smartlocks.
=begin html
<a name="NUKIDevice"></a>
<h3>NUKIDevice</h3>
<ul>
<u><b>NUKIDevice - Controls the Nuki Smartlock</b></u>
<br>
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>
Normally the Nuki devices are automatically created by the bridge module.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type is 0 for the Smartlock and 2 for the Opener.
<br><br>
Example:
<ul><br>
<code>define Frontdoor NUKIDevice 1 NBridge1 0</code><br>
</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>
<li>name - name of the device</li>
<li>paired - paired information false/true</li>
<li>rssi - value of rssi</li>
<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>
<li>batteryState - battery status, ok / low</li>
</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>
<li>unpair - Removes the pairing with a given Smart Lock</li>
<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>
</ul>
=end html
=begin html_DE
<a name="NUKIDevice"></a>
<h3>NUKIDevice</h3>
<ul>
<u><b>NUKIDevice - Steuert das Nuki Smartlock</b></u>
<br>
Das Nuki Modul verbindet FHEM über die Nuki Bridge mit einem Nuki Smartlock oder Nuki Opener. Es ist dann m&ouml;glich das Schloss zu ver- und entriegeln.<br>
In der Regel werden die Nuki Devices automatisch durch das Bridgemodul angelegt.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type ist 0 f&uuml;r das Smartlock und 2 f&üuml;r den Opener.
<br><br>
Beispiel:
<ul><br>
<code>define Haust&uuml;r NUKIDevice 1 NBridge1 0</code><br>
</ul>
<br>
Diese Anweisung erstellt ein NUKIDevice mit Namen Haust&uuml;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&szlig;status uncalibrated, locked, unlocked, unlocked (lock n go), unlatched, locking, unlocking, unlatching, motor blocked, undefined.</li>
<li>name - Name des Smart Locks</li>
<li>paired - pairing Status des Smart Locks</li>
<li>rssi - rssi Wert des Smart Locks</li>
<li>succes - true, false Gibt des Status des letzten Schlie&szlig;befehles wieder. Geklappt oder nicht geklappt.</li>
<li>batteryCritical - Ist die Batterie in einem kritischen Zustand? true, false</li>
<li>batteryState - Status der Batterie, ok/low</li>
</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&szlig;en</li>
<li>unlock - aufschlie&szlig;en</li>
<li>unlatch - entriegeln/Falle &ouml;ffnen.</li>
<li>unpair - entfernt das pairing mit dem Smart Lock</li>
<li>locknGo - verschlie&szlig;en wenn gegangen</li>
<li>locknGoWithUnlatch - verschlie&szlig;en nach dem die Falle ge&ouml;ffnet wurde.</li>
<br>
</ul>
<br><br>
<a name="NUKIDeviceattribut"></a>
<b>Attribute</b>
<ul>
<li>disable - deaktiviert das Nuki Device</li>
<br>
</ul>
</ul>
=end html_DE
=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": "stable",
"license": "GPL_2",
"version": "v2.0.0",
"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
=cut

View File

@ -43,7 +43,6 @@ use warnings;
use FHEM::Meta; use FHEM::Meta;
use HttpUtils; use HttpUtils;
# try to use JSON::MaybeXS wrapper # try to use JSON::MaybeXS wrapper
# for chance of better performance + open code # for chance of better performance + open code
eval { eval {
@ -139,30 +138,27 @@ my %lockActionsOpener = (
); );
sub Define { sub Define {
my ( $hash, $def ) = @_; my $hash = shift;
my $def = shift // return;
my @a = split( "[ \t][ \t]*", $def );
return $@ unless ( FHEM::Meta::SetInternals($hash) ); return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' ); use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
my ( $name, undef, $host, $token ) = split( m{\s+}xms, $def );
return ('too few parameters: define <name> NUKIBridge <HOST> <TOKEN>') return ('too few parameters: define <name> NUKIBridge <HOST> <TOKEN>')
if ( @a != 4 ); if ( !defined($host)
|| !defined($token) );
my $name = $a[0];
my $host = $a[2];
my $token = $a[3];
my $port = 8080; my $port = 8080;
my $infix = 'NUKIBridge';
$hash->{HOST} = $host; $hash->{HOST} = $host;
$hash->{PORT} = $port; $hash->{PORT} = $port;
$hash->{TOKEN} = $token; $hash->{TOKEN} = $token;
$hash->{NOTIFYDEV} = 'global,' . $name; $hash->{NOTIFYDEV} = 'global,' . $name;
$hash->{VERSION} = version->parse($VERSION)->normal; $hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{BRIDGEAPI} = FHEM::Meta::Get( $hash, 'x_apiversion' ); $hash->{BRIDGEAPI} = FHEM::Meta::Get( $hash, 'x_apiversion' );
$hash->{helper}->{actionQueue} = []; $hash->{helper}->{actionQueue} = [];
$hash->{helper}->{iowrite} = 0; $hash->{helper}->{iowrite} = 0;
my $infix = 'NUKIBridge';
::Log3( $name, 3, ::Log3( $name, 3,
"NUKIBridge ($name) - defined with host $host on port $port, Token $token" "NUKIBridge ($name) - defined with host $host on port $port, Token $token"
@ -171,7 +167,14 @@ sub Define {
::CommandAttr( undef, $name . ' room NUKI' ) ::CommandAttr( undef, $name . ' room NUKI' )
if ( ::AttrVal( $name, 'room', 'none' ) eq 'none' ); if ( ::AttrVal( $name, 'room', 'none' ) eq 'none' );
if ( addExtension( $name, 'NUKIBridge_CGI', $infix . "-" . $host ) ) { if (
addExtension(
$name,
\&FHEM::Devices::Nuki::Bridge::CGI,
$infix . "-" . $host
)
)
{
$hash->{fhem}{infix} = $infix; $hash->{fhem}{infix} = $infix;
} }
@ -187,7 +190,7 @@ sub Define {
} }
sub Undef { sub Undef {
my ( $hash, $arg ) = @_; my $hash = shift;
my $host = $hash->{HOST}; my $host = $hash->{HOST};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -203,7 +206,10 @@ sub Undef {
} }
sub Attr { sub Attr {
my ( $cmd, $name, $attrName, $attrVal ) = @_; my $cmd = shift;
my $name = shift;
my $attrName = shift;
my $attrVal = shift;
my $hash = $::defs{$name}; my $hash = $::defs{$name};
my $orig = $attrVal; my $orig = $attrVal;
@ -245,30 +251,31 @@ sub Attr {
return ( return (
"Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing" "Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing"
) )
if ( $attrVal if (
$attrVal
&& $attrName eq 'webhookFWinstance' && $attrName eq 'webhookFWinstance'
&& && ( !defined( $::defs{$attrVal} )
( !defined( $::defs{$attrVal} ) || $::defs{$attrVal}{TYPE} ne 'FHEMWEB' ) ); || $::defs{$attrVal}{TYPE} ne 'FHEMWEB' )
);
return ( return (
"Invalid value for attribute $attrName: needs to be an integer value") "Invalid value for attribute $attrName: needs to be an integer value")
if ( $attrVal && $attrName eq 'webhookPort' if ( $attrVal
&& $attrName eq 'webhookPort'
&& $attrVal !~ /^\d+$/ ); && $attrVal !~ /^\d+$/ );
if ( $attrName =~ /^webhook.*/ ) { if ( $attrName =~ /^webhook.*/ ) {
my $webhookHttpHostname = ( my $webhookHttpHostname = (
$attrName eq 'webhookHttpHostname' $attrName eq 'webhookHttpHostname' && defined($attrVal)
&& defined($attrVal) ? $attrVal
? $attrVal : ::AttrVal( $name, 'webhookHttpHostname', '' )
: ::AttrVal( $name, 'webhookHttpHostname', '' )
); );
my $webhookFWinstance = ( my $webhookFWinstance = (
$attrName eq 'webhookFWinstance' $attrName eq 'webhookFWinstance' && defined($attrVal)
&& defined($attrVal) ? $attrVal
? $attrVal : ::AttrVal( $name, 'webhookFWinstance', '' )
: ::AttrVal( $name, 'webhookFWinstance', '' )
); );
$hash->{WEBHOOK_URI} = '/' $hash->{WEBHOOK_URI} = '/'
@ -299,7 +306,8 @@ sub Attr {
. $hash->{WEBHOOK_URI}; . $hash->{WEBHOOK_URI};
::Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" ); ::Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" );
# Write( $hash, 'callback/add', $url, undef, undef )
# Write( $hash, 'callback/add', $url, undef, undef )
Write( $hash, 'callback/add', '{"param":"' . $url . '"}' ) Write( $hash, 'callback/add', '{"param":"' . $url . '"}' )
if ($::init_done); if ($::init_done);
$hash->{WEBHOOK_REGISTER} = 'sent'; $hash->{WEBHOOK_REGISTER} = 'sent';
@ -314,8 +322,10 @@ sub Attr {
sub Notify { sub Notify {
my ( $hash, $dev ) = @_; my $hash = shift;
my $dev = shift // return;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
return if ( ::IsDisabled($name) ); return if ( ::IsDisabled($name) );
my $devname = $dev->{NAME}; my $devname = $dev->{NAME};
@ -335,15 +345,17 @@ sub Notify {
or grep /^DEFINED.$name$/, or grep /^DEFINED.$name$/,
@{$events} @{$events}
) )
&& $devname eq 'global' && $devname eq 'global'
&& $::init_done && $::init_done
); );
return; return;
} }
sub addExtension { sub addExtension {
my ( $name, $func, $link ) = @_; my $name = shift;
my $func = shift;
my $link = shift;
my $url = '/' . $link; my $url = '/' . $link;
@ -359,7 +371,7 @@ sub addExtension {
} }
sub removeExtension { sub removeExtension {
my ($link) = @_; my $link = shift;
my $url = '/' . $link; my $url = '/' . $link;
my $name = $::data{FWEXT}{$url}{deviceName}; my $name = $::data{FWEXT}{$url}{deviceName};
@ -368,56 +380,52 @@ sub removeExtension {
"NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url..." "NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url..."
); );
delete $::data{FWEXT}{$url}; delete $::data{FWEXT}{$url};
return; return;
} }
sub Set { sub Set {
my ( $hash, $name, $cmd, @args ) = @_; my $hash = shift;
my $name = shift;
my $cmd = shift // return "set $name needs at least one argument !";
my $arg = shift // '';
my ( $arg, @params ) = @args;
my $endpoint; my $endpoint;
if ( lc($cmd) eq 'getdevicelist' ) { if ( lc($cmd) eq 'getdevicelist' ) {
return 'usage: getDeviceList' if ( @args != 0 ); return 'usage: getDeviceList' if ($arg);
$endpoint = 'list'; $endpoint = 'list';
} }
elsif ( $cmd eq 'info' ) { elsif ( $cmd eq 'info' ) {
return 'usage: statusRequest' if ( @args != 0 );
$endpoint = 'info'; $endpoint = 'info';
} }
elsif ( lc($cmd) eq 'fwupdate' ) { elsif ( lc($cmd) eq 'fwupdate' ) {
return 'usage: fwUpdate' if ( @args != 0 );
$endpoint = 'fwupdate'; $endpoint = 'fwupdate';
} }
elsif ( $cmd eq 'reboot' ) { elsif ( $cmd eq 'reboot' ) {
return 'usage: reboot' if ( @args != 0 ); return 'usage: reboot' if ( defined($arg) );
$endpoint = 'reboot'; $endpoint = 'reboot';
} }
elsif ( lc($cmd) eq 'clearlog' ) { elsif ( lc($cmd) eq 'clearlog' ) {
return 'usage: clearLog' if ( @args != 0 ); return 'usage: clearLog' if ( defined($arg) );
$endpoint = 'clearlog'; $endpoint = 'clearlog';
} }
elsif ( lc($cmd) eq 'factoryreset' ) { elsif ( lc($cmd) eq 'factoryreset' ) {
return 'usage: clearLog' if ( @args != 0 ); return 'usage: clearLog' if ( defined($arg) );
$endpoint = 'factoryReset'; $endpoint = 'factoryReset';
} }
elsif ( lc($cmd) eq 'callbackremove' ) { elsif ( lc($cmd) eq 'callbackremove' ) {
return 'usage: callbackRemove' if ( @args > 1 ); return 'usage: callbackRemove' if ( split( m{\s+}xms, $arg ) > 1 );
my $id = ( @args > 0 ? join( ' ', @args ) : 0 ); my $id = ( defined($arg) ? $arg : 0 );
# Write( $hash, 'callback/remove', $id, undef, undef )
Write( $hash, 'callback/remove', '{"param":"' . $id . '"}' ) Write( $hash, 'callback/remove', '{"param":"' . $id . '"}' )
if ( !::IsDisabled($name) ); if ( !::IsDisabled($name) );
return undef; return;
} }
else { else {
my $list = ''; my $list = '';
@ -426,26 +434,28 @@ sub Set {
if ( ::ReadingsVal( $name, 'bridgeType', 'Software' ) eq 'Hardware' ); if ( ::ReadingsVal( $name, 'bridgeType', 'Software' ) eq 'Hardware' );
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list ); return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
} }
Write( $hash, $endpoint, undef ) Write( $hash, $endpoint, undef )
if ( !::IsDisabled($name) ); if ( !::IsDisabled($name) );
return; return;
} }
sub Get { sub Get {
my ( $hash, $name, $cmd, @args ) = @_; my $hash = shift;
my $name = shift;
my $cmd = shift // return "set $name needs at least one argument !";
my $arg = shift;
my ( $arg, @params ) = @args;
my $endpoint; my $endpoint;
if ( lc($cmd) eq 'logfile' ) { if ( lc($cmd) eq 'logfile' ) {
return 'usage: logFile' if ( @args != 0 ); return 'usage: logFile' if ( defined($arg) );
$endpoint = 'log'; $endpoint = 'log';
} }
elsif ( lc($cmd) eq 'callbacklist' ) { elsif ( lc($cmd) eq 'callbacklist' ) {
return 'usage: callbackList' if ( @args != 0 ); return 'usage: callbackList' if ( defined($arg) );
$endpoint = 'callback/list'; $endpoint = 'callback/list';
} }
@ -457,55 +467,55 @@ sub Get {
return 'Unknown argument ' . $cmd . ', choose one of ' . $list; return 'Unknown argument ' . $cmd . ', choose one of ' . $list;
} }
return Write( $hash, $endpoint, undef ) return Write( $hash, $endpoint, undef );
} }
sub GetCheckBridgeAlive { sub GetCheckBridgeAlive {
my ($hash) = @_; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
::RemoveInternalTimer($hash); ::RemoveInternalTimer($hash);
::Log3( $name, 4, "NUKIBridge ($name) - GetCheckBridgeAlive" ); ::Log3( $name, 4, "NUKIBridge ($name) - GetCheckBridgeAlive" );
if ( !::IsDisabled($name) if ( !::IsDisabled($name)
&& $hash->{helper}->{iowrite} == 0 ) && $hash->{helper}->{iowrite} == 0 )
{ {
Write( $hash, 'info', undef); Write( $hash, 'info', undef );
::Log3( $name, 4, "NUKIBridge ($name) - run Write" ); ::Log3( $name, 4, "NUKIBridge ($name) - run Write" );
} }
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Call InternalTimer for GetCheckBridgeAlive" ); "NUKIBridge ($name) - Call InternalTimer for GetCheckBridgeAlive" );
return ::InternalTimer( ::gettimeofday() + 30, return ::InternalTimer( ::gettimeofday() + 30,
\&FHEM::Devices::Nuki::Bridge::GetCheckBridgeAlive, $hash ); \&FHEM::Devices::Nuki::Bridge::GetCheckBridgeAlive, $hash );
} }
sub FirstRun { sub FirstRun {
my ($hash) = @_; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
::RemoveInternalTimer($hash); ::RemoveInternalTimer($hash);
Write( $hash, 'list', undef ) Write( $hash, 'list', undef )
if ( !::IsDisabled($name) ); if ( !::IsDisabled($name) );
return ::InternalTimer( ::gettimeofday() + 5, return ::InternalTimer( ::gettimeofday() + 5,
\&FHEM::Devices::Nuki::Bridge::GetCheckBridgeAlive, $hash ); \&FHEM::Devices::Nuki::Bridge::GetCheckBridgeAlive, $hash );
} }
sub Write { sub Write {
my ( $hash, $endpoint, $json ) = @_; my $hash = shift;
my $endpoint = shift // return;
my $json = shift;
my $decode_json = eval { decode_json($json) } my $decode_json = eval { decode_json($json) }
if ( defined($json) ); if ( defined($json) );
my $nukiId = $decode_json->{nukiId} // undef; my $nukiId = $decode_json->{nukiId} // undef;
my $deviceType = $decode_json->{deviceType} // undef; my $deviceType = $decode_json->{deviceType} // undef;
my $param = $decode_json->{param} // undef; my $param = $decode_json->{param} // undef;
my $obj = { my $obj = {
endpoint => $endpoint, endpoint => $endpoint,
@ -518,7 +528,7 @@ sub Write {
if ( if (
( defined($param) && $param ) ( defined($param) && $param )
|| ( defined($nukiId) || ( defined($nukiId)
&& $nukiId ) && $nukiId )
); );
unshift( @{ $hash->{helper}->{actionQueue} }, $obj ); unshift( @{ $hash->{helper}->{actionQueue} }, $obj );
@ -527,7 +537,8 @@ sub Write {
} }
sub CreateUri { sub CreateUri {
my ( $hash, $obj ) = @_; my $hash = shift;
my $obj = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
@ -539,16 +550,19 @@ sub CreateUri {
my $deviceType = $obj->{deviceType}; my $deviceType = $obj->{deviceType};
my $uri = 'http://' . $host . ':' . $port; my $uri = 'http://' . $host . ':' . $port;
$uri .= '/' . $endpoint if ( defined $endpoint ); $uri .= '/' . $endpoint if ( defined $endpoint );
$uri .= '?token=' . $token if ( defined($token) ); $uri .= '?token=' . $token if ( defined($token) );
if ( defined($param) if ( defined($param)
&& defined($deviceType) ) && defined($deviceType) )
{ {
$uri .= '&action=' . $lockActionsSmartLock{$param} $uri .= '&action='
if ( $endpoint ne 'callback/add' . $lockActionsSmartLock{$param}
&& ( $deviceType == 0 if (
|| deviceType == 4 ) ); $endpoint ne 'callback/add'
&& ( $deviceType == 0
|| $deviceType == 4 )
);
$uri .= '&action=' . $lockActionsOpener{$param} $uri .= '&action=' . $lockActionsOpener{$param}
if ( $endpoint ne 'callback/add' if ( $endpoint ne 'callback/add'
@ -569,7 +583,7 @@ sub CreateUri {
if ( defined($deviceType) ); if ( defined($deviceType) );
::Log3( $name, 4, "NUKIBridge ($name) - created uri: $uri" ); ::Log3( $name, 4, "NUKIBridge ($name) - created uri: $uri" );
return $uri; return $uri;
} }
@ -599,13 +613,8 @@ sub BridgeCall {
}; };
$param->{cl} = $hash->{CL} $param->{cl} = $hash->{CL}
if ( if ( ( $endpoint eq 'callback/list' || $endpoint eq 'log' )
( && ref( $hash->{CL} ) eq 'HASH' );
$endpoint eq 'callback/list'
|| $endpoint eq 'log'
)
&& ref( $hash->{CL} ) eq 'HASH'
);
::HttpUtils_NonblockingGet($param); ::HttpUtils_NonblockingGet($param);
::Log3( $name, 4, ::Log3( $name, 4,
@ -617,12 +626,14 @@ sub BridgeCall {
if ( defined($endpoint) if ( defined($endpoint)
&& $endpoint eq 'lockAction' ); && $endpoint eq 'lockAction' );
} }
return; return;
} }
sub Distribution { sub Distribution {
my ( $param, $err, $json ) = @_; my $param = shift;
my $err = shift;
my $json = shift;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $doTrigger = $param->{doTrigger}; my $doTrigger = $param->{doTrigger};
@ -639,7 +650,7 @@ sub Distribution {
::Log3( $name, 4, "NUKIBridge ($name) - Response JSON: $json" ); ::Log3( $name, 4, "NUKIBridge ($name) - Response JSON: $json" );
::Log3( $name, 4, "NUKIBridge ($name) - Response ERROR: $err" ); ::Log3( $name, 4, "NUKIBridge ($name) - Response ERROR: $err" );
::Log3( $name, 4, "NUKIBridge ($name) - Response CODE: $param->{code}" ) ::Log3( $name, 4, "NUKIBridge ($name) - Response CODE: $param->{code}" )
if ( defined($param->{code}) if ( defined( $param->{code} )
&& $param->{code} ); && $param->{code} );
$hash->{helper}->{iowrite} = 0 $hash->{helper}->{iowrite} = 0
@ -669,22 +680,22 @@ sub Distribution {
} }
} }
if ( ( $json eq '' if ( ( $json eq '' || $json =~ /Unavailable/i )
|| $json =~ /Unavailable/i )
&& exists( $param->{code} ) && exists( $param->{code} )
&& $param->{code} != 200 ) && $param->{code} != 200 )
{ {
if ( $param->{code} == 503 if ( $param->{code} == 503
&& $json eq 'HTTP 503 Unavailable' ) && $json eq 'HTTP 503 Unavailable' )
{ {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Response from Bridge: $param->{code}, $json" ); "NUKIBridge ($name) - Response from Bridge: $param->{code}, $json"
);
::readingsEndUpdate( $hash, 1 ); ::readingsEndUpdate( $hash, 1 );
if ( defined( $hash->{helper}->{lastDeviceAction} ) if ( defined( $hash->{helper}->{lastDeviceAction} )
&& $hash->{helper}->{lastDeviceAction} ) && $hash->{helper}->{lastDeviceAction} )
{ {
push( push(
@{ $hash->{helper}->{actionQueue} }, @{ $hash->{helper}->{actionQueue} },
@ -709,7 +720,7 @@ sub Distribution {
. " without any data after requesting" ); . " without any data after requesting" );
::readingsEndUpdate( $hash, 1 ); ::readingsEndUpdate( $hash, 1 );
::asyncOutput( $param->{cl}, "Request Error: $err\r\n" ) ::asyncOutput( $param->{cl}, "Request Error: $err\r\n" )
if ( $param->{cl} && $param->{cl}{canAsyncOutput} ); if ( $param->{cl} && $param->{cl}{canAsyncOutput} );
@ -718,8 +729,8 @@ sub Distribution {
. ' without any data after requesting' ); . ' without any data after requesting' );
} }
if ( ($json =~ /Error/i ) if ( ( $json =~ /Error/i )
&& exists($param->{code}) ) && exists( $param->{code} ) )
{ {
::readingsBulkUpdate( $hash, 'lastError', 'invalid API token' ) ::readingsBulkUpdate( $hash, 'lastError', 'invalid API token' )
@ -753,11 +764,11 @@ sub Distribution {
if ( $param->{endpoint} eq 'callback/list' ) { if ( $param->{endpoint} eq 'callback/list' ) {
getCallbackList( $param, $json ); getCallbackList( $param, $json );
return undef; return;
} }
elsif ( $param->{endpoint} eq 'log' ) { elsif ( $param->{endpoint} eq 'log' ) {
getLogfile( $param, $json ); getLogfile( $param, $json );
return undef; return;
} }
if ( $hash == $dhash ) { if ( $hash == $dhash ) {
@ -777,7 +788,8 @@ sub Distribution {
::Dispatch( $hash, $json, undef ); ::Dispatch( $hash, $json, undef );
} }
::InternalTimer( ::gettimeofday() + 3, \&FHEM::Devices::Nuki::Bridge::BridgeCall, $hash ) ::InternalTimer( ::gettimeofday() + 3,
\&FHEM::Devices::Nuki::Bridge::BridgeCall, $hash )
if ( defined( $hash->{helper}->{actionQueue} ) if ( defined( $hash->{helper}->{actionQueue} )
&& scalar( @{ $hash->{helper}->{actionQueue} } ) > 0 ); && scalar( @{ $hash->{helper}->{actionQueue} } ) > 0 );
@ -785,7 +797,9 @@ sub Distribution {
} }
sub ResponseProcessing { sub ResponseProcessing {
my ( $hash, $json, $endpoint ) = @_; my $hash = shift;
my $json = shift;
my $endpoint = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json; my $decode_json;
@ -870,14 +884,13 @@ sub ResponseProcessing {
} }
else { else {
return ::Log3( return ::Log3( $name, 5,
$name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt: $json" "NUKIBridge ($name) - Rückgabe Path nicht korrekt: $json" );
);
} }
} }
sub CGI() { sub CGI() {
my ($request) = @_; my $request = shift;
my $hash; my $hash;
my $name; my $name;
@ -894,11 +907,13 @@ sub CGI() {
my $json = ( split( '&', $request, 2 ) )[1]; my $json = ( split( '&', $request, 2 ) )[1];
if ( !$json ) { if ( !$json ) {
::Log3( $name, 3, "NUKIBridge WEBHOOK ($name) - empty message received" ); ::Log3( $name, 3,
"NUKIBridge WEBHOOK ($name) - empty message received" );
return; return;
} }
elsif ( $json =~ m'HTTP/1.1 200 OK' ) { elsif ( $json =~ m'HTTP/1.1 200 OK' ) {
::Log3( $name, 4, "NUKIBridge WEBHOOK ($name) - empty answer received" ); ::Log3( $name, 4,
"NUKIBridge WEBHOOK ($name) - empty answer received" );
return; return;
} }
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) { elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
@ -912,16 +927,15 @@ sub CGI() {
if ( $json =~ m/^\{.*\}$/ ) { if ( $json =~ m/^\{.*\}$/ ) {
$hash->{WEBHOOK_COUNTER}++; $hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow(); $hash->{WEBHOOK_LAST} = ::TimeNow();
::Log3( ::Log3( $name, 3,
$name, 3, "NUKIBridge WEBHOOK ($name) - Received webhook for "NUKIBridge WEBHOOK ($name) - Received webhook for matching NukiId at device $name"
matching NukiId at device $name"
); );
::Dispatch( $hash, $json, undef ); ::Dispatch( $hash, $json, undef );
return(undef,undef); return ( undef, undef );
} }
# no data received # no data received
@ -930,11 +944,12 @@ matching NukiId at device $name"
"NUKIBridge WEBHOOK - received malformed request\n$request" ); "NUKIBridge WEBHOOK - received malformed request\n$request" );
} }
::return ( 'text/plain; charset=utf-8', 'Call failure: ' . $request ); ::return( 'text/plain; charset=utf-8', 'Call failure: ' . $request );
} }
sub WriteReadings { sub WriteReadings {
my ( $hash, $decode_json ) = @_; my $hash = shift;
my $decode_json = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -953,21 +968,23 @@ sub WriteReadings {
$decode_json->{versions}->{wifiFirmwareVersion} ); $decode_json->{versions}->{wifiFirmwareVersion} );
::readingsBulkUpdate( $hash, 'bridgeType', ::readingsBulkUpdate( $hash, 'bridgeType',
$bridgeType{ $decode_json->{bridgeType} } ); $bridgeType{ $decode_json->{bridgeType} } );
::readingsBulkUpdate( $hash, 'hardwareId', $decode_json->{ids}{hardwareId} ); ::readingsBulkUpdate( $hash, 'hardwareId',
::readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} ); $decode_json->{ids}{hardwareId} );
::readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} ); ::readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} );
::readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} );
::readingsBulkUpdate( $hash, 'currentGMTime', $decode_json->{currentTime} ); ::readingsBulkUpdate( $hash, 'currentGMTime', $decode_json->{currentTime} );
::readingsBulkUpdate( $hash, 'serverConnected', ::readingsBulkUpdate( $hash, 'serverConnected',
$decode_json->{serverConnected} ); $decode_json->{serverConnected} );
::readingsBulkUpdate( $hash, 'wlanConnected', ::readingsBulkUpdate( $hash, 'wlanConnected',
$decode_json->{wlanConnected} ); $decode_json->{wlanConnected} );
::readingsEndUpdate( $hash, 1 ); ::readingsEndUpdate( $hash, 1 );
return; return;
} }
sub getLogfile { sub getLogfile {
my ( $param, $json ) = @_; my $param = shift;
my $json = shift;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -981,20 +998,19 @@ sub getLogfile {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Log data are collected and processed" ); "NUKIBridge ($name) - Log data are collected and processed" );
if ( $param->{cl} if ( $param->{cl}
&& $param->{cl}->{TYPE} eq 'FHEMWEB' ) && $param->{cl}->{TYPE} eq 'FHEMWEB' )
{ {
if ( ref($decode_json) eq 'ARRAY' if ( ref($decode_json) eq 'ARRAY'
&& scalar( @{$decode_json} ) > 0 ) && scalar( @{$decode_json} ) > 0 )
{ {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - created Table with log file" ); "NUKIBridge ($name) - created Table with log file" );
my $header = '<html>' my $header = '<html>' . '<div style="float: left">Log List</div>';
. '<div style="float: left">Log List</div>';
my $ret = $header.'<table width=100%><tr><td>'; my $ret = $header . '<table width=100%><tr><td>';
$ret .= '<table class="block wide">'; $ret .= '<table class="block wide">';
for my $logs ( @{$decode_json} ) { for my $logs ( @{$decode_json} ) {
@ -1032,12 +1048,13 @@ sub getLogfile {
&& $param->{cl}{canAsyncOutput} ); && $param->{cl}{canAsyncOutput} );
} }
} }
return; return;
} }
sub getCallbackList { sub getCallbackList {
my ( $param, $json ) = @_; my $param = shift;
my $json = shift;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1051,8 +1068,8 @@ sub getCallbackList {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Callback data are collected and processed" ); "NUKIBridge ($name) - Callback data are collected and processed" );
if ( $param->{cl} if ( $param->{cl}
&& $param->{cl}->{TYPE} eq 'FHEMWEB' ) && $param->{cl}->{TYPE} eq 'FHEMWEB' )
{ {
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY' ) { if ( ref( $decode_json->{callbacks} ) eq 'ARRAY' ) {
@ -1061,10 +1078,10 @@ sub getCallbackList {
my $space = '&nbsp;'; my $space = '&nbsp;';
my $aHref; my $aHref;
my $header = '<html>' my $header =
. '<div style="float: left">Callback List</div>'; '<html>' . '<div style="float: left">Callback List</div>';
my $ret = $header.'<table width=100%><tr><td>'; my $ret = $header . '<table width=100%><tr><td>';
$ret .= '<table class="block wide">'; $ret .= '<table class="block wide">';
$ret .= '<tr class="odd">'; $ret .= '<tr class="odd">';
$ret .= '<td><b>URL</b></td>'; $ret .= '<td><b>URL</b></td>';
@ -1075,16 +1092,16 @@ sub getCallbackList {
for my $cb ( @{ $decode_json->{callbacks} } ) { for my $cb ( @{ $decode_json->{callbacks} } ) {
$aHref = $aHref =
"<a href=\"" "<a href=\""
. $::FW_httpheader->{host} # . $::FW_httpheader->{host}
. "/fhem?cmd=set+" . "/fhem?cmd=set+"
. $name . $name
. "+callbackRemove+" . "+callbackRemove+"
. $cb->{id} . $cb->{id}
. $::FW_CSRF . $::FW_CSRF
. "\"><font color=\"red\"><b>X</b></font></a>"; . "\"><font color=\"red\"><b>X</b></font></a>";
$ret .= '<td>' . $cb->{url} . '</td>'; $ret .= '<td>' . $cb->{url} . '</td>';
$ret .= '<td>'.$aHref.'</td>'; $ret .= '<td>' . $aHref . '</td>';
$ret .= '</tr>'; $ret .= '</tr>';
} }
} }
@ -1103,12 +1120,13 @@ sub getCallbackList {
&& $param->{cl}{canAsyncOutput} ); && $param->{cl}{canAsyncOutput} );
} }
} }
return; return;
} }
sub getCallbackList2 { sub getCallbackList2 {
my ( $param, $json ) = @_; my $param = shift;
my $json = shift;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1122,38 +1140,39 @@ sub getCallbackList2 {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Callback data are collected and processed" ); "NUKIBridge ($name) - Callback data are collected and processed" );
if ( $param->{cl} if ( $param->{cl}
&& $param->{cl}->{TYPE} eq 'FHEMWEB' ) && $param->{cl}->{TYPE} eq 'FHEMWEB' )
{ {
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY' ) { if ( ref( $decode_json->{callbacks} ) eq 'ARRAY' ) {
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - created Table with Callback List" ); "NUKIBridge ($name) - created Table with Callback List" );
my $j1 =
'<script language=\"javascript\" type=\"text/javascript\">{';
$j1 .=
"function callbackRemove(){FW_cmd(FW_root+'?cmd=get $name callbackList&XHR=1')}";
$j1 .= '}</script>';
my $j1 = '<script language=\"javascript\" type=\"text/javascript\">{'; # FW_cmd(FW_root+"?cmd="+type+" "+dev+
$j1 .= "function callbackRemove(){FW_cmd(FW_root+'?cmd=get $name callbackList&XHR=1')}"; # (params[0]=="state" ? "":" "+params[0])+" "+arg+"&XHR=1");
$j1 .= '}</script>';
# FW_cmd(FW_root+"?cmd="+type+" "+dev+
# (params[0]=="state" ? "":" "+params[0])+" "+arg+"&XHR=1");
my $header = '<html>'; my $header = '<html>';
my $footer = '</html>'; my $footer = '</html>';
my $ret = '<div style="float: left">Callback List</div>' my $ret =
. '<table width=100%><tr><td>' '<div style="float: left">Callback List</div>'
. '<table class="block wide">' . '<table width=100%><tr><td>'
. '<tr class="odd">' . '<table class="block wide">'
. '<td><b>URL</b></td>' . '<tr class="odd">'
. '<td><b>Remove</b></td>' . '<td><b>URL</b></td>'
. '</tr>'; . '<td><b>Remove</b></td>' . '</tr>';
if ( scalar( @{ $decode_json->{callbacks} } ) > 0 ) { if ( scalar( @{ $decode_json->{callbacks} } ) > 0 ) {
for my $cb ( @{ $decode_json->{callbacks} } ) { for my $cb ( @{ $decode_json->{callbacks} } ) {
$ret .= '<td>' . $cb->{url} . '</td>'; $ret .= '<td>' . $cb->{url} . '</td>';
$ret .= "<td><input title=\"CallbackRemove\" name=\"Remove\" type=\"button\" value=\"Remove\" onclick=\" javascript: callbackRemove() \"></td>"; $ret .=
"<td><input title=\"CallbackRemove\" name=\"Remove\" type=\"button\" value=\"Remove\" onclick=\" javascript: callbackRemove() \"></td>";
$ret .= '</tr>'; $ret .= '</tr>';
} }
} }
@ -1166,21 +1185,23 @@ sub getCallbackList2 {
$ret .= '</table></td></tr>'; $ret .= '</table></td></tr>';
$ret .= '</table>'; $ret .= '</table>';
::Log3( $name, 4, ::Log3( $name, 4,
"NUKIBridge ($name) - Callback List Table created and call asyncOutput Fn" ); "NUKIBridge ($name) - Callback List Table created and call asyncOutput Fn"
);
::asyncOutput( $param->{cl}, $header . $ret . $j1 . $footer ) ::asyncOutput( $param->{cl}, $header . $ret . $j1 . $footer )
if ( $param->{cl} if ( $param->{cl}
&& $param->{cl}{canAsyncOutput} ); && $param->{cl}{canAsyncOutput} );
} }
} }
return; return;
} }
sub ParseJSON { sub ParseJSON {
my ( $hash, $buffer ) = @_; my $hash = shift;
my $buffer = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $open = 0; my $open = 0;
@ -1191,16 +1212,16 @@ sub ParseJSON {
if ($buffer) { if ($buffer) {
for my $c ( split //, $buffer ) { for my $c ( split //, $buffer ) {
if ( $open == $close if ( $open == $close
&& $open > 0 ) && $open > 0 )
{ {
$tail .= $c; $tail .= $c;
::Log3( $name, 5, ::Log3( $name, 5,
"NUKIBridge ($name) - $open == $close and $open > 0" ); "NUKIBridge ($name) - $open == $close and $open > 0" );
} }
elsif ( $open == $close elsif ($open == $close
&& $c ne '{' ) && $c ne '{' )
{ {
::Log3( $name, 5, ::Log3( $name, 5,
"NUKIBridge ($name) - Garbage character before message: " "NUKIBridge ($name) - Garbage character before message: "
@ -1230,7 +1251,4 @@ sub ParseJSON {
return ( $msg, $tail ); return ( $msg, $tail );
} }
1; 1;

View File

@ -0,0 +1,584 @@
###############################################################################
#
# Developed with Kate
#
# (c) 2016-2021 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
# 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 FHEM::Devices::Nuki::Device;
use strict;
use warnings;
use FHEM::Meta;
# 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;
}
}
}
}
}
######## Begin Device
my %deviceTypes = (
0 => 'smartlock',
2 => 'opener',
4 => 'smartlockNG'
);
my %modes = (
2 => {
0 => 'door mode',
2 => 'door mode'
},
3 => {
0 => '-',
2 => ' continuous mode'
}
);
my %lockStates = (
0 => {
0 => 'uncalibrated',
2 => 'untrained',
4 => 'uncalibrated'
},
1 => {
0 => 'locked',
2 => 'online',
4 => 'locked'
},
2 => {
0 => 'unlocking',
2 => '-',
4 => 'unlocking'
},
3 => {
0 => 'unlocked',
2 => 'rto active',
4 => 'unlocked'
},
4 => {
0 => 'locking',
2 => '-',
4 => 'locking'
},
5 => {
0 => 'unlatched',
2 => 'open',
4 => 'unlatched'
},
6 => {
0 => 'unlocked (lock n go)',
2 => '-',
4 => 'unlocked (lock n go)'
},
7 => {
0 => 'unlatching',
2 => 'opening',
4 => 'unlatching'
},
253 => {
0 => '-',
2 => 'boot run',
4 => '-'
},
254 => {
0 => 'motor blocked',
2 => '-',
4 => 'motor blocked'
},
255 => {
0 => 'undefined',
2 => 'undefined',
4 => 'undefined'
}
);
my %deviceTypeIds = reverse(%deviceTypes);
sub Define {
my $hash = shift;
my $def = shift // return;
return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
my ( $name, undef, $nukiId, $deviceType ) = split( m{\s+}xms, $def );
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>'
if ( !defined($nukiId)
|| !defined($name) );
$deviceType =
defined($deviceType)
? $deviceType
: 0;
$hash->{NUKIID} = $nukiId;
$hash->{DEVICETYPEID} = $deviceType;
$hash->{VERSION} = version->parse($VERSION)->normal;
$hash->{STATE} = 'Initialized';
$hash->{NOTIFYDEV} = 'global,autocreate,' . $name;
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} );
}
else {
::Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
}
$iodev = $hash->{IODev}->{NAME};
$hash->{BRIDGEAPI} = $::defs{$iodev}->{BRIDGEAPI};
my $d = $::modules{NUKIDevice}{defptr}{$nukiId};
return
'NUKIDevice device '
. $name
. ' on NUKIBridge '
. $iodev
. ' already defined.'
if ( defined($d)
&& $d->{IODev} == $hash->{IODev}
&& $d->{NAME} ne $name );
::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' );
$::modules{NUKIDevice}{defptr}{$nukiId} = $hash;
GetUpdate($hash)
if ( ::ReadingsVal( $name, 'success', 'none' ) eq 'none'
&& $::init_done );
return;
}
sub Undef {
my $hash = shift;
my $nukiId = $hash->{NUKIID};
my $name = $hash->{NAME};
::Log3( $name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId" );
delete( $::modules{NUKIDevice}{defptr}{$nukiId} );
return;
}
sub Attr {
my $cmd = shift;
my $name = shift;
my $attrName = shift;
my $attrVal = shift;
my $hash = $::defs{$name};
my $token = $hash->{IODev}->{TOKEN};
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' && $attrVal == 1 ) {
::readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
::Log3( $name, 3, "NUKIDevice ($name) - disabled" );
}
elsif ( $cmd eq 'del' ) {
::readingsSingleUpdate( $hash, 'state', 'active', 1 );
::Log3( $name, 3, "NUKIDevice ($name) - enabled" );
}
}
elsif ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
::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" );
}
}
elsif ( $attrName eq 'model' ) {
if ( $cmd eq 'set' ) {
::Log3( $name, 3, "NUKIDevice ($name) - change model" );
$hash->{DEVICETYPEID} = $deviceTypeIds{$attrVal};
}
}
return;
}
sub Notify {
my $hash = shift;
my $dev = shift // return;
my $name = $hash->{NAME};
return if ( ::IsDisabled($name) );
my $devname = $dev->{NAME};
my $devtype = $dev->{TYPE};
my $events = ::deviceEvents( $dev, 1 );
return if ( !$events );
GetUpdate($hash)
if (
(
grep /^INITIALIZED$/,
@{$events}
or grep /^REREADCFG$/,
@{$events}
or grep /^MODIFIED.$name$/,
@{$events}
or grep /^DEFINED.$name$/,
@{$events}
)
&& $devname eq 'global'
&& $::init_done
);
return;
}
sub Set {
my $hash = shift;
my $name = shift;
my $cmd = shift // return "set $name needs at least one argument !";
my $lockAction;
if ( lc($cmd) eq 'statusrequest' ) {
GetUpdate($hash);
return;
}
elsif ($cmd eq 'lock'
|| lc($cmd) eq 'deactivaterto'
|| $cmd eq 'unlock'
|| lc($cmd) eq 'activaterto'
|| $cmd eq 'unlatch'
|| lc($cmd) eq 'electricstrikeactuation'
|| lc($cmd) eq 'lockngo'
|| lc($cmd) eq 'activatecontinuousmode'
|| lc($cmd) eq 'lockngowithunlatch'
|| lc($cmd) eq 'deactivatecontinuousmode'
|| $cmd eq 'unpair' )
{
$lockAction = $cmd;
}
else {
my $list = '';
$list =
'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
if ( $hash->{DEVICETYPEID} == 0
|| $hash->{DEVICETYPEID} == 4 );
$list =
'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg'
if ( $hash->{DEVICETYPEID} == 2 );
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
}
$hash->{helper}{lockAction} = $lockAction;
::IOWrite( $hash, 'lockAction',
'{"param":"'
. $lockAction
. '","nukiId":'
. $hash->{NUKIID}
. ',"deviceType":'
. $hash->{DEVICETYPEID}
. '}' );
return;
}
sub GetUpdate {
my $hash = shift;
my $name = $hash->{NAME};
if ( !::IsDisabled($name) ) {
::IOWrite( $hash, 'lockState',
'{"nukiId":'
. $hash->{NUKIID}
. ',"deviceType":'
. $hash->{DEVICETYPEID}
. '}' );
::Log3( $name, 2, "NUKIDevice ($name) - GetUpdate Call IOWrite" );
}
return;
}
sub Parse {
my $hash = shift;
my $json = shift // return;
my $name = $hash->{NAME};
::Log3( $name, 5, "NUKIDevice ($name) - Parse with result: $json" );
#########################################
####### Errorhandling #############
if ( $json !~ m/^[\[{].*[}\]]$/ ) {
::Log3( $name, 3, "NUKIDevice ($name) - invalid json detected: $json" );
return "NUKIDevice ($name) - invalid json detected: $json";
}
#########################################
#### verarbeiten des JSON Strings #######
my $decode_json = eval { decode_json($json) };
if ($@) {
::Log3( $name, 3, "NUKIDevice ($name) - JSON error while request: $@" );
return;
}
if ( ref($decode_json) ne 'HASH' ) {
::Log3( $name, 2,
"NUKIDevice ($name) - got wrong status message for $name: $decode_json"
);
return;
}
my $nukiId = $decode_json->{nukiId};
if ( my $hash = $::modules{NUKIDevice}{defptr}{$nukiId} ) {
my $name = $hash->{NAME};
WriteReadings( $hash, $decode_json );
::Log3( $name, 4,
"NUKIDevice ($name) - find logical device: $hash->{NAME}" );
##################
## 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} } );
::Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
}
return $hash->{NAME};
}
else {
::Log3( $name, 4,
"NUKIDevice ($name) - autocreate new device "
. ::makeDeviceName( $decode_json->{name} )
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
);
return
'UNDEFINED '
. ::makeDeviceName( $decode_json->{name} )
. " NUKIDevice $decode_json->{nukiId} $decode_json->{deviceType}";
}
::Log3( $name, 5, "NUKIDevice ($name) - parse status message for $name" );
WriteReadings( $hash, $decode_json );
}
sub WriteReadings {
my $hash = shift;
my $decode_json = shift;
my $name = $hash->{NAME};
############################
#### Status des Smartlock
if ( defined( $hash->{helper}{lockAction} ) ) {
my $state;
if (
defined( $decode_json->{success} )
&& ( $decode_json->{success} eq 'true'
|| $decode_json->{success} == 1 )
)
{
$state = $hash->{helper}{lockAction};
::IOWrite( $hash, 'lockState',
'{"nukiId":'
. $hash->{NUKIID}
. ',"deviceType":'
. $hash->{DEVICETYPEID}
. '}' )
if (
::ReadingsVal( $hash->{IODev}->{NAME},
'bridgeType', 'Software' ) eq 'Software'
);
}
elsif (
defined( $decode_json->{success} )
&& ( $decode_json->{success} eq 'false'
|| $decode_json->{success} == 0 )
)
{
$state = $deviceTypes{ $hash->{DEVICETYPEID} } . ' response error';
::IOWrite( $hash, 'lockState',
'{"nukiId":'
. $hash->{NUKIID}
. ',"deviceType":'
. $hash->{DEVICETYPEID}
. '}' );
}
$decode_json->{'state'} = $state;
delete $hash->{helper}{lockAction};
}
::readingsBeginUpdate($hash);
my $t;
my $v;
if ( defined( $decode_json->{lastKnownState} )
&& ref( $decode_json->{lastKnownState} ) eq 'HASH' )
{
while ( ( $t, $v ) = each %{ $decode_json->{lastKnownState} } ) {
$decode_json->{$t} = $v;
}
delete $decode_json->{lastKnownState};
}
while ( ( $t, $v ) = each %{$decode_json} ) {
::readingsBulkUpdate( $hash, $t, $v )
if ( $t ne 'state'
&& $t ne 'mode'
&& $t ne 'deviceType'
&& $t ne 'paired'
&& $t ne 'batteryCritical'
&& $t ne 'timestamp' );
::readingsBulkUpdate( $hash, $t,
( $v =~ m/^[0-9]$/ ? $lockStates{$v}{ $hash->{DEVICETYPEID} } : $v ) )
if ( $t eq 'state' );
::readingsBulkUpdate( $hash, $t, $modes{$v}{ $hash->{DEVICETYPEID} } )
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' );
}
::readingsEndUpdate( $hash, 1 );
::Log3( $name, 5,
"NUKIDevice ($name) - lockAction readings set for $name" );
return;
}
1;