First version with read and write to physical modul

added:
UpdateAll
UpdateSingle
After MiGateway define all connected devices to the gateway are added to
FHEM with there status
Cleanup logging lines
[PRE] added for support for switch sensor
motion, magnet, sensorht filed devStateIcon oder Stateformat
This commit is contained in:
2017-03-12 15:48:29 +01:00
parent 2c35b66ac9
commit e899ba7886
2 changed files with 266 additions and 74 deletions

View File

@@ -1,16 +1,40 @@
# 71_XiaomiSmartHome_Device.pm 2017-08-02 13:07:33Z torte $
###############################################################################
#
# 03.2017 torte
# 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.
#
#
###############################################################################
package main;
use strict;
use warnings;
my $version = "0.02";
sub XiaomiSmartHome_Device_updateSReading($);
#####################################
sub XiaomiSmartHome_Device_Initialize($)
{
my ($hash) = @_;
$hash->{Match} = "^.+magnet|motion|sensor_ht";
$hash->{Match} = "^.+magnet|motion|sensor_ht|switch";
$hash->{DefFn} = "XiaomiSmartHome_Device_Define";
#$hash->{SetFn} = "XiaomiSmartHome_Device_Set";
$hash->{UndefFn} = "XiaomiSmartHome_Device_Undef";
@@ -35,13 +59,48 @@ sub XiaomiSmartHome_Device_mot($$)
sub XiaomiSmartHome_Device_on_timeout($){
my ($hash) = @_;
my $name = $hash->{LASTInputDev};
if ($hash->{STATE} eq 'motion') {
readingsSingleUpdate($hash, "state", "off", 1 );
Log3 $name, 3, "$name>" . " SID: " . $hash->{SID} . " Type: " . $hash->{MODEL} . " Status: off";
}
}
#####################################
sub XiaomiSmartHome_Device_Read($$$){
my ($hash, $msg, $name) = @_;
my $decoded = decode_json($msg);
my $sid = $decoded->{'sid'};
my $model = $decoded->{'model'};
Log3 $name, 5, "$name: SID: " . $hash->{SID} . " " . $hash->{TYPE};
my @status = split('\"', $decoded->{'data'});
if ($status[1] eq 'status'){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $status[3];
readingsSingleUpdate($hash, "state", "$status[3]", 1 );
}
elsif($status[1] eq 'voltage'){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $status[3];
readingsSingleUpdate($hash, "voltage", "$status[3]", 1 );
}
elsif($status[1] eq 'temperature'){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $status[3];
$status[3] =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
readingsSingleUpdate($hash, "temperature", "$status[3]", 1 );
}
elsif($status[1] eq 'humidity'){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $status[3];
$status[3] =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
readingsSingleUpdate($hash, "humidity", "$status[3]", 1 );
}
elsif ($decoded->{'cmd'} eq 'heartbeat'){
readingsSingleUpdate($hash, $decoded->{'sid'}, 'heartbeat', 1 );
}
XiaomiSmartHome_Device_update($hash);
return $hash->{NAME};
}
#####################################
sub XiaomiSmartHome_Device_Parse($$) {
my ($io_hash, $msg) = @_;
@@ -49,38 +108,19 @@ sub XiaomiSmartHome_Device_Parse($$) {
my $sid = $decoded->{'sid'};
my $model = $decoded->{'model'};
my $name = $io_hash->{NAME};
if (my $hash = $modules{XiaomiSmartHome_Device}{defptr}{$sid})
{
my $name = $hash->{NAME};
Log3 $name, 5, "$name: SID: " . $hash->{SID} . " " . $hash->{TYPE};
my @status = split('\"', $decoded->{'data'});
if ($status[1] eq 'status'){
Log3 $name, 3, "$name: Sensor: " . $hash->{MODEL} . " SID: " . $sid . " Status: " . $status[3];
readingsSingleUpdate($hash, "state", "$status[3]", 1 );
}
elsif($status[1] eq 'voltage'){
Log3 $name, 3, "$name: Sensor: " . $hash->{MODEL} . " SID: " . $sid . " Voltage: " . $status[3];
readingsSingleUpdate($hash, "voltage", "$status[3]", 1 );
}
elsif($status[1] eq 'temperature'){
Log3 $name, 3, "$name: Sensor: " . $hash->{MODEL} . " SID: " . $sid . " Temperature: " . $status[3];
$status[3] =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
readingsSingleUpdate($hash, "temperature", "$status[3]", 1 );
}
elsif($status[1] eq 'humidity'){
Log3 $name, 3, "$name: Sensor: " . $hash->{MODEL} . " SID: " . $sid . " Humidity: " . $status[3];
$status[3] =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
readingsSingleUpdate($hash, "humidity", "$status[3]", 1 );
}
XiaomiSmartHome_Device_update($hash);
return $hash->{NAME};
Log3 $name, 4, "$name> IS DEFINED " . $model . " : " .$sid;
XiaomiSmartHome_Device_Read($hash, $msg, $name);
}
else
{
return "UNDEFINED $sid XiaomiSmartHome_Device $model $sid";
}
Log3 $name, 4, "$name> UNDEFINED " . $model . " : " .$sid;
return "UNDEFINED $sid XiaomiSmartHome_Device $model $name";
}
}
#####################################
@@ -107,25 +147,69 @@ sub XiaomiSmartHome_Device_update($){
sub XiaomiSmartHome_Device_Define($$) {
my ($hash, $def) = @_;
my ($name, $modul, $type, $sid) = split("[ \t]+", $def);
Log3 $name, 3, "$name: $modul $type $sid";
$hash->{TYPE} = $modul;
my ($name, $modul, $type, $iodev) = split("[ \t]+", $def);
#Log3 "test", 3, "Define status = " . $status;
$hash->{TYPE} = $modul;
$hash->{MODEL} = $type;
$hash->{SID} = $sid;
$hash->{NAME} = $sid;
$hash->{STATE} = "initialized";
$modules{XiaomiSmartHome_Device}{defptr}{$sid} = $hash;
AssignIoPort($hash);
$hash->{SID} = $name;
$hash->{NAME} = $name;
$hash->{VERSION} = $version;
$hash->{STATE} = 'initialized';
$modules{XiaomiSmartHome_Device}{defptr}{$name} = $hash;
AssignIoPort($hash,$iodev);
if(defined($hash->{IODev}->{NAME})) {
my $IOname = $hash->{IODev}->{NAME};
Log3 $name, 3, $IOname . "> " .$name. ": " . $type . " I/O device is " . $hash->{IODev}->{NAME};
} else {
Log3 $name, 1, "$name $type - no I/O device";
}
$iodev = $hash->{IODev}->{NAME};
my $d = $modules{XiaomiSmartHome_Device}{defptr}{$name};
return "XiaomiSmartHome device $hash->{SID} on XiaomiSmartHome $iodev already defined as $d->{NAME}." if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $name );
Log3 $name, 3, $iodev . "> " . $name . "- defined with Code: ". $hash->{DEV};
$attr{$name}{room} = "MiSmartHome" if( !defined( $attr{$name}{room} ) );
if( $type eq 'motion') {
$attr{$name}{devStateIcon} = 'motion:motion_detector@red off:motion_detector@green' if( !defined( $attr{$name}{devStateIcon} ) );
}
elsif ( $type eq 'magnet') {
$attr{$name}{devStateIcon} = 'open:fts_door_open@red close:fts_door@green' if( !defined( $attr{$name}{devStateIcon} ) );
}
elsif ( $type eq 'sensor_ht') {
$attr{$name}{stateFormat} = 'temperature °C, humidity %' if( !defined( $attr{$name}{stateFormat} ) );
}
if( $init_done ) {
InternalTimer( gettimeofday()+int(rand(2)), "XiaomiSmartHome_Device_updateSReading", $hash, 0 );
Log3 $name, 4, $iodev . "> " . $name . " Init Done set InternalTimer for Update";
}
}
#####################################
sub XiaomiSmartHome_Device_updateSReading($) {
my $hash = shift;
#my $name = $hash->{NAME};
#Log3 $name, 3, $name . " Updae SR";
IOWrite($hash,'read',"$hash->{SID}");
}
#####################################
#####################################
sub XiaomiSmartHome_Device_Undef($)
{
my ($hash, $arg) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
delete($modules{XiaomiSmartHome_Device}{defptr}{$hash->{SID}});
return undef;
my $code = $hash->{IODev}->{NAME} if( defined($hash->{IODev}->{NAME}) );
delete($modules{HEOSPlayer}{defptr}{$code});
Log3 $name, 3, "$code> $name - device deleted";
return undef;
}
1;
#####################################