New sensor wleak(water), learning sensors, some fixes and improvments

NEW PERL MODULE MATH ROUND IS NECESSARY
- added: new Wleak.aq1 Water sensor
- added: learn, learning sensors with fhem
- added: round, temperature and humidity can now be rounded
- added: new reading batterystate (low or ok)
- change: on verbose 3 only status messages will be logged
- change: new sensors will inherit his room from gateway
- fix: smoke and gas sensor copy and paste error
This commit is contained in:
2017-10-24 09:25:31 +02:00
parent a0f59fd96e
commit 3f9b1bc745
3 changed files with 63 additions and 31 deletions

View File

@@ -40,6 +40,8 @@ eval "use Crypt::CBC";
return "\nERROR: Please install Crypt::CBC" if($@);
eval "use Net::Ping";
return "\nERROR: Please install Net::Ping" if($@);
eval "use Math::Round";
return "\nERROR: Please install libmath-round-perl" if($@);
use Color;
use SetExtensions;
@@ -49,7 +51,7 @@ use SetExtensions;
sub XiaomiSmartHome_Notify($$);
sub XiaomiSmartHome_updateSingleReading($$);
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 = (
"getDevices" => ["get_id_list", '^.+get_id_list_ack' ],
@@ -64,6 +66,7 @@ my %sets = (
"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,
"ringvol" => 2,
"learn" => 0,
);
@@ -102,7 +105,8 @@ sub XiaomiSmartHome_Initialize($) {
"16:XiaomiSmartHome_Device" => "^.+natgas",
"17:XiaomiSmartHome_Device" => "^.+smoke",
"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();
}
#####################################
@@ -344,7 +348,7 @@ sub XiaomiSmartHome_Define($$) {
eval {
$GATEWAY_IP = inet_ntoa(inet_aton($hash->{GATEWAY})) ;
$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 ($@) {
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) .'\"}" }';
}
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});
@@ -620,6 +629,10 @@ sub XiaomiSmartHome_Set($@)
}
readingsEndUpdate( $hash, 1 );
}
elsif($cmd eq "learn")
{
XiaomiSmartHome_Write($hash,'learn', 1);
}
elsif($cmd eq "pct")
{
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: $!";
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_ttl(32);
$sock->mcast_loopback(1);
@@ -748,7 +761,7 @@ sub XiaomiSmartHome_disconnect($)
}
return if (!$hash->{CD});
Log3 $name, 3, "$name: disconnect> disconnecting";
Log3 $name, 1, "$name: disconnect> disconnecting";
close($hash->{CD}) if($hash->{CD});
delete($hash->{FD});
@@ -872,6 +885,7 @@ sub XiaomiSmartHome_updateAllReadings($)
<li>ctrl_neutral2: Double bond ignition switch</li>
<li>rgbw_light: Smart lights (report only)</li>
<li>curtain: Curtain (Control only if device has reporte curtain_level)</li>
<li>water: water detector</li>
<li>smoke: smoke alarm detector</li>
<ul>
<li>0: disarm</li>
@@ -970,6 +984,7 @@ sub XiaomiSmartHome_updateAllReadings($)
<li>ctrl_neutral2: Doppelter Wandschalter schaltbar</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>water: Wasser Sensor</li>
<li>smoke: Rauchmelder</li>
<ul>
<li>0: disarm</li>

View File

@@ -24,8 +24,9 @@ package main;
use strict;
use warnings;
use Math::Round qw/nearest/;
my $version = "1.08";
my $version = "1.12";
sub XiaomiSmartHome_Device_updateSReading($);
@@ -35,7 +36,7 @@ sub XiaomiSmartHome_Device_Initialize($)
{
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->{SetFn} = "XiaomiSmartHome_Device_Set";
$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 ".
"do_not_notify:1,0 ignore:1,0 dummy:1,0 showtime:1,0 valueFn:textField-long ".
"round:1,2,3 ".
$readingFnAttributes ;
}
#####################################
@@ -190,6 +192,8 @@ sub XiaomiSmartHome_Device_on_timeout($){
#####################################
sub XiaomiSmartHome_Device_Read($$$){
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)};
if ($@) {
Log3 $name, 1, "$name: DEV_Read> Error while request: $@";
@@ -215,27 +219,35 @@ sub XiaomiSmartHome_Device_Read($$$){
}
}
if (defined $data->{no_motion}){
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
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{2}(?=\d))/$1./g;
Log3 $name, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp;
$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 );
}
@@ -262,45 +274,45 @@ sub XiaomiSmartHome_Device_Read($$$){
#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};
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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $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, 3, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Level: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Hue: " . $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, 3, "$name:" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Saturation: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Color_temperature: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " X: " . $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, 3, "$name: DEV_Read>" . " Name: " . $hash->{NAME} . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Y: " . $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
@@ -313,13 +325,17 @@ sub XiaomiSmartHome_Device_Read($$$){
#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 );
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} . " Rotate: " . $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
@@ -369,7 +385,7 @@ sub XiaomiSmartHome_Device_update($){
my $LASTCMD = ReadingsVal($name,"lastCmd",undef);
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) );
}
if( $model =~ /motion/) {
@@ -392,6 +408,7 @@ sub XiaomiSmartHome_Device_Define($$) {
$hash->{STATE} = 'initialized';
$modules{XiaomiSmartHome_Device}{defptr}{$sid} = $hash;
AssignIoPort($hash,$iodev);
my $room = $attr{$iodev}{room};
if(defined($hash->{IODev}->{NAME})) {
my $IOname = $hash->{IODev}->{NAME};
@@ -406,7 +423,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 );
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/) {
$attr{$name}{devStateIcon} = 'motion:motion_detector@red off:motion_detector@green no_motion:motion_detector@green' if( !defined( $attr{$name}{devStateIcon} ) );
}
@@ -431,7 +448,7 @@ sub XiaomiSmartHome_Device_updateSReading($) {
my $hash = shift;
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');
IOWrite($hash,'read',"$hash->{SID}");
}
@@ -445,7 +462,7 @@ sub XiaomiSmartHome_Device_Undef($)
my $iodev = $hash->{IODev}->{NAME};
RemoveInternalTimer($hash);
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;
}

View File

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