From b068007bf3c8ecf35b679e488ce52064bc20d43c Mon Sep 17 00:00:00 2001
From: Beta-User <>
Date: Thu, 4 Apr 2019 04:08:41 +0000
Subject: [PATCH] 00_MYSENSORS: enhance support for multiple GW-nodes
 10_MYSENSORS_DEVICE: - change internal timer usage to comply to fhem
 standards - add support for attrTemplate

git-svn-id: https://svn.fhem.de/fhem/trunk@19108 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
 fhem/CHANGED                                  |   3 +
 fhem/FHEM/00_MYSENSORS.pm                     |  25 ++--
 fhem/FHEM/10_MYSENSORS_DEVICE.pm              |  18 ++-
 fhem/FHEM/lib/AttrTemplate/mysensors.template | 110 ++++++++++++++++++
 fhem/MAINTAINER.txt                           |   1 +
 5 files changed, 147 insertions(+), 10 deletions(-)
 create mode 100644 fhem/FHEM/lib/AttrTemplate/mysensors.template

diff --git a/fhem/CHANGED b/fhem/CHANGED
index 90e45d9fc..f5360354f 100644
--- a/fhem/CHANGED
+++ b/fhem/CHANGED
@@ -1,5 +1,8 @@
 # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
 # Do not insert empty lines here, update check depends on it.
+ - feature: 10_MYSENSORS_DEVICE: add attrTemplate support
+ - change:  00_MYSENSORS: enhance support for node functions
+                      when using multiple GW's
  - feature: 93_DbRep: new aggregation type year
  - bugfix   73_AMADCommBridge: fix little bug
  - bugfix:  70_BRAVIA: fix channel presets, fix use of ReadingsAge
diff --git a/fhem/FHEM/00_MYSENSORS.pm b/fhem/FHEM/00_MYSENSORS.pm
index a00193187..988a3e2d4 100644
--- a/fhem/FHEM/00_MYSENSORS.pm
+++ b/fhem/FHEM/00_MYSENSORS.pm
@@ -56,15 +56,18 @@ sub MYSENSORS_Initialize($) {
   $hash->{AttrFn}   = "MYSENSORS::Attr";
   $hash->{NotifyFn} = "MYSENSORS::Notify";
 
-  $hash->{AttrList} = 
-    "autocreate:1 ".
-    "requestAck:1 ".
-    "first-sensorid ".
-    "last-sensorid ".
-    "stateFormat ".
-    "OTA_firmwareConfig";
+   my @attrList = qw(
+    autocreate:1
+    requestAck:1
+    first-sensorid
+    last-sensorid
+    stateFormat
+    OTA_firmwareConfig
+  );
+  $hash->{AttrList} = $hash->{AttrList} = join(" ", @attrList)
 }
 
+  
 package MYSENSORS;
 
 use Exporter ('import');
