From 317c1a87967c4193a65f08ba8e845851807abf0d Mon Sep 17 00:00:00 2001 From: risiko79 <> Date: Sun, 13 Nov 2016 18:07:45 +0000 Subject: [PATCH] 30_pilight_contact: new module to support contact sensors git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@12568 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- CHANGED | 1 + FHEM/10_pilight_ctrl.pm | 19 +++-- FHEM/30_pilight_contact.pm | 147 +++++++++++++++++++++++++++++++++++++ HISTORY | 3 + MAINTAINER.txt | 2 + 5 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 FHEM/30_pilight_contact.pm diff --git a/CHANGED b/CHANGED index 5633421e7..194056530 100644 --- a/CHANGED +++ b/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 30_pilight_contact: new module to handle pilight contact sensors - feature: 59_Weather: attribute disable - new: 10_EQ3BT: Support EQ3 Bluetooth thermostat define living.thermostat EQ3BT diff --git a/FHEM/10_pilight_ctrl.pm b/FHEM/10_pilight_ctrl.pm index 742c5ef0d..7fff010a0 100644 --- a/FHEM/10_pilight_ctrl.pm +++ b/FHEM/10_pilight_ctrl.pm @@ -44,6 +44,8 @@ # V 1.17 2016-06-28 - FIX: Experimental splice on scalar is now forbidden - use explizit array notation # V 1.18 2016-06-28 - NEW: support smoke sensors (protocol: secudo_smoke_sensor) # V 1.19 2016-09-20 - FIX: PERL WARNING: Subroutine from Blocking.pm redefined +# V 1.20 2016-10-27 - FIX: ContactAsSwitch protocol independend +# V 1.21 2016-11-13 - NEW: support contact sensors ############################################## package main; @@ -70,7 +72,8 @@ my %matchList = ( "1:pilight_switch" => "^PISWITCH", "2:pilight_dimmer" => "^PISWITCH|^PIDIMMER|^PISCREEN", "3:pilight_temp" => "^PITEMP", "4:pilight_raw" => "^PIRAW", - "5:pilight_smoke" => "^PISMOKE"); + "5:pilight_smoke" => "^PISMOKE", + "6:pilight_contact"=> "^PICONTACT"); my @idList = ("id","systemcode","gpio"); my @unitList = ("unit","unitcode","programcode"); @@ -99,7 +102,7 @@ sub pilight_ctrl_Initialize($) $hash->{StateFn} = "pilight_ctrl_State"; $hash->{AttrList}= "ignoreProtocol brands ContactAsSwitch SendTimeout ".$readingFnAttributes; - $hash->{Clients} = ":pilight_switch:pilight_dimmer:pilight_temp:pilight_raw::pilight_smoke:"; + $hash->{Clients} = ":pilight_switch:pilight_dimmer:pilight_temp:pilight_raw:pilight_smoke:pilight_contact:"; #$hash->{MatchList} = \%matchList; #only for autocreate } @@ -570,7 +573,7 @@ sub pilight_ctrl_createWhiteList($) foreach my $d (keys %defs) { my $module = $defs{$d}{TYPE}; - next if ($module !~ /pilight_[d|s|t].*/); + next if ($module !~ /pilight_[d|s|t|c].*/); pilight_ctrl_addWhiteList($own,$defs{$d}); } @@ -782,11 +785,11 @@ sub pilight_ctrl_Parse($$) # handling ContactAsSwitch befor white list check my $asSwitch = $attr{$me}{ContactAsSwitch}; - if ( defined($asSwitch) && $proto =~ /contact/ && $asSwitch =~ /$id/) { + if ( defined($asSwitch) && $asSwitch =~ /$id/ && ($state =~ /opened/ || $state =~ /closed/) ) { $proto =~ s/contact/switch/g; $state =~ s/opened/on/g; $state =~ s/closed/off/g; - Log3 $me, 5, "$me(Parse): contact as switch for $id"; + Log3 $me, 4, "$me(Parse): contact as switch for $id"; } my @ignoreIDs = split(",",AttrVal($me, "ignoreProtocol","")); @@ -830,7 +833,11 @@ sub pilight_ctrl_Parse($$) case m/quigg_gt/ {$protoID = 1;} case m/dimmer/ {$protoID = 2;} + + #contact sensors case m/contact/ {$protoID = 3;} + case m/ev1527/ {$protoID = 3;} + case m/sc2262/ {$protoID = 3;} #Weather Stations temperature, humidity case m/alecto/ {$protoID = 4;} @@ -878,7 +885,7 @@ sub pilight_ctrl_Parse($$) Log3 $me, 4, "$me(Dispatch): $msg"; return Dispatch($hash, $msg ,undef); } - case 3 {return;} + case 3 { return Dispatch($hash, "PICONTACT,$proto,$id,$unit,$state",undef); } case 4 { my $piTempData = ""; $piTempData .= ",temperature:$data->{$s}{temperature}" if (defined($data->{$s}{temperature})); diff --git a/FHEM/30_pilight_contact.pm b/FHEM/30_pilight_contact.pm new file mode 100644 index 000000000..3540c8fe3 --- /dev/null +++ b/FHEM/30_pilight_contact.pm @@ -0,0 +1,147 @@ +############################################## +# $Id$ +# +# Usage +# +# define pilight_contact [unit] +# +# Changelog +# +# V 0.10 2016-11-13 - initial alpha version +############################################## + +package main; + +use strict; +use warnings; +use Time::HiRes qw(gettimeofday); +use JSON; + +sub pilight_contact_Parse($$); +sub pilight_contact_Define($$); + +sub pilight_contact_Initialize($) +{ + my ($hash) = @_; + + $hash->{DefFn} = "pilight_contact_Define"; + $hash->{Match} = "^PICONTACT"; + $hash->{ParseFn} = "pilight_contact_Parse"; + $hash->{StateFn} = "pilight_contact_State"; + $hash->{AttrList} = "IODev ".$readingFnAttributes; +} + +##################################### +sub pilight_contact_Define($$) +{ + my ($hash, $def) = @_; + my @a = split("[ \t][ \t]*", $def); + + if(@a < 4) { + my $msg = "wrong syntax: define pilight_contact [unit]"; + Log3 undef, 2, $msg; + return $msg; + } + + my $me = $a[0]; + my $protocol = $a[2]; + my $id = $a[3]; + my $unit = undef; + $unit = $a[4] if (@a == 5); + + $hash->{STATE} = "defined"; + $hash->{PROTOCOL} = lc($protocol); + $hash->{ID} = $id; + $hash->{UNIT} = $unit; + + #$attr{$me}{verbose} = 5; + + $modules{pilight_contact}{defptr}{lc($protocol)}{$me} = $hash; + AssignIoPort($hash); + return undef; +} + +##################################### +sub pilight_contact_State($$$$) +{ + my ($hash, $time, $name, $val) = @_; + my $me = $hash->{NAME}; + + #$hash->{STATE} wird nur ersetzt, wenn $hash->{STATE} == ??? fhem.pl Z: 2469 + #machen wir es also selbst + $hash->{STATE} = $val if ($name eq "state"); + return undef; +} + + +########################################### +sub pilight_contact_Parse($$) +{ + my ($mhash, $rmsg, $rawdata) = @_; + my $backend = $mhash->{NAME}; + + Log3 $backend, 4, "pilight_contact_Parse ($backend): RCV -> $rmsg"; + + my ($dev,$protocol,$id,$unit,$state,@args) = split(",",$rmsg); + return () if($dev ne "PICONTACT"); + + my $chash; + foreach my $n (keys %{ $modules{pilight_contact}{defptr}{lc($protocol)} }) { + my $lh = $modules{pilight_contact}{defptr}{$protocol}{$n}; + next if ( !defined($lh->{ID}) ); + if ($lh->{ID} eq $id && $lh->{UNIT} eq $unit) { + $chash = $lh; + last; + } + } + + return () if (!defined($chash->{NAME})); + + readingsBeginUpdate($chash); + readingsBulkUpdate($chash,"state",$state); + readingsEndUpdate($chash, 1); + + return $chash->{NAME}; +} + + +1; + +=pod +=item summary pilight contact sensors +=item summary_DE pilight Kontaktsensoren +=begin html + + +

