added HA module for mysensors

This commit is contained in:
theolind 2015-04-02 19:59:44 +02:00
parent b0bf775da8
commit e87652e95f
3 changed files with 81 additions and 0 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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

1
homeassistant/external/pymysensors vendored Submodule

@ -0,0 +1 @@
Subproject commit 9b86a46c245630e30149ba4e3b0729ef2803a702