From e87652e95f7da667ce4e68a3dbbf32546854905c Mon Sep 17 00:00:00 2001
From: theolind <theodor.lindquist@gmail.com>
Date: Thu, 2 Apr 2015 19:59:44 +0200
Subject: [PATCH] added HA module for mysensors

---
 .gitmodules                                  |  3 +
 homeassistant/components/sensor/mysensors.py | 77 ++++++++++++++++++++
 homeassistant/external/pymysensors           |  1 +
 3 files changed, 81 insertions(+)
 create mode 100644 homeassistant/components/sensor/mysensors.py
 create mode 160000 homeassistant/external/pymysensors

diff --git a/.gitmodules b/.gitmodules
index ae38be7c61b..ca0b1f024b8 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -19,3 +19,6 @@
 [submodule "homeassistant/external/nzbclients"]
         path = homeassistant/external/nzbclients
         url = https://github.com/jamespcole/home-assistant-nzb-clients.git
+[submodule "homeassistant/external/pymysensors"]
+	path = homeassistant/external/pymysensors
+	url = https://github.com/theolind/pymysensors
diff --git a/homeassistant/components/sensor/mysensors.py b/homeassistant/components/sensor/mysensors.py
new file mode 100644
index 00000000000..357aa05331c
--- /dev/null
+++ b/homeassistant/components/sensor/mysensors.py
@@ -0,0 +1,77 @@
+import homeassistant.external.pymysensors.mysensors as mysensors
+from homeassistant.helpers.entity import Entity
+import logging
+
+#
+# Config:
+#  sensor:
+#  platform: mysensors
+#  gateway: serial
+#  port: '/dev/ttyACM0'
+#
+
+from homeassistant.const import (
+    ATTR_BATTERY_LEVEL )
+
+_LOGGER = logging.getLogger(__name__)
+
+devices = {}    # keep track of devices added to HA
+gw = mysensors.Gateway();
+
+def setup_platform(hass, config, add_devices, discovery_info=None):
+
+    # Passed to pymysensors and called when a sensor is updated
+    def sensor_update(type, nid, cid = None, value = None):
+        s = gw.sensors[nid]
+        if s.sketch_name is not None:
+            if nid in devices:
+                devices[nid]._battery_level = s.battery_level
+                for c in s.children:
+                    child = s.children[c]
+                    devices[nid]._children[child.id] = MySensorsChildSensor(child.type, child.value)
+            else:
+                devices[nid] = MySensorsSensor(s.sketch_name)
+                add_devices([devices[nid]])
+
+    if config['gateway'] == 'serial':
+        gw = mysensors.SerialGateway('/dev/ttyACM0', sensor_update)
+    else:
+        _LOGGER.error('mysensors gateway type: ' + config.gateway + ' not supported')
+
+    gw.listen()
+
+
+class MySensorsSensor(Entity):
+    def __init__(self, name):
+        self._name = name
+        self._state = ''
+        self._battery_level = 0
+        self._children = {}
+
+    @property
+    def name(self):
+        return self._name
+
+    @property
+    def state(self):
+        """ Returns the state of the device. """
+        return self._state
+
+    @property
+    def state_attributes(self):
+        """ Returns the state attributes. """
+        attrs = {}
+        attrs[ATTR_BATTERY_LEVEL] = self._battery_level
+        
+        for c in self._children.values():
+            attrs[c._type] = c._value
+
+        return attrs
+
+    #def update(self):
+        # get latest values from gateway
+
+class MySensorsChildSensor():
+    def __init__(self, type, value):
+        self._type = type
+        self._value = value
\ No newline at end of file
diff --git a/homeassistant/external/pymysensors b/homeassistant/external/pymysensors
new file mode 160000
index 00000000000..9b86a46c245
--- /dev/null
+++ b/homeassistant/external/pymysensors
@@ -0,0 +1 @@
+Subproject commit 9b86a46c245630e30149ba4e3b0729ef2803a702
-- 
GitLab