10_MYSENSORS_DEVICE.pm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. ##############################################
  2. #
  3. # fhem bridge to MySensors (see http://mysensors.org)
  4. #
  5. # Copyright (C) 2014 Norbert Truchsess
  6. #
  7. # This file is part of fhem.
  8. #
  9. # Fhem is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # Fhem is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. # $Id: 10_MYSENSORS_DEVICE.pm 15790 2018-01-05 12:04:05Z Hauswart $
  23. #
  24. ##############################################
  25. use strict;
  26. use warnings;
  27. my %gets = (
  28. "version" => "",
  29. );
  30. sub MYSENSORS_DEVICE_Initialize($) {
  31. my $hash = shift @_;
  32. # Consumer
  33. $hash->{DefFn} = "MYSENSORS::DEVICE::Define";
  34. $hash->{UndefFn} = "MYSENSORS::DEVICE::UnDefine";
  35. $hash->{SetFn} = "MYSENSORS::DEVICE::Set";
  36. $hash->{AttrFn} = "MYSENSORS::DEVICE::Attr";
  37. $hash->{AttrList} =
  38. "config:M,I " .
  39. "mode:node,repeater " .
  40. "version:1.4 " .
  41. "setCommands " .
  42. "setReading_.+ " .
  43. "mapReadingType_.+ " .
  44. "mapReading_.+ " .
  45. "requestAck:1 " .
  46. "IODev " .
  47. "showtime:0,1 " .
  48. $main::readingFnAttributes;
  49. main::LoadModule("MYSENSORS");
  50. }
  51. package MYSENSORS::DEVICE;
  52. use strict;
  53. use warnings;
  54. use GPUtils qw(:all);
  55. use Device::MySensors::Constants qw(:all);
  56. use Device::MySensors::Message qw(:all);
  57. use SetExtensions qw/ :all /;
  58. BEGIN {
  59. MYSENSORS->import(qw(:all));
  60. GP_Import(qw(
  61. AttrVal
  62. readingsSingleUpdate
  63. CommandAttr
  64. CommandDeleteAttr
  65. CommandDeleteReading
  66. AssignIoPort
  67. Log3
  68. SetExtensions
  69. ReadingsVal
  70. ))
  71. };
  72. my %static_types = (
  73. S_DOOR => { receives => [], sends => [V_TRIPPED,V_ARMED] }, # Door and window sensors
  74. S_MOTION => { receives => [], sends => [V_TRIPPED,V_ARMED] }, # Motion sensors
  75. S_SMOKE => { receives => [], sends => [V_TRIPPED,V_ARMED] }, # Smoke sensor
  76. S_BINARY => { receives => [V_STATUS,V_WATT], sends => [V_STATUS,V_WATT] }, # Binary device (on/off)
  77. S_DIMMER => { receives => [V_STATUS,V_PERCENTAGE,V_WATT], sends => [V_STATUS,V_PERCENTAGE,V_WATT] }, # Dimmable device of some kind
  78. S_COVER => { receives => [V_UP,V_DOWN,V_STOP,V_PERCENTAGE], sends => [V_UP,V_DOWN,V_STOP,V_PERCENTAGE] }, # Window covers or shades
  79. S_TEMP => { receives => [], sends => [V_TEMP,V_ID] }, # Temperature sensor
  80. S_HUM => { receives => [], sends => [V_HUM] }, # Humidity sensor
  81. S_BARO => { receives => [], sends => [V_PRESSURE,V_FORECAST] }, # Barometer sensor (Pressure)
  82. S_WIND => { receives => [], sends => [V_WIND,V_GUST,V_DIRECTION] }, # Wind sensor
  83. S_RAIN => { receives => [], sends => [V_RAIN,V_RAINRATE] }, # Rain sensor
  84. S_UV => { receives => [], sends => [V_UV] }, # UV sensor
  85. S_WEIGHT => { receives => [], sends => [V_WEIGHT,V_IMPEDANCE] }, # Weight sensor for scales etc.
  86. S_POWER => { receives => [V_VAR1], sends => [V_WATT,V_KWH,V_VAR,V_VA,V_POWER_FACTOR,V_VAR1] }, # Power measuring device, like power meters
  87. S_HEATER => { receives => [], sends => [V_HVAC_SETPOINT_HEAT,V_HVAC_FLOW_STATE,V_TEMP,V_STATUS] }, # Heater device
  88. S_DISTANCE => { receives => [], sends => [V_DISTANCE,V_UNIT_PREFIX] }, # Distance sensor
  89. S_LIGHT_LEVEL => { receives => [], sends => [V_LIGHT_LEVEL,V_LEVEL] }, # Light sensor
  90. S_ARDUINO_NODE => { receives => [], sends => [] }, # Arduino node device
  91. S_ARDUINO_REPEATER_NODE => { receives => [], sends => [] }, # Arduino repeating node device
  92. S_LOCK => { receives => [V_LOCK_STATUS], sends => [V_LOCK_STATUS] }, # Lock device
  93. S_IR => { receives => [V_IR_SEND], sends => [V_IR_RECEIVE,V_IR_RECORD,V_IR_SEND] }, # Ir sender/receiver device
  94. S_WATER => { receives => [V_VAR1], sends => [V_FLOW,V_VOLUME,V_VAR1] }, # Water meter
  95. S_AIR_QUALITY => { receives => [], sends => [V_LEVEL,V_UNIT_PREFIX] }, # Air quality sensor e.g. MQ-2
  96. S_CUSTOM => { receives => [V_VAR1,V_VAR2,V_VAR3,V_VAR4,V_VAR5], sends => [V_VAR1,V_VAR2,V_VAR3,V_VAR4,V_VAR5] }, # Use this for custom sensors where no other fits.
  97. S_DUST => { receives => [], sends => [V_LEVEL,V_UNIT_PREFIX] }, # Dust level sensor
  98. S_SCENE_CONTROLLER => { receives => [], sends => [V_SCENE_ON,V_SCENE_OFF] }, # Scene controller device
  99. S_RGB_LIGHT => { receives => [V_RGB,V_WATT], sends => [V_RGB,V_WATT] }, # RGB light
  100. S_RGBW_LIGHT => { receives => [V_RGBW,V_WATT], sends => [V_RGBW,V_WATT] }, # RGBW light (with separate white component)
  101. S_COLOR_SENSOR => { receives => [V_RGB], sends => [V_RGB] }, # Color sensor
  102. S_HVAC => { receives => [], sends => [V_STATUS,V_TEMP,V_HVAC_SETPOINT_HEAT,V_HVAC_SETPOINT_COOL,V_HVAC_FLOW_STATE,V_HVAC_FLOW_MODE,V_HVAC_SPEED] }, # Thermostat/HVAC device
  103. S_MULTIMETER => { receives => [], sends => [V_VOLTAGE,V_CURRENT,V_IMPEDANCE] }, # Multimeter device
  104. S_SPRINKLER => { receives => [], sends => [V_STATUS,V_TRIPPED] }, # Sprinkler device
  105. S_WATER_LEAK => { receives => [], sends => [V_TRIPPED,V_ARMED] }, # Water leak sensor
  106. S_SOUND => { receives => [], sends => [V_LEVEL,V_TRIPPED,V_ARMED] }, # Sound sensor
  107. S_VIBRATION => { receives => [], sends => [V_LEVEL,V_TRIPPED,V_ARMED] }, # Vibration sensor
  108. S_MOISTURE => { receives => [], sends => [V_LEVEL,V_TRIPPED,V_ARMED] }, # Moisture sensor
  109. S_INFO => { receives => [V_TEXT], sends => [V_TEXT] }, # LCD text device
  110. S_GAS => { receives => [], sends => [V_FLOW,V_VOLUME] }, # Gas meter
  111. S_GPS => { receives => [], sends => [V_POSITION] }, # GPS Sensor
  112. S_WATER_QUALITY => { receives => [], sends => [V_TEMP,V_PH,V_ORP,V_EC,V_STATUS] }, # Water quality sensor
  113. );
  114. my %static_mappings = (
  115. V_TEMP => { type => "temperature" },
  116. V_HUM => { type => "humidity" },
  117. V_STATUS => { type => "status", val => { 0 => 'off', 1 => 'on' }},
  118. V_PERCENTAGE => { type => "percentage", range => { min => 0, step => 1, max => 100 }},
  119. V_PRESSURE => { type => "pressure" },
  120. V_FORECAST => { type => "forecast", val => { # PressureSensor, DP/Dt explanation
  121. 0 => 'stable', # 0 = "Stable Weather Pattern"
  122. 1 => 'sunny', # 1 = "Slowly rising Good Weather", "Clear/Sunny"
  123. 2 => 'cloudy', # 2 = "Slowly falling L-Pressure ", "Cloudy/Rain"
  124. 3 => 'unstable', # 3 = "Quickly rising H-Press", "Not Stable"
  125. 4 => 'thunderstorm',# 4 = "Quickly falling L-Press", "Thunderstorm"
  126. 5 => 'unknown' }}, # 5 = "Unknown (More Time needed)
  127. V_RAIN => { type => "rain" },
  128. V_RAINRATE => { type => "rainrate" },
  129. V_WIND => { type => "wind" },
  130. V_GUST => { type => "gust" },
  131. V_DIRECTION => { type => "direction" },
  132. V_UV => { type => "uv" },
  133. V_WEIGHT => { type => "weight" },
  134. V_DISTANCE => { type => "distance" },
  135. V_IMPEDANCE => { type => "impedance" },
  136. V_ARMED => { type => "armed", val => { 0 => 'off', 1 => 'on' }},
  137. V_TRIPPED => { type => "tripped", val => { 0 => 'off', 1 => 'on' }},
  138. V_WATT => { type => "power" },
  139. V_KWH => { type => "energy" },
  140. V_SCENE_ON => { type => "button_on" },
  141. V_SCENE_OFF => { type => "button_off" },
  142. V_HVAC_FLOW_STATE => { type => "hvacflowstate" },
  143. V_HVAC_SPEED => { type => "hvacspeed" },
  144. V_LIGHT_LEVEL => { type => "brightness", range => { min => 0, step => 1, max => 100 }},
  145. V_VAR1 => { type => "value1" },
  146. V_VAR2 => { type => "value2" },
  147. V_VAR3 => { type => "value3" },
  148. V_VAR4 => { type => "value4" },
  149. V_VAR5 => { type => "value5" },
  150. V_UP => { type => "up" },
  151. V_DOWN => { type => "down" },
  152. V_STOP => { type => "stop" },
  153. V_IR_SEND => { type => "ir_send" },
  154. V_IR_RECEIVE => { type => "ir_receive" },
  155. V_FLOW => { type => "flow" },
  156. V_VOLUME => { type => "volume" },
  157. V_LOCK_STATUS => { type => "lockstatus", val => { 0 => 'off', 1 => 'on' }},
  158. V_LEVEL => { type => "level" },
  159. V_VOLTAGE => { type => "voltage" },
  160. V_CURRENT => { type => "current" },
  161. V_RGB => { type => "rgb" },
  162. V_RGBW => { type => "rgbw" },
  163. V_ID => { type => "id" },
  164. V_UNIT_PREFIX => { type => "unitprefix" },
  165. V_HVAC_SETPOINT_COOL => { type => "hvacsetpointcool" },
  166. V_HVAC_SETPOINT_HEAT => { type => "hvacsetpointheat" },
  167. V_HVAC_FLOW_MODE => { type => "hvacflowmode" },
  168. V_TEXT => { type => "text" },
  169. V_CUSTOM => { type => "custom" },
  170. V_POSITION => { type => "position" },
  171. V_IR_RECORD => { type => "ir_record" },
  172. V_PH => { type => "ph" },
  173. V_ORP => { type => "orp" },
  174. V_EC => { type => "ec" },
  175. V_VAR => { type => "value" },
  176. V_VA => { type => "va" },
  177. V_POWER_FACTOR => { type => "power_factor" },
  178. );
  179. sub Define($$) {
  180. my ( $hash, $def ) = @_;
  181. my ($name, $type, $radioId) = split("[ \t]+", $def);
  182. return "requires 1 parameters" unless (defined $radioId and $radioId ne "");
  183. $hash->{radioId} = $radioId;
  184. $hash->{sets} = {
  185. 'time' => "",
  186. reboot => "",
  187. # clear => "",
  188. };
  189. $hash->{ack} = 0;
  190. $hash->{typeMappings} = {map {variableTypeToIdx($_) => $static_mappings{$_}} keys %static_mappings};
  191. $hash->{sensorMappings} = {map {sensorTypeToIdx($_) => $static_types{$_}} keys %static_types};
  192. $hash->{readingMappings} = {};
  193. AssignIoPort($hash);
  194. };
  195. sub UnDefine($) {
  196. my ($hash) = @_;
  197. return undef;
  198. }
  199. sub Set($@) {
  200. my ($hash,$name,$command,@values) = @_;
  201. return "Need at least one parameters" unless defined $command;
  202. if(!defined($hash->{sets}->{$command})) {
  203. my $list = join(" ", map {$hash->{sets}->{$_} ne "" ? "$_:$hash->{sets}->{$_}" : $_} sort keys %{$hash->{sets}});
  204. return grep (/(^on$)|(^off$)/,keys %{$hash->{sets}}) == 2 ? SetExtensions($hash, $list, $name, $command, @values) : "Unknown argument $command, choose one of $list";
  205. }
  206. COMMAND_HANDLER: {
  207. # $command eq "clear" and do {
  208. # # Test 102 anstatt 255 :) und Log
  209. # sendClientMessage($hash, childId => 255, cmd => C_INTERNAL, subType => I_CHILDREN, payload => "C");
  210. # Log3 ($name,3,"MYSENSORS_DEVICE $name: clear");
  211. # # Test
  212. # last;
  213. # };
  214. $command eq "time" and do {
  215. sendClientMessage($hash, childId => 255, cmd => C_INTERNAL, subType => I_TIME, payload => time);
  216. last;
  217. };
  218. $command eq "reboot" and do {
  219. sendClientMessage($hash, childId => 255, cmd => C_INTERNAL, subType => I_REBOOT);
  220. last;
  221. };
  222. (defined ($hash->{setcommands}->{$command})) and do {
  223. my $setcommand = $hash->{setcommands}->{$command};
  224. eval {
  225. my ($type,$childId,$mappedValue) = mappedReadingToRaw($hash,$setcommand->{var},$setcommand->{val});
  226. sendClientMessage($hash,
  227. childId => $childId,
  228. cmd => C_SET,
  229. subType => $type,
  230. payload => $mappedValue,
  231. );
  232. readingsSingleUpdate($hash,$setcommand->{var},$setcommand->{val},1) unless ($hash->{ack} or $hash->{IODev}->{ack});
  233. };
  234. return "$command not defined: ".GP_Catch($@) if $@;
  235. last;
  236. };
  237. my $value = @values ? join " ",@values : "";
  238. eval {
  239. my ($type,$childId,$mappedValue) = mappedReadingToRaw($hash,$command,$value);
  240. sendClientMessage($hash, childId => $childId, cmd => C_SET, subType => $type, payload => $mappedValue);
  241. readingsSingleUpdate($hash,$command,$value,1) unless ($hash->{ack} or $hash->{IODev}->{ack});
  242. };
  243. return "$command not defined: ".GP_Catch($@) if $@;
  244. }
  245. }
  246. sub Attr($$$$) {
  247. my ($command,$name,$attribute,$value) = @_;
  248. my $hash = $main::defs{$name};
  249. ATTRIBUTE_HANDLER: {
  250. $attribute eq "config" and do {
  251. if ($main::init_done) {
  252. sendClientMessage($hash, cmd => C_INTERNAL, childId => 255, subType => I_CONFIG, payload => $command eq 'set' ? $value : "M");
  253. }
  254. last;
  255. };
  256. $attribute eq "mode" and do {
  257. if ($command eq "set" and $value eq "repeater") {
  258. $hash->{repeater} = 1;
  259. # $hash->{sets}->{clear} = "";
  260. } else {
  261. $hash->{repeater} = 0;
  262. # delete $hash->{sets}->{clear};
  263. }
  264. last;
  265. };
  266. $attribute eq "version" and do {
  267. if ($command eq "set") {
  268. $hash->{protocol} = $value;
  269. } else {
  270. delete $hash->{protocol};
  271. }
  272. last;
  273. };
  274. $attribute eq "setCommands" and do {
  275. foreach my $set (keys %{$hash->{setcommands}}) {
  276. delete $hash->{sets}->{$set};
  277. }
  278. $hash->{setcommands} = {};
  279. if ($command eq "set" and $value) {
  280. foreach my $setCmd (split ("[, \t]+",$value)) {
  281. if ($setCmd =~ /^(.+):(.+):(.+)$/) {
  282. $hash->{sets}->{$1}="";
  283. $hash->{setcommands}->{$1} = {
  284. var => $2,
  285. val => $3,
  286. };
  287. } else {
  288. return "unparsable value in setCommands for $name: $setCmd";
  289. }
  290. }
  291. }
  292. last;
  293. };
  294. $attribute =~ /^setReading_(.+)$/ and do {
  295. if ($command eq "set") {
  296. $hash->{sets}->{$1}= (defined $value) ? join(",",split ("[, \t]+",$value)) : "";
  297. } else {
  298. CommandDeleteReading(undef,"$hash->{NAME} $1");
  299. delete $hash->{sets}->{$1};
  300. }
  301. last;
  302. };
  303. $attribute =~ /^mapReadingType_(.+)/ and do {
  304. my $type = variableTypeToIdx("V_$1");
  305. if ($command eq "set") {
  306. my @values = split ("[, \t]",$value);
  307. $hash->{typeMappings}->{$type}={
  308. type => shift @values,
  309. val => {map {$_ =~ /^(.+):(.+)$/; $1 => $2} @values},
  310. }
  311. } else {
  312. if ($static_mappings{"V_$1"}) {
  313. $hash->{typeMappings}->{$type}=$static_mappings{"V_$1"};
  314. } else {
  315. delete $hash->{typeMappings}->{$type};
  316. }
  317. my $readings = $hash->{READINGS};
  318. my $readingMappings = $hash->{readingMappings};
  319. foreach my $todelete (map {$readingMappings->{$_}->{name}} grep {$readingMappings->{$_}->{type} == $type} keys %$readingMappings) {
  320. CommandDeleteReading(undef,"$hash->{NAME} $todelete"); #TODO do propper remap of existing readings
  321. }
  322. }
  323. last;
  324. };
  325. $attribute =~ /^mapReading_(.+)/ and do {
  326. my $readingMappings = $hash->{readingMappings};
  327. FIND: foreach my $id (keys %$readingMappings) {
  328. my $readingsForId = $readingMappings->{$id};
  329. foreach my $type (keys %$readingsForId) {
  330. if (($readingsForId->{$type}->{name} // "") eq $1) {
  331. delete $readingsForId->{$type};
  332. unless (keys %$readingsForId) {
  333. delete $readingMappings->{$id};
  334. }
  335. last FIND;
  336. }
  337. }
  338. }
  339. if ($command eq "set") {
  340. my ($id,$typeStr,@values) = split ("[, \t]",$value);
  341. my $typeMappings = $hash->{typeMappings};
  342. if (my @match = grep {$typeMappings->{$_}->{type} eq $typeStr} keys %$typeMappings) {
  343. my $type = shift @match;
  344. $readingMappings->{$id}->{$type}->{name} = $1;
  345. if (@values) {
  346. $readingMappings->{$id}->{$type}->{val} = {map {$_ =~ /^(.+):(.+)$/; $1 => $2} @values}; #TODO range?
  347. }
  348. } else {
  349. return "unknown reading type $typeStr";
  350. }
  351. } else {
  352. CommandDeleteReading(undef,"$hash->{NAME} $1");
  353. }
  354. last;
  355. };
  356. $attribute eq "requestAck" and do {
  357. if ($command eq "set") {
  358. $hash->{ack} = 1;
  359. } else {
  360. $hash->{ack} = 0;
  361. }
  362. last;
  363. };
  364. }
  365. }
  366. sub onGatewayStarted($) {
  367. my ($hash) = @_;
  368. }
  369. sub onPresentationMessage($$) {
  370. my ($hash,$msg) = @_;
  371. my $name = $hash->{NAME};
  372. my $nodeType = $msg->{subType};
  373. my $id = $msg->{childId};
  374. if ($id == 255) { #special id
  375. NODETYPE: {
  376. $nodeType == S_ARDUINO_NODE and do {
  377. CommandAttr(undef, "$name mode node");
  378. last;
  379. };
  380. $nodeType == S_ARDUINO_REPEATER_NODE and do {
  381. CommandAttr(undef, "$name mode repeater");
  382. last;
  383. };
  384. };
  385. CommandAttr(undef, "$name version $msg->{payload}");
  386. };
  387. my $readingMappings = $hash->{readingMappings};
  388. my $typeMappings = $hash->{typeMappings};
  389. if (my $sensorMappings = $hash->{sensorMappings}->{$nodeType}) {
  390. my $idStr = ($id > 0 ? $id : "");
  391. my @ret = ();
  392. foreach my $type (@{$sensorMappings->{sends}}) {
  393. next if (defined $readingMappings->{$id}->{$type});
  394. my $typeStr = $typeMappings->{$type}->{type};
  395. if ($hash->{IODev}->{'inclusion-mode'}) {
  396. if (my $ret = CommandAttr(undef,"$name mapReading_$typeStr$idStr $id $typeStr")) {
  397. push @ret,$ret;
  398. }
  399. } else {
  400. push @ret,"no mapReading for $id, $typeStr";
  401. }
  402. }
  403. foreach my $type (@{$sensorMappings->{receives}}) {
  404. my $typeMapping = $typeMappings->{$type};
  405. my $typeStr = $typeMapping->{type};
  406. next if (defined $hash->{sets}->{"$typeStr$idStr"});
  407. if ($hash->{IODev}->{'inclusion-mode'}) {
  408. my @values = ();
  409. if ($typeMapping->{range}) {
  410. @values = ('slider',$typeMapping->{range}->{min},$typeMapping->{range}->{step},$typeMapping->{range}->{max});
  411. } elsif ($typeMapping->{val}) {
  412. @values = values %{$typeMapping->{val}};
  413. }
  414. if (my $ret = CommandAttr(undef,"$name setReading_$typeStr$idStr".(@values ? " ".join (",",@values) : ""))) {
  415. push @ret,$ret;
  416. }
  417. } else {
  418. push @ret,"no setReading for $id, $typeStr";
  419. }
  420. }
  421. Log3 ($hash->{NAME}, 4, "MYSENSORS_DEVICE $hash->{NAME}: errors on C_PRESENTATION-message for childId $id, subType ".sensorTypeToStr($nodeType)." ".join (", ",@ret)) if @ret;
  422. }
  423. }
  424. sub onSetMessage($$) {
  425. my ($hash,$msg) = @_;
  426. if (defined $msg->{payload}) {
  427. eval {
  428. my ($reading,$value) = rawToMappedReading($hash,$msg->{subType},$msg->{childId},$msg->{payload});
  429. readingsSingleUpdate($hash, $reading, $value, 1);
  430. };
  431. Log3 ($hash->{NAME}, 4, "MYSENSORS_DEVICE $hash->{NAME}: ignoring C_SET-message ".GP_Catch($@)) if $@;
  432. } else {
  433. Log3 ($hash->{NAME}, 5, "MYSENSORS_DEVICE $hash->{NAME}: ignoring C_SET-message without payload");
  434. }
  435. }
  436. sub onRequestMessage($$) {
  437. my ($hash,$msg) = @_;
  438. eval {
  439. my ($readingname,$val) = rawToMappedReading($hash, $msg->{subType}, $msg->{childId}, $msg->{payload});
  440. sendClientMessage($hash,
  441. childId => $msg->{childId},
  442. cmd => C_SET,
  443. subType => $msg->{subType},
  444. payload => ReadingsVal($hash->{NAME},$readingname,$val)
  445. );
  446. };
  447. Log3 ($hash->{NAME}, 4, "MYSENSORS_DEVICE $hash->{NAME}: ignoring C_REQ-message ".GP_Catch($@)) if $@;
  448. }
  449. sub onInternalMessage($$) {
  450. my ($hash,$msg) = @_;
  451. my $name = $hash->{NAME};
  452. my $type = $msg->{subType};
  453. my $typeStr = internalMessageTypeToStr($type);
  454. INTERNALMESSAGE: {
  455. $type == I_BATTERY_LEVEL and do {
  456. readingsSingleUpdate($hash, "batterylevel", $msg->{payload}, 1);
  457. Log3 ($name, 4, "MYSENSORS_DEVICE $name: batterylevel $msg->{payload}");
  458. last;
  459. };
  460. $type == I_TIME and do {
  461. if ($msg->{ack}) {
  462. Log3 ($name, 4, "MYSENSORS_DEVICE $name: response to time-request acknowledged");
  463. } else {
  464. sendClientMessage($hash,cmd => C_INTERNAL, childId => 255, subType => I_TIME, payload => time);
  465. Log3 ($name, 4, "MYSENSORS_DEVICE $name: update of time requested");
  466. }
  467. last;
  468. };
  469. $type == I_VERSION and do {
  470. $hash->{$typeStr} = $msg->{payload};
  471. last;
  472. };
  473. $type == I_ID_REQUEST and do {
  474. $hash->{$typeStr} = $msg->{payload};
  475. last;
  476. };
  477. $type == I_ID_RESPONSE and do {
  478. $hash->{$typeStr} = $msg->{payload};
  479. last;
  480. };
  481. $type == I_INCLUSION_MODE and do {
  482. $hash->{$typeStr} = $msg->{payload};
  483. last;
  484. };
  485. $type == I_CONFIG and do {
  486. if ($msg->{ack}) {
  487. Log3 ($name, 4, "MYSENSORS_DEVICE $name: response to config-request acknowledged");
  488. } else {
  489. readingsSingleUpdate($hash, "parentId", $msg->{payload}, 1);
  490. sendClientMessage($hash,cmd => C_INTERNAL, childId => 255, subType => I_CONFIG, payload => AttrVal($name,"config","M"));
  491. Log3 ($name, 4, "MYSENSORS_DEVICE $name: respond to config-request, node parentId = " . $msg->{payload});
  492. }
  493. last;
  494. };
  495. $type == I_FIND_PARENT and do {
  496. $hash->{$typeStr} = $msg->{payload};
  497. last;
  498. };
  499. $type == I_FIND_PARENT_RESPONSE and do {
  500. $hash->{$typeStr} = $msg->{payload};
  501. last;
  502. };
  503. $type == I_LOG_MESSAGE and do {
  504. $hash->{$typeStr} = $msg->{payload};
  505. last;
  506. };
  507. $type == I_CHILDREN and do {
  508. readingsSingleUpdate($hash, "state", "routingtable cleared", 1);
  509. Log3 ($name, 3, "MYSENSORS_DEVICE $name: routingtable cleared");
  510. last;
  511. };
  512. $type == I_SKETCH_NAME and do {
  513. $hash->{$typeStr} = $msg->{payload};
  514. readingsSingleUpdate($hash, "SKETCH_NAME", $msg->{payload}, 1);
  515. last;
  516. };
  517. $type == I_SKETCH_VERSION and do {
  518. $hash->{$typeStr} = $msg->{payload};
  519. readingsSingleUpdate($hash, "SKETCH_VERSION", $msg->{payload}, 1);
  520. last;
  521. };
  522. $type == I_REBOOT and do {
  523. $hash->{$typeStr} = $msg->{payload};
  524. last;
  525. };
  526. $type == I_GATEWAY_READY and do {
  527. $hash->{$typeStr} = $msg->{payload};
  528. last;
  529. };
  530. $type == I_REQUEST_SIGNING and do {
  531. $hash->{$typeStr} = $msg->{payload};
  532. last;
  533. };
  534. $type == I_GET_NONCE and do {
  535. $hash->{$typeStr} = $msg->{payload};
  536. last;
  537. };
  538. $type == I_GET_NONCE_RESPONSE and do {
  539. $hash->{$typeStr} = $msg->{payload};
  540. last;
  541. };
  542. }
  543. }
  544. sub sendClientMessage($%) {
  545. my ($hash,%msg) = @_;
  546. $msg{radioId} = $hash->{radioId};
  547. $msg{ack} = $hash->{ack} unless defined $msg{ack};
  548. sendMessage($hash->{IODev},%msg);
  549. }
  550. sub rawToMappedReading($$$$) {
  551. my($hash, $type, $childId, $value) = @_;
  552. my $name;
  553. if (defined (my $mapping = $hash->{readingMappings}->{$childId}->{$type})) {
  554. my $val = $mapping->{val} // $hash->{typeMappings}->{$type}->{val};
  555. return ($mapping->{name},defined $val ? ($val->{$value} // $value) : $value);
  556. }
  557. die "no reading-mapping for childId $childId, type ".($hash->{typeMappings}->{$type}->{type} ? $hash->{typeMappings}->{$type}->{type} : variableTypeToStr($type));
  558. }
  559. sub mappedReadingToRaw($$$) {
  560. my ($hash,$reading,$value) = @_;
  561. my $readingsMapping = $hash->{readingMappings};
  562. foreach my $id (keys %$readingsMapping) {
  563. my $readingTypesForId = $readingsMapping->{$id};
  564. foreach my $type (keys %$readingTypesForId) {
  565. if (($readingTypesForId->{$type}->{name} // "") eq $reading) {
  566. if (my $valueMappings = $readingTypesForId->{$type}->{val} // $hash->{typeMappings}->{$type}->{val}) {
  567. if (my @mappedValues = grep {$valueMappings->{$_} eq $value} keys %$valueMappings) {
  568. return ($type,$id,shift @mappedValues);
  569. }
  570. }
  571. return ($type,$id,$value);
  572. }
  573. }
  574. }
  575. die "no mapping for reading $reading";
  576. }
  577. 1;
  578. =pod
  579. =item device
  580. =item summary includes MYSENSOR clients
  581. =item summary_DE integriert MYSENSOR Sensoren
  582. =begin html
  583. <a name="MYSENSORS_DEVICE"></a>
  584. <h3>MYSENSORS_DEVICE</h3>
  585. <ul>
  586. <p>represents a mysensors sensor attached to a mysensor-node</p>
  587. <p>requires a <a href="#MYSENSOR">MYSENSOR</a>-device as IODev</p>
  588. <a name="MYSENSORS_DEVICEdefine"></a>
  589. <p><b>Define</b></p>
  590. <ul>
  591. <p><code>define &lt;name&gt; MYSENSORS_DEVICE &lt;Sensor-type&gt; &lt;node-id&gt;</code><br/>
  592. Specifies the MYSENSOR_DEVICE device.</p>
  593. </ul>
  594. <a name="MYSENSORS_DEVICEset"></a>
  595. <p><b>Set</b></p>
  596. <ul>
  597. <li>
  598. <p><code>set &lt;name&gt; clear</code><br/>
  599. clears routing-table of a repeater-node</p>
  600. </li>
  601. <li>
  602. <p><code>set &lt;name&gt; time</code><br/>
  603. sets time for nodes (that support it)</p>
  604. </li>
  605. <li>
  606. <p><code>set &lt;name&gt; reboot</code><br/>
  607. reboots a node (requires a bootloader that supports it).<br/>
  608. Attention: Nodes that run the standard arduino-bootloader will enter a bootloop!<br/>
  609. Dis- and reconnect the nodes power to restart in this case.</p>
  610. </li>
  611. </ul>
  612. <a name="MYSENSORS_DEVICEattr"></a>
  613. <p><b>Attributes</b></p>
  614. <ul>
  615. <li>
  616. <p><code>attr &lt;name&gt; config [&lt;M|I&gt;]</code><br/>
  617. configures metric (M) or inch (I). Defaults to 'M'</p>
  618. </li>
  619. <li>
  620. <p><code>attr &lt;name&gt; setCommands [&lt;command:reading:value&gt;]*</code><br/>
  621. configures one or more commands that can be executed by set.<br/>
  622. e.g.: <code>attr &lt;name&gt; setCommands on:switch_1:on off:switch_1:off</code><br/>
  623. if list of commands contains both 'on' and 'off' <a href="#setExtensions">set extensions</a> are supported</p>
  624. </li>
  625. <li>
  626. <p><code>attr &lt;name&gt; setReading_&lt;reading&gt; [&lt;value&gt;]*</code><br/>
  627. configures a reading that can be modified by set-command<br/>
  628. e.g.: <code>attr &lt;name&gt; setReading_switch_1 on,off</code></p>
  629. </li>
  630. <li>
  631. <p><code>attr &lt;name&gt; mapReading_&lt;reading&gt; &lt;childId&gt; &lt;readingtype&gt; [&lt;value&gt;:&lt;mappedvalue&gt;]*</code><br/>
  632. configures the reading-name for a given childId and sensortype<br/>
  633. E.g.: <code>attr xxx mapReading_aussentemperatur 123 temperature</code></p>
  634. </li>
  635. <li>
  636. <p><code>att &lt;name&gt; requestAck</code><br/>
  637. request acknowledge from nodes.<br/>
  638. if set the Readings of nodes are updated not before requested acknowledge is received<br/>
  639. if not set the Readings of nodes are updated immediatly (not awaiting the acknowledge).<br/>
  640. May also be configured on the gateway for all nodes at once</p>
  641. </li>
  642. <li>
  643. <p><code>attr &lt;name&gt; mapReadingType_&lt;reading&gt; &lt;new reading name&gt; [&lt;value&gt;:&lt;mappedvalue&gt;]*</code><br/>
  644. configures reading type names that should be used instead of technical names<br/>
  645. E.g.: <code>attr xxx mapReadingType_LIGHT switch 0:on 1:off</code>
  646. to be used for mysensor Variabletypes that have no predefined defaults (yet)</p>
  647. </li>
  648. </ul>
  649. </ul>
  650. =end html
  651. =cut