Add mysensors component and switch platform

* Add a general mysensors component. This sets up the serial comm
        with the gateway through pymysensors. The component also
        contains a decorator function for the callback function of
        mysensors platforms. Mysensors platforms should create a
        function that listens for the node update event fired by the
        mysensors component. This function should call another
        function, that uses the decorator, and returns a dict. The dict
        should contain a list of which mysensors V_TYPE values the
        platform handles, the platfrom class and the add_devices
        function (from setup_platform).
    * Change existing mysensors sensor platform to depend on the new
        mysensors component.
    * Add a mysensors switch platform. The switch platform takes
        advantage of new functionality from the the fork of pymysensors
        https://github.com/MartinHjelmare/pymysensors, that enables the
        gateway to send commands to change node child values.
    * Change const and is_metric to global constants, in the mysensors
        component and import const depending on the mysensors version
        used.
    * Change variables devices and gateway to global variables.
    * Add some debug logging at INFO log level.
This commit is contained in:
MartinHjelmare 2015-11-04 04:53:59 +01:00
parent d69b08ecf5
commit 45fe37a301
3 changed files with 321 additions and 72 deletions

View file

@ -11,102 +11,63 @@ import logging
from homeassistant.helpers.entity import Entity
from homeassistant.const import (
ATTR_BATTERY_LEVEL, EVENT_HOMEASSISTANT_STOP,
ATTR_BATTERY_LEVEL,
TEMP_CELCIUS, TEMP_FAHRENHEIT,
STATE_ON, STATE_OFF)
CONF_PORT = "port"
CONF_DEBUG = "debug"
CONF_PERSISTENCE = "persistence"
CONF_PERSISTENCE_FILE = "persistence_file"
CONF_VERSION = "version"
import homeassistant.components.mysensors as mysensors
ATTR_NODE_ID = "node_id"
ATTR_CHILD_ID = "child_id"
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/theolind/pymysensors/archive/'
'd4b809c2167650691058d1e29bfd2c4b1792b4b0.zip'
'#pymysensors==0.3']
DEPENDENCIES = ['mysensors']
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Setup the mysensors platform. """
""" Setup the mysensors platform for sensors. """
import mysensors.mysensors as mysensors
import mysensors.const_14 as const
v_types = []
for _, member in mysensors.CONST.SetReq.__members__.items():
if (member.value != mysensors.CONST.SetReq.V_STATUS and
member.value != mysensors.CONST.SetReq.V_LIGHT and
member.value != mysensors.CONST.SetReq.V_LOCK_STATUS):
v_types.append(member)
devices = {} # keep track of devices added to HA
# Just assume celcius means that the user wants metric for now.
# It may make more sense to make this a global config option in the future.
is_metric = (hass.config.temperature_unit == TEMP_CELCIUS)
@mysensors.mysensors_update
def _sensor_update(gateway, devices, nid):
"""Internal callback for sensor updates."""
_LOGGER.info("sensor update = %s", devices)
return {'types_to_handle': v_types,
'platform_class': MySensorsSensor,
'add_devices': add_devices}
def sensor_update(update_type, nid):
""" Callback for sensor updates from the MySensors gateway. """
_LOGGER.info("sensor_update %s: node %s", update_type, nid)
sensor = gateway.sensors[nid]
if sensor.sketch_name is None:
return
if nid not in devices:
devices[nid] = {}
def sensor_update(event):
""" Callback for sensor updates from the MySensors component. """
_LOGGER.info(
'update %s: node %s', event.data[mysensors.UPDATE_TYPE],
event.data[mysensors.NODE_ID])
_sensor_update(mysensors.GATEWAY, mysensors.DEVICES,
event.data[mysensors.NODE_ID])
node = devices[nid]
new_devices = []
for child_id, child in sensor.children.items():
if child_id not in node:
node[child_id] = {}
for value_type, value in child.values.items():
if value_type not in node[child_id]:
name = '{} {}.{}'.format(sensor.sketch_name, nid, child.id)
node[child_id][value_type] = \
MySensorsNodeValue(
nid, child_id, name, value_type, is_metric, const)
new_devices.append(node[child_id][value_type])
else:
node[child_id][value_type].update_sensor(
value, sensor.battery_level)
if new_devices:
_LOGGER.info("adding new devices: %s", new_devices)
add_devices(new_devices)
port = config.get(CONF_PORT)
if port is None:
_LOGGER.error("Missing required key 'port'")
return False
persistence = config.get(CONF_PERSISTENCE, True)
persistence_file = config.get(CONF_PERSISTENCE_FILE, 'mysensors.pickle')
version = config.get(CONF_VERSION, '1.4')
gateway = mysensors.SerialGateway(port, sensor_update,
persistence=persistence,
persistence_file=persistence_file,
protocol_version=version)
gateway.metric = is_metric
gateway.debug = config.get(CONF_DEBUG, False)
gateway.start()
if persistence:
for nid in gateway.sensors:
sensor_update('sensor_update', nid)
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
lambda event: gateway.stop())
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, sensor_update)
class MySensorsNodeValue(Entity):
class MySensorsSensor(Entity):
""" Represents the value of a MySensors child node. """
# pylint: disable=too-many-arguments, too-many-instance-attributes
def __init__(self, node_id, child_id, name, value_type, metric, const):
def __init__(self, gateway, node_id, child_id, name, value_type):
self.gateway = gateway
self._name = name
self.node_id = node_id
self.child_id = child_id
self.battery_level = 0
self.value_type = value_type
self.metric = metric
self.metric = mysensors.IS_METRIC
self._value = ''
self.const = const
self.const = mysensors.CONST
@property
def should_poll(self):
@ -144,7 +105,7 @@ class MySensorsNodeValue(Entity):
}
def update_sensor(self, value, battery_level):
""" Update a sensor with the latest value from the controller. """
""" Update the controller with the latest value from a sensor. """
_LOGGER.info("%s value = %s", self._name, value)
if self.value_type == self.const.SetReq.V_TRIPPED or \
self.value_type == self.const.SetReq.V_ARMED: