30_pilight_switch.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ##############################################
  2. # $Id: 30_pilight_switch.pm 11306 2016-04-24 17:03:16Z risiko79 $
  3. #
  4. # Usage
  5. #
  6. # define <name> pilight_switch <protocol> <id> <unit>
  7. #
  8. # Changelog
  9. #
  10. # V 0.10 2015-02-22 - initial beta version
  11. # V 0.11 2015-03-29 - FIX: $readingFnAttributes
  12. # V 0.12 2015-05-18 - FIX: add version information
  13. # V 0.13 2015-05-30 - FIX: StateFn, noArg
  14. # V 0.14 2015-07-27 - NEW: SetExtensions on-for-timer
  15. # V 0.15 2015-12-17 - NEW: Attribut IODev to switch IO-Device
  16. # V 0.16 2016-03-28 - NEW: protocol daycom with three id's (id, systemcode, unit)
  17. # V 0.17 2016-04-24 - NEW: Attribut sendCount
  18. ##############################################
  19. package main;
  20. use strict;
  21. use warnings;
  22. use Time::HiRes qw(gettimeofday);
  23. use JSON;
  24. use SetExtensions;
  25. my %sets = ("on:noArg"=>0, "off:noArg"=>0);
  26. sub pilight_switch_Parse($$);
  27. sub pilight_switch_Define($$);
  28. sub pilight_switch_Initialize($)
  29. {
  30. my ($hash) = @_;
  31. $hash->{DefFn} = "pilight_switch_Define";
  32. $hash->{Match} = "^PISWITCH";
  33. $hash->{ParseFn} = "pilight_switch_Parse";
  34. $hash->{SetFn} = "pilight_switch_Set";
  35. $hash->{StateFn} = "pilight_switch_State";
  36. $hash->{AttrList} = "IODev sendCount:1,2,3,4,5 ".$readingFnAttributes;
  37. }
  38. #####################################
  39. sub pilight_switch_Define($$)
  40. {
  41. my ($hash, $def) = @_;
  42. my @a = split("[ \t][ \t]*", $def);
  43. if(@a < 5) {
  44. my $msg = "wrong syntax: define <name> pilight_switch <protocol> <id> <unit> [systemcode]";
  45. Log3 undef, 2, $msg;
  46. return $msg;
  47. }
  48. my $me = $a[0];
  49. my $protocol = $a[2];
  50. my $id = $a[3];
  51. my $unit = $a[4];
  52. $hash->{STATE} = "defined";
  53. $hash->{PROTOCOL} = lc($protocol);
  54. $hash->{ID} = $id;
  55. $hash->{UNIT} = $unit;
  56. $hash->{SYSCODE} = undef;
  57. $hash->{SYSCODE} = $a[5] if (@a == 6);
  58. #$attr{$me}{verbose} = 5;
  59. $modules{pilight_switch}{defptr}{lc($protocol)}{$me} = $hash;
  60. AssignIoPort($hash);
  61. return undef;
  62. }
  63. #####################################
  64. sub pilight_switch_State($$$$)
  65. {
  66. my ($hash, $time, $name, $val) = @_;
  67. my $me = $hash->{NAME};
  68. #$hash->{STATE} wird nur ersetzt, wenn $hash->{STATE} == ??? fhem.pl Z: 2469
  69. #machen wir es also selbst
  70. $hash->{STATE} = $val if ($name eq "state");
  71. return undef;
  72. }
  73. ###########################################
  74. sub pilight_switch_Parse($$)
  75. {
  76. my ($mhash, $rmsg, $rawdata) = @_;
  77. my $backend = $mhash->{NAME};
  78. Log3 $backend, 4, "pilight_switch_Parse: RCV -> $rmsg";
  79. my ($dev,$protocol,$id,$unit,$state,@args) = split(",",$rmsg);
  80. return () if($dev ne "PISWITCH");
  81. my $chash;
  82. foreach my $n (keys %{ $modules{pilight_switch}{defptr}{lc($protocol)} }) {
  83. my $lh = $modules{pilight_switch}{defptr}{$protocol}{$n};
  84. next if ( !defined($lh->{ID}) || !defined($lh->{UNIT}) );
  85. if ($lh->{ID} eq $id && $lh->{UNIT} eq $unit) {
  86. if (defined($lh->{SYSCODE})) { #protocol daycom needs three id's id, systemcode, unit
  87. next if (@args<=0);
  88. next if ($lh->{SYSCODE} ne $args[0]);
  89. }
  90. $chash = $lh;
  91. last;
  92. }
  93. }
  94. return () if (!defined($chash->{NAME}));
  95. readingsBeginUpdate($chash);
  96. readingsBulkUpdate($chash,"state",$state);
  97. readingsEndUpdate($chash, 1);
  98. return $chash->{NAME};
  99. }
  100. #####################################
  101. sub pilight_switch_Set($$)
  102. {
  103. my ($hash, $me, $cmd, @a) = @_;
  104. return "no set value specified" unless defined($cmd);
  105. my @match = grep( $_ =~ /^$cmd($|:)/, keys %sets );
  106. return SetExtensions($hash, join(" ", keys %sets), $me, $cmd, @a) unless @match == 1;
  107. return "$cmd expects $sets{$match[0]} parameters" unless (@a eq $sets{$match[0]});
  108. my $v = join(" ", @a);
  109. my $msg = "$me,$cmd";
  110. my $sndCount = AttrVal($me,"sendCount",1);
  111. for (my $i = 0; $i < $sndCount; $i++) {
  112. Log3 $me, 5, "$me(Set): $cmd $v".($i+1)." of $sndCount";
  113. IOWrite($hash, $msg);
  114. }
  115. #keinen Trigger bei Set auslösen
  116. #Aktualisierung erfolgt in Parse
  117. my $skipTrigger = 1;
  118. return undef,$skipTrigger;
  119. }
  120. 1;
  121. =pod
  122. =begin html
  123. <a name="pilight_switch"></a>
  124. <h3>pilight_switch</h3>
  125. <ul>
  126. pilight_switch represents a switch controled with\from pilight<br>
  127. You have to define the base device pilight_ctrl first.<br>
  128. Further information to pilight: <a href="http://www.pilight.org/">http://www.pilight.org/</a><br>
  129. Supported switches: <a href="http://wiki.pilight.org/doku.php/protocols#switches">http://wiki.pilight.org/doku.php/protocols#switches</a><br>
  130. <br>
  131. <a name="pilight_switch_define"></a>
  132. <b>Define</b>
  133. <ul>
  134. <code>define &lt;name&gt; pilight_switch protocol id unit</code>
  135. <br><br>
  136. Example:
  137. <ul>
  138. <code>define myctrl pilight_switch kaku_switch_old 0 0</code><br>
  139. </ul>
  140. </ul>
  141. <br>
  142. <a name="pilight_switch_set"></a>
  143. <p><b>Set</b></p>
  144. <ul>
  145. <li>
  146. <b>on</b>
  147. </li>
  148. <li>
  149. <b>off</b>
  150. </li>
  151. <li>
  152. <a href="#setExtensions">set extensions</a> are supported<br>
  153. </li>
  154. </ul>
  155. <br>
  156. <a name="pilight_switch_readings"></a>
  157. <p><b>Readings</b></p>
  158. <ul>
  159. <li>
  160. state<br>
  161. state of the switch on or off
  162. </li>
  163. </ul>
  164. <a name="pilight_switch_attr"></a>
  165. <b>Attributs</b>
  166. <ul>
  167. <li>
  168. IODev<br>
  169. </li>
  170. <li>
  171. sendCount<br>
  172. How many times the command is send. Default: 1
  173. </li>
  174. </ul>
  175. <br>
  176. </ul>
  177. =end html
  178. =cut