some fix, add support for plug an cube

fix: RGB message didnt show anymore
add: readings and support for the cube sensor
add: support for plug, PRE switch on / off
This commit is contained in:
2017-04-03 10:54:20 +02:00
parent aac105544a
commit 90dc028c89
3 changed files with 84 additions and 35 deletions

View File

@@ -36,7 +36,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 = "0.08"; my $version = "0.09";
my %XiaomiSmartHome_gets = ( my %XiaomiSmartHome_gets = (
"getDevices" => ["get_id_list", '^.+get_id_list_ack' ], "getDevices" => ["get_id_list", '^.+get_id_list_ack' ],
@@ -73,7 +73,8 @@ sub XiaomiSmartHome_Initialize($) {
"2:XiaomiSmartHome_Device" => "^.+motion", "2:XiaomiSmartHome_Device" => "^.+motion",
"3:XiaomiSmartHome_Device" => "^.+sensor_ht", "3:XiaomiSmartHome_Device" => "^.+sensor_ht",
"4:XiaomiSmartHome_Device" => "^.+switch", "4:XiaomiSmartHome_Device" => "^.+switch",
"5:XiaomiSmartHome_Device" => "^.+plug"}; "5:XiaomiSmartHome_Device" => "^.+cube",
"6:XiaomiSmartHome_Device" => "^.+plug"};
FHEM_colorpickerInit(); FHEM_colorpickerInit();
} }
##################################### #####################################
@@ -96,23 +97,25 @@ sub XiaomiSmartHome_Read($) {
if ($json) if ($json)
{ {
Log3 $name, 5, "$name> Read:" . $buf; Log3 $name, 5, "$name> Read:" . $buf;
readingsBeginUpdate( $hash );
if ($decoded->{'model'} eq 'gateway'){ if ($decoded->{'model'} eq 'gateway'){
if ($decoded->{'cmd'} eq 'report'){ if ($decoded->{'cmd'} eq 'report'){
my $data = decode_json($decoded->{data}); my $data = decode_json($decoded->{data});
if (defined $data->{rgb}){ if (defined $data->{rgb}){
Log3 $name, 4, "$name>" . " SID: " . $decoded->{'sid'} . " Type: Gateway" . " RGB: " . $data->{rgb} ; Log3 $name, 4, "$name>" . " SID: " . $decoded->{'sid'} . " Type: Gateway" . " RGB: " . $data->{rgb} ;
readingsSingleUpdate($hash, "RGB", $data->{rgb} , 1 ); readingsBulkUpdate($hash, "RGB", $data->{rgb} , 1 );
} }
} }
elsif ($decoded->{'cmd'} eq 'heartbeat'){ elsif ($decoded->{'cmd'} eq 'heartbeat'){
readingsSingleUpdate($hash, 'HEARTBEAT', $decoded->{'sid'}, 1 ); readingsBulkUpdate($hash, 'HEARTBEAT', $decoded->{'sid'}, 1 );
readingsSingleUpdate($hash, 'token', $decoded->{'token'}, 1 ); readingsBulkUpdate($hash, 'token', $decoded->{'token'}, 1 );
} }
} }
else { else {
Log3 $name, 4, "$name> Dispatch! " . $buf; Log3 $name, 4, "$name> Dispatch! " . $buf;
Dispatch($hash, $buf, undef); Dispatch($hash, $buf, undef);
} }
readingsEndUpdate( $hash, 1 );
} }
} }
##################################### #####################################
@@ -169,29 +172,31 @@ sub XiaomiSmartHome_Undef($$) {
} }
##################################### #####################################
sub XiaomiSmartHome_Write($$$) sub XiaomiSmartHome_Write($@)
{ {
my ($hash,$cmd,$val) = @_; my ($hash,$cmd,$val,$iohash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $msg; my $msg;
if ($cmd eq 'read') if ($cmd eq 'read')
{ {
$msg = '{"cmd":"' .$cmd . '","sid":"' . $val . '"}'; $msg = '{"cmd":"' .$cmd . '","sid":"' . $val . '"}';
} }
elsif ($cmd eq 'rgb') if ($cmd eq 'rgb')
{ {
# TODO SID des Gateway nicht im und aus dem Reading!! # TODO SID des Gateway nicht im und aus dem Reading!!
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }'; $msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"rgb\":' . $val . ',\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
} }
elsif ($cmd eq 'illumination') if ($cmd eq 'illumination')
{ {
# TODO SID des Gateway nicht im und aus dem Reading!! # TODO SID des Gateway nicht im und aus dem Reading!!
#$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"illumination\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }'; #$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"illumination\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"mid\":\"3\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }'; $msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"mid\":\"3\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
#$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"hue\":\"170\",\"saturation\":\"254\", \"color_temperature\":\"65279\", \"x\":\"10\", \"y\":\"10\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }'; #$msg = '{"cmd":"write","model":"gateway","sid":"' . $hash->{READINGS}{HEARTBEAT}{VAL} . '","short_id":0,"key":"8","data":"{\"hue\":\"170\",\"saturation\":\"254\", \"color_temperature\":\"65279\", \"x\":\"10\", \"y\":\"10\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
}
if ($cmd eq 'power')
{
$msg = '{"cmd":"write","model":"plug","sid":"' . $iohash->{SID} . '","data":"{\"status\":\"' . $val . '\",\"key\":\"'. XiaomiSmartHome_EncryptKey($hash) .'\"}" }';
} }
return Log3 $name, 4, "Master ($name) - socket not connected" unless($hash->{CD}); return Log3 $name, 4, "Master ($name) - socket not connected" unless($hash->{CD});
@@ -294,23 +299,25 @@ sub XiaomiSmartHome_Set($@)
{ {
$hash->{helper}{prevrgbvalue} = $hash->{READINGS}{rgb}{VAL}; $hash->{helper}{prevrgbvalue} = $hash->{READINGS}{rgb}{VAL};
readingsSingleUpdate( $hash, 'state', 'off', 1 ); readingsSingleUpdate( $hash, 'state', 'off', 1 );
readingsSingleUpdate( $hash, 'rgb', 'off', 1 ); readingsSingleUpdate( $hash, 'rgb', 'off', 0 );
XiaomiSmartHome_Write($hash,'rgb', 0); XiaomiSmartHome_Write($hash,'rgb', 0);
} }
elsif($cmd eq "on") elsif($cmd eq "on")
{ {
readingsSingleUpdate( $hash, 'state', 'on', 1 ); readingsBeginUpdate( $hash );
readingsBulkUpdate( $hash, 'state', 'on', 1 );
if ($hash->{helper}{prevrgbvalue}) if ($hash->{helper}{prevrgbvalue})
{ {
$dec_num = sprintf("%d", hex('ff' . $hash->{helper}{prevrgbvalue})); $dec_num = sprintf("%d", hex('ff' . $hash->{helper}{prevrgbvalue}));
readingsSingleUpdate( $hash, 'rgb', $hash->{helper}{prevrgbvalue}, 1 ); readingsBulkUpdate( $hash, 'rgb', $hash->{helper}{prevrgbvalue}, 1 );
XiaomiSmartHome_Write($hash,'rgb', $dec_num); XiaomiSmartHome_Write($hash,'rgb', $dec_num);
} }
else else
{ {
readingsBulkUpdate( $hash, 'rgb', "00ff00", 1 );
XiaomiSmartHome_Write($hash,'rgb', 1677786880); XiaomiSmartHome_Write($hash,'rgb', 1677786880);
readingsSingleUpdate( $hash, 'rgb', '00ff00', 1 );
} }
readingsEndUpdate( $hash, 1 );
} }
elsif($cmd eq "pct") elsif($cmd eq "pct")
{ {

View File

@@ -25,7 +25,7 @@ package main;
use strict; use strict;
use warnings; use warnings;
my $version = "0.08"; my $version = "0.09";
sub XiaomiSmartHome_Device_updateSReading($); sub XiaomiSmartHome_Device_updateSReading($);
##################################### #####################################
@@ -34,9 +34,9 @@ sub XiaomiSmartHome_Device_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{Match} = "^.+magnet|motion|sensor_ht|switch|plug"; $hash->{Match} = "^.+magnet|motion|sensor_ht|switch|plug|cube";
$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";
$hash->{ParseFn} = "XiaomiSmartHome_Device_Parse"; $hash->{ParseFn} = "XiaomiSmartHome_Device_Parse";
@@ -57,6 +57,35 @@ sub XiaomiSmartHome_Device_mot($$)
} }
##################################### #####################################
sub XiaomiSmartHome_Device_Set($@)
{
my ( $hash, $name, $cmd, @args ) = @_;
return "\"set $name\" needs at least one argument" unless(defined($cmd));
my $setlist = "";
$setlist .= "power:on,off " if ($hash->{MODEL} eq 'plug');
if($cmd eq "power")
{
if($args[0] eq "on")
{
IOWrite($hash,"power","on",$hash);
}
elsif($args[0] eq "off")
{
IOWrite($hash,"power","off",$hash);
}
}
else
{
return "Unknown argument $cmd, choose one of $setlist";
}
}
#####################################
sub XiaomiSmartHome_Device_on_timeout($){ sub XiaomiSmartHome_Device_on_timeout($){
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{LASTInputDev}; my $name = $hash->{LASTInputDev};
@@ -74,56 +103,69 @@ sub XiaomiSmartHome_Device_Read($$$){
my $model = $decoded->{'model'}; my $model = $decoded->{'model'};
Log3 $name, 5, "$name: SID: " . $hash->{SID} . " " . $hash->{TYPE}; Log3 $name, 5, "$name: SID: " . $hash->{SID} . " " . $hash->{TYPE};
my $data = decode_json($decoded->{data}); my $data = decode_json($decoded->{data});
#my @status = split('\"', $decoded->{'data'}); readingsBeginUpdate( $hash );
if (defined $data->{status}){ if (defined $data->{status}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Status: " . $data->{status};
readingsSingleUpdate($hash, "state", "$data->{status}", 1 ); readingsBulkUpdate($hash, "state", "$data->{status}", 1 );
if ($data->{status} eq 'motion' && $hash->{MODEL} eq 'motion'){ if ($data->{status} eq 'motion' && $hash->{MODEL} eq 'motion'){
readingsSingleUpdate($hash, "no_motion", "0", 1 ); readingsBulkUpdate($hash, "no_motion", "0", 1 );
} }
if ($data->{status} eq 'close' && $hash->{MODEL} eq 'magnet'){ if ($data->{status} eq 'close' && $hash->{MODEL} eq 'magnet'){
readingsSingleUpdate($hash, "no_close", "0", 1 ); readingsBulkUpdate($hash, "no_close", "0", 1 );
}
} }
if(defined $data->{no_motion}){ if(defined $data->{no_motion}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_motion: " . $data->{no_motion};
readingsSingleUpdate($hash, "no_motion", "$data->{no_motion}", 1 ); readingsBulkUpdate($hash, "no_motion", "$data->{no_motion}", 1 );
} }
if(defined $data->{no_close}){ if(defined $data->{no_close}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " NO_close: " . $data->{no_close};
readingsSingleUpdate($hash, "no_close", "$data->{no_close}", 1 ); readingsBulkUpdate($hash, "no_close", "$data->{no_close}", 1 );
} }
if(defined $data->{voltage}){ if(defined $data->{voltage}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Voltage: " . $data->{voltage};
readingsSingleUpdate($hash, "voltage", "$data->{voltage}", 1 ); readingsBulkUpdate($hash, "voltage", "$data->{voltage}", 1 );
} }
if(defined $data->{temperature}){ if(defined $data->{temperature}){
my $temp = $data->{temperature}; my $temp = $data->{temperature};
$temp =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g; $temp =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Temperature: " . $temp;
readingsSingleUpdate($hash, "temperature", "$temp", 1 ); readingsBulkUpdate($hash, "temperature", "$temp", 1 );
} }
if(defined $data->{humidity}){ if(defined $data->{humidity}){
my $hum = $data->{humidity}; my $hum = $data->{humidity};
$hum =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g; $hum =~ s/(^[-+]?\d+?(?=(?>(?:\d{2})+)(?!\d))|\G\d{2}(?=\d))/$1./g;
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Humidity: " . $hum;
readingsSingleUpdate($hash, "humidity", "$hum", 1 ); readingsBulkUpdate($hash, "humidity", "$hum", 1 );
} }
#plug start
if(defined $data->{load_voltage}){ if(defined $data->{load_voltage}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Voltage: " . $data->{load_voltage};
readingsSingleUpdate($hash, "LOAD_Voltage", "$data->{load_voltage}", 1 ); readingsBulkUpdate($hash, "LOAD_Voltage", "$data->{load_voltage}", 1 );
} }
if(defined $data->{load_power}){ if(defined $data->{load_power}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " LOAD_Power: " . $data->{load_power};
readingsSingleUpdate($hash, "LOAD_Power", "$data->{load_power}", 1 ); readingsBulkUpdate($hash, "LOAD_Power", "$data->{load_power}", 1 );
} }
if(defined $data->{power_consumed}){ if(defined $data->{power_consumed}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed}; Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " POWER_Consumed: " . $data->{power_consumed};
readingsSingleUpdate($hash, "LOAD_Power", "$data->{power_consumed}", 1 ); readingsBulkUpdate($hash, "POWER_Consumed", "$data->{power_consumed}", 1 );
}
if(defined $data->{inuse}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " InUse: " . $data->{inuse};
readingsBulkUpdate($hash, "inuse", "$data->{inuse}", 1 );
}
#plug end
#cube start
if(defined $data->{rotate}){
Log3 $name, 3, "$name>" . " SID: " . $sid . " Type: " . $hash->{MODEL} . " Rotate: " . $data->{rotate};
readingsBulkUpdate($hash, "rotate", "$data->{rotate}", 1 );
}
#cube end
} }
if ($decoded->{'cmd'} eq 'heartbeat'){ if ($decoded->{'cmd'} eq 'heartbeat'){
readingsSingleUpdate($hash, 'heartbeat', $decoded->{'sid'} , 1 ); readingsBulkUpdate($hash, 'heartbeat', $decoded->{'sid'} , 1 );
} }
readingsEndUpdate( $hash, 1 );
XiaomiSmartHome_Device_update($hash); XiaomiSmartHome_Device_update($hash);
return $hash->{NAME}; return $hash->{NAME};

View File

@@ -1,2 +1,2 @@
UPD 2017-03-30_08:35:44 14962 FHEM/71_XiaomiSmartHome.pm UPD 2017-04-03_10:50:26 15333 FHEM/71_XiaomiSmartHome.pm
UPD 2017-03-30_08:33:08 8937 FHEM/71_XiaomiSmartHome_Device.pm UPD 2017-04-03_10:45:17 9916 FHEM/71_XiaomiSmartHome_Device.pm