@@ -330,7 +333,10 @@ sub onPresentationMsg($$) {
   unless ($client) {
     if ($hash->{'inclusion-mode'}) {
       $clientname = "MYSENSOR_$msg->{radioId}";
+      $clientname = "$hash->{NAME}_DEVICE_0"if defined $main::defs{$clientname}; 
       CommandDefine(undef,"$clientname MYSENSORS_DEVICE $msg->{radioId}");
+      CommandAttr(undef,"$clientname IODev $hash->{NAME}");
+      CommandAttr(undef,"$clientname room MYSENSORS_DEVICE");
       $client = $main::defs{$clientname};
       return unless ($client);
     } else {
@@ -385,8 +391,9 @@ sub onInternalMsg($$) {
         last;
       };
       $type == I_HEARTBEAT_RESPONSE and do {
-        RemoveInternalTimer($hash,"MYSENSORS::Start"); ## Reset reconnect because timeout was not reached
-	readingsSingleUpdate($hash, "heartbeat", "last", 0);
+         RemoveInternalTimer($hash,"MYSENSORS::Start"); ## Reset reconnect because timeout was not reached
+         readingsSingleUpdate($hash, "heartbeat", "alive", 0);
+         if (my $client = matchClient($hash,$msg)){ MYSENSORS::DEVICE::onInternalMessage($client,$msg) };
       };
       $type == I_VERSION and do {
         $hash->{version} = $msg->{payload};
diff --git a/fhem/FHEM/10_MYSENSORS_DEVICE.pm b/fhem/FHEM/10_MYSENSORS_DEVICE.pm
index 35eb422c2..651b510dc 100755
--- a/fhem/FHEM/10_MYSENSORS_DEVICE.pm
+++ b/fhem/FHEM/10_MYSENSORS_DEVICE.pm
@@ -55,6 +55,7 @@ sub MYSENSORS_DEVICE_Initialize($) {
     OTA_autoUpdate:0,1
     OTA_BL_Type:Optiboot,MYSBootloader
     OTA_Chan76_IODev
+    model
   );
   use warnings 'qw';
   $hash->{AttrList} = join(" ", @attrList)." ".$readingFnAttributes;
@@ -250,7 +251,7 @@ sub Set($@) {
       $hash->{sets}->{fwType} = join(",", MYSENSORS::getFirmwareTypes($hash->{IODev}));
       my $list = join(" ", map {$hash->{sets}->{$_} ne "" ? "$_:$hash->{sets}->{$_}" : $_} sort keys %{$hash->{sets}});
       $hash->{sets}->{fwType} = "";
-      return grep (/(^on$)|(^off$)/,keys %{$hash->{sets}}) == 2 ? SetExtensions($hash, $list, $name, $command, @values) : "Unknown argument $command, choose one of $list";
+      return SetExtensions($hash, $list, $name, $command, @values);
     }
     
     COMMAND_HANDLER: {
@@ -1163,24 +1164,39 @@ sub sendRetainedMessages($) {
     <a name="MYSENSORS_DEVICEset"></a>
     <p><b>Set</b></p>
     <ul>
+      <b>AttrTemplate</b>
+      <li>Helps to easily configure your devices. Just get a list of all available attrTremplates by issuing
+      <ul>
+        <p><code>set &lt;name&gt; attrTemplate ?</code></p>
+      </ul>
+      Have a look at the descriptions and choose a suitable one. Then use the drop-down list and click "set" or issue a.<br>
+      <ul>
+        <p><code>set &lt;name&gt; attrTemplate A_02a_atmospheric_pressure</code></p>
+      </ul>
+      </li><br>
+      <b>clear</b>
       <li>
          <p><code>set &lt;name&gt; clear</code><br/>clears MySensors EEPROM area and reboot (i.e. "factory" reset) - requires MY_SPECIAL_DEBUG</p>
       </li>
+      <b>flash</b>
       <li>
          <p><code>set &lt;name&gt; flash</code><br/>
          Checks whether a newer firmware version is available. If a newer firmware version is
          available the flash procedure is started. The sensor node must support FOTA for
          this.</p>
       </li>
+      <b>fwType</b>
       <li>
          <p><code>set &lt;name&gt; fwType &lt;value&gt;</code><br/>
          assigns a firmware type to this node (must be a numeric value in the range 0 .. 65536).
          Should be contained in the <a href="#MYSENSORSattrOTA_firmwareConfig">FOTA configuration
          file</a>.</p>
       </li>
+      <b>time</b>
       <li>
         <p><code>set &lt;name&gt; time</code><br/>sets time for nodes (that support it)</p>
       </li>
+      <b>reboot</b>
       <li>
         <p><code>set &lt;name&gt; reboot</code><br/>reboots a node (requires a bootloader that supports it).<br/>Attention: Nodes that run the standard arduino-bootloader will enter a bootloop!<br/>Dis- and reconnect the nodes power to restart in this case.</p>
     </li>
diff --git a/fhem/FHEM/lib/AttrTemplate/mysensors.template b/fhem/FHEM/lib/AttrTemplate/mysensors.template
new file mode 100644
index 000000000..366aacab4
--- /dev/null
+++ b/fhem/FHEM/lib/AttrTemplate/mysensors.template
@@ -0,0 +1,110 @@
+###########################################
+# $Id: $
+#
+# Comments start with #. Empty lines are ignored.
+# Syntax of one entry: name: line, one optional filter: line, zero or more par: lines,  FHEM-Commands
+# filter:INTERNAL=VALUE (optional)
+# par: name of the parameter; comment; perl_code (optional)
+# perl_code returns a value for the parameter, or undef.
+# If undef, the user has to specify them (the comment is shown to the user)
+
+
+###########################################
+# Example sketches from MySensors.org
+# simple stateFormat versions
+name:A_01a1_air_humidity_dht
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard DHT sketch without battery reading<br>NOTE: Untested first template version
+attr DEVICE stateFormat T: temperature1 H: humidity
+attr DEVICE model A_01a1_air_humidity_dht
+
+name:A_01a2_air_humidity_Si7021
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard DHT sketch with battery reading<br>NOTE: Untested first template version
+attr DEVICE stateFormat T: temperature1 H: humidity Bat: batteryLevel
+attr DEVICE model A_01a2_air_humidity_Si7021
+
+name:A_02a_atmospheric_pressure
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard Atmospheric Pressure sketch (BME280)NOTE: Untested first template version
+attr DEVICE stateFormat T: temperature H: humidity1 P: pressure2 Forecast: forecast2
+attr DEVICE model A_02a_atmospheric_pressure
+
+name:A_03a_bed_occupancy
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard bed occupancy sketch (MPR121) <br>NOTE: Sketch is still in MySensors 1.x format and has to be changed for use with recent arduino libs.NOTE: Untested first template version
+attr DEVICE stateFormat Left: motion Right: motion1
+attr DEVICE model A_03a_bed_occupancy
+
+#A_04a_Dimmer - LED
+#A_05a_Display and Time
+#A_06a_Distance
+#A_07a_Dollhouse
+#A_08a_Door/Window/Button
+#A_09a_Dust
+#A_10a_Gas Detection
+#A_11a_Gesture Controller
+#A_12a_GPS Sensor
+#A_13a_Heatpump Control
+#A_14a_IR Sender/Receiver
+#A_15a_Irrigation Controller
+#A_16a1_Light Level - BH1750
+#A_16a2_Light Level - LM393
+
+name:A_17a_Motion
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard motion sketch
+attr DEVICE stateFormat Motion: motion1
+attr DEVICE model A_17a_Motion
+
+#A_18a_Orientation Actuator
+#A_19a_Orientation Sensor
+#A_20a_Parking Sensor
+#A_21a_Pulse Power Meter
+#A_22a_Pulse Water Meter
+#A_23a_Rain Gauge
+#A_24a_Relay Actuator
+#A_25a_RFID
+#A_26a_Scene Controller
+#A_27a_Secret Knock
+#A_28a_Servo
+#A_29a_Smart Alarm Clock
+#A_30a_Soil Moisture
+#A_31a_Sonoff Relay
+#A_32a_Starry Sky
+
+
+name:A_33a_temperature
+filter:TYPE=MYSENSORS_DEVICE
+desc:Applies to standard temperature sketch (DS18B20) NOTE: Untested first template version
+attr DEVICE stateFormat T0: temperature T1: temperature1 T2: temperature2
+attr DEVICE model A_33a_temperature
+
+#A_34a_UV
+#A_35a_Whole House Fan
+
+###########################################
+# Example sketches from MySensors.org
+# Advanced stateFormat and devStateIcon versions
+
+
+###########################################
+# Advanced sketches 
+
+#4 relay
+name:C_04_4ch_unified_icon
+filter:TYPE=MYSENSORS_DEVICE
+desc:Device with 4 relays attached <br>NOTE: Clicking on icons will issue a corresponding toggle command
+attr DEVICE devStateIcon {\
+    "<div><a href=\"/fhem?cmd.dummy=set ".$name." status1 toggle&XHR=1\">status1:"\
+    . FW_makeImage(lc ReadingsVal($name, "status1", "off"))\
+    . "</a> <a href=\"/fhem?cmd.dummy=set ".$name." status2 toggle&XHR=1\">status2:"\
+    . FW_makeImage(lc ReadingsVal($name, "status2", "off")) . "</a></div>"\
+    . "</a> <a href=\"/fhem?cmd.dummy=set ".$name." status3 toggle&XHR=1\">status3:"\
+    . FW_makeImage(lc ReadingsVal($name, "status3", "off")) . "</a></div>"\
+    . "</a> <a href=\"/fhem?cmd.dummy=set ".$name." status4 toggle&XHR=1\">status4:"\
+    . FW_makeImage(lc ReadingsVal($name, "status4", "off")) . "</a></div>"\
+    }
+attr DEVICE stateFormat P1: status1 P2: status2 P3: status3 P4: status4 Status: state
+attr DEVICE webCmd :
+attr DEVICE model C_04_4ch_unified_icon
diff --git a/fhem/MAINTAINER.txt b/fhem/MAINTAINER.txt
index df444d5e5..af5827f32 100644
--- a/fhem/MAINTAINER.txt
+++ b/fhem/MAINTAINER.txt
@@ -565,6 +565,7 @@ FHEM/lib/*deviceconfig.xml.gz        krikan       ZWave
 FHEM/lib/*manufacturer_specific.xml  krikan       ZWave
 FHEM/lib/AttrTemplate/httpmod.template Beta-User  https://forum.fhem.de/index.php/topic,97694.0.html
 FHEM/lib/AttrTemplate/mqtt2.template Beta-User    https://forum.fhem.de/index.php/topic,94494.0.html
+FHEM/lib/AttrTemplate/mysensors.template Beta-User Bastelecke/MySensors
 FHEM/lib/Device/Firmata/*    jensb                Sonstige Systeme
 FHEM/lib/Device/MySensors/*  Hauswart/Beta-User   Bastelecke/MySensors
 FHEM/lib/MP3/*               Reinerlein           Multimedia