46_PW_Switch.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #################################################################################
  2. # 46_PW_Switch.pm
  3. #
  4. # FHEM module Plugwise switches
  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_Switch.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_Switch_Initialize($)
  33. {
  34. my ($hash) = @_;
  35. $hash->{Match} = "PW_Switch";
  36. $hash->{DefFn} = "PW_Switch_Define";
  37. $hash->{UndefFn} = "PW_Switch_Undef";
  38. $hash->{ParseFn} = "PW_Switch_Parse";
  39. $hash->{SetFn} = "PW_Switch_Set";
  40. $hash->{AttrList} = "IODev do_not_notify:1,0 ".
  41. $readingFnAttributes;
  42. Log3 $hash, 5, "PW_Switch_Initialize() Initialize";
  43. }
  44. #####################################
  45. sub PW_Switch_Define($$)
  46. {
  47. my ($hash, $def) = @_;
  48. my @a = split("[ \t][ \t]*", $def);
  49. my $a = int(@a);
  50. Log3 $hash,5,"PW_Switch: Define called --> $def";
  51. return "wrong syntax: define <name> PW_Switch address" if(int(@a) != 3);
  52. my $name = $a[0];
  53. my $code = $a[2];
  54. my $device_name = "PW_Switch".$DOT.$code;
  55. $hash->{CODE} = $code;
  56. $modules{PW_Switch}{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_Switch_Undef($$)
  65. {
  66. my ($hash, $name) = @_;
  67. delete($modules{PW_Switch}{defptr}{$name});
  68. return undef;
  69. }
  70. sub PW_Switch_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_Switch-Set: N:$name O:$opt V:$value";
  78. if($opt =~ /(o2n|o2ff)/) {
  79. if ($value =~/(left|right)/) {IOWrite($hash,$hash->{CODE},$opt,$value);}
  80. } elsif ($opt eq "getLog")
  81. {
  82. IOWrite($hash,$hash->{CODE},$opt,$value);
  83. } elsif ($opt eq "syncTime") {
  84. IOWrite($hash,$hash->{CODE},$opt);
  85. } elsif ($opt eq "removeNode") {
  86. IOWrite($hash,$hash->{CODE},$opt);
  87. } elsif ($opt eq "ping") {
  88. IOWrite($hash,$hash->{CODE},$opt);
  89. }
  90. else
  91. {
  92. return "Unknown argument $opt, choose one of syncTime removeNode ping";
  93. }
  94. }
  95. sub PW_Switch_Parse($$)
  96. {
  97. my ($hash, $msg2) = @_;
  98. my $msg=$hash->{RAWMSG};
  99. #Log 3,"PW_Switch: got a msg";
  100. my $time = time();
  101. if ($msg->{type} eq "err") {return undef};
  102. Log3 $hash,5,"PW_Switch: Parse called ".$msg->{short};
  103. $time_old = $time;
  104. my $device_name = "PW_Switch".$DOT.$msg->{short};
  105. Log3 $hash,5,"New Devicename: $device_name";
  106. my $def = $modules{PW_Switch}{defptr}{"$device_name"};
  107. if(!$def) {
  108. Log3 $hash, 3, "PW_Switch: Unknown device $device_name, please define it";
  109. return "UNDEFINED $device_name PW_Switch $msg->{short}";
  110. }
  111. # Use $def->{NAME}, because the device may be renamed:
  112. my $name = $def->{NAME};
  113. my $type = $msg->{type};
  114. readingsBeginUpdate($def);
  115. if($type eq "humtemp") {
  116. readingsBulkUpdate($def, "temperature", $msg->{val2});
  117. readingsBulkUpdate($def, "humidity", $msg->{val1});
  118. }
  119. if($type eq "sense") {
  120. # readingsBulkUpdate($def, "key_left", $msg->{val1}) if (ReadingsVal($name,"key_left","-") ne $msg->{val1});
  121. # readingsBulkUpdate($def, "key_right", $msg->{val2}) if (ReadingsVal($name,"key_right","-") ne $msg->{val2});
  122. readingsBulkUpdate($def, "state", "on") if ($msg->{val1}==1 && ReadingsVal($def,"state","0") ne "on");
  123. readingsBulkUpdate($def, "state", "off") if ($msg->{val1}==0 && ReadingsVal($def,"state","0") ne "off");
  124. }
  125. readingsEndUpdate($def, 1);
  126. return $name;
  127. }
  128. "Cogito, ergo sum.";
  129. =pod
  130. =item device
  131. =item summary Submodule for 45_Plugwise
  132. =item summary_DE Untermodul zu 45_Plugwise
  133. =begin html
  134. <a name="PW_Switch"></a>
  135. <h3>PW_Switch</h3>
  136. <ul>
  137. The PW_Switch module is invoked by Plugwise. You need to define a Plugwise-Stick first.
  138. See <a href="#PW_Switch">PW_Switch</a>.
  139. <br>
  140. <a name="PW_Switch define"></a>
  141. <br>
  142. <b>Define</b>
  143. <ul>
  144. <code>define &lt;name&gt; PW_Switch &lt;ShortAddress&gt;</code> <br>
  145. <br>
  146. <code>&lt;ShortAddress&gt;</code>
  147. <ul>
  148. specifies the short (last 4 Bytes) of the Circle received by the Plugwise-Stick. <br>
  149. </ul>
  150. <br>
  151. Example: <br>
  152. <code>define PW_Switch_2907CC9 PW_Switch 2907CC9</code>
  153. <br>
  154. </ul>
  155. <b>Set</b>
  156. <ul>
  157. <code>syncTime</code> <br>
  158. <ul>
  159. Syncronises the internal clock of the Circle with your PC's clock<br><br>
  160. </ul>
  161. <code>removeNode</code> <br>
  162. <ul>
  163. Removes this device from your Plugwise-network<br><br>
  164. </ul>
  165. <code>ping</code> <br>
  166. <ul>
  167. Ping the circle and write the Ping-Runtime to reading "ping" in format "q_in - q_out - pingTime"<br><br>
  168. </ul>
  169. </ul>
  170. <br>
  171. </ul>
  172. =end html
  173. =begin html_DE
  174. <a name="PW_Switch"></a>
  175. <h3>PW_Switch</h3>
  176. <ul>
  177. Das PW_Switch Module basiert auf dem Plugwise-System. Es muss zuerst ein Plugwise-Stick angelegt werden.
  178. Siehe <a href="#PW_Switch">PW_Switch</a>.
  179. <br>
  180. <a name="PW_Switch define"></a>
  181. <br>
  182. <b>Define</b>
  183. <ul>
  184. <code>define &lt;name&gt; PW_Switch &lt;ShortAddress&gt;</code> <br>
  185. <br>
  186. <code>&lt;ShortAddress&gt;</code>
  187. <ul>
  188. gibt die Kurzadresse (die letzten 4 Bytes) des Circles an. <br>
  189. </ul>
  190. <br>
  191. Beispiel: <br>
  192. <code>define PW_Switch_2907CC9 PW_Switch 2907CC9</code>
  193. <br>
  194. </ul><br>
  195. <b>Set</b>
  196. <ul>
  197. <code>syncTime</code> <br>
  198. <ul>
  199. Synchronisiert die interne Uhr des Circles mit der lokalen Systemzeit<br><br>
  200. </ul>
  201. <code>removeNode</code> <br>
  202. <ul>
  203. Entfernt den Circle aus dem Plugwise-Netzwerk<br><br>
  204. </ul>
  205. <code>ping</code> <br>
  206. <ul>
  207. Sendet ein Ping an den Circle und setzt das Reading "ping" im Format "q_in - q_out - pingZeit"<br><br>
  208. </ul>
  209. </ul>
  210. <br>
  211. </ul>
  212. =end html
  213. =cut