10_MQTT_DEVICE.pm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ##############################################
  2. #
  3. # fhem bridge to mqtt (see http://mqtt.org)
  4. #
  5. # Copyright (C) 2017 Stephan Eisler
  6. # Copyright (C) 2014 - 2016 Norbert Truchsess
  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. # $Id: 10_MQTT_DEVICE.pm 13318 2017-02-03 09:42:52Z eisler $
  24. #
  25. ##############################################
  26. use strict;
  27. use warnings;
  28. my %gets = (
  29. "version" => "",
  30. );
  31. sub MQTT_DEVICE_Initialize($) {
  32. my $hash = shift @_;
  33. # Consumer
  34. $hash->{DefFn} = "MQTT::DEVICE::Define";
  35. $hash->{UndefFn} = "MQTT::Client_Undefine";
  36. $hash->{SetFn} = "MQTT::DEVICE::Set";
  37. $hash->{AttrFn} = "MQTT::DEVICE::Attr";
  38. $hash->{AttrList} =
  39. "IODev ".
  40. "qos:".join(",",keys %MQTT::qos)." ".
  41. "retain:0,1 ".
  42. "publishSet ".
  43. "publishSet_.* ".
  44. "subscribeReading_.* ".
  45. "autoSubscribeReadings ".
  46. $main::readingFnAttributes;
  47. main::LoadModule("MQTT");
  48. }
  49. package MQTT::DEVICE;
  50. use strict;
  51. use warnings;
  52. use GPUtils qw(:all);
  53. use Net::MQTT::Constants;
  54. BEGIN {
  55. MQTT->import(qw(:all));
  56. GP_Import(qw(
  57. CommandDeleteReading
  58. CommandAttr
  59. readingsSingleUpdate
  60. Log3
  61. ))
  62. };
  63. sub Define() {
  64. my ( $hash, $def ) = @_;
  65. $hash->{sets} = {};
  66. return MQTT::Client_Define($hash,$def);
  67. };
  68. sub Set($$$@) {
  69. my ($hash,$name,$command,@values) = @_;
  70. return "Need at least one parameters" unless defined $command;
  71. return "Unknown argument $command, choose one of " . join(" ", map {$hash->{sets}->{$_} eq "" ? $_ : "$_:".$hash->{sets}->{$_}} sort keys %{$hash->{sets}})
  72. if(!defined($hash->{sets}->{$command}));
  73. my $msgid;
  74. if (@values) {
  75. my $value = join " ",@values;
  76. $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{$command}->{topic}, message => $value, qos => $hash->{qos}, retain => $hash->{retain});
  77. readingsSingleUpdate($hash,$command,$value,1);
  78. } else {
  79. $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{""}->{topic}, message => $command, qos => $hash->{qos}, retain => $hash->{retain});
  80. readingsSingleUpdate($hash,"state",$command,1);
  81. }
  82. $hash->{message_ids}->{$msgid}++ if defined $msgid;
  83. readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
  84. return undef;
  85. }
  86. sub Attr($$$$) {
  87. my ($command,$name,$attribute,$value) = @_;
  88. my $hash = $main::defs{$name};
  89. ATTRIBUTE_HANDLER: {
  90. $attribute =~ /^subscribeReading_(.+)/ and do {
  91. if ($command eq "set") {
  92. unless (defined $hash->{subscribeReadings}->{$value} and $hash->{subscribeReadings}->{$value} eq $1) {
  93. unless (defined $hash->{subscribeReadings}->{$value}) {
  94. client_subscribe_topic($hash,$value);
  95. }
  96. $hash->{subscribeReadings}->{$value} = $1;
  97. }
  98. } else {
  99. foreach my $topic (keys %{$hash->{subscribeReadings}}) {
  100. if ($hash->{subscribeReadings}->{$topic} eq $1) {
  101. client_unsubscribe_topic($hash,$topic);
  102. delete $hash->{subscribeReadings}->{$topic};
  103. CommandDeleteReading(undef,"$hash->{NAME} $1");
  104. last;
  105. }
  106. }
  107. }
  108. last;
  109. };
  110. $attribute eq "autoSubscribeReadings" and do {
  111. if ($command eq "set") {
  112. unless (defined $hash->{'.autoSubscribeTopic'} and $hash->{'.autoSubscribeTopic'} eq $value) {
  113. if (defined $hash->{'.autoSubscribeTopic'}) {
  114. client_unsubscribe_topic($hash,$hash->{'.autoSubscribeTopic'});
  115. }
  116. $hash->{'.autoSubscribeTopic'} = $value;
  117. $hash->{'.autoSubscribeExpr'} = topic_to_regexp($value);
  118. client_subscribe_topic($hash,$value);
  119. }
  120. } else {
  121. if (defined $hash->{'.autoSubscribeTopic'}) {
  122. client_unsubscribe_topic($hash,$hash->{'.autoSubscribeTopic'});
  123. delete $hash->{'.autoSubscribeTopic'};
  124. delete $hash->{'.autoSubscribeExpr'};
  125. }
  126. }
  127. last;
  128. };
  129. $attribute =~ /^publishSet(_?)(.*)/ and do {
  130. if ($command eq "set") {
  131. my @values = split ("[ \t]+",$value);
  132. my $topic = pop @values;
  133. $hash->{publishSets}->{$2} = {
  134. 'values' => \@values,
  135. topic => $topic,
  136. };
  137. if ($2 eq "") {
  138. foreach my $set (@values) {
  139. $hash->{sets}->{$set}="";
  140. }
  141. } else {
  142. $hash->{sets}->{$2}=join(",",@values);
  143. }
  144. } else {
  145. if ($2 eq "") {
  146. foreach my $set (@{$hash->{publishSets}->{$2}->{'values'}}) {
  147. delete $hash->{sets}->{$set};
  148. }
  149. } else {
  150. CommandDeleteReading(undef,"$hash->{NAME} $2");
  151. delete $hash->{sets}->{$2};
  152. }
  153. delete $hash->{publishSets}->{$2};
  154. }
  155. last;
  156. };
  157. client_attr($hash,$command,$name,$attribute,$value);
  158. }
  159. }
  160. sub onmessage($$$) {
  161. my ($hash,$topic,$message) = @_;
  162. if (defined (my $reading = $hash->{subscribeReadings}->{$topic})) {
  163. Log3($hash->{NAME},5,"calling readingsSingleUpdate($hash->{NAME},$reading,$message,1");
  164. readingsSingleUpdate($hash,$reading,$message,1);
  165. } elsif ($topic =~ $hash->{'.autoSubscribeExpr'}) {
  166. Log3($hash->{NAME},5,"calling readingsSingleUpdate($hash->{NAME},$1,$message,1");
  167. CommandAttr(undef,"$hash->{NAME} subscribeReading_$1 $topic");
  168. readingsSingleUpdate($hash,$1,$message,1);
  169. }
  170. }
  171. 1;
  172. =pod
  173. =item [device]
  174. =item summary MQTT_DEVICE acts as a fhem-device that is mapped to mqtt-topics
  175. =begin html
  176. <a name="MQTT_DEVICE"></a>
  177. <h3>MQTT_DEVICE</h3>
  178. <ul>
  179. <p>acts as a fhem-device that is mapped to <a href="http://mqtt.org/">mqtt</a>-topics.</p>
  180. <p>requires a <a href="#MQTT">MQTT</a>-device as IODev<br/>
  181. Note: this module is based on <a href="https://metacpan.org/pod/distribution/Net-MQTT/lib/Net/MQTT.pod">Net::MQTT</a> which needs to be installed from CPAN first.</p>
  182. <a name="MQTT_DEVICEdefine"></a>
  183. <p><b>Define</b></p>
  184. <ul>
  185. <p><code>define &lt;name&gt; MQTT_DEVICE</code><br/>
  186. Specifies the MQTT device.</p>
  187. </ul>
  188. <a name="MQTT_DEVICEset"></a>
  189. <p><b>Set</b></p>
  190. <ul>
  191. <li>
  192. <p><code>set &lt;name&gt; &lt;command&gt;</code><br/>
  193. sets reading 'state' and publishes the command to topic configured via attr publishSet</p>
  194. </li>
  195. <li>
  196. <p><code>set &lt;name&gt; &lt;h;reading&gt; &lt;value&gt;</code><br/>
  197. sets reading &lt;h;reading&gt; and publishes the command to topic configured via attr publishSet_&lt;h;reading&gt;</p>
  198. </li>
  199. </ul>
  200. <a name="MQTT_DEVICEattr"></a>
  201. <p><b>Attributes</b></p>
  202. <ul>
  203. <li>
  204. <p><code>attr &lt;name&gt; publishSet [&lt;commands&gt;] &lt;topic&gt;</code><br/>
  205. configures set commands that may be used to both set reading 'state' and publish to configured topic</p>
  206. </li>
  207. <li>
  208. <p><code>attr &lt;name&gt; publishSet_&lt;reading&gt; [&lt;values&gt;] &lt;topic&gt;</code><br/>
  209. configures reading that may be used to both set 'reading' (to optionally configured values) and publish to configured topic</p>
  210. </li>
  211. <li>
  212. <p><code>attr &lt;name&gt; autoSubscribeReadings &lt;topic&gt;</code><br/>
  213. specify a mqtt-topic pattern with wildcard (e.c. 'myhouse/kitchen/+') and MQTT_DEVICE automagically creates readings based on the wildcard-match<br/>
  214. e.g a message received with topic 'myhouse/kitchen/temperature' would create and update a reading 'temperature'</p>
  215. </li>
  216. <li>
  217. <p><code>attr &lt;name&gt; subscribeReading_&lt;reading&gt; &lt;topic&gt;</code><br/>
  218. mapps a reading to a specific topic. The reading is updated whenever a message to the configured topic arrives</p>
  219. </li>
  220. </ul>
  221. </ul>
  222. =end html
  223. =cut