30_pilight_smoke.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ##############################################
  2. # $Id: 30_pilight_smoke.pm 11733 2016-07-03 13:05:19Z risiko79 $
  3. #
  4. # Usage
  5. #
  6. # define <name> pilight_smoke <protocol> <id>
  7. #
  8. # Changelog
  9. #
  10. # V 0.10 2016-06-28 - 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_smoke_Parse($$);
  18. sub pilight_smoke_Define($$);
  19. sub pilight_smoke_Initialize($)
  20. {
  21. my ($hash) = @_;
  22. $hash->{DefFn} = "pilight_smoke_Define";
  23. $hash->{Match} = "^PISMOKE";
  24. $hash->{ParseFn} = "pilight_smoke_Parse";
  25. $hash->{StateFn} = "pilight_smoke_State";
  26. $hash->{AttrList} = "resetTime IODev ".$readingFnAttributes;
  27. }
  28. #####################################
  29. sub pilight_smoke_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_smoke <protocol> <id>";
  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. $hash->{STATE} = "defined";
  42. $hash->{PROTOCOL} = lc($protocol);
  43. $hash->{ID} = $id;
  44. #$attr{$me}{verbose} = 5;
  45. $modules{pilight_smoke}{defptr}{lc($protocol)}{$me} = $hash;
  46. AssignIoPort($hash);
  47. return undef;
  48. }
  49. #####################################
  50. sub pilight_smoke_State($$$$)
  51. {
  52. my ($hash, $time, $name, $val) = @_;
  53. my $me = $hash->{NAME};
  54. #$hash->{STATE} wird nur ersetzt, wenn $hash->{STATE} == ??? fhem.pl Z: 2469
  55. #machen wir es also selbst
  56. $hash->{STATE} = $val if ($name eq "state");
  57. return undef;
  58. }
  59. ###########################################
  60. sub pilight_smoke_resetState($)
  61. {
  62. my $hash = shift;
  63. my $me = $hash->{NAME};
  64. RemoveInternalTimer($hash);
  65. readingsBeginUpdate($hash);
  66. readingsBulkUpdate($hash,"state","none");
  67. readingsEndUpdate($hash, 1);
  68. }
  69. ###########################################
  70. sub pilight_smoke_Parse($$)
  71. {
  72. my ($mhash, $rmsg, $rawdata) = @_;
  73. my $backend = $mhash->{NAME};
  74. Log3 $backend, 4, "pilight_smoke_Parse ($backend): RCV -> $rmsg";
  75. my ($dev,$protocol,$id,$state,@args) = split(",",$rmsg);
  76. return () if($dev ne "PISMOKE");
  77. my $chash;
  78. foreach my $n (keys %{ $modules{pilight_smoke}{defptr}{lc($protocol)} }) {
  79. my $lh = $modules{pilight_smoke}{defptr}{$protocol}{$n};
  80. next if ( !defined($lh->{ID}) );
  81. if ($lh->{ID} eq $id) {
  82. $chash = $lh;
  83. last;
  84. }
  85. }
  86. return () if (!defined($chash->{NAME}));
  87. my $resetTime = AttrVal($chash->{NAME}, "resetTime",5);
  88. RemoveInternalTimer($chash);
  89. readingsBeginUpdate($chash);
  90. readingsBulkUpdate($chash,"state",$state);
  91. readingsEndUpdate($chash, 1);
  92. InternalTimer(gettimeofday()+$resetTime,"pilight_smoke_resetState", $chash, 0);
  93. return $chash->{NAME};
  94. }
  95. 1;
  96. =pod
  97. =begin html
  98. <a name="pilight_smoke"></a>
  99. <h3>pilight_smoke</h3>
  100. <ul>
  101. pilight_smoke represents a smoke sensor receiving data from pilight<br>
  102. You have to define the base device pilight_ctrl first.<br>
  103. Further information to pilight: <a href="http://www.pilight.org/">http://www.pilight.org/</a><br>
  104. <br>
  105. <a name="pilight_smoke_define"></a>
  106. <b>Define</b>
  107. <ul>
  108. <code>define &lt;name&gt; pilight_smoke protocol id</code>
  109. <br><br>
  110. Example:
  111. <ul>
  112. <code>define myctrl pilight_smoke secudo_smoke_sensor 0</code><br>
  113. </ul>
  114. </ul>
  115. <br>
  116. <a name="pilight_smoke_readings"></a>
  117. <p><b>Readings</b></p>
  118. <ul>
  119. <li>
  120. state<br>
  121. present the current state (alarm|none)
  122. </li>
  123. </ul>
  124. <br>
  125. <a name="pilight_smoke_attr"></a>
  126. <b>Attributes</b>
  127. <ul>
  128. <li><a name="resetTime">resetTime</a><br>
  129. Time [sec] to reset the state to none.
  130. </li>
  131. </ul>
  132. </ul>
  133. =end html
  134. =cut