-fix: heartbeat no longer report status again

This commit is contained in:
2017-10-25 10:26:59 +02:00
parent cdde04b39d
commit 9caee3a95f
3 changed files with 165 additions and 130 deletions

View File

@@ -40,6 +40,8 @@ eval "use Crypt::CBC";
return "\nERROR: Please install Crypt::CBC" if($@); return "\nERROR: Please install Crypt::CBC" if($@);
eval "use Net::Ping"; eval "use Net::Ping";
return "\nERROR: Please install Net::Ping" if($@); return "\nERROR: Please install Net::Ping" if($@);
eval "use Math::Round";
return "\nERROR: Please install libmath-round-perl" if($@);
use Color; use Color;
use SetExtensions; use SetExtensions;
@@ -49,7 +51,7 @@ use SetExtensions;
sub XiaomiSmartHome_Notify($$); sub XiaomiSmartHome_Notify($$);
sub XiaomiSmartHome_updateSingleReading($$); sub XiaomiSmartHome_updateSingleReading($$);
my $iv="\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58\x56\x2e"; my $iv="\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58\x56\x2e";
my $version = "1.08"; my $version = "1.12";
my %XiaomiSmartHome_gets = ( my %XiaomiSmartHome_gets = (
"getDevices" => ["get_id_list", '^.+get_id_list_ack' ], "getDevices" => ["get_id_list", '^.+get_id_list_ack' ],
@@ -64,6 +66,7 @@ my %sets = (
"volume:slider,1,1,100" => 1, "volume:slider,1,1,100" => 1,
"ringtone:0,1,2,3,4,5,6,7,8,13,21,22,23,24,25,26,27,28,29,10000,10001" => 1, "ringtone:0,1,2,3,4,5,6,7,8,13,21,22,23,24,25,26,27,28,29,10000,10001" => 1,
"ringvol" => 2, "ringvol" => 2,
"learn" => 0,
); );
@@ -102,7 +105,8 @@ sub XiaomiSmartHome_Initialize($) {
"16:XiaomiSmartHome_Device" => "^.+natgas", "16:XiaomiSmartHome_Device" => "^.+natgas",
"17:XiaomiSmartHome_Device" => "^.+smoke", "17:XiaomiSmartHome_Device" => "^.+smoke",
"18:XiaomiSmartHome_Device" => "^.+weather.v1", "18:XiaomiSmartHome_Device" => "^.+weather.v1",
"19:XiaomiSmartHome_Device" => "^.+sensor_motion.aq2"}; "19:XiaomiSmartHome_Device" => "^.+sensor_motion.aq2",
"20:XiaomiSmartHome_Device" => "^.+sensor_wleak.aq1"};
FHEM_colorpickerInit(); FHEM_colorpickerInit();
} }
##################################### #####################################
@@ -344,7 +348,7 @@ sub XiaomiSmartHome_Define($$) {
eval { eval {
$GATEWAY_IP = inet_ntoa(inet_aton($hash->{GATEWAY})) ; $GATEWAY_IP = inet_ntoa(inet_aton($hash->{GATEWAY})) ;
$hash->{GATEWAY_IP} = $GATEWAY_IP; $hash->{GATEWAY_IP} = $GATEWAY_IP;
Log3 $name, 1, "$name: Define> Set GATEWAYs IP: " . $GATEWAY_IP; Log3 $name, 5, "$name: Define> Set GATEWAYs IP: " . $GATEWAY_IP;
}; };
if ($@) { if ($@) {
Log3 $name, 1, "$name: Define> Error $@\n"; Log3 $name, 1, "$name: Define> Error $@\n";
@@ -475,6 +479,11 @@ sub XiaomiSmartHome_Write($@)
{ {
$msg = '{"cmd":"write","model":"curtain","sid":"' . $iohash->{SID} . '","data":"{\"curtain_level\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }'; $msg = '{"cmd":"write","model":"curtain","sid":"' . $iohash->{SID} . '","data":"{\"curtain_level\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
} }
if ($cmd eq 'learn')
{
my $t = '"yes"';
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{SID} . '","data":"{\"join_permission\":' . $t . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
} }
} }
return Log3 $name, 4, "$name: Write> - socket not connected" unless($hash->{CD}); return Log3 $name, 4, "$name: Write> - socket not connected" unless($hash->{CD});
@@ -620,6 +629,10 @@ sub XiaomiSmartHome_Set($@)
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
} }
elsif($cmd eq "learn")
{
XiaomiSmartHome_Write($hash,'learn', 1);
}
elsif($cmd eq "pct") elsif($cmd eq "pct")
{ {
my $ownName = $hash->{NAME}; my $ownName = $hash->{NAME};
@@ -710,7 +723,7 @@ sub XiaomiSmartHome_connect($)
$sock->setsockopt(SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', 30, 0)) or die "setsockopt: $!"; $sock->setsockopt(SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', 30, 0)) or die "setsockopt: $!";
if ($sock) if ($sock)
{ {
Log3 $name, 3, "$name: connect> Connected"; Log3 $name, 4, "$name: connect> Connected";
$sock->mcast_add('224.0.0.50', $hash->{fhemIP} ) || die "Couldn't set group: $!\n"; #$hash->{fhemIP} $sock->mcast_add('224.0.0.50', $hash->{fhemIP} ) || die "Couldn't set group: $!\n"; #$hash->{fhemIP}
$sock->mcast_ttl(32); $sock->mcast_ttl(32);
$sock->mcast_loopback(1); $sock->mcast_loopback(1);
@@ -748,7 +761,7 @@ sub XiaomiSmartHome_disconnect($)
} }
return if (!$hash->{CD}); return if (!$hash->{CD});
Log3 $name, 3, "$name: disconnect> disconnecting"; Log3 $name, 1, "$name: disconnect> disconnecting";
close($hash->{CD}) if($hash->{CD}); close($hash->{CD}) if($hash->{CD});
delete($hash->{FD}); delete($hash->{FD});
@@ -872,6 +885,7 @@ sub XiaomiSmartHome_updateAllReadings($)
<li>ctrl_neutral2: Double bond ignition switch</li> <li>ctrl_neutral2: Double bond ignition switch</li>
<li>rgbw_light: Smart lights (report only)</li> <li>rgbw_light: Smart lights (report only)</li>
<li>curtain: Curtain (Control only if device has reporte curtain_level)</li> <li>curtain: Curtain (Control only if device has reporte curtain_level)</li>
<li>water: water detector</li>
<li>smoke: smoke alarm detector</li> <li>smoke: smoke alarm detector</li>
<ul> <ul>
<li>0: disarm</li> <li>0: disarm</li>
@@ -970,6 +984,7 @@ sub XiaomiSmartHome_updateAllReadings($)
<li>ctrl_neutral2: Doppelter Wandschalter schaltbar</li> <li>ctrl_neutral2: Doppelter Wandschalter schaltbar</li>
<li>rgbw_light: RBGW Lampe (nur Anzeige)</li> <li>rgbw_light: RBGW Lampe (nur Anzeige)</li>
<li>curtain: Vorhangmotor (ohne das das device den curtain_level gemeldet hat ist ein steuern nicht möglich)</li> <li>curtain: Vorhangmotor (ohne das das device den curtain_level gemeldet hat ist ein steuern nicht möglich)</li>
<li>water: Wasser Sensor</li>
<li>smoke: Rauchmelder</li> <li>smoke: Rauchmelder</li>
<ul> <ul>
<li>0: disarm</li> <li>0: disarm</li>

View File

@@ -24,8 +24,9 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Math::Round qw/nearest/;
my $version = "1.08"; my $version = "1.13";
sub XiaomiSmartHome_Device_updateSReading($); sub XiaomiSmartHome_Device_updateSReading($);
@@ -35,7 +36,7 @@ sub XiaomiSmartHome_Device_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{Match} = ".*magnet.*|.*motion.*|sensor_ht|.*switch.*|plug|cube|86sw1|86sw2|ctrl_neutral1|ctrl_neutral2|rgbw_light|curtain|ctrl_ln1|ctrl_ln2|86plug|natgas|smoke|weather.v1"; $hash->{Match} = ".*magnet.*|.*motion.*|sensor_ht|.*switch.*|plug|cube|86sw1|86sw2|ctrl_neutral1|ctrl_neutral2|rgbw_light|curtain|ctrl_ln1|ctrl_ln2|86plug|natgas|smoke|weather.v1|sensor_wleak.aq1";
$hash->{DefFn} = "XiaomiSmartHome_Device_Define"; $hash->{DefFn} = "XiaomiSmartHome_Device_Define";
$hash->{SetFn} = "XiaomiSmartHome_Device_Set"; $hash->{SetFn} = "XiaomiSmartHome_Device_Set";
$hash->{UndefFn} = "XiaomiSmartHome_Device_Undef"; $hash->{UndefFn} = "XiaomiSmartHome_Device_Undef";
@@ -43,6 +44,7 @@ sub XiaomiSmartHome_Device_Initialize($)
$hash->{AttrList} = "IODev follow-on-for-timer:1,0 follow-on-timer ". $hash->{AttrList} = "IODev follow-on-for-timer:1,0 follow-on-timer ".
"do_not_notify:1,0 ignore:1,0 dummy:1,0 showtime:1,0 valueFn:textField-long ". "do_not_notify:1,0 ignore:1,0 dummy:1,0 showtime:1,0 valueFn:textField-long ".
"round:1,2,3 ".
$readingFnAttributes ; $readingFnAttributes ;
} }
##################################### #####################################
@@ -190,6 +192,8 @@ sub XiaomiSmartHome_Device_on_timeout($){
##################################### #####################################
sub XiaomiSmartHome_Device_Read($$$){ sub XiaomiSmartHome_Device_Read($$$){
my ($hash, $msg, $name) = @_; my ($hash, $msg, $name) = @_;
my @arround = ("0.1","0.01","0.001");
my $round = AttrVal( $hash->{NAME}, "round", "2" );
my $decoded = eval{decode_json($msg)}; my $decoded = eval{decode_json($msg)};
if ($@) { if ($@) {
Log3 $name, 1, "$name: DEV_Read> Error while request: $@"; Log3 $name, 1, "$name: DEV_Read> Error while request: $@";
@@ -204,128 +208,143 @@ sub XiaomiSmartHome_Device_Read($$$){
return; return;
} }
readingsBeginUpdate( $hash ); readingsBeginUpdate( $hash );
if (defined $data->{status}){ if ($data->{cmd} ne 'heartbeat'){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status}; if (defined $data->{status}){
readingsBulkUpdate($hash, "state", "$data->{status}", 1 ); Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status};
if ($data->{status} eq 'motion' && $hash->{MODEL} =~ /motion/){ readingsBulkUpdate($hash, "state", "$data->{status}", 1 );
readingsBulkUpdate($hash, "no_motion", "0", 1 ); if ($data->{status} eq 'motion' && $hash->{MODEL} =~ /motion/){
} readingsBulkUpdate($hash, "no_motion", "0", 1 );
if ($data->{status} eq 'close' && $hash->{MODEL} =~ /magnet/){ }
readingsBulkUpdate($hash, "no_close", "0", 1 ); if ($data->{status} eq 'close' && $hash->{MODEL} =~ /magnet/){
readingsBulkUpdate($hash, "no_close", "0", 1 );
}
} }
if (defined $data->{no_motion}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion};
readingsBulkUpdate($hash, "no_motion", "$data->{no_motion}", 1 );
}
if (defined $data->{no_close}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close};
readingsBulkUpdate($hash, "no_close", "$data->{no_close}", 1 );
}
if (defined $data->{voltage}){
my $bat = ($data->{voltage}/1000);
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
readingsBulkUpdate($hash, "battery", $bat, 1 );
if ($bat < 2.2) {
readingsBulkUpdate($hash, "batterystate", "low", 1 );
}
else {
readingsBulkUpdate($hash, "batterystate", "ok", 1 )
}
}
if (defined $data->{temperature}){
my $temp = $data->{temperature};
$temp =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{3}(?=\d))/$1./g;
$temp = nearest(@arround[$round-1] ,$temp );
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp . " Round: " . $round;
readingsBulkUpdate($hash, "temperature", "$temp", 1 );
}
if (defined $data->{humidity}){
my $hum = $data->{humidity};
$hum =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
$hum = nearest(@arround[$round-1] ,$hum );
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum;
readingsBulkUpdate($hash, "humidity", "$hum", 1 );
}
if (defined $data->{pressure}){
my $pres = $data->{pressure};
$pres =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Pressure: " . $pres;
readingsBulkUpdate($hash, "pressure", "$pres", 1 );
}
if (defined $data->{lux}){
my $lux = $data->{lux};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Illuminance: " . $lux;
readingsBulkUpdate($hash, "lux", "$lux", 1 );
}
#86sw1 + 86sw2 + ctrl_neutral1 + ctrl_neutral2 start
if (defined $data->{channel_0}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_0: " . $data->{channel_0};
readingsBulkUpdate($hash, "channel_0", "$data->{channel_0}", 1 );
}
if (defined $data->{channel_1}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_1: " . $data->{channel_1};
readingsBulkUpdate($hash, "channel_1", "$data->{channel_1}", 1 );
}
#86sw1 + 86sw2 + ctrl_neutral1 + ctrl_neutral2 end
#plug & 86plug start
if (defined $data->{load_voltage}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage};
readingsBulkUpdate($hash, "LOAD_Voltage", "$data->{load_voltage}", 1 );
}
if (defined $data->{load_power}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power};
readingsBulkUpdate($hash, "LOAD_Power", "$data->{load_power}", 1 );
}
if (defined $data->{power_consumed}){
Log3 $name, 4, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed};
readingsBulkUpdate($hash, "POWER_Consumed", "$data->{power_consumed}", 1 );
}
if (defined $data->{inuse}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $data->{inuse};
readingsBulkUpdate($hash, "inuse", "$data->{inuse}", 1 );
}
#plug & 86plug end
#rgbw_light start
if (defined $data->{level}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Level: " . $data->{level};
readingsBulkUpdate($hash, "level", "$data->{level}", 1 );
}
if (defined $data->{hue}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Hue: " . $data->{hue};
readingsBulkUpdate($hash, "hue", "$data->{hue}", 1 );
}
if (defined $data->{saturation}){
Log3 $name, 4, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Saturation: " . $data->{saturation};
readingsBulkUpdate($hash, "saturation", "$data->{saturation}", 1 );
}
if (defined $data->{color_temperature}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Color_temperature: " . $data->{color_temperature};
readingsBulkUpdate($hash, "color_temperature", "$data->{color_temperature}", 1 );
}
if (defined $data->{x}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " X: " . $data->{x};
readingsBulkUpdate($hash, "x", "$data->{x}", 1 );
}
if (defined $data->{y}){
Log3 $name, 4, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Y: " . $data->{y};
readingsBulkUpdate($hash, "y", "$data->{y}", 1 );
}
#rgbw_light end
#cube start
if (defined $data->{rotate}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{rotate};
readingsBulkUpdate($hash, "rotate", "$data->{rotate}", 1 );
readingsBulkUpdate($hash, "state", "rotate", 1 );
}
#cube end
#smoke & natgast start
if (defined $data->{alarm}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Alarm: " . $data->{alarm};
readingsBulkUpdate($hash, "alarm", "$data->{alarm}", 1 );
}
if (defined $data->{density}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Density: " . $data->{density};
readingsBulkUpdate($hash, "density", "$data->{density}", 1 );
}
#smoke & natgast end
#curtain start
if (defined $data->{curtain_level}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Curtain: " . $data->{curtain_level};
readingsBulkUpdate($hash, "arlarm", "$data->{curtain_level}", 1 );
}
#curtain end
} }
if (defined $data->{no_motion}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion};
readingsBulkUpdate($hash, "no_motion", "$data->{no_motion}", 1 );
}
if (defined $data->{no_close}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close};
readingsBulkUpdate($hash, "no_close", "$data->{no_close}", 1 );
}
if (defined $data->{voltage}){
my $bat = ($data->{voltage}/1000);
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
readingsBulkUpdate($hash, "battery", $bat, 1 );
}
if (defined $data->{temperature}){
my $temp = $data->{temperature};
$temp =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp;
readingsBulkUpdate($hash, "temperature", "$temp", 1 );
}
if (defined $data->{humidity}){
my $hum = $data->{humidity};
$hum =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum;
readingsBulkUpdate($hash, "humidity", "$hum", 1 );
}
if (defined $data->{pressure}){
my $pres = $data->{pressure};
$pres =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Pressure: " . $pres;
readingsBulkUpdate($hash, "pressure", "$pres", 1 );
}
if (defined $data->{lux}){
my $lux = $data->{lux};
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Illuminance: " . $lux;
readingsBulkUpdate($hash, "lux", "$lux", 1 );
}
#86sw1 + 86sw2 + ctrl_neutral1 + ctrl_neutral2 start
if (defined $data->{channel_0}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_0: " . $data->{channel_0};
readingsBulkUpdate($hash, "channel_0", "$data->{channel_0}", 1 );
}
if (defined $data->{channel_1}){
Log3 $name, 3, "$name: DEV_Read>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Channel_1: " . $data->{channel_1};
readingsBulkUpdate($hash, "channel_1", "$data->{channel_1}", 1 );
}
#86sw1 + 86sw2 + ctrl_neutral1 + ctrl_neutral2 end
#plug & 86plug start
if (defined $data->{load_voltage}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage};
readingsBulkUpdate($hash, "LOAD_Voltage", "$data->{load_voltage}", 1 );
}
if (defined $data->{load_power}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power};
readingsBulkUpdate($hash, "LOAD_Power", "$data->{load_power}", 1 );
}
if (defined $data->{power_consumed}){
Log3 $name, 3, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed};
readingsBulkUpdate($hash, "POWER_Consumed", "$data->{power_consumed}", 1 );
}
if (defined $data->{inuse}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $data->{inuse};
readingsBulkUpdate($hash, "inuse", "$data->{inuse}", 1 );
}
#plug & 86plug end
#rgbw_light start
if (defined $data->{level}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Level: " . $data->{level};
readingsBulkUpdate($hash, "level", "$data->{level}", 1 );
}
if (defined $data->{hue}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Hue: " . $data->{hue};
readingsBulkUpdate($hash, "hue", "$data->{hue}", 1 );
}
if (defined $data->{saturation}){
Log3 $name, 3, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Saturation: " . $data->{saturation};
readingsBulkUpdate($hash, "saturation", "$data->{saturation}", 1 );
}
if (defined $data->{color_temperature}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Color_temperature: " . $data->{color_temperature};
readingsBulkUpdate($hash, "color_temperature", "$data->{color_temperature}", 1 );
}
if (defined $data->{x}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " X: " . $data->{x};
readingsBulkUpdate($hash, "x", "$data->{x}", 1 );
}
if (defined $data->{y}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Y: " . $data->{y};
readingsBulkUpdate($hash, "y", "$data->{y}", 1 );
}
#rgbw_light end
#cube start
if (defined $data->{rotate}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{rotate};
readingsBulkUpdate($hash, "rotate", "$data->{rotate}", 1 );
readingsBulkUpdate($hash, "state", "rotate", 1 );
}
#cube end
#smoke & natgast start
if (defined $data->{alarm}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{arlarm};
readingsBulkUpdate($hash, "arlarm", "$data->{arlarm}", 1 );
}
#smoke & natgast end
#curtain start
if (defined $data->{curtain_level}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{curtain_level};
readingsBulkUpdate($hash, "arlarm", "$data->{curtain_level}", 1 );
}
#curtain end
if ($decoded->{'cmd'} eq 'heartbeat'){ if ($decoded->{'cmd'} eq 'heartbeat'){
readingsBulkUpdate($hash, 'heartbeat', $decoded->{'sid'} , 1 ); readingsBulkUpdate($hash, 'heartbeat', $decoded->{'sid'} , 1 );
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
XiaomiSmartHome_Device_update($hash); XiaomiSmartHome_Device_update($hash);
return $hash->{NAME}; return $hash->{NAME};
@@ -369,7 +388,7 @@ sub XiaomiSmartHome_Device_update($){
my $LASTCMD = ReadingsVal($name,"lastCmd",undef); my $LASTCMD = ReadingsVal($name,"lastCmd",undef);
my $value_fn = eval $value_fn; my $value_fn = eval $value_fn;
Log3 $name, 3, $name .": DEV_Update valueFn: ". $@ if($@); Log3 $name, 4, $name .": DEV_Update valueFn: ". $@ if($@);
return undef if( !defined($value_fn) ); return undef if( !defined($value_fn) );
} }
if( $model =~ /motion/) { if( $model =~ /motion/) {
@@ -392,6 +411,7 @@ sub XiaomiSmartHome_Device_Define($$) {
$hash->{STATE} = 'initialized'; $hash->{STATE} = 'initialized';
$modules{XiaomiSmartHome_Device}{defptr}{$sid} = $hash; $modules{XiaomiSmartHome_Device}{defptr}{$sid} = $hash;
AssignIoPort($hash,$iodev); AssignIoPort($hash,$iodev);
my $room = $attr{$iodev}{room};
if(defined($hash->{IODev}->{NAME})) { if(defined($hash->{IODev}->{NAME})) {
my $IOname = $hash->{IODev}->{NAME}; my $IOname = $hash->{IODev}->{NAME};
@@ -406,7 +426,7 @@ sub XiaomiSmartHome_Device_Define($$) {
return "XiaomiSmartHome device $hash->{SID} on XiaomiSmartHome $iodev already defined as $d->{NAME}." if( defined($d) && $d->{IODev} == $hash->{IODev} && $d->{NAME} ne $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, 4, $iodev . ": DEV_Define> " . $name . ": defined as ". $hash->{MODEL}; Log3 $name, 4, $iodev . ": DEV_Define> " . $name . ": defined as ". $hash->{MODEL};
$attr{$name}{room} = "MiSmartHome" if( !defined( $attr{$name}{room} ) ); $attr{$name}{room} = $room if( !defined( $attr{$name}{room} ) );
if( $type =~ /motion/) { if( $type =~ /motion/) {
$attr{$name}{devStateIcon} = 'motion:motion_detector@red off:motion_detector@green no_motion:motion_detector@green' if( !defined( $attr{$name}{devStateIcon} ) ); $attr{$name}{devStateIcon} = 'motion:motion_detector@red off:motion_detector@green no_motion:motion_detector@green' if( !defined( $attr{$name}{devStateIcon} ) );
} }
@@ -431,7 +451,7 @@ sub XiaomiSmartHome_Device_updateSReading($) {
my $hash = shift; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3 $name, 3, "$name: DEV_updateSReading> for $hash->{SID}"; Log3 $name, 4, "$name: DEV_updateSReading> for $hash->{SID}";
RemoveInternalTimer($hash,'XiaomiSmartHome_Device_updateSReading'); RemoveInternalTimer($hash,'XiaomiSmartHome_Device_updateSReading');
IOWrite($hash,'read',"$hash->{SID}"); IOWrite($hash,'read',"$hash->{SID}");
} }
@@ -445,7 +465,7 @@ sub XiaomiSmartHome_Device_Undef($)
my $iodev = $hash->{IODev}->{NAME}; my $iodev = $hash->{IODev}->{NAME};
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
delete($modules{XiaomiSmartHome_Device}{defptr}{$hash->{SID}}); delete($modules{XiaomiSmartHome_Device}{defptr}{$hash->{SID}});
Log3 $name, 3, "$iodev: DEV_Undef> $name - device deleted"; Log3 $name, 4, "$iodev: DEV_Undef> $name - device deleted";
return undef; return undef;
} }

View File

@@ -1,2 +1,2 @@
UPD 2017-08-13_17:46:25 36385 FHEM/71_XiaomiSmartHome.pm UPD 2017-10-24_08:55:31 36922 FHEM/71_XiaomiSmartHome.pm
UPD 2017-08-13_17:46:25 24517 FHEM/71_XiaomiSmartHome_Device.pm UPD 2017-10-24_08:44:58 25216 FHEM/71_XiaomiSmartHome_Device.pm