2015-02-26 07:27:17 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for Z-Wave.
|
2015-10-21 19:26:16 +00:00
|
|
|
|
2015-11-09 12:12:18 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/zwave/
|
2015-02-26 07:27:17 +00:00
|
|
|
"""
|
2017-02-23 21:06:28 +00:00
|
|
|
import asyncio
|
2017-03-14 23:55:33 +00:00
|
|
|
import copy
|
2016-03-27 18:29:39 +00:00
|
|
|
import logging
|
2015-02-26 07:27:17 +00:00
|
|
|
from pprint import pprint
|
2016-10-05 12:40:08 +00:00
|
|
|
|
2016-08-21 21:36:44 +00:00
|
|
|
import voluptuous as vol
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2017-04-10 20:11:39 +00:00
|
|
|
from homeassistant.core import CoreState
|
2017-02-23 21:06:28 +00:00
|
|
|
from homeassistant.loader import get_platform
|
2017-02-13 07:55:27 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2017-06-16 17:07:17 +00:00
|
|
|
from homeassistant.helpers.entity import generate_entity_id
|
2017-03-23 15:37:20 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2015-02-23 01:36:28 +00:00
|
|
|
from homeassistant.const import (
|
2017-03-23 15:37:20 +00:00
|
|
|
ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
2017-02-16 23:19:22 +00:00
|
|
|
from homeassistant.helpers.entity_values import EntityValues
|
2016-06-07 16:29:15 +00:00
|
|
|
from homeassistant.helpers.event import track_time_change
|
2018-03-11 14:30:03 +00:00
|
|
|
from homeassistant.util import convert
|
2018-01-24 08:27:58 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-08-21 21:36:44 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-03-04 17:13:24 +00:00
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
async_dispatcher_connect, async_dispatcher_send)
|
2017-02-16 23:19:22 +00:00
|
|
|
|
2016-09-30 15:43:18 +00:00
|
|
|
from . import const
|
2017-06-03 06:03:00 +00:00
|
|
|
from .const import DOMAIN, DATA_DEVICES, DATA_NETWORK, DATA_ENTITY_VALUES
|
2017-03-23 15:37:20 +00:00
|
|
|
from .node_entity import ZWaveBaseEntity, ZWaveNodeEntity
|
2017-02-08 04:37:11 +00:00
|
|
|
from . import workaround
|
2017-03-14 23:55:33 +00:00
|
|
|
from .discovery_schemas import DISCOVERY_SCHEMAS
|
2017-03-23 15:37:20 +00:00
|
|
|
from .util import check_node_schema, check_value_schema, node_name
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2018-02-03 16:08:48 +00:00
|
|
|
REQUIREMENTS = ['pydispatcher==2.0.5', 'python_openzwave==0.4.3']
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2016-10-05 12:40:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-02-09 12:40:35 +00:00
|
|
|
CLASS_ID = 'class_id'
|
2016-10-05 12:40:08 +00:00
|
|
|
CONF_AUTOHEAL = 'autoheal'
|
|
|
|
CONF_DEBUG = 'debug'
|
|
|
|
CONF_POLLING_INTENSITY = 'polling_intensity'
|
|
|
|
CONF_POLLING_INTERVAL = 'polling_interval'
|
|
|
|
CONF_USB_STICK_PATH = 'usb_path'
|
|
|
|
CONF_CONFIG_PATH = 'config_path'
|
2016-10-14 15:36:55 +00:00
|
|
|
CONF_IGNORED = 'ignored'
|
2017-03-28 20:01:29 +00:00
|
|
|
CONF_INVERT_OPENCLOSE_BUTTONS = 'invert_openclose_buttons'
|
2016-11-18 20:59:01 +00:00
|
|
|
CONF_REFRESH_VALUE = 'refresh_value'
|
|
|
|
CONF_REFRESH_DELAY = 'delay'
|
2017-02-13 07:55:27 +00:00
|
|
|
CONF_DEVICE_CONFIG = 'device_config'
|
2017-02-16 23:19:22 +00:00
|
|
|
CONF_DEVICE_CONFIG_GLOB = 'device_config_glob'
|
|
|
|
CONF_DEVICE_CONFIG_DOMAIN = 'device_config_domain'
|
2017-05-19 06:49:15 +00:00
|
|
|
CONF_NETWORK_KEY = 'network_key'
|
2016-10-05 12:40:08 +00:00
|
|
|
|
2017-02-19 00:51:13 +00:00
|
|
|
ATTR_POWER = 'power_consumption'
|
|
|
|
|
2016-06-07 16:29:15 +00:00
|
|
|
DEFAULT_CONF_AUTOHEAL = True
|
2016-10-05 12:40:08 +00:00
|
|
|
DEFAULT_CONF_USB_STICK_PATH = '/zwaveusbstick'
|
|
|
|
DEFAULT_POLLING_INTERVAL = 60000
|
2017-01-02 17:45:44 +00:00
|
|
|
DEFAULT_DEBUG = False
|
2016-10-14 15:36:55 +00:00
|
|
|
DEFAULT_CONF_IGNORED = False
|
2017-03-28 20:01:29 +00:00
|
|
|
DEFAULT_CONF_INVERT_OPENCLOSE_BUTTONS = False
|
2016-11-18 20:59:01 +00:00
|
|
|
DEFAULT_CONF_REFRESH_VALUE = False
|
2017-02-20 14:49:34 +00:00
|
|
|
DEFAULT_CONF_REFRESH_DELAY = 5
|
2016-10-05 12:40:08 +00:00
|
|
|
|
2016-08-21 21:36:44 +00:00
|
|
|
RENAME_NODE_SCHEMA = vol.Schema({
|
2017-04-05 12:11:37 +00:00
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
2016-09-30 15:43:18 +00:00
|
|
|
vol.Required(const.ATTR_NAME): cv.string,
|
2016-08-21 21:36:44 +00:00
|
|
|
})
|
2017-06-03 06:03:00 +00:00
|
|
|
|
|
|
|
RENAME_VALUE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_VALUE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2016-10-05 05:04:19 +00:00
|
|
|
SET_CONFIG_PARAMETER_SCHEMA = vol.Schema({
|
2016-10-14 06:45:00 +00:00
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
2016-10-05 05:04:19 +00:00
|
|
|
vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Coerce(int),
|
2017-05-19 00:39:31 +00:00
|
|
|
vol.Required(const.ATTR_CONFIG_VALUE): vol.Any(vol.Coerce(int), cv.string),
|
|
|
|
vol.Optional(const.ATTR_CONFIG_SIZE, default=2): vol.Coerce(int)
|
2016-10-05 05:04:19 +00:00
|
|
|
})
|
2017-06-03 06:03:00 +00:00
|
|
|
|
2017-09-11 18:30:48 +00:00
|
|
|
SET_POLL_INTENSITY_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_VALUE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_POLL_INTENSITY): vol.Coerce(int),
|
|
|
|
})
|
|
|
|
|
2017-01-01 20:10:45 +00:00
|
|
|
PRINT_CONFIG_PARAMETER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Coerce(int),
|
|
|
|
})
|
|
|
|
|
2017-03-02 11:36:40 +00:00
|
|
|
NODE_SERVICE_SCHEMA = vol.Schema({
|
2017-02-17 20:03:55 +00:00
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
})
|
|
|
|
|
2017-03-04 17:13:24 +00:00
|
|
|
REFRESH_ENTITY_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
|
|
|
})
|
|
|
|
|
2017-05-21 18:15:24 +00:00
|
|
|
RESET_NODE_METERS_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Optional(const.ATTR_INSTANCE, default=1): vol.Coerce(int)
|
|
|
|
})
|
|
|
|
|
2016-10-16 18:36:06 +00:00
|
|
|
CHANGE_ASSOCIATION_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_ASSOCIATION): cv.string,
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_TARGET_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_GROUP): vol.Coerce(int),
|
|
|
|
vol.Optional(const.ATTR_INSTANCE, default=0x00): vol.Coerce(int)
|
|
|
|
})
|
2016-08-21 21:36:44 +00:00
|
|
|
|
2017-02-10 16:54:48 +00:00
|
|
|
SET_WAKEUP_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Required(const.ATTR_CONFIG_VALUE):
|
|
|
|
vol.All(vol.Coerce(int), cv.positive_int),
|
|
|
|
})
|
|
|
|
|
2017-11-05 17:19:19 +00:00
|
|
|
HEAL_NODE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Optional(const.ATTR_RETURN_ROUTES, default=False): cv.boolean,
|
|
|
|
})
|
|
|
|
|
|
|
|
TEST_NODE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
|
|
|
vol.Optional(const.ATTR_MESSAGES, default=1): cv.positive_int,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-02-14 05:34:36 +00:00
|
|
|
DEVICE_CONFIG_SCHEMA_ENTRY = vol.Schema({
|
2017-01-28 20:29:51 +00:00
|
|
|
vol.Optional(CONF_POLLING_INTENSITY): cv.positive_int,
|
2016-10-14 15:36:55 +00:00
|
|
|
vol.Optional(CONF_IGNORED, default=DEFAULT_CONF_IGNORED): cv.boolean,
|
2017-03-28 20:01:29 +00:00
|
|
|
vol.Optional(CONF_INVERT_OPENCLOSE_BUTTONS,
|
|
|
|
default=DEFAULT_CONF_INVERT_OPENCLOSE_BUTTONS): cv.boolean,
|
2016-11-18 20:59:01 +00:00
|
|
|
vol.Optional(CONF_REFRESH_VALUE, default=DEFAULT_CONF_REFRESH_VALUE):
|
|
|
|
cv.boolean,
|
|
|
|
vol.Optional(CONF_REFRESH_DELAY, default=DEFAULT_CONF_REFRESH_DELAY):
|
|
|
|
cv.positive_int
|
2016-10-05 12:40:08 +00:00
|
|
|
})
|
|
|
|
|
2017-03-04 17:13:24 +00:00
|
|
|
SIGNAL_REFRESH_ENTITY_FORMAT = 'zwave_refresh_entity_{}'
|
|
|
|
|
2016-10-05 12:40:08 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(CONF_AUTOHEAL, default=DEFAULT_CONF_AUTOHEAL): cv.boolean,
|
|
|
|
vol.Optional(CONF_CONFIG_PATH): cv.string,
|
2017-05-19 06:49:15 +00:00
|
|
|
vol.Optional(CONF_NETWORK_KEY): cv.string,
|
2017-02-13 07:55:27 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CONFIG, default={}):
|
2017-02-14 05:34:36 +00:00
|
|
|
vol.Schema({cv.entity_id: DEVICE_CONFIG_SCHEMA_ENTRY}),
|
2017-02-16 23:19:22 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CONFIG_GLOB, default={}):
|
2017-04-30 17:55:03 +00:00
|
|
|
vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
|
2017-02-16 23:19:22 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CONFIG_DOMAIN, default={}):
|
|
|
|
vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
|
2017-01-02 17:45:44 +00:00
|
|
|
vol.Optional(CONF_DEBUG, default=DEFAULT_DEBUG): cv.boolean,
|
2016-10-05 12:40:08 +00:00
|
|
|
vol.Optional(CONF_POLLING_INTERVAL, default=DEFAULT_POLLING_INTERVAL):
|
|
|
|
cv.positive_int,
|
|
|
|
vol.Optional(CONF_USB_STICK_PATH, default=DEFAULT_CONF_USB_STICK_PATH):
|
|
|
|
cv.string,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2016-03-27 18:29:39 +00:00
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
def _obj_to_dict(obj):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Convert an object into a hash for debug."""
|
2015-02-26 07:27:17 +00:00
|
|
|
return {key: getattr(obj, key) for key
|
|
|
|
in dir(obj)
|
2018-01-30 22:44:05 +00:00
|
|
|
if key[0] != '_' and not callable(getattr(obj, key))}
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
|
2016-02-03 11:52:35 +00:00
|
|
|
def _value_name(value):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the name of the value."""
|
2017-06-16 17:07:17 +00:00
|
|
|
return '{} {}'.format(node_name(value.node), value.label).strip()
|
2016-02-03 11:52:35 +00:00
|
|
|
|
|
|
|
|
2015-02-23 08:01:04 +00:00
|
|
|
def nice_print_node(node):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Print a nice formatted node to the output (debug method)."""
|
2015-02-26 07:27:17 +00:00
|
|
|
node_dict = _obj_to_dict(node)
|
|
|
|
node_dict['values'] = {value_id: _obj_to_dict(value)
|
|
|
|
for value_id, value in node.values.items()}
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
print("\n\n\n")
|
2015-02-23 08:01:04 +00:00
|
|
|
print("FOUND NODE", node.product_name)
|
2015-02-26 07:27:17 +00:00
|
|
|
pprint(node_dict)
|
|
|
|
print("\n\n\n")
|
2015-02-23 08:01:04 +00:00
|
|
|
|
|
|
|
|
2017-02-12 23:42:09 +00:00
|
|
|
def get_config_value(node, value_index, tries=5):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the current configuration value for a specific index."""
|
2015-10-31 22:34:19 +00:00
|
|
|
try:
|
|
|
|
for value in node.values.values():
|
2017-04-07 13:17:23 +00:00
|
|
|
if (value.command_class == const.COMMAND_CLASS_CONFIGURATION
|
|
|
|
and value.index == value_index):
|
2015-10-31 22:34:19 +00:00
|
|
|
return value.data
|
|
|
|
except RuntimeError:
|
2018-01-27 19:58:27 +00:00
|
|
|
# If we get a runtime error the dict has changed while
|
2015-10-31 22:34:19 +00:00
|
|
|
# we was looking for a value, just do it again
|
2017-02-12 23:42:09 +00:00
|
|
|
return None if tries <= 0 else get_config_value(
|
|
|
|
node, value_index, tries=tries - 1)
|
|
|
|
return None
|
2015-10-31 20:23:33 +00:00
|
|
|
|
|
|
|
|
2018-03-17 03:27:05 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_devices,
|
|
|
|
discovery_info=None):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Set up the Z-Wave platform (generic part)."""
|
2017-05-26 04:11:02 +00:00
|
|
|
if discovery_info is None or DATA_NETWORK not in hass.data:
|
2017-02-23 21:06:28 +00:00
|
|
|
return False
|
2017-04-10 20:11:39 +00:00
|
|
|
|
2017-05-26 04:11:02 +00:00
|
|
|
device = hass.data[DATA_DEVICES].pop(
|
2017-04-10 20:11:39 +00:00
|
|
|
discovery_info[const.DISCOVERY_DEVICE], None)
|
|
|
|
if device is None:
|
2017-02-23 21:06:28 +00:00
|
|
|
return False
|
|
|
|
|
2017-04-10 20:11:39 +00:00
|
|
|
async_add_devices([device])
|
|
|
|
return True
|
|
|
|
|
2017-02-23 21:06:28 +00:00
|
|
|
|
2016-03-13 17:51:09 +00:00
|
|
|
# pylint: disable=R0914
|
2015-02-23 01:36:28 +00:00
|
|
|
def setup(hass, config):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up Z-Wave.
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
Will automatically load components to support devices found on the network.
|
|
|
|
"""
|
2015-03-01 06:49:27 +00:00
|
|
|
from pydispatch import dispatcher
|
2017-04-12 17:09:29 +00:00
|
|
|
# pylint: disable=import-error
|
2015-02-23 01:36:28 +00:00
|
|
|
from openzwave.option import ZWaveOption
|
|
|
|
from openzwave.network import ZWaveNetwork
|
2016-10-16 18:36:06 +00:00
|
|
|
from openzwave.group import ZWaveGroup
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2016-02-03 11:52:35 +00:00
|
|
|
# Load configuration
|
2016-10-05 12:40:08 +00:00
|
|
|
use_debug = config[DOMAIN].get(CONF_DEBUG)
|
|
|
|
autoheal = config[DOMAIN].get(CONF_AUTOHEAL)
|
2017-02-23 21:06:28 +00:00
|
|
|
device_config = EntityValues(
|
2017-02-16 23:19:22 +00:00
|
|
|
config[DOMAIN][CONF_DEVICE_CONFIG],
|
|
|
|
config[DOMAIN][CONF_DEVICE_CONFIG_DOMAIN],
|
|
|
|
config[DOMAIN][CONF_DEVICE_CONFIG_GLOB])
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
# Setup options
|
|
|
|
options = ZWaveOption(
|
2016-10-05 12:40:08 +00:00
|
|
|
config[DOMAIN].get(CONF_USB_STICK_PATH),
|
2015-11-20 04:35:18 +00:00
|
|
|
user_path=hass.config.config_dir,
|
2017-05-05 21:57:14 +00:00
|
|
|
config_path=config[DOMAIN].get(CONF_CONFIG_PATH))
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-02-23 08:01:04 +00:00
|
|
|
options.set_console_output(use_debug)
|
2017-05-19 06:49:15 +00:00
|
|
|
|
|
|
|
if CONF_NETWORK_KEY in config[DOMAIN]:
|
|
|
|
options.addOption("NetworkKey", config[DOMAIN][CONF_NETWORK_KEY])
|
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
options.lock()
|
|
|
|
|
2017-05-26 04:11:02 +00:00
|
|
|
network = hass.data[DATA_NETWORK] = ZWaveNetwork(options, autostart=False)
|
|
|
|
hass.data[DATA_DEVICES] = {}
|
2017-06-03 06:03:00 +00:00
|
|
|
hass.data[DATA_ENTITY_VALUES] = []
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2017-04-10 20:11:39 +00:00
|
|
|
if use_debug: # pragma: no cover
|
2015-02-26 07:27:17 +00:00
|
|
|
def log_all(signal, value=None):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Log all the signals."""
|
2015-02-23 08:01:04 +00:00
|
|
|
print("")
|
2015-03-19 02:15:48 +00:00
|
|
|
print("SIGNAL *****", signal)
|
2015-02-26 07:27:17 +00:00
|
|
|
if value and signal in (ZWaveNetwork.SIGNAL_VALUE_CHANGED,
|
2016-07-29 19:56:03 +00:00
|
|
|
ZWaveNetwork.SIGNAL_VALUE_ADDED,
|
|
|
|
ZWaveNetwork.SIGNAL_SCENE_EVENT,
|
2016-08-08 14:52:28 +00:00
|
|
|
ZWaveNetwork.SIGNAL_NODE_EVENT,
|
|
|
|
ZWaveNetwork.SIGNAL_AWAKE_NODES_QUERIED,
|
|
|
|
ZWaveNetwork.SIGNAL_ALL_NODES_QUERIED):
|
2015-02-26 07:27:17 +00:00
|
|
|
pprint(_obj_to_dict(value))
|
2016-02-03 11:52:35 +00:00
|
|
|
|
2015-02-23 08:01:04 +00:00
|
|
|
print("")
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-03-01 06:49:27 +00:00
|
|
|
dispatcher.connect(log_all, weak=False)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
def value_added(node, value):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle new added value to a node on the network."""
|
2017-03-14 23:55:33 +00:00
|
|
|
# Check if this value should be tracked by an existing entity
|
2017-06-03 06:03:00 +00:00
|
|
|
for values in hass.data[DATA_ENTITY_VALUES]:
|
2017-03-14 23:55:33 +00:00
|
|
|
values.check_value(value)
|
2017-03-13 06:35:10 +00:00
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
for schema in DISCOVERY_SCHEMAS:
|
|
|
|
if not check_node_schema(node, schema):
|
|
|
|
continue
|
|
|
|
if not check_value_schema(
|
|
|
|
value,
|
2017-03-23 15:37:20 +00:00
|
|
|
schema[const.DISC_VALUES][const.DISC_PRIMARY]):
|
2017-03-13 06:35:10 +00:00
|
|
|
continue
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
values = ZWaveDeviceEntityValues(
|
|
|
|
hass, schema, value, config, device_config)
|
2017-06-03 06:03:00 +00:00
|
|
|
|
|
|
|
# We create a new list and update the reference here so that
|
|
|
|
# the list can be safely iterated over in the main thread
|
|
|
|
new_values = hass.data[DATA_ENTITY_VALUES] + [values]
|
|
|
|
hass.data[DATA_ENTITY_VALUES] = new_values
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2017-03-23 15:37:20 +00:00
|
|
|
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
|
|
|
|
|
|
|
def node_added(node):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle a new node on the network."""
|
2018-03-11 14:30:03 +00:00
|
|
|
entity = ZWaveNodeEntity(node, network)
|
2017-06-16 17:07:17 +00:00
|
|
|
name = node_name(node)
|
2018-03-11 14:30:03 +00:00
|
|
|
generated_id = generate_entity_id(DOMAIN + '.{}', name, [])
|
2017-06-16 17:07:17 +00:00
|
|
|
node_config = device_config.get(generated_id)
|
2017-03-23 15:37:20 +00:00
|
|
|
if node_config.get(CONF_IGNORED):
|
|
|
|
_LOGGER.info(
|
2017-05-02 20:47:20 +00:00
|
|
|
"Ignoring node entity %s due to device settings",
|
2017-06-16 17:07:17 +00:00
|
|
|
generated_id)
|
2017-03-23 15:37:20 +00:00
|
|
|
return
|
|
|
|
component.add_entities([entity])
|
|
|
|
|
2016-08-08 14:52:28 +00:00
|
|
|
def network_ready():
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle the query of all awake nodes."""
|
|
|
|
_LOGGER.info("Zwave network is ready for use. All awake nodes "
|
|
|
|
"have been queried. Sleeping nodes will be "
|
|
|
|
"queried when they awake.")
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.bus.fire(const.EVENT_NETWORK_READY)
|
2016-08-08 14:52:28 +00:00
|
|
|
|
|
|
|
def network_complete():
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle the querying of all nodes on network."""
|
|
|
|
_LOGGER.info("Z-Wave network is complete. All nodes on the network "
|
|
|
|
"have been queried")
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.bus.fire(const.EVENT_NETWORK_COMPLETE)
|
2016-08-08 14:52:28 +00:00
|
|
|
|
2015-03-01 06:49:27 +00:00
|
|
|
dispatcher.connect(
|
2015-02-26 07:27:17 +00:00
|
|
|
value_added, ZWaveNetwork.SIGNAL_VALUE_ADDED, weak=False)
|
2017-03-23 15:37:20 +00:00
|
|
|
dispatcher.connect(
|
|
|
|
node_added, ZWaveNetwork.SIGNAL_NODE_ADDED, weak=False)
|
2016-08-08 14:52:28 +00:00
|
|
|
dispatcher.connect(
|
|
|
|
network_ready, ZWaveNetwork.SIGNAL_AWAKE_NODES_QUERIED, weak=False)
|
|
|
|
dispatcher.connect(
|
|
|
|
network_complete, ZWaveNetwork.SIGNAL_ALL_NODES_QUERIED, weak=False)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
def add_node(service):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Switch into inclusion mode."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave add_node have been initialized")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.add_node()
|
2016-01-10 13:44:46 +00:00
|
|
|
|
2016-08-02 18:17:10 +00:00
|
|
|
def add_node_secure(service):
|
|
|
|
"""Switch into secure inclusion mode."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave add_node_secure have been initialized")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.add_node(True)
|
2016-08-02 18:17:10 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
def remove_node(service):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Switch into exclusion mode."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wwave remove_node have been initialized")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.remove_node()
|
2016-01-10 13:44:46 +00:00
|
|
|
|
2016-08-08 14:52:28 +00:00
|
|
|
def cancel_command(service):
|
|
|
|
"""Cancel a running controller command."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Cancel running Z-Wave command")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.cancel_command()
|
2016-08-08 14:52:28 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
def heal_network(service):
|
2016-03-13 17:51:09 +00:00
|
|
|
"""Heal the network."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave heal running")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.heal()
|
2016-03-13 17:51:09 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
def soft_reset(service):
|
2016-03-13 17:51:09 +00:00
|
|
|
"""Soft reset the controller."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave soft_reset have been initialized")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.soft_reset()
|
2016-03-13 17:51:09 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
def test_network(service):
|
2016-03-30 08:29:11 +00:00
|
|
|
"""Test the network by sending commands to all the nodes."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave test_network have been initialized")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.test()
|
2016-03-30 08:29:11 +00:00
|
|
|
|
2017-04-07 13:17:23 +00:00
|
|
|
def stop_network(_service_or_event):
|
2016-08-08 14:52:28 +00:00
|
|
|
"""Stop Z-Wave network."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Stopping Z-Wave network")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.stop()
|
2017-04-10 20:11:39 +00:00
|
|
|
if hass.state == CoreState.running:
|
2016-10-22 12:08:24 +00:00
|
|
|
hass.bus.fire(const.EVENT_NETWORK_STOP)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2016-08-21 21:36:44 +00:00
|
|
|
def rename_node(service):
|
|
|
|
"""Rename a node."""
|
2017-04-05 12:11:37 +00:00
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-05-10 01:56:41 +00:00
|
|
|
node = network.nodes[node_id]
|
2016-09-30 15:43:18 +00:00
|
|
|
name = service.data.get(const.ATTR_NAME)
|
2016-08-21 21:36:44 +00:00
|
|
|
node.name = name
|
|
|
|
_LOGGER.info(
|
2017-05-02 20:47:20 +00:00
|
|
|
"Renamed Z-Wave node %d to %s", node_id, name)
|
2016-08-21 21:36:44 +00:00
|
|
|
|
2017-06-03 06:03:00 +00:00
|
|
|
def rename_value(service):
|
|
|
|
"""Rename a node value."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
value_id = service.data.get(const.ATTR_VALUE_ID)
|
|
|
|
node = network.nodes[node_id]
|
|
|
|
value = node.values[value_id]
|
|
|
|
name = service.data.get(const.ATTR_NAME)
|
|
|
|
value.label = name
|
|
|
|
_LOGGER.info(
|
|
|
|
"Renamed Z-Wave value (Node %d Value %d) to %s",
|
|
|
|
node_id, value_id, name)
|
|
|
|
|
2017-09-11 18:30:48 +00:00
|
|
|
def set_poll_intensity(service):
|
|
|
|
"""Set the polling intensity of a node value."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
value_id = service.data.get(const.ATTR_VALUE_ID)
|
|
|
|
node = network.nodes[node_id]
|
|
|
|
value = node.values[value_id]
|
|
|
|
intensity = service.data.get(const.ATTR_POLL_INTENSITY)
|
|
|
|
if intensity == 0:
|
|
|
|
if value.disable_poll():
|
|
|
|
_LOGGER.info("Polling disabled (Node %d Value %d)",
|
|
|
|
node_id, value_id)
|
|
|
|
return
|
|
|
|
_LOGGER.info("Polling disabled failed (Node %d Value %d)",
|
|
|
|
node_id, value_id)
|
|
|
|
else:
|
|
|
|
if value.enable_poll(intensity):
|
|
|
|
_LOGGER.info(
|
|
|
|
"Set polling intensity (Node %d Value %d) to %s",
|
|
|
|
node_id, value_id, intensity)
|
|
|
|
return
|
|
|
|
_LOGGER.info("Set polling intensity failed (Node %d Value %d)",
|
|
|
|
node_id, value_id)
|
|
|
|
|
2017-03-02 11:36:40 +00:00
|
|
|
def remove_failed_node(service):
|
|
|
|
"""Remove failed node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Trying to remove zwave node %d", node_id)
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.remove_failed_node(node_id)
|
2017-03-02 11:36:40 +00:00
|
|
|
|
|
|
|
def replace_failed_node(service):
|
|
|
|
"""Replace failed node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Trying to replace zwave node %d", node_id)
|
2017-04-12 17:09:29 +00:00
|
|
|
network.controller.replace_failed_node(node_id)
|
2017-03-02 11:36:40 +00:00
|
|
|
|
2016-10-05 05:04:19 +00:00
|
|
|
def set_config_parameter(service):
|
|
|
|
"""Set a config parameter to a node."""
|
2016-10-14 06:45:00 +00:00
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-04-12 17:09:29 +00:00
|
|
|
node = network.nodes[node_id]
|
2016-10-05 05:04:19 +00:00
|
|
|
param = service.data.get(const.ATTR_CONFIG_PARAMETER)
|
2017-01-02 17:53:46 +00:00
|
|
|
selection = service.data.get(const.ATTR_CONFIG_VALUE)
|
2017-05-19 00:39:31 +00:00
|
|
|
size = service.data.get(const.ATTR_CONFIG_SIZE)
|
2017-01-02 17:53:46 +00:00
|
|
|
for value in (
|
|
|
|
node.get_values(class_id=const.COMMAND_CLASS_CONFIGURATION)
|
|
|
|
.values()):
|
2017-05-19 00:39:31 +00:00
|
|
|
if value.index != param:
|
|
|
|
continue
|
|
|
|
if value.type in [const.TYPE_LIST, const.TYPE_BOOL]:
|
|
|
|
value.data = selection
|
|
|
|
_LOGGER.info("Setting config list parameter %s on Node %s "
|
|
|
|
"with selection %s", param, node_id,
|
|
|
|
selection)
|
|
|
|
return
|
2017-07-06 03:02:16 +00:00
|
|
|
value.data = int(selection)
|
|
|
|
_LOGGER.info("Setting config parameter %s on Node %s "
|
|
|
|
"with selection %s", param, node_id,
|
|
|
|
selection)
|
|
|
|
return
|
2017-05-19 00:39:31 +00:00
|
|
|
node.set_config_param(param, selection, size)
|
|
|
|
_LOGGER.info("Setting unknown config parameter %s on Node %s "
|
|
|
|
"with selection %s", param, node_id,
|
|
|
|
selection)
|
2016-10-05 05:04:19 +00:00
|
|
|
|
2017-01-01 20:10:45 +00:00
|
|
|
def print_config_parameter(service):
|
|
|
|
"""Print a config parameter from a node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-04-12 17:09:29 +00:00
|
|
|
node = network.nodes[node_id]
|
2017-01-01 20:10:45 +00:00
|
|
|
param = service.data.get(const.ATTR_CONFIG_PARAMETER)
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Config parameter %s on Node %s: %s",
|
2017-01-01 20:10:45 +00:00
|
|
|
param, node_id, get_config_value(node, param))
|
|
|
|
|
2017-02-17 20:03:55 +00:00
|
|
|
def print_node(service):
|
|
|
|
"""Print all information about z-wave node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-04-12 17:09:29 +00:00
|
|
|
node = network.nodes[node_id]
|
2017-02-17 20:03:55 +00:00
|
|
|
nice_print_node(node)
|
|
|
|
|
2017-02-10 16:54:48 +00:00
|
|
|
def set_wakeup(service):
|
|
|
|
"""Set wake-up interval of a node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-04-12 17:09:29 +00:00
|
|
|
node = network.nodes[node_id]
|
2017-02-10 16:54:48 +00:00
|
|
|
value = service.data.get(const.ATTR_CONFIG_VALUE)
|
|
|
|
if node.can_wake_up():
|
|
|
|
for value_id in node.get_values(
|
|
|
|
class_id=const.COMMAND_CLASS_WAKE_UP):
|
|
|
|
node.values[value_id].data = value
|
|
|
|
_LOGGER.info("Node %s wake-up set to %d", node_id, value)
|
|
|
|
else:
|
|
|
|
_LOGGER.info("Node %s is not wakeable", node_id)
|
|
|
|
|
2016-10-16 18:36:06 +00:00
|
|
|
def change_association(service):
|
|
|
|
"""Change an association in the zwave network."""
|
|
|
|
association_type = service.data.get(const.ATTR_ASSOCIATION)
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
target_node_id = service.data.get(const.ATTR_TARGET_NODE_ID)
|
|
|
|
group = service.data.get(const.ATTR_GROUP)
|
|
|
|
instance = service.data.get(const.ATTR_INSTANCE)
|
|
|
|
|
2017-04-12 17:09:29 +00:00
|
|
|
node = ZWaveGroup(group, network, node_id)
|
2016-10-16 18:36:06 +00:00
|
|
|
if association_type == 'add':
|
|
|
|
node.add_association(target_node_id, instance)
|
|
|
|
_LOGGER.info("Adding association for node:%s in group:%s "
|
|
|
|
"target node:%s, instance=%s", node_id, group,
|
|
|
|
target_node_id, instance)
|
|
|
|
if association_type == 'remove':
|
|
|
|
node.remove_association(target_node_id, instance)
|
|
|
|
_LOGGER.info("Removing association for node:%s in group:%s "
|
|
|
|
"target node:%s, instance=%s", node_id, group,
|
|
|
|
target_node_id, instance)
|
|
|
|
|
2018-03-17 03:27:05 +00:00
|
|
|
async def async_refresh_entity(service):
|
2017-03-04 17:13:24 +00:00
|
|
|
"""Refresh values that specific entity depends on."""
|
|
|
|
entity_id = service.data.get(ATTR_ENTITY_ID)
|
|
|
|
async_dispatcher_send(
|
|
|
|
hass, SIGNAL_REFRESH_ENTITY_FORMAT.format(entity_id))
|
|
|
|
|
|
|
|
def refresh_node(service):
|
|
|
|
"""Refresh all node info."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
2017-04-12 17:09:29 +00:00
|
|
|
node = network.nodes[node_id]
|
2017-03-04 17:13:24 +00:00
|
|
|
node.refresh_info()
|
|
|
|
|
2017-05-21 18:15:24 +00:00
|
|
|
def reset_node_meters(service):
|
|
|
|
"""Reset meter counters of a node."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
instance = service.data.get(const.ATTR_INSTANCE)
|
|
|
|
node = network.nodes[node_id]
|
|
|
|
for value in (
|
|
|
|
node.get_values(class_id=const.COMMAND_CLASS_METER)
|
|
|
|
.values()):
|
2017-06-14 12:41:20 +00:00
|
|
|
if value.index != const.INDEX_METER_RESET:
|
2017-05-21 18:15:24 +00:00
|
|
|
continue
|
|
|
|
if value.instance != instance:
|
|
|
|
continue
|
|
|
|
network.manager.pressButton(value.value_id)
|
|
|
|
network.manager.releaseButton(value.value_id)
|
|
|
|
_LOGGER.info("Resetting meters on node %s instance %s....",
|
|
|
|
node_id, instance)
|
|
|
|
return
|
|
|
|
_LOGGER.info("Node %s on instance %s does not have resettable "
|
|
|
|
"meters.", node_id, instance)
|
|
|
|
|
2017-11-05 17:19:19 +00:00
|
|
|
def heal_node(service):
|
|
|
|
"""Heal a node on the network."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
update_return_routes = service.data.get(const.ATTR_RETURN_ROUTES)
|
|
|
|
node = network.nodes[node_id]
|
|
|
|
_LOGGER.info("Z-Wave node heal running for node %s", node_id)
|
|
|
|
node.heal(update_return_routes)
|
|
|
|
|
|
|
|
def test_node(service):
|
|
|
|
"""Send test messages to a node on the network."""
|
|
|
|
node_id = service.data.get(const.ATTR_NODE_ID)
|
|
|
|
messages = service.data.get(const.ATTR_MESSAGES)
|
|
|
|
node = network.nodes[node_id]
|
|
|
|
_LOGGER.info("Sending %s test-messages to node %s.", messages, node_id)
|
|
|
|
node.test(messages)
|
|
|
|
|
2016-08-02 17:08:04 +00:00
|
|
|
def start_zwave(_service_or_event):
|
2016-08-08 14:52:28 +00:00
|
|
|
"""Startup Z-Wave network."""
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Starting Z-Wave network...")
|
2017-04-12 17:09:29 +00:00
|
|
|
network.start()
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.bus.fire(const.EVENT_NETWORK_START)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2018-03-17 03:27:05 +00:00
|
|
|
async def _check_awaked():
|
2018-01-24 08:27:58 +00:00
|
|
|
"""Wait for Z-wave awaked state (or timeout) and finalize start."""
|
2016-06-11 05:53:31 +00:00
|
|
|
_LOGGER.debug(
|
2017-05-10 01:56:41 +00:00
|
|
|
"network state: %d %s", network.state,
|
2017-04-12 17:09:29 +00:00
|
|
|
network.state_str)
|
2016-03-27 18:29:39 +00:00
|
|
|
|
2018-01-24 08:27:58 +00:00
|
|
|
start_time = dt_util.utcnow()
|
|
|
|
while True:
|
|
|
|
waited = int((dt_util.utcnow()-start_time).total_seconds())
|
|
|
|
|
|
|
|
if network.state >= network.STATE_AWAKED:
|
|
|
|
# Need to be in STATE_AWAKED before talking to nodes.
|
|
|
|
_LOGGER.info("Z-Wave ready after %d seconds", waited)
|
|
|
|
break
|
|
|
|
elif waited >= const.NETWORK_READY_WAIT_SECS:
|
|
|
|
# Wait up to NETWORK_READY_WAIT_SECS seconds for the Z-Wave
|
|
|
|
# network to be ready.
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Z-Wave not ready after %d seconds, continuing anyway",
|
|
|
|
waited)
|
|
|
|
_LOGGER.info(
|
|
|
|
"final network state: %d %s", network.state,
|
|
|
|
network.state_str)
|
|
|
|
break
|
|
|
|
else:
|
2018-03-17 03:27:05 +00:00
|
|
|
await asyncio.sleep(1, loop=hass.loop)
|
2018-01-24 08:27:58 +00:00
|
|
|
|
|
|
|
hass.async_add_job(_finalize_start)
|
|
|
|
|
|
|
|
hass.add_job(_check_awaked)
|
|
|
|
|
|
|
|
def _finalize_start():
|
|
|
|
"""Perform final initializations after Z-Wave network is awaked."""
|
2016-02-03 11:52:35 +00:00
|
|
|
polling_interval = convert(
|
|
|
|
config[DOMAIN].get(CONF_POLLING_INTERVAL), int)
|
2015-11-15 16:45:20 +00:00
|
|
|
if polling_interval is not None:
|
2017-04-12 17:09:29 +00:00
|
|
|
network.set_poll_interval(polling_interval, False)
|
2015-11-15 16:45:20 +00:00
|
|
|
|
2017-04-12 17:09:29 +00:00
|
|
|
poll_interval = network.get_poll_interval()
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave polling interval set to %d ms", poll_interval)
|
2016-03-27 18:29:39 +00:00
|
|
|
|
2017-04-07 13:17:23 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_network)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2016-08-02 18:17:10 +00:00
|
|
|
# Register node services for Z-Wave network
|
2018-01-07 22:54:16 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_ADD_NODE, add_node)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_ADD_NODE_SECURE,
|
2018-01-07 22:54:16 +00:00
|
|
|
add_node_secure)
|
|
|
|
hass.services.register(DOMAIN, const.SERVICE_REMOVE_NODE, remove_node)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_CANCEL_COMMAND,
|
2018-01-07 22:54:16 +00:00
|
|
|
cancel_command)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_HEAL_NETWORK,
|
2018-01-07 22:54:16 +00:00
|
|
|
heal_network)
|
|
|
|
hass.services.register(DOMAIN, const.SERVICE_SOFT_RESET, soft_reset)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_TEST_NETWORK,
|
2018-01-07 22:54:16 +00:00
|
|
|
test_network)
|
2017-04-07 13:17:23 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_STOP_NETWORK,
|
2018-01-07 22:54:16 +00:00
|
|
|
stop_network)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_START_NETWORK,
|
2018-01-07 22:54:16 +00:00
|
|
|
start_zwave)
|
2016-09-30 15:43:18 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_RENAME_NODE, rename_node,
|
2016-08-21 21:36:44 +00:00
|
|
|
schema=RENAME_NODE_SCHEMA)
|
2017-06-03 06:03:00 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_RENAME_VALUE,
|
|
|
|
rename_value,
|
|
|
|
schema=RENAME_VALUE_SCHEMA)
|
2016-10-05 05:04:19 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_SET_CONFIG_PARAMETER,
|
|
|
|
set_config_parameter,
|
|
|
|
schema=SET_CONFIG_PARAMETER_SCHEMA)
|
2017-01-01 20:10:45 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_PRINT_CONFIG_PARAMETER,
|
|
|
|
print_config_parameter,
|
|
|
|
schema=PRINT_CONFIG_PARAMETER_SCHEMA)
|
2017-03-02 11:36:40 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_REMOVE_FAILED_NODE,
|
|
|
|
remove_failed_node,
|
|
|
|
schema=NODE_SERVICE_SCHEMA)
|
|
|
|
hass.services.register(DOMAIN, const.SERVICE_REPLACE_FAILED_NODE,
|
|
|
|
replace_failed_node,
|
|
|
|
schema=NODE_SERVICE_SCHEMA)
|
|
|
|
|
2016-10-16 18:36:06 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_CHANGE_ASSOCIATION,
|
|
|
|
change_association,
|
|
|
|
schema=CHANGE_ASSOCIATION_SCHEMA)
|
2017-02-10 16:54:48 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_SET_WAKEUP,
|
|
|
|
set_wakeup,
|
|
|
|
schema=SET_WAKEUP_SCHEMA)
|
2017-02-17 20:03:55 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_PRINT_NODE,
|
|
|
|
print_node,
|
2017-03-02 11:36:40 +00:00
|
|
|
schema=NODE_SERVICE_SCHEMA)
|
2017-03-04 17:13:24 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_REFRESH_ENTITY,
|
|
|
|
async_refresh_entity,
|
|
|
|
schema=REFRESH_ENTITY_SCHEMA)
|
|
|
|
hass.services.register(DOMAIN, const.SERVICE_REFRESH_NODE,
|
|
|
|
refresh_node,
|
|
|
|
schema=NODE_SERVICE_SCHEMA)
|
2017-05-21 18:15:24 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_RESET_NODE_METERS,
|
|
|
|
reset_node_meters,
|
|
|
|
schema=RESET_NODE_METERS_SCHEMA)
|
2017-09-11 18:30:48 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_SET_POLL_INTENSITY,
|
|
|
|
set_poll_intensity,
|
|
|
|
schema=SET_POLL_INTENSITY_SCHEMA)
|
2017-11-05 17:19:19 +00:00
|
|
|
hass.services.register(DOMAIN, const.SERVICE_HEAL_NODE,
|
|
|
|
heal_node,
|
|
|
|
schema=HEAL_NODE_SCHEMA)
|
|
|
|
hass.services.register(DOMAIN, const.SERVICE_TEST_NODE,
|
|
|
|
test_node,
|
|
|
|
schema=TEST_NODE_SCHEMA)
|
2016-01-10 13:44:46 +00:00
|
|
|
|
2016-06-11 05:53:31 +00:00
|
|
|
# Setup autoheal
|
|
|
|
if autoheal:
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.info("Z-Wave network autoheal is enabled")
|
2016-06-11 05:53:31 +00:00
|
|
|
track_time_change(hass, heal_network, hour=0, minute=0, second=0)
|
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_zwave)
|
2015-03-03 16:49:31 +00:00
|
|
|
|
|
|
|
return True
|
2016-01-26 21:11:27 +00:00
|
|
|
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
class ZWaveDeviceEntityValues():
|
|
|
|
"""Manages entity access to the underlying zwave value objects."""
|
|
|
|
|
|
|
|
def __init__(self, hass, schema, primary_value, zwave_config,
|
|
|
|
device_config):
|
|
|
|
"""Initialize the values object with the passed entity schema."""
|
|
|
|
self._hass = hass
|
|
|
|
self._zwave_config = zwave_config
|
|
|
|
self._device_config = device_config
|
|
|
|
self._schema = copy.deepcopy(schema)
|
|
|
|
self._values = {}
|
|
|
|
self._entity = None
|
|
|
|
self._workaround_ignore = False
|
|
|
|
|
2017-03-23 15:37:20 +00:00
|
|
|
for name in self._schema[const.DISC_VALUES].keys():
|
2017-03-14 23:55:33 +00:00
|
|
|
self._values[name] = None
|
|
|
|
self._schema[const.DISC_VALUES][name][const.DISC_INSTANCE] = \
|
|
|
|
[primary_value.instance]
|
|
|
|
|
|
|
|
self._values[const.DISC_PRIMARY] = primary_value
|
|
|
|
self._node = primary_value.node
|
|
|
|
self._schema[const.DISC_NODE_ID] = [self._node.node_id]
|
|
|
|
|
|
|
|
# Check values that have already been discovered for node
|
|
|
|
for value in self._node.values.values():
|
|
|
|
self.check_value(value)
|
|
|
|
|
|
|
|
self._check_entity_ready()
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
"""Get the specified value for this entity."""
|
|
|
|
return self._values[name]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""Allow iteration over all values."""
|
|
|
|
return iter(self._values.values())
|
|
|
|
|
|
|
|
def check_value(self, value):
|
|
|
|
"""Check if the new value matches a missing value for this entity.
|
|
|
|
|
|
|
|
If a match is found, it is added to the values mapping.
|
|
|
|
"""
|
|
|
|
if not check_node_schema(value.node, self._schema):
|
|
|
|
return
|
|
|
|
for name in self._values:
|
|
|
|
if self._values[name] is not None:
|
|
|
|
continue
|
|
|
|
if not check_value_schema(
|
|
|
|
value, self._schema[const.DISC_VALUES][name]):
|
|
|
|
continue
|
|
|
|
self._values[name] = value
|
|
|
|
if self._entity:
|
2017-04-03 18:56:48 +00:00
|
|
|
self._entity.value_added()
|
2017-03-14 23:55:33 +00:00
|
|
|
self._entity.value_changed()
|
|
|
|
|
|
|
|
self._check_entity_ready()
|
|
|
|
|
|
|
|
def _check_entity_ready(self):
|
|
|
|
"""Check if all required values are discovered and create entity."""
|
|
|
|
if self._workaround_ignore:
|
|
|
|
return
|
|
|
|
if self._entity is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
for name in self._schema[const.DISC_VALUES]:
|
|
|
|
if self._values[name] is None and \
|
|
|
|
not self._schema[const.DISC_VALUES][name].get(
|
|
|
|
const.DISC_OPTIONAL):
|
|
|
|
return
|
|
|
|
|
|
|
|
component = self._schema[const.DISC_COMPONENT]
|
|
|
|
|
|
|
|
workaround_component = workaround.get_device_component_mapping(
|
|
|
|
self.primary)
|
|
|
|
if workaround_component and workaround_component != component:
|
|
|
|
if workaround_component == workaround.WORKAROUND_IGNORE:
|
2017-06-16 17:07:17 +00:00
|
|
|
_LOGGER.info("Ignoring Node %d Value %d due to workaround.",
|
|
|
|
self.primary.node.node_id, self.primary.value_id)
|
2017-03-14 23:55:33 +00:00
|
|
|
# No entity will be created for this value
|
|
|
|
self._workaround_ignore = True
|
|
|
|
return
|
|
|
|
_LOGGER.debug("Using %s instead of %s",
|
|
|
|
workaround_component, component)
|
|
|
|
component = workaround_component
|
|
|
|
|
2017-06-16 17:07:17 +00:00
|
|
|
value_name = _value_name(self.primary)
|
2018-03-11 14:30:03 +00:00
|
|
|
generated_id = generate_entity_id(component + '.{}', value_name, [])
|
2017-06-16 17:07:17 +00:00
|
|
|
node_config = self._device_config.get(generated_id)
|
2017-03-14 23:55:33 +00:00
|
|
|
|
|
|
|
# Configure node
|
|
|
|
_LOGGER.debug("Adding Node_id=%s Generic_command_class=%s, "
|
|
|
|
"Specific_command_class=%s, "
|
|
|
|
"Command_class=%s, Value type=%s, "
|
|
|
|
"Genre=%s as %s", self._node.node_id,
|
|
|
|
self._node.generic, self._node.specific,
|
|
|
|
self.primary.command_class, self.primary.type,
|
|
|
|
self.primary.genre, component)
|
|
|
|
|
|
|
|
if node_config.get(CONF_IGNORED):
|
|
|
|
_LOGGER.info(
|
2017-06-16 17:07:17 +00:00
|
|
|
"Ignoring entity %s due to device settings", generated_id)
|
2017-03-14 23:55:33 +00:00
|
|
|
# No entity will be created for this value
|
|
|
|
self._workaround_ignore = True
|
|
|
|
return
|
|
|
|
|
|
|
|
polling_intensity = convert(
|
|
|
|
node_config.get(CONF_POLLING_INTENSITY), int)
|
|
|
|
if polling_intensity:
|
|
|
|
self.primary.enable_poll(polling_intensity)
|
|
|
|
|
|
|
|
platform = get_platform(component, DOMAIN)
|
|
|
|
device = platform.get_device(
|
|
|
|
node=self._node, values=self,
|
|
|
|
node_config=node_config, hass=self._hass)
|
2017-04-08 13:34:59 +00:00
|
|
|
if device is None:
|
2017-03-14 23:55:33 +00:00
|
|
|
# No entity will be created for this value
|
|
|
|
self._workaround_ignore = True
|
|
|
|
return
|
|
|
|
|
|
|
|
self._entity = device
|
|
|
|
|
|
|
|
dict_id = id(self)
|
|
|
|
|
2018-03-17 03:27:05 +00:00
|
|
|
async def discover_device(component, device, dict_id):
|
2017-03-14 23:55:33 +00:00
|
|
|
"""Put device in a dictionary and call discovery on it."""
|
2017-05-26 04:11:02 +00:00
|
|
|
self._hass.data[DATA_DEVICES][dict_id] = device
|
2018-03-17 03:27:05 +00:00
|
|
|
await discovery.async_load_platform(
|
2017-03-14 23:55:33 +00:00
|
|
|
self._hass, component, DOMAIN,
|
|
|
|
{const.DISCOVERY_DEVICE: dict_id}, self._zwave_config)
|
|
|
|
self._hass.add_job(discover_device, component, device, dict_id)
|
|
|
|
|
|
|
|
|
2017-03-23 15:37:20 +00:00
|
|
|
class ZWaveDeviceEntity(ZWaveBaseEntity):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Representation of a Z-Wave node entity."""
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
def __init__(self, values, domain):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the z-Wave device."""
|
2017-01-27 06:21:33 +00:00
|
|
|
# pylint: disable=import-error
|
2017-03-23 15:37:20 +00:00
|
|
|
super().__init__()
|
2017-01-27 06:21:33 +00:00
|
|
|
from openzwave.network import ZWaveNetwork
|
|
|
|
from pydispatch import dispatcher
|
2017-03-14 23:55:33 +00:00
|
|
|
self.values = values
|
|
|
|
self.node = values.primary.node
|
|
|
|
self.values.primary.set_change_verified(False)
|
|
|
|
|
|
|
|
self._name = _value_name(self.values.primary)
|
2018-01-30 09:39:39 +00:00
|
|
|
self._unique_id = "{}-{}".format(self.node.node_id,
|
|
|
|
self.values.primary.object_id)
|
2017-02-18 07:56:05 +00:00
|
|
|
self._update_attributes()
|
2016-01-26 21:11:27 +00:00
|
|
|
|
2017-01-27 06:21:33 +00:00
|
|
|
dispatcher.connect(
|
|
|
|
self.network_value_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED)
|
|
|
|
|
|
|
|
def network_value_changed(self, value):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle a value change on the network."""
|
2017-03-14 23:55:33 +00:00
|
|
|
if value.value_id in [v.value_id for v in self.values if v]:
|
2017-03-02 06:41:19 +00:00
|
|
|
return self.value_changed()
|
2017-01-27 06:21:33 +00:00
|
|
|
|
2017-04-03 18:56:48 +00:00
|
|
|
def value_added(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle a new value of this entity."""
|
2017-04-03 18:56:48 +00:00
|
|
|
pass
|
|
|
|
|
2017-03-02 06:41:19 +00:00
|
|
|
def value_changed(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle a changed value for this entity's node."""
|
2017-02-18 07:56:05 +00:00
|
|
|
self._update_attributes()
|
2017-01-27 06:21:33 +00:00
|
|
|
self.update_properties()
|
2017-03-23 15:37:20 +00:00
|
|
|
self.maybe_schedule_update()
|
2017-01-27 06:21:33 +00:00
|
|
|
|
2018-03-17 03:27:05 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-03-04 17:13:24 +00:00
|
|
|
"""Add device to dict."""
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
SIGNAL_REFRESH_ENTITY_FORMAT.format(self.entity_id),
|
|
|
|
self.refresh_from_network)
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
def _update_attributes(self):
|
|
|
|
"""Update the node attributes. May only be used inside callback."""
|
|
|
|
self.node_id = self.node.node_id
|
2017-03-02 06:41:19 +00:00
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
if self.values.power:
|
|
|
|
self.power_consumption = round(
|
|
|
|
self.values.power.data, self.values.power.precision)
|
|
|
|
else:
|
|
|
|
self.power_consumption = None
|
2017-02-09 12:40:35 +00:00
|
|
|
|
2017-01-27 06:21:33 +00:00
|
|
|
def update_properties(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Update on data changes for node values."""
|
2017-01-27 06:21:33 +00:00
|
|
|
pass
|
|
|
|
|
2016-01-26 21:11:27 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""No polling needed."""
|
2016-01-26 21:11:27 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-03-03 18:23:55 +00:00
|
|
|
"""Return a unique ID."""
|
2017-03-03 12:47:59 +00:00
|
|
|
return self._unique_id
|
2016-01-26 21:11:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the name of the device."""
|
2017-03-03 12:47:59 +00:00
|
|
|
return self._name
|
2016-01-26 21:11:27 +00:00
|
|
|
|
|
|
|
@property
|
2016-02-04 01:12:33 +00:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the device specific state attributes."""
|
2016-01-26 21:11:27 +00:00
|
|
|
attrs = {
|
2017-02-18 07:56:05 +00:00
|
|
|
const.ATTR_NODE_ID: self.node_id,
|
2017-06-16 17:07:17 +00:00
|
|
|
const.ATTR_VALUE_INDEX: self.values.primary.index,
|
|
|
|
const.ATTR_VALUE_INSTANCE: self.values.primary.instance,
|
2017-09-11 18:30:48 +00:00
|
|
|
const.ATTR_VALUE_ID: str(self.values.primary.value_id),
|
2016-01-26 21:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 00:51:13 +00:00
|
|
|
if self.power_consumption is not None:
|
|
|
|
attrs[ATTR_POWER] = self.power_consumption
|
|
|
|
|
2016-01-26 21:11:27 +00:00
|
|
|
return attrs
|
2017-03-04 17:13:24 +00:00
|
|
|
|
|
|
|
def refresh_from_network(self):
|
|
|
|
"""Refresh all dependent values from zwave network."""
|
2017-03-14 23:55:33 +00:00
|
|
|
for value in self.values:
|
2017-04-07 13:17:23 +00:00
|
|
|
if value is not None:
|
|
|
|
self.node.refresh_value(value.value_id)
|