2015-11-04 03:53:59 +00:00
|
|
|
"""
|
2016-02-28 13:56:07 +00:00
|
|
|
Connect to a MySensors gateway via pymysensors API.
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2016-01-27 08:23:44 +00:00
|
|
|
https://home-assistant.io/components/sensor.mysensors/
|
2015-11-04 03:53:59 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2017-01-25 06:04:44 +00:00
|
|
|
import os
|
2016-04-20 04:00:56 +00:00
|
|
|
import socket
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2016-08-27 20:41:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-01-15 02:53:14 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2017-01-25 06:04:44 +00:00
|
|
|
from homeassistant.components.mqtt import (valid_publish_topic,
|
|
|
|
valid_subscribe_topic)
|
2017-01-15 02:53:14 +00:00
|
|
|
from homeassistant.const import (ATTR_BATTERY_LEVEL, CONF_NAME,
|
|
|
|
CONF_OPTIMISTIC, EVENT_HOMEASSISTANT_START,
|
2016-07-14 00:44:46 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON)
|
2016-08-27 20:41:21 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2016-07-14 00:44:46 +00:00
|
|
|
from homeassistant.loader import get_component
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2016-08-27 20:41:21 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTR_NODE_ID = 'node_id'
|
|
|
|
ATTR_CHILD_ID = 'child_id'
|
|
|
|
ATTR_DESCRIPTION = 'description'
|
|
|
|
ATTR_DEVICE = 'device'
|
|
|
|
CONF_BAUD_RATE = 'baud_rate'
|
2016-04-20 04:00:56 +00:00
|
|
|
CONF_DEVICE = 'device'
|
2015-11-04 03:53:59 +00:00
|
|
|
CONF_DEBUG = 'debug'
|
2016-08-27 20:41:21 +00:00
|
|
|
CONF_GATEWAYS = 'gateways'
|
2015-11-04 03:53:59 +00:00
|
|
|
CONF_PERSISTENCE = 'persistence'
|
|
|
|
CONF_PERSISTENCE_FILE = 'persistence_file'
|
2016-04-20 04:00:56 +00:00
|
|
|
CONF_TCP_PORT = 'tcp_port'
|
2016-07-14 00:44:46 +00:00
|
|
|
CONF_TOPIC_IN_PREFIX = 'topic_in_prefix'
|
|
|
|
CONF_TOPIC_OUT_PREFIX = 'topic_out_prefix'
|
|
|
|
CONF_RETAIN = 'retain'
|
2016-08-27 20:41:21 +00:00
|
|
|
CONF_VERSION = 'version'
|
|
|
|
DEFAULT_VERSION = 1.4
|
2016-02-10 20:24:18 +00:00
|
|
|
DEFAULT_BAUD_RATE = 115200
|
2016-04-20 04:00:56 +00:00
|
|
|
DEFAULT_TCP_PORT = 5003
|
2015-11-04 03:53:59 +00:00
|
|
|
DOMAIN = 'mysensors'
|
2016-11-06 18:49:43 +00:00
|
|
|
MYSENSORS_GATEWAYS = 'mysensors_gateways'
|
2016-08-25 17:07:22 +00:00
|
|
|
MQTT_COMPONENT = 'mqtt'
|
2015-12-05 23:29:03 +00:00
|
|
|
REQUIREMENTS = [
|
2015-12-18 02:37:49 +00:00
|
|
|
'https://github.com/theolind/pymysensors/archive/'
|
2016-10-20 18:56:26 +00:00
|
|
|
'0b705119389be58332f17753c53167f551254b6c.zip#pymysensors==0.8']
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2017-01-25 06:04:44 +00:00
|
|
|
|
|
|
|
def is_socket_address(value):
|
|
|
|
"""Validate that value is a valid address."""
|
|
|
|
try:
|
|
|
|
socket.getaddrinfo(value, None)
|
|
|
|
return value
|
|
|
|
except OSError:
|
|
|
|
raise vol.Invalid('Device is not a valid domain name or ip address')
|
|
|
|
|
|
|
|
|
|
|
|
def has_parent_dir(value):
|
|
|
|
"""Validate that value is in an existing directory which is writetable."""
|
|
|
|
parent = os.path.dirname(os.path.realpath(value))
|
|
|
|
is_dir_writable = os.path.isdir(parent) and os.access(parent, os.W_OK)
|
|
|
|
if not is_dir_writable:
|
|
|
|
raise vol.Invalid(
|
|
|
|
'{} directory does not exist or is not writetable'.format(parent))
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def has_all_unique_files(value):
|
|
|
|
"""Validate that all persistence files are unique and set if any is set."""
|
|
|
|
persistence_files = [
|
|
|
|
gateway.get(CONF_PERSISTENCE_FILE) for gateway in value]
|
|
|
|
if None in persistence_files and any(
|
|
|
|
name is not None for name in persistence_files):
|
|
|
|
raise vol.Invalid(
|
|
|
|
'persistence file name of all devices must be set if any is set')
|
|
|
|
if not all(name is None for name in persistence_files):
|
|
|
|
schema = vol.Schema(vol.Unique())
|
|
|
|
schema(persistence_files)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2016-08-27 20:41:21 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
2017-01-25 06:04:44 +00:00
|
|
|
vol.Required(CONF_GATEWAYS): vol.All(
|
|
|
|
cv.ensure_list, has_all_unique_files,
|
|
|
|
[{
|
|
|
|
vol.Required(CONF_DEVICE):
|
|
|
|
vol.Any(cv.isdevice, MQTT_COMPONENT, is_socket_address),
|
|
|
|
vol.Optional(CONF_PERSISTENCE_FILE):
|
|
|
|
vol.All(cv.string, has_parent_dir),
|
2016-08-27 20:41:21 +00:00
|
|
|
vol.Optional(
|
|
|
|
CONF_BAUD_RATE,
|
|
|
|
default=DEFAULT_BAUD_RATE): cv.positive_int,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_TCP_PORT,
|
|
|
|
default=DEFAULT_TCP_PORT): cv.port,
|
2017-01-25 06:04:44 +00:00
|
|
|
vol.Optional(
|
|
|
|
CONF_TOPIC_IN_PREFIX, default=''): valid_subscribe_topic,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_TOPIC_OUT_PREFIX, default=''): valid_publish_topic,
|
|
|
|
}]
|
|
|
|
),
|
2016-08-27 20:41:21 +00:00
|
|
|
vol.Optional(CONF_DEBUG, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_PERSISTENCE, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_RETAIN, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.Coerce(float),
|
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2015-12-31 04:48:23 +00:00
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
def setup(hass, config):
|
2015-12-23 22:20:39 +00:00
|
|
|
"""Setup the MySensors component."""
|
2016-01-10 03:10:38 +00:00
|
|
|
import mysensors.mysensors as mysensors
|
2015-12-23 22:20:39 +00:00
|
|
|
|
2016-08-27 20:41:21 +00:00
|
|
|
version = config[DOMAIN].get(CONF_VERSION)
|
|
|
|
persistence = config[DOMAIN].get(CONF_PERSISTENCE)
|
2015-12-05 23:29:03 +00:00
|
|
|
|
2016-07-14 00:44:46 +00:00
|
|
|
def setup_gateway(device, persistence_file, baud_rate, tcp_port, in_prefix,
|
|
|
|
out_prefix):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Return gateway after setup of the gateway."""
|
2016-08-25 17:07:22 +00:00
|
|
|
if device == MQTT_COMPONENT:
|
|
|
|
if not setup_component(hass, MQTT_COMPONENT, config):
|
2016-07-14 00:44:46 +00:00
|
|
|
return
|
2016-08-25 17:07:22 +00:00
|
|
|
mqtt = get_component(MQTT_COMPONENT)
|
2016-08-27 20:41:21 +00:00
|
|
|
retain = config[DOMAIN].get(CONF_RETAIN)
|
2016-07-14 00:44:46 +00:00
|
|
|
|
|
|
|
def pub_callback(topic, payload, qos, retain):
|
|
|
|
"""Call mqtt publish function."""
|
|
|
|
mqtt.publish(hass, topic, payload, qos, retain)
|
|
|
|
|
|
|
|
def sub_callback(topic, callback, qos):
|
|
|
|
"""Call mqtt subscribe function."""
|
|
|
|
mqtt.subscribe(hass, topic, callback, qos)
|
|
|
|
gateway = mysensors.MQTTGateway(
|
|
|
|
pub_callback, sub_callback,
|
|
|
|
event_callback=None, persistence=persistence,
|
|
|
|
persistence_file=persistence_file,
|
|
|
|
protocol_version=version, in_prefix=in_prefix,
|
|
|
|
out_prefix=out_prefix, retain=retain)
|
|
|
|
else:
|
|
|
|
try:
|
2017-01-25 06:04:44 +00:00
|
|
|
socket.getaddrinfo(device, None)
|
2016-07-14 00:44:46 +00:00
|
|
|
# valid ip address
|
|
|
|
gateway = mysensors.TCPGateway(
|
|
|
|
device, event_callback=None, persistence=persistence,
|
|
|
|
persistence_file=persistence_file,
|
|
|
|
protocol_version=version, port=tcp_port)
|
|
|
|
except OSError:
|
|
|
|
# invalid ip address
|
|
|
|
gateway = mysensors.SerialGateway(
|
|
|
|
device, event_callback=None, persistence=persistence,
|
|
|
|
persistence_file=persistence_file,
|
|
|
|
protocol_version=version, baud=baud_rate)
|
2016-08-27 20:41:21 +00:00
|
|
|
gateway.metric = hass.config.units.is_metric
|
|
|
|
gateway.debug = config[DOMAIN].get(CONF_DEBUG)
|
|
|
|
optimistic = config[DOMAIN].get(CONF_OPTIMISTIC)
|
|
|
|
gateway = GatewayWrapper(gateway, optimistic, device)
|
2016-01-10 03:10:38 +00:00
|
|
|
# pylint: disable=attribute-defined-outside-init
|
|
|
|
gateway.event_callback = gateway.callback_factory()
|
2015-12-09 03:43:18 +00:00
|
|
|
|
2015-12-31 04:48:23 +00:00
|
|
|
def gw_start(event):
|
|
|
|
"""Callback to trigger start of gateway and any persistence."""
|
|
|
|
gateway.start()
|
|
|
|
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)
|
2015-12-05 23:29:03 +00:00
|
|
|
|
2015-12-31 04:48:23 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, gw_start)
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2015-12-23 22:20:39 +00:00
|
|
|
return gateway
|
2015-12-05 23:29:03 +00:00
|
|
|
|
2016-11-06 18:49:43 +00:00
|
|
|
gateways = hass.data.get(MYSENSORS_GATEWAYS)
|
|
|
|
if gateways is not None:
|
|
|
|
_LOGGER.error(
|
|
|
|
'%s already exists in %s, will not setup %s component',
|
|
|
|
MYSENSORS_GATEWAYS, hass.data, DOMAIN)
|
|
|
|
return False
|
|
|
|
|
2016-04-20 04:00:56 +00:00
|
|
|
# Setup all devices from config
|
2016-11-06 18:49:43 +00:00
|
|
|
gateways = []
|
2015-12-23 22:20:39 +00:00
|
|
|
conf_gateways = config[DOMAIN][CONF_GATEWAYS]
|
2015-12-31 04:48:23 +00:00
|
|
|
|
2015-12-23 22:20:39 +00:00
|
|
|
for index, gway in enumerate(conf_gateways):
|
2016-04-20 04:00:56 +00:00
|
|
|
device = gway[CONF_DEVICE]
|
2015-12-23 22:20:39 +00:00
|
|
|
persistence_file = gway.get(
|
|
|
|
CONF_PERSISTENCE_FILE,
|
|
|
|
hass.config.path('mysensors{}.pickle'.format(index + 1)))
|
2016-08-27 20:41:21 +00:00
|
|
|
baud_rate = gway.get(CONF_BAUD_RATE)
|
|
|
|
tcp_port = gway.get(CONF_TCP_PORT)
|
|
|
|
in_prefix = gway.get(CONF_TOPIC_IN_PREFIX)
|
|
|
|
out_prefix = gway.get(CONF_TOPIC_OUT_PREFIX)
|
2016-11-06 18:49:43 +00:00
|
|
|
ready_gateway = setup_gateway(
|
2016-07-14 00:44:46 +00:00
|
|
|
device, persistence_file, baud_rate, tcp_port, in_prefix,
|
|
|
|
out_prefix)
|
2016-11-06 18:49:43 +00:00
|
|
|
if ready_gateway is not None:
|
|
|
|
gateways.append(ready_gateway)
|
2015-12-31 04:48:23 +00:00
|
|
|
|
2016-11-06 18:49:43 +00:00
|
|
|
if not gateways:
|
2016-08-25 17:07:22 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
'No devices could be setup as gateways, check your configuration')
|
|
|
|
return False
|
|
|
|
|
2016-11-06 18:49:43 +00:00
|
|
|
hass.data[MYSENSORS_GATEWAYS] = gateways
|
|
|
|
|
2016-09-30 23:36:04 +00:00
|
|
|
for component in ['sensor', 'switch', 'light', 'binary_sensor', 'climate',
|
|
|
|
'cover']:
|
2016-06-15 05:51:46 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
discovery.load_platform(
|
|
|
|
hass, 'notify', DOMAIN, {CONF_NAME: DOMAIN}, config)
|
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def pf_callback_factory(map_sv_types, devices, entity_class, add_devices=None):
|
2015-12-31 04:48:23 +00:00
|
|
|
"""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)
|
2015-11-04 03:53:59 +00:00
|
|
|
return
|
2015-12-31 04:48:23 +00:00
|
|
|
|
|
|
|
for child in gateway.sensors[node_id].children.values():
|
2015-12-23 22:20:39 +00:00
|
|
|
for value_type in child.values.keys():
|
2016-01-26 02:35:34 +00:00
|
|
|
key = node_id, child.id, value_type
|
2016-01-27 18:47:48 +00:00
|
|
|
if child.type not in map_sv_types or \
|
|
|
|
value_type not in map_sv_types[child.type]:
|
2015-12-31 04:48:23 +00:00
|
|
|
continue
|
2016-01-26 02:35:34 +00:00
|
|
|
if key in devices:
|
2017-01-15 02:53:14 +00:00
|
|
|
if add_devices:
|
|
|
|
devices[key].schedule_update_ha_state(True)
|
|
|
|
else:
|
|
|
|
devices[key].update()
|
2016-01-26 02:35:34 +00:00
|
|
|
continue
|
2016-04-14 04:22:40 +00:00
|
|
|
name = '{} {} {}'.format(
|
2016-01-26 02:35:34 +00:00
|
|
|
gateway.sensors[node_id].sketch_name, node_id, child.id)
|
2016-02-13 17:32:13 +00:00
|
|
|
if isinstance(entity_class, dict):
|
|
|
|
device_class = entity_class[child.type]
|
|
|
|
else:
|
|
|
|
device_class = entity_class
|
|
|
|
devices[key] = device_class(
|
2016-02-20 02:59:06 +00:00
|
|
|
gateway, node_id, child.id, name, value_type, child.type)
|
2017-01-15 02:53:14 +00:00
|
|
|
if add_devices:
|
|
|
|
_LOGGER.info('Adding new devices: %s', devices[key])
|
|
|
|
add_devices([devices[key]])
|
|
|
|
devices[key].schedule_update_ha_state(True)
|
|
|
|
else:
|
|
|
|
devices[key].update()
|
2015-12-31 04:48:23 +00:00
|
|
|
return mysensors_callback
|
|
|
|
|
|
|
|
|
2016-01-10 03:10:38 +00:00
|
|
|
class GatewayWrapper(object):
|
2016-02-20 01:11:15 +00:00
|
|
|
"""Gateway wrapper class."""
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2016-08-27 20:41:21 +00:00
|
|
|
def __init__(self, gateway, optimistic, device):
|
2015-12-31 04:48:23 +00:00
|
|
|
"""Setup class attributes on instantiation.
|
|
|
|
|
|
|
|
Args:
|
2016-08-27 20:41:21 +00:00
|
|
|
gateway (mysensors.SerialGateway): Gateway to wrap.
|
2016-01-26 23:15:19 +00:00
|
|
|
optimistic (bool): Send values to actuators without feedback state.
|
2016-07-14 00:44:46 +00:00
|
|
|
device (str): Path to serial port, ip adress or mqtt.
|
2015-12-31 04:48:23 +00:00
|
|
|
|
|
|
|
Attributes:
|
2016-08-27 20:41:21 +00:00
|
|
|
_wrapped_gateway (mysensors.SerialGateway): Wrapped gateway.
|
2015-12-31 04:48:23 +00:00
|
|
|
platform_callbacks (list): Callback functions, one per platform.
|
2016-02-20 01:11:15 +00:00
|
|
|
optimistic (bool): Send values to actuators without feedback state.
|
2016-08-27 20:41:21 +00:00
|
|
|
device (str): Device configured as gateway.
|
2016-01-10 03:10:38 +00:00
|
|
|
__initialised (bool): True if GatewayWrapper is initialised.
|
2015-12-31 04:48:23 +00:00
|
|
|
"""
|
2016-01-10 03:10:38 +00:00
|
|
|
self._wrapped_gateway = gateway
|
2015-12-31 04:48:23 +00:00
|
|
|
self.platform_callbacks = []
|
2016-01-26 23:15:19 +00:00
|
|
|
self.optimistic = optimistic
|
2016-07-14 00:44:46 +00:00
|
|
|
self.device = device
|
2016-01-10 03:10:38 +00:00
|
|
|
self.__initialised = True
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""See if this object has attribute name."""
|
|
|
|
# Do not use hasattr, it goes into infinite recurrsion
|
|
|
|
if name in self.__dict__:
|
2016-04-30 13:27:59 +00:00
|
|
|
# This object has the attribute.
|
2016-01-10 03:10:38 +00:00
|
|
|
return getattr(self, name)
|
2016-04-30 13:27:59 +00:00
|
|
|
# The wrapped object has the attribute.
|
2016-01-10 03:10:38 +00:00
|
|
|
return getattr(self._wrapped_gateway, name)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
"""See if this object has attribute name then set to value."""
|
|
|
|
if '_GatewayWrapper__initialised' not in self.__dict__:
|
|
|
|
return object.__setattr__(self, name, value)
|
|
|
|
elif name in self.__dict__:
|
|
|
|
object.__setattr__(self, name, value)
|
|
|
|
else:
|
|
|
|
object.__setattr__(self._wrapped_gateway, name, value)
|
2015-12-31 04:48:23 +00:00
|
|
|
|
|
|
|
def callback_factory(self):
|
|
|
|
"""Return a new callback function."""
|
|
|
|
def node_update(update_type, node_id):
|
|
|
|
"""Callback for node updates from the MySensors gateway."""
|
2016-08-25 17:07:22 +00:00
|
|
|
_LOGGER.debug('Update %s: node %s', update_type, node_id)
|
2015-12-31 04:48:23 +00:00
|
|
|
for callback in self.platform_callbacks:
|
|
|
|
callback(self, node_id)
|
|
|
|
|
|
|
|
return node_update
|
2016-04-30 13:27:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MySensorsDeviceEntity(object):
|
|
|
|
"""Represent a MySensors entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, gateway, node_id, child_id, name, value_type, child_type):
|
|
|
|
"""
|
|
|
|
Setup class attributes on instantiation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
gateway (GatewayWrapper): Gateway object.
|
|
|
|
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.
|
|
|
|
child_type (str): Child type of child.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
gateway (GatewayWrapper): Gateway object.
|
|
|
|
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.
|
|
|
|
child_type (str): Child type of child.
|
|
|
|
battery_level (int): Node battery level.
|
|
|
|
_values (dict): Child values. Non state values set as state attributes.
|
|
|
|
mysensors (module): Mysensors main component module.
|
|
|
|
"""
|
|
|
|
self.gateway = gateway
|
|
|
|
self.node_id = node_id
|
|
|
|
self.child_id = child_id
|
|
|
|
self._name = name
|
|
|
|
self.value_type = value_type
|
|
|
|
self.child_type = child_type
|
|
|
|
self._values = {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Mysensor gateway pushes its state to HA."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""The name of this entity."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return device specific state attributes."""
|
2016-08-27 20:41:21 +00:00
|
|
|
node = self.gateway.sensors[self.node_id]
|
|
|
|
child = node.children[self.child_id]
|
2016-04-30 13:27:59 +00:00
|
|
|
attr = {
|
2016-08-27 20:41:21 +00:00
|
|
|
ATTR_BATTERY_LEVEL: node.battery_level,
|
|
|
|
ATTR_CHILD_ID: self.child_id,
|
|
|
|
ATTR_DESCRIPTION: child.description,
|
2016-07-14 00:44:46 +00:00
|
|
|
ATTR_DEVICE: self.gateway.device,
|
2016-04-30 13:27:59 +00:00
|
|
|
ATTR_NODE_ID: self.node_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
set_req = self.gateway.const.SetReq
|
|
|
|
|
|
|
|
for value_type, value in self._values.items():
|
|
|
|
try:
|
|
|
|
attr[set_req(value_type).name] = value
|
|
|
|
except ValueError:
|
2016-08-25 17:07:22 +00:00
|
|
|
_LOGGER.error('Value_type %s is not valid for mysensors '
|
2016-04-30 13:27:59 +00:00
|
|
|
'version %s', value_type,
|
2016-08-27 20:41:21 +00:00
|
|
|
self.gateway.protocol_version)
|
2016-04-30 13:27:59 +00:00
|
|
|
return attr
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.value_type in self._values
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the controller with the latest value from a sensor."""
|
|
|
|
node = self.gateway.sensors[self.node_id]
|
|
|
|
child = node.children[self.child_id]
|
|
|
|
set_req = self.gateway.const.SetReq
|
|
|
|
for value_type, value in child.values.items():
|
|
|
|
_LOGGER.debug(
|
|
|
|
"%s: value_type %s, value = %s", self._name, value_type, value)
|
|
|
|
if value_type in (set_req.V_ARMED, set_req.V_LIGHT,
|
|
|
|
set_req.V_LOCK_STATUS, set_req.V_TRIPPED):
|
|
|
|
self._values[value_type] = (
|
|
|
|
STATE_ON if int(value) == 1 else STATE_OFF)
|
2016-09-30 23:36:04 +00:00
|
|
|
elif value_type == set_req.V_DIMMER:
|
|
|
|
self._values[value_type] = int(value)
|
2016-04-30 13:27:59 +00:00
|
|
|
else:
|
|
|
|
self._values[value_type] = value
|