84_IOhomecontrolDevice.pm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # $Id: 84_IOhomecontrolDevice.pm 16990 2018-07-16 11:02:03Z neubert $
  2. ##############################################################################
  3. #
  4. # 84_IOhomecontrolDevice.pm
  5. # Copyright by Dr. Boris Neubert
  6. # e-mail: omega at online dot de
  7. #
  8. # This file is part of fhem.
  9. #
  10. # Fhem is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Fhem is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. package main;
  25. use strict;
  26. use warnings;
  27. sub IOhomecontrolDevice_Initialize($) {
  28. my ($hash) = @_;
  29. $hash->{DefFn} = "IOhomecontrolDevice_Define";
  30. $hash->{SetFn} = "IOhomecontrolDevice_Set";
  31. $hash->{parseParams} = 1;
  32. $hash->{AttrList} = "setCmds " . $readingFnAttributes;
  33. }
  34. sub IOhomecontrolDevice_Define($$) {
  35. # define <name> IOhomecontrolDevice <interface>
  36. my ( $hash, $argref, undef ) = @_;
  37. my @def = @{$argref};
  38. if ( $#def != 2 ) {
  39. my $msg = "wrong syntax: define <name> IOhomecontrolDevice <interface>";
  40. Log 2, $msg;
  41. return $msg;
  42. }
  43. my $name = $def[0];
  44. my $master = $def[2];
  45. my $interface = $defs{$master};
  46. $hash->{"INTERFACE"} = $interface;
  47. if ( !defined($interface) || $interface->{TYPE} ne "IOhomecontrol" ) {
  48. return "No such IOhomecontrol interface: $master";
  49. }
  50. else { return; }
  51. }
  52. sub IOhomecontrolDevice_getSetCmds($) {
  53. my $hash = shift;
  54. my $name = $hash->{NAME};
  55. my $attr = AttrVal( $name, "setCmds", "" );
  56. my ( undef, $setCmds ) = parseParams( $attr, "," );
  57. return $setCmds;
  58. }
  59. sub IOhomecontrolDevice_runSceneByIdCallback($$$$) {
  60. my ( $hash, $httpParams, $err, $result ) = @_;
  61. my $name = $hash->{NAME};
  62. my $interface = $hash->{INTERFACE};
  63. my $id = $httpParams->{params}{id};
  64. my $sn = $interface->{fhem}{".scenes"}->{$id};
  65. if ( defined($err) ) {
  66. Log3 $hash, 2,
  67. "IOhomecontrolDevice $name: running scene id $id, name $sn, failed ($err)";
  68. }
  69. else {
  70. Log3 $hash, 5,
  71. "IOhomecontrolDevice $name: running scene id $id, name $sn, completed";
  72. readingsSingleUpdate( $hash, "state", $sn, 1 );
  73. }
  74. }
  75. sub IOhomecontrolDevice_Set($$$) {
  76. my ( $hash, $argsref, undef ) = @_;
  77. my @a = @{$argsref};
  78. return "set needs at least one parameter" if ( @a < 2 );
  79. my $name = shift @a;
  80. my $cmd = shift @a;
  81. my $setCmds = IOhomecontrolDevice_getSetCmds($hash);
  82. my $usage = "Unknown argument $cmd, choose one of scene"
  83. . join( " ", ( keys %{$setCmds} ) );
  84. if ( exists( $setCmds->{$cmd} ) ) {
  85. readingsSingleUpdate( $hash, "state", $cmd, 1 );
  86. my $subst = $setCmds->{$cmd};
  87. Log3 $hash, 5,
  88. "IOhomecontrolDevice $name: substitute set command $cmd by $subst";
  89. ( $argsref, undef ) = parseParams($subst);
  90. @a = @{$argsref};
  91. $cmd = shift @a;
  92. }
  93. if ( $cmd eq "scene" ) {
  94. if ($#a) {
  95. return "Command scene needs exactly one argument.";
  96. }
  97. else {
  98. my $id = $a[0];
  99. my $interface = $hash->{INTERFACE};
  100. return IOhomecontrol_setScene( $interface, $id,
  101. \&IOhomecontrolDevice_runSceneByIdCallback );
  102. }
  103. }
  104. else {
  105. return $usage;
  106. }
  107. return undef;
  108. }
  109. #####################################
  110. 1;
  111. =pod
  112. =item device
  113. =item summary control IOhomecontrol devices via IOhomecontrol interface
  114. =item summary_DE IOhomecontrol-Ger&auml;te mittels IOhomecontrol-Interface steuern
  115. =begin html
  116. <a name="IOhomecontrolDevice"></a>
  117. <h3>IOhomecontrolDevice</h3>
  118. <ul>
  119. <a name="IOhomecontrolDevicedefine"></a>
  120. <b>Define</b><br><br>
  121. <ul>
  122. <code>define &lt;name&gt; IOhomecontrolDevice &lt;interface&gt; </code><br><br>
  123. Defines an IOhomecontrol device. <code>&lt;interface&gt;</code> is the
  124. name of the IOhomecontrol interface device (gateway) that is used to
  125. communicate with the IOhomecontrol devices.
  126. <br><br>
  127. Example:
  128. <ul>
  129. <code>define shutter1 IOhomecontrolDevice myKLF200</code><br>
  130. </ul>
  131. <br><br>
  132. </ul>
  133. <a name="IOhomecontrolDeviceset"></a>
  134. <b>Set</b><br><br>
  135. <ul>
  136. <code>set &lt;name&gt; scene &lt;id&gt;</code>
  137. <br><br>
  138. Runs the scene identified by <code>&lt;id&gt;</code> which can be either
  139. the numeric id of the scene or the scene's name.
  140. <br><br>
  141. Examples:
  142. <ul>
  143. <code>set shutter1 scene 1</code><br>
  144. <code>set shutter1 scene "3.dz.roll2 100%"</code><br>
  145. </ul>
  146. <br>
  147. Scene names with blanks must be enclosed in double quotes.
  148. <br><br>
  149. </ul>
  150. <a name="IOhomecontrolDeviceattr"></a>
  151. <b>Attributes</b>
  152. <br>
  153. <br>
  154. <ul>
  155. <li>setCmds: a comma-separated list of set command definitions.
  156. Every definition is of the form <code>&lt;shorthand&gt;=&lt;command&gt;</code>. This defines a new single-word command <code>&lt;shorthand&gt</code> as a substitute for <code>&lt;command&gt;</code>.<br>
  157. Example: <code>attr shutter1 setCmds up=scene "3.dz.roll2 100%",down=scene "3.dz.roll2 0%"</code><br>
  158. Substituted commands (and only these) are shown in the state reading.
  159. This is useful in conjunction with the <code>devStateIcon</code> attribute,
  160. e.g. <code>attr shutter1 devStateIcon down:shutter_closed up:shutter_open</code>.</li>
  161. <br>
  162. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  163. </ul>
  164. <br><br>
  165. <b>Full example</b>
  166. <ul><code>
  167. define myKLF200 IOhomecontrol KLF200 velux.local /opt/fhem/etc/veluxpw.txt<br>
  168. attr myKLF200 verbose 5<br>
  169. attr myKLF200 logTraffic 1<br>
  170. <br>
  171. define shutter1 IOhomecontrolDevice myKLF200<br>
  172. attr shutter1 setCmds up=scene "3.dz.roll2 0%",down=scene "3.dz.roll2 100%"<br>
  173. attr shutter1 webCmd up:down<br>
  174. attr shutter1 devStateIcon down:shutter_closed up:shutter_open<br>
  175. </code></ul>
  176. <br><br>
  177. </ul>
  178. =end html
  179. =cut