pilight_contact

+
    + + pilight_contact represents a contact sensor receiving data from pilight
    + You have to define the base device pilight_ctrl first.
    + Further information to pilight: http://www.pilight.org/
    +
    + + Define +
      + define <name> pilight_contact protocol id [unit] +

      + + Example: +
        + define myctrl pilight_contact arctech_contact 12836682 1
        +
      +
    +
    + +

    Readings

    +
      +
    • + state
      + present the current state (open|closed) +
    • +
    +
+ +=end html + +=cut diff --git a/HISTORY b/HISTORY index b816f5012..6b00ea411 100644 --- a/HISTORY +++ b/HISTORY @@ -709,3 +709,6 @@ - Wed Nov 5 2016 (loredo) - adding general (but optional) unit support for FHEM and FHEM modules + +- Sun Nov 13 2016 (risiko) + - adding new module 30_pilight_contact diff --git a/MAINTAINER.txt b/MAINTAINER.txt index 95072777b..045e34698 100644 --- a/MAINTAINER.txt +++ b/MAINTAINER.txt @@ -135,6 +135,8 @@ FHEM/30_pilight_dimmer.pm risiko http://forum.fhem.de Sonstige FHEM/30_pilight_switch.pm risiko http://forum.fhem.de Sonstige Systeme FHEM/30_pilight_temp.pm risiko http://forum.fhem.de Sonstige Systeme FHEM/30_pilight_raw.pm risiko http://forum.fhem.de Sonstige Systeme +FHEM/30_pilight_smoke.pm risiko http://forum.fhem.de Sonstige Systeme +FHEM/30_pilight_contact.pm risiko http://forum.fhem.de Sonstige Systeme FHEM/31_HUEDevice.pm justme1968 http://forum.fhem.de Beleuchtung FHEM/31_MilightDevice.pm mattwire http://forum.fhem.de Beleuchtung FHEM/31_ENECSYSINV.pm akw http://forum.fhem.de Sonstige Systeme