46_PW_Sense.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #################################################################################
  2. # 46_PW_Sense.pm
  3. #
  4. # FHEM module Plugwise motion scanners
  5. #
  6. # Copyright (C) 2015 Stefan Guttmann
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. # The GNU General Public License may also be found at http://www.gnu.org/licenses/gpl-2.0.html .
  23. ###################################
  24. #
  25. # $Id: 46_PW_Sense.pm 11989 2016-08-19 17:50:52Z icinger $
  26. package main;
  27. use strict;
  28. use warnings;
  29. use Data::Dumper;
  30. my $time_old = 0;
  31. my $DOT = q{_};
  32. sub PW_Sense_Initialize($)
  33. {
  34. my ($hash) = @_;
  35. $hash->{Match} = "PW_Sense";
  36. $hash->{DefFn} = "PW_Sense_Define";
  37. $hash->{UndefFn} = "PW_Sense_Undef";
  38. $hash->{ParseFn} = "PW_Sense_Parse";
  39. $hash->{SetFn} = "PW_Sense_Set";
  40. $hash->{AttrList} = "IODev do_not_notify:1,0 ".
  41. $readingFnAttributes;
  42. Log3 $hash, 5, "PW_Sense_Initialize() Initialize";
  43. }
  44. #####################################
  45. sub PW_Sense_Define($$)
  46. {
  47. my ($hash, $def) = @_;
  48. my @a = split("[ \t][ \t]*", $def);
  49. my $a = int(@a);
  50. Log3 $hash,5,"PW_Sense: Define called --> $def";
  51. return "wrong syntax: define <name> PW_Sense address" if(int(@a) != 3);
  52. my $name = $a[0];
  53. my $code = $a[2];
  54. my $device_name = "PW_Sense".$DOT.$code;
  55. $hash->{CODE} = $code;
  56. $modules{PW_Sense}{defptr}{$device_name} = $hash;
  57. AssignIoPort($hash);
  58. if( $init_done ) {
  59. $attr{$name}{room}='Plugwise';
  60. }
  61. return undef;
  62. }
  63. #####################################
  64. sub PW_Sense_Undef($$)
  65. {
  66. my ($hash, $name) = @_;
  67. delete($modules{PW_Sense}{defptr}{$name});
  68. return undef;
  69. }
  70. sub PW_Sense_Set($@)
  71. {
  72. my ( $hash, @a ) = @_;
  73. return "\"set X\" needs at least an argument" if ( @a < 2 );
  74. my $name = shift @a;
  75. my $opt = shift @a;
  76. my $value = join("", @a);
  77. Log3 $hash,5,"$hash->{NAME} - PW_Sense-Set: N:$name O:$opt V:$value";
  78. if ($opt eq "removeNode") {
  79. IOWrite($hash,$hash->{CODE},$opt);
  80. } elsif ($opt eq "ping") {
  81. IOWrite($hash,$hash->{CODE},$opt);
  82. }
  83. else
  84. {
  85. return "Unknown argument $opt, choose one removeNode ping";
  86. }
  87. }
  88. sub PW_Sense_Parse($$)
  89. {
  90. my ($hash, $msg2) = @_;
  91. my $msg=$hash->{RAWMSG};
  92. #Log 3,"PW_Sense: got a msg";
  93. my $time = time();
  94. if ($msg->{type} eq "err") {return undef};
  95. Log3 $hash,5,"PW_Sense: Parse called ".$msg->{short};
  96. $time_old = $time;
  97. Log3 $hash,5, Dumper($msg);
  98. my $device_name = "PW_Sense".$DOT.$msg->{short};
  99. Log3 $hash,5,"New Devicename: $device_name";
  100. my $def = $modules{PW_Sense}{defptr}{"$device_name"};
  101. if(!$def) {
  102. Log3 $hash, 3, "PW_Sense: Unknown device $device_name, please define it";
  103. return "UNDEFINED $device_name PW_Sense $msg->{short}";
  104. }
  105. # Use $def->{NAME}, because the device may be renamed:
  106. my $name = $def->{NAME};
  107. my $type = $msg->{type};
  108. Log3 $hash,5,"PW_Sense: Type is '$type'";
  109. Log3 $hash,5,Dumper($msg);
  110. readingsBeginUpdate($def);
  111. if($type eq "humtemp") {
  112. readingsBulkUpdate($def, "temperature", $msg->{val2});
  113. readingsBulkUpdate($def, "humidity", $msg->{val1});
  114. }
  115. if($type eq "PW_Sense") {
  116. readingsBulkUpdate($def, "Sense", ($msg->{val1}) eq 0 ? 'off' : 'on');
  117. }
  118. readingsEndUpdate($def, 1);
  119. return $name;
  120. }
  121. "Cogito, ergo sum.";
  122. =pod
  123. =item device
  124. =item summary Submodule for 45_Plugwise
  125. =item summary_DE Untermodul zu 45_Plugwise
  126. =begin html
  127. <a name="PW_Sense"></a>
  128. <h3>PW_Sense</h3>
  129. <ul>
  130. The PW_Sense module is invoked by Plugwise. You need to define a Plugwise-Stick first.
  131. See <a href="#PW_Sense">PW_Sense</a>.
  132. <br>
  133. <a name="PW_Sense define"></a>
  134. <br>
  135. <b>Define</b>
  136. <ul>
  137. <code>define &lt;name&gt; PW_Sense &lt;ShortAddress&gt;</code> <br>
  138. <br>
  139. <code>&lt;ShortAddress&gt;</code>
  140. <ul>
  141. specifies the short (last 4 Bytes) of the PW_Sense received by the Plugwise-Stick. <br>
  142. </ul>
  143. <br>
  144. Example: <br>
  145. <code>define PW_Sense_2907CC9 PW_Sense 2907CC9</code>
  146. <br>
  147. </ul>
  148. <br>
  149. </ul>
  150. =end html
  151. =begin html_DE
  152. <a name="PW_Sense"></a>
  153. <h3>PW_Sense</h3>
  154. <ul>
  155. Das PW_Sense Modul setzt auf das Plugwise-System auf. Es muss zuerst ein Plugwise-Stick angelegt werden.
  156. Siehe <a href="#Plugwise">Plugwise</a>.
  157. <br>
  158. <a name="PW_Sense define"></a>
  159. <br>
  160. <b>Define</b>
  161. <ul>
  162. <code>define &lt;name&gt; PW_Sense &lt;ShortAddress&gt;</code> <br>
  163. <br>
  164. <code>&lt;ShortAddress&gt;</code>
  165. <ul>
  166. gibt die Kurzadresse (die letzten 4 Bytes) des Gerätes an. <br>
  167. </ul>
  168. <br><br>
  169. </ul>
  170. </ul>
  171. =end html
  172. =cut