30_pilight_contact.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ##############################################
  2. # $Id: 30_pilight_contact.pm 14105 2017-04-25 18:05:58Z Risiko $
  3. #
  4. # Usage
  5. #
  6. # define <name> pilight_contact <protocol> <id> [unit]
  7. #
  8. # Changelog
  9. #
  10. # V 0.10 2016-11-13 - initial alpha version
  11. ##############################################
  12. package main;
  13. use strict;
  14. use warnings;
  15. use Time::HiRes qw(gettimeofday);
  16. use JSON;
  17. sub pilight_contact_Parse($$);
  18. sub pilight_contact_Define($$);
  19. sub pilight_contact_Initialize($)
  20. {
  21. my ($hash) = @_;
  22. $hash->{DefFn} = "pilight_contact_Define";
  23. $hash->{Match} = "^PICONTACT";
  24. $hash->{ParseFn} = "pilight_contact_Parse";
  25. $hash->{StateFn} = "pilight_contact_State";
  26. $hash->{AttrList} = "IODev ".$readingFnAttributes;
  27. }
  28. #####################################
  29. sub pilight_contact_Define($$)
  30. {
  31. my ($hash, $def) = @_;
  32. my @a = split("[ \t][ \t]*", $def);
  33. if(@a < 4) {
  34. my $msg = "wrong syntax: define <name> pilight_contact <protocol> <id> [unit]";
  35. Log3 undef, 2, $msg;
  36. return $msg;
  37. }
  38. my $me = $a[0];
  39. my $protocol = $a[2];
  40. my $id = $a[3];
  41. my $unit = undef;
  42. $unit = $a[4] if (@a == 5);
  43. $hash->{STATE} = "defined";
  44. $hash->{PROTOCOL} = $protocol;
  45. $hash->{ID} = $id;
  46. $hash->{UNIT} = $unit;
  47. #$attr{$me}{verbose} = 5;
  48. $modules{pilight_contact}{defptr}{$protocol}{$me} = $hash;
  49. AssignIoPort($hash);
  50. return undef;
  51. }
  52. #####################################
  53. sub pilight_contact_State($$$$)
  54. {
  55. my ($hash, $time, $name, $val) = @_;
  56. my $me = $hash->{NAME};
  57. #$hash->{STATE} wird nur ersetzt, wenn $hash->{STATE} == ??? fhem.pl Z: 2469
  58. #machen wir es also selbst
  59. $hash->{STATE} = $val if ($name eq "state");
  60. return undef;
  61. }
  62. ###########################################
  63. sub pilight_contact_Parse($$)
  64. {
  65. my ($mhash, $rmsg, $rawdata) = @_;
  66. my $backend = $mhash->{NAME};
  67. Log3 $backend, 4, "pilight_contact_Parse ($backend): RCV -> $rmsg";
  68. my ($dev,$protocol,$id,$unit,$state,@args) = split(",",$rmsg);
  69. return () if($dev ne "PICONTACT");
  70. my $chash;
  71. foreach my $n (keys %{ $modules{pilight_contact}{defptr}{$protocol} }) {
  72. my $lh = $modules{pilight_contact}{defptr}{$protocol}{$n};
  73. next if ( !defined($lh->{ID}) );
  74. if ($lh->{ID} eq $id) {
  75. if (defined($lh->{UNIT})) {
  76. next if ($lh->{UNIT} ne $unit);
  77. }
  78. $chash = $lh;
  79. last;
  80. }
  81. }
  82. return () if (!defined($chash->{NAME}));
  83. readingsBeginUpdate($chash);
  84. foreach my $arg (@args){
  85. my($feature,$value) = split(":",$arg);
  86. readingsBulkUpdate($chash,$feature,$value);
  87. }
  88. readingsBulkUpdate($chash,"state",$state);
  89. readingsEndUpdate($chash, 1);
  90. return $chash->{NAME};
  91. }
  92. 1;
  93. =pod
  94. =item summary pilight contact sensors
  95. =item summary_DE pilight Kontaktsensoren
  96. =begin html
  97. <a name="pilight_contact"></a>
  98. <h3>pilight_contact</h3>
  99. <ul>
  100. pilight_contact represents a contact sensor receiving data from pilight<br>
  101. You have to define the base device pilight_ctrl first.<br>
  102. Further information to pilight: <a href="http://www.pilight.org/">http://www.pilight.org/</a><br>
  103. <br>
  104. <a name="pilight_contact_define"></a>
  105. <b>Define</b>
  106. <ul>
  107. <code>define &lt;name&gt; pilight_contact protocol id [unit]</code>
  108. <br><br>
  109. Example:
  110. <ul>
  111. <code>define myctrl pilight_contact arctech_contact 12836682 1</code><br>
  112. </ul>
  113. </ul>
  114. <br>
  115. <a name="pilight_contact_readings"></a>
  116. <p><b>Readings</b></p>
  117. <ul>
  118. <li>
  119. state<br>
  120. present the current state (open|closed)
  121. </li>
  122. </ul>
  123. </ul>
  124. =end html
  125. =cut