Add gateway wrapper, fix discovery and callbacks

* Add gateway wrapper by subclassing serial gateway.
* Fix platform setup with discovery service.
* Fix platform callback functions with callback factory.
This commit is contained in:
MartinHjelmare 2015-12-31 05:48:23 +01:00
parent be25ea4f09
commit 69ed6fe6e7
5 changed files with 244 additions and 216 deletions

View file

@ -29,14 +29,19 @@ mysensors:
"""
import logging
try:
import mysensors.mysensors as mysensors
except ImportError:
mysensors = None
from homeassistant.helpers import validate_config
import homeassistant.bootstrap as bootstrap
from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
TEMP_CELCIUS,
CONF_PLATFORM)
EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE, ATTR_DISCOVERED,
TEMP_CELCIUS,)
CONF_GATEWAYS = 'gateways'
CONF_PORT = 'port'
@ -45,7 +50,6 @@ CONF_PERSISTENCE = 'persistence'
CONF_PERSISTENCE_FILE = 'persistence_file'
CONF_VERSION = 'version'
DEFAULT_VERSION = '1.4'
VERSION = None
DOMAIN = 'mysensors'
DEPENDENCIES = []
@ -56,86 +60,54 @@ _LOGGER = logging.getLogger(__name__)
ATTR_NODE_ID = 'node_id'
ATTR_CHILD_ID = 'child_id'
COMPONENTS_WITH_MYSENSORS_PLATFORM = [
'sensor',
'switch',
]
IS_METRIC = None
CONST = None
GATEWAYS = None
SCAN_INTERVAL = 30
DISCOVER_SENSORS = "mysensors.sensors"
DISCOVER_SWITCHES = "mysensors.switches"
# Maps discovered services to their platforms
DISCOVERY_COMPONENTS = [
('sensor', DISCOVER_SENSORS),
('switch', DISCOVER_SWITCHES),
]
def setup(hass, config):
"""Setup the MySensors component."""
# pylint: disable=too-many-locals
import mysensors.mysensors as mysensors
if not validate_config(config,
{DOMAIN: [CONF_GATEWAYS]},
_LOGGER):
return False
global VERSION
VERSION = config[DOMAIN].get(CONF_VERSION, DEFAULT_VERSION)
global mysensors # pylint: disable=invalid-name
if mysensors is None:
import mysensors.mysensors as _mysensors
mysensors = _mysensors
global CONST
if VERSION == '1.5':
import mysensors.const_15 as const
CONST = const
else:
import mysensors.const_14 as const
CONST = const
version = str(config[DOMAIN].get(CONF_VERSION, DEFAULT_VERSION))
is_metric = (hass.config.temperature_unit == TEMP_CELCIUS)
# 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.
global IS_METRIC
IS_METRIC = (hass.config.temperature_unit == TEMP_CELCIUS)
# Setup mysensors platforms
mysensors_config = config.copy()
for component in COMPONENTS_WITH_MYSENSORS_PLATFORM:
mysensors_config[component] = {CONF_PLATFORM: 'mysensors'}
if not bootstrap.setup_component(hass, component, mysensors_config):
return False
import homeassistant.components.sensor.mysensors as mysensors_sensor
import homeassistant.components.switch.mysensors as mysensors_switch
def callback_factory(gateway, port, devices):
"""Return a new callback function. Run once per gateway setup."""
def node_update(update_type, nid):
"""Callback for node updates from the MySensors gateway."""
_LOGGER.info('update %s: node %s', update_type, nid)
mysensors_sensor.sensor_update(gateway, port, devices, nid)
mysensors_switch.sensor_update(gateway, port, devices, nid)
return node_update
def setup_gateway(port, persistence, persistence_file):
def setup_gateway(port, persistence, persistence_file, version):
"""Return gateway after setup of the gateway."""
devices = {} # keep track of devices added to HA
gateway = mysensors.SerialGateway(port,
persistence=persistence,
persistence_file=persistence_file,
protocol_version=VERSION)
gateway.event_callback = callback_factory(gateway, port, devices)
gateway.metric = IS_METRIC
gateway = GatewayWrapper(
port, persistence, persistence_file, version)
# pylint: disable=attribute-defined-outside-init
gateway.metric = is_metric
gateway.debug = config[DOMAIN].get(CONF_DEBUG, False)
def gw_start(event):
"""Callback to trigger start of gateway and any persistence."""
gateway.start()
def persistence_update(event):
"""Callback to trigger update from persistence file."""
for nid in gateway.sensors:
gateway.event_callback('persistence', nid)
if persistence:
hass.bus.listen_once(
EVENT_HOMEASSISTANT_START, persistence_update)
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
lambda event: gateway.stop())
if persistence:
for node_id in gateway.sensors:
gateway.event_callback('persistence', node_id)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, gw_start)
return gateway
@ -146,48 +118,99 @@ def setup(hass, config):
if isinstance(conf_gateways, dict):
conf_gateways = [conf_gateways]
persistence = config[DOMAIN].get(CONF_PERSISTENCE, True)
for index, gway in enumerate(conf_gateways):
port = gway[CONF_PORT]
persistence_file = gway.get(
CONF_PERSISTENCE_FILE,
hass.config.path('mysensors{}.pickle'.format(index + 1)))
GATEWAYS[port] = setup_gateway(
port, persistence, persistence_file)
port, persistence, persistence_file, version)
for (component, discovery_service) in DISCOVERY_COMPONENTS:
# Ensure component is loaded
if not bootstrap.setup_component(hass, component, config):
return False
# Fire discovery event
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
ATTR_SERVICE: discovery_service,
ATTR_DISCOVERED: {}})
return True
def mysensors_update(platform_type):
"""Decorator for callback function for mysensor updates."""
def wrapper(gateway, port, devices, nid):
"""Wrapper function in the decorator."""
if gateway.sensors[nid].sketch_name is None:
_LOGGER.info('No sketch_name: node %s', nid)
def pf_callback_factory(
s_types, v_types, devices, add_devices, entity_class):
"""Return a new callback for the platform."""
def mysensors_callback(gateway, node_id):
"""Callback for mysensors platform."""
if gateway.sensors[node_id].sketch_name is None:
_LOGGER.info('No sketch_name: node %s', node_id)
return
if nid not in devices:
devices[nid] = {}
node = devices[nid]
# Get platform specific S_TYPES, V_TYPES, class and add_devices.
(platform_s_types,
platform_v_types,
platform_class,
add_devices) = platform_type(gateway, port, devices, nid)
for child_id, child in gateway.sensors[nid].children.items():
if child_id not in node:
node[child_id] = {}
for value_type in child.values.keys():
if (value_type not in node[child_id] and
child.type in platform_s_types and
value_type in platform_v_types):
# previously discovered, just update state with latest info
if node_id in devices:
for entity in devices[node_id]:
entity.update_ha_state(True)
return
# First time we see this node, detect sensors
for child in gateway.sensors[node_id].children.values():
name = '{} {}.{}'.format(
gateway.sensors[nid].sketch_name, nid, child.id)
node[child_id][value_type] = platform_class(
port, nid, child_id, name, value_type)
_LOGGER.info('adding new device: %s',
node[child_id][value_type])
add_devices([node[child_id][value_type]])
if (child.type in platform_s_types and
value_type in platform_v_types):
node[child_id][value_type].update_sensor(
child.values, gateway.sensors[nid].battery_level)
return wrapper
gateway.sensors[node_id].sketch_name, node_id, child.id)
for value_type in child.values.keys():
if child.type not in s_types or value_type not in v_types:
continue
devices[node_id].append(
entity_class(gateway, node_id, child.id, name, value_type))
if devices[node_id]:
_LOGGER.info('adding new devices: %s', devices[node_id])
add_devices(devices[node_id])
for entity in devices[node_id]:
entity.update_ha_state(True)
return mysensors_callback
class GatewayWrapper(mysensors.SerialGateway):
"""Gateway wrapper class, by subclassing serial gateway."""
def __init__(self, port, persistence, persistence_file, version):
"""Setup class attributes on instantiation.
Args:
port: Port of gateway to wrap.
persistence: Persistence, true or false.
persistence_file: File to store persistence info.
version: Version of mysensors API.
Attributes:
version (str): Version of mysensors API.
platform_callbacks (list): Callback functions, one per platform.
const (module): Mysensors API constants.
"""
super().__init__(port, event_callback=self.callback_factory(),
persistence=persistence,
persistence_file=persistence_file,
protocol_version=version)
self.version = version
self.platform_callbacks = []
self.const = self.get_const()
def get_const(self):
"""Get mysensors API constants."""
if self.version == '1.5':
import mysensors.const_15 as const
else:
import mysensors.const_14 as const
return const
def callback_factory(self):
"""Return a new callback function."""
def node_update(update_type, node_id):
"""Callback for node updates from the MySensors gateway."""
_LOGGER.info('update %s: node %s', update_type, node_id)
for callback in self.platform_callbacks:
callback(self, node_id)
return node_update

View file

@ -9,7 +9,8 @@ https://home-assistant.io/components/sensor/
import logging
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.components import wink, zwave, isy994, verisure, ecobee
from homeassistant.components import (
wink, zwave, isy994, verisure, ecobee, mysensors)
DOMAIN = 'sensor'
SCAN_INTERVAL = 30
@ -22,7 +23,8 @@ DISCOVERY_PLATFORMS = {
zwave.DISCOVER_SENSORS: 'zwave',
isy994.DISCOVER_SENSORS: 'isy994',
verisure.DISCOVER_SENSORS: 'verisure',
ecobee.DISCOVER_SENSORS: 'ecobee'
ecobee.DISCOVER_SENSORS: 'ecobee',
mysensors.DISCOVER_SENSORS: 'mysensors',
}

View file

@ -7,6 +7,7 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.mysensors/
"""
import logging
from collections import defaultdict
from homeassistant.helpers.entity import Entity
@ -20,74 +21,71 @@ import homeassistant.components.mysensors as mysensors
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = []
ADD_DEVICES = None
S_TYPES = None
V_TYPES = None
@mysensors.mysensors_update
def sensor_update(gateway, port, devices, nid):
"""Internal callback for sensor updates."""
return (S_TYPES, V_TYPES, MySensorsSensor, ADD_DEVICES)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the mysensors platform for sensors."""
# Define the S_TYPES and V_TYPES that the platform should handle as states.
global ADD_DEVICES
ADD_DEVICES = add_devices
global S_TYPES
S_TYPES = [
mysensors.CONST.Presentation.S_TEMP,
mysensors.CONST.Presentation.S_HUM,
mysensors.CONST.Presentation.S_BARO,
mysensors.CONST.Presentation.S_WIND,
mysensors.CONST.Presentation.S_RAIN,
mysensors.CONST.Presentation.S_UV,
mysensors.CONST.Presentation.S_WEIGHT,
mysensors.CONST.Presentation.S_POWER,
mysensors.CONST.Presentation.S_DISTANCE,
mysensors.CONST.Presentation.S_LIGHT_LEVEL,
mysensors.CONST.Presentation.S_IR,
mysensors.CONST.Presentation.S_WATER,
mysensors.CONST.Presentation.S_AIR_QUALITY,
mysensors.CONST.Presentation.S_CUSTOM,
mysensors.CONST.Presentation.S_DUST,
mysensors.CONST.Presentation.S_SCENE_CONTROLLER,
# Only act if loaded via mysensors by discovery event.
# Otherwise gateway is not setup.
if discovery_info is None:
return
for gateway in mysensors.GATEWAYS.values():
# Define the S_TYPES and V_TYPES that the platform should handle as
# states.
s_types = [
gateway.const.Presentation.S_TEMP,
gateway.const.Presentation.S_HUM,
gateway.const.Presentation.S_BARO,
gateway.const.Presentation.S_WIND,
gateway.const.Presentation.S_RAIN,
gateway.const.Presentation.S_UV,
gateway.const.Presentation.S_WEIGHT,
gateway.const.Presentation.S_POWER,
gateway.const.Presentation.S_DISTANCE,
gateway.const.Presentation.S_LIGHT_LEVEL,
gateway.const.Presentation.S_IR,
gateway.const.Presentation.S_WATER,
gateway.const.Presentation.S_AIR_QUALITY,
gateway.const.Presentation.S_CUSTOM,
gateway.const.Presentation.S_DUST,
gateway.const.Presentation.S_SCENE_CONTROLLER,
]
not_v_types = [
mysensors.CONST.SetReq.V_ARMED,
mysensors.CONST.SetReq.V_LIGHT,
mysensors.CONST.SetReq.V_LOCK_STATUS,
gateway.const.SetReq.V_ARMED,
gateway.const.SetReq.V_LIGHT,
gateway.const.SetReq.V_LOCK_STATUS,
]
if float(mysensors.VERSION) >= 1.5:
S_TYPES.extend([
mysensors.CONST.Presentation.S_COLOR_SENSOR,
mysensors.CONST.Presentation.S_MULTIMETER,
if float(gateway.version) >= 1.5:
s_types.extend([
gateway.const.Presentation.S_COLOR_SENSOR,
gateway.const.Presentation.S_MULTIMETER,
])
not_v_types.extend([mysensors.CONST.SetReq.V_STATUS, ])
global V_TYPES
V_TYPES = [member for member in mysensors.CONST.SetReq
not_v_types.extend([gateway.const.SetReq.V_STATUS, ])
v_types = [member for member in gateway.const.SetReq
if member.value not in not_v_types]
devices = defaultdict(list)
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
s_types, v_types, devices, add_devices, MySensorsSensor))
class MySensorsSensor(Entity):
"""Represent the value of a MySensors child node."""
# pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=too-many-arguments
def __init__(self, port, node_id, child_id, name, value_type):
def __init__(self, gateway, node_id, child_id, name, value_type):
"""Setup class attributes on instantiation.
Args:
port (str): Gateway port.
gateway (str): Gateway.
node_id (str): Id of node.
child_id (str): Id of child.
name (str): Entity name.
value_type (str): Value type of child. Value is entity state.
Attributes:
port (str): Gateway port.
gateway (str): Gateway.
node_id (str): Id of node.
child_id (str): Id of child.
_name (str): Entity name.
@ -95,7 +93,7 @@ class MySensorsSensor(Entity):
battery_level (int): Node battery level.
_values (dict): Child values. Non state values set as state attributes.
"""
self.port = port
self.gateway = gateway
self.node_id = node_id
self.child_id = child_id
self._name = name
@ -124,25 +122,25 @@ class MySensorsSensor(Entity):
def unit_of_measurement(self):
"""Unit of measurement of this entity."""
# pylint:disable=too-many-return-statements
if self.value_type == mysensors.CONST.SetReq.V_TEMP:
return TEMP_CELCIUS if mysensors.IS_METRIC else TEMP_FAHRENHEIT
elif self.value_type == mysensors.CONST.SetReq.V_HUM or \
self.value_type == mysensors.CONST.SetReq.V_DIMMER or \
self.value_type == mysensors.CONST.SetReq.V_PERCENTAGE or \
self.value_type == mysensors.CONST.SetReq.V_LIGHT_LEVEL:
if self.value_type == self.gateway.const.SetReq.V_TEMP:
return TEMP_CELCIUS if self.gateway.metric else TEMP_FAHRENHEIT
elif self.value_type == self.gateway.const.SetReq.V_HUM or \
self.value_type == self.gateway.const.SetReq.V_DIMMER or \
self.value_type == self.gateway.const.SetReq.V_PERCENTAGE or \
self.value_type == self.gateway.const.SetReq.V_LIGHT_LEVEL:
return '%'
elif self.value_type == mysensors.CONST.SetReq.V_WATT:
elif self.value_type == self.gateway.const.SetReq.V_WATT:
return 'W'
elif self.value_type == mysensors.CONST.SetReq.V_KWH:
elif self.value_type == self.gateway.const.SetReq.V_KWH:
return 'kWh'
elif self.value_type == mysensors.CONST.SetReq.V_VOLTAGE:
elif self.value_type == self.gateway.const.SetReq.V_VOLTAGE:
return 'V'
elif self.value_type == mysensors.CONST.SetReq.V_CURRENT:
elif self.value_type == self.gateway.const.SetReq.V_CURRENT:
return 'A'
elif self.value_type == mysensors.CONST.SetReq.V_IMPEDANCE:
elif self.value_type == self.gateway.const.SetReq.V_IMPEDANCE:
return 'ohm'
elif mysensors.CONST.SetReq.V_UNIT_PREFIX in self._values:
return self._values[mysensors.CONST.SetReq.V_UNIT_PREFIX]
elif self.gateway.const.SetReq.V_UNIT_PREFIX in self._values:
return self._values[self.gateway.const.SetReq.V_UNIT_PREFIX]
return None
@property
@ -168,16 +166,17 @@ class MySensorsSensor(Entity):
return data
def update_sensor(self, values, battery_level):
def update(self):
"""Update the controller with the latest values from a sensor."""
for value_type, value in values.items():
node = self.gateway.sensors[self.node_id]
child = node.children[self.child_id]
for value_type, value in child.values.items():
_LOGGER.info(
"%s: value_type %s, value = %s", self._name, value_type, value)
if value_type == mysensors.CONST.SetReq.V_TRIPPED:
if value_type == self.gateway.const.SetReq.V_TRIPPED:
self._values[value_type] = STATE_ON if int(
value) == 1 else STATE_OFF
else:
self._values[value_type] = value
self.battery_level = battery_level
self.update_ha_state()
self.battery_level = node.battery_level

View file

@ -17,7 +17,7 @@ from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
from homeassistant.components import (
group, discovery, wink, isy994, verisure, zwave)
group, discovery, wink, isy994, verisure, zwave, mysensors)
DOMAIN = 'switch'
SCAN_INTERVAL = 30
@ -40,6 +40,7 @@ DISCOVERY_PLATFORMS = {
isy994.DISCOVER_SWITCHES: 'isy994',
verisure.DISCOVER_SWITCHES: 'verisure',
zwave.DISCOVER_SWITCHES: 'zwave',
mysensors.DISCOVER_SWITCHES: 'mysensors',
}
PROP_TO_ATTR = {

View file

@ -7,6 +7,7 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.mysensors.html
"""
import logging
from collections import defaultdict
from homeassistant.components.switch import SwitchDevice
@ -24,49 +25,50 @@ S_TYPES = None
V_TYPES = None
@mysensors.mysensors_update
def sensor_update(gateway, port, devices, nid):
"""Internal callback for sensor updates."""
return (S_TYPES, V_TYPES, MySensorsSwitch, ADD_DEVICES)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the mysensors platform for switches."""
# Define the S_TYPES and V_TYPES that the platform should handle as states.
global ADD_DEVICES
ADD_DEVICES = add_devices
global S_TYPES
S_TYPES = [
mysensors.CONST.Presentation.S_DOOR,
mysensors.CONST.Presentation.S_MOTION,
mysensors.CONST.Presentation.S_SMOKE,
mysensors.CONST.Presentation.S_LIGHT,
mysensors.CONST.Presentation.S_BINARY,
mysensors.CONST.Presentation.S_LOCK,
# Only act if loaded via mysensors by discovery event.
# Otherwise gateway is not setup.
if discovery_info is None:
return
for gateway in mysensors.GATEWAYS.values():
# Define the S_TYPES and V_TYPES that the platform should handle as
# states.
s_types = [
gateway.const.Presentation.S_DOOR,
gateway.const.Presentation.S_MOTION,
gateway.const.Presentation.S_SMOKE,
gateway.const.Presentation.S_LIGHT,
gateway.const.Presentation.S_BINARY,
gateway.const.Presentation.S_LOCK,
]
global V_TYPES
V_TYPES = [
mysensors.CONST.SetReq.V_ARMED,
mysensors.CONST.SetReq.V_LIGHT,
mysensors.CONST.SetReq.V_LOCK_STATUS,
v_types = [
gateway.const.SetReq.V_ARMED,
gateway.const.SetReq.V_LIGHT,
gateway.const.SetReq.V_LOCK_STATUS,
]
if float(mysensors.VERSION) >= 1.5:
S_TYPES.extend([
mysensors.CONST.Presentation.S_SPRINKLER,
mysensors.CONST.Presentation.S_WATER_LEAK,
mysensors.CONST.Presentation.S_SOUND,
mysensors.CONST.Presentation.S_VIBRATION,
mysensors.CONST.Presentation.S_MOISTURE,
if float(gateway.version) >= 1.5:
s_types.extend([
gateway.const.Presentation.S_SPRINKLER,
gateway.const.Presentation.S_WATER_LEAK,
gateway.const.Presentation.S_SOUND,
gateway.const.Presentation.S_VIBRATION,
gateway.const.Presentation.S_MOISTURE,
])
V_TYPES.extend([mysensors.CONST.SetReq.V_STATUS, ])
v_types.extend([gateway.const.SetReq.V_STATUS, ])
devices = defaultdict(list)
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
s_types, v_types, devices, add_devices, MySensorsSwitch))
class MySensorsSwitch(SwitchDevice):
"""Represent the value of a MySensors child node."""
# pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=too-many-arguments
def __init__(self, port, node_id, child_id, name, value_type):
def __init__(self, gateway, node_id, child_id, name, value_type):
"""Setup class attributes on instantiation.
Args:
@ -85,7 +87,7 @@ class MySensorsSwitch(SwitchDevice):
battery_level (int): Node battery level.
_values (dict): Child values. Non state values set as state attributes.
"""
self.port = port
self.gateway = gateway
self.node_id = node_id
self.child_id = child_id
self._name = name
@ -135,30 +137,31 @@ class MySensorsSwitch(SwitchDevice):
def turn_on(self):
"""Turn the switch on."""
mysensors.GATEWAYS[self.port].set_child_value(
self.gateway.set_child_value(
self.node_id, self.child_id, self.value_type, 1)
self._values[self.value_type] = STATE_ON
self.update_ha_state()
def turn_off(self):
"""Turn the switch off."""
mysensors.GATEWAYS[self.port].set_child_value(
self.gateway.set_child_value(
self.node_id, self.child_id, self.value_type, 0)
self._values[self.value_type] = STATE_OFF
self.update_ha_state()
def update_sensor(self, values, battery_level):
def update(self):
"""Update the controller with the latest value from a sensor."""
for value_type, value in values.items():
node = self.gateway.sensors[self.node_id]
child = node.children[self.child_id]
for value_type, value in child.values.items():
_LOGGER.info(
"%s: value_type %s, value = %s", self._name, value_type, value)
if value_type == mysensors.CONST.SetReq.V_ARMED or \
value_type == mysensors.CONST.SetReq.V_STATUS or \
value_type == mysensors.CONST.SetReq.V_LIGHT or \
value_type == mysensors.CONST.SetReq.V_LOCK_STATUS:
if value_type == self.gateway.const.SetReq.V_ARMED or \
value_type == self.gateway.const.SetReq.V_STATUS or \
value_type == self.gateway.const.SetReq.V_LIGHT or \
value_type == self.gateway.const.SetReq.V_LOCK_STATUS:
self._values[value_type] = (
STATE_ON if int(value) == 1 else STATE_OFF)
else:
self._values[value_type] = value
self.battery_level = battery_level
self.update_ha_state()
self.battery_level = node.battery_level