Merge pull request #1636 from srcLurker/zwave_polling

Zwave cleanup for startup and poll_intensity.
This commit is contained in:
Paulus Schoutsen 2016-03-28 14:12:10 -07:00
commit 0549bc0290

View File

@ -4,8 +4,10 @@ Support for Z-Wave.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/zwave/ https://home-assistant.io/components/zwave/
""" """
import logging
import os.path import os.path
import sys import sys
import time
from pprint import pprint from pprint import pprint
from homeassistant import bootstrap from homeassistant import bootstrap
@ -26,6 +28,9 @@ CONF_POLLING_INTENSITY = "polling_intensity"
DEFAULT_ZWAVE_CONFIG_PATH = os.path.join(sys.prefix, 'share', DEFAULT_ZWAVE_CONFIG_PATH = os.path.join(sys.prefix, 'share',
'python-openzwave', 'config') 'python-openzwave', 'config')
# How long to wait for the zwave network to be ready.
NETWORK_READY_WAIT_SECS = 30
SERVICE_ADD_NODE = "add_node" SERVICE_ADD_NODE = "add_node"
SERVICE_REMOVE_NODE = "remove_node" SERVICE_REMOVE_NODE = "remove_node"
SERVICE_HEAL_NETWORK = "heal_network" SERVICE_HEAL_NETWORK = "heal_network"
@ -91,6 +96,8 @@ ATTR_SCENE_ID = "scene_id"
NETWORK = None NETWORK = None
_LOGGER = logging.getLogger(__name__)
def _obj_to_dict(obj): def _obj_to_dict(obj):
"""Convert an object into a hash for debug.""" """Convert an object into a hash for debug."""
@ -217,8 +224,10 @@ def setup(hass, config):
node_config = customize.get(name, {}) node_config = customize.get(name, {})
polling_intensity = convert( polling_intensity = convert(
node_config.get(CONF_POLLING_INTENSITY), int) node_config.get(CONF_POLLING_INTENSITY), int)
if polling_intensity is not None: if polling_intensity:
value.enable_poll(polling_intensity) value.enable_poll(polling_intensity)
else:
value.disable_poll()
# Fire discovery event # Fire discovery event
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, { hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
@ -268,11 +277,31 @@ def setup(hass, config):
"""Startup Z-Wave.""" """Startup Z-Wave."""
NETWORK.start() NETWORK.start()
# Need to be in STATE_AWAKED before talking to nodes.
# Wait up to NETWORK_READY_WAIT_SECS seconds for the zwave network
# to be ready.
for i in range(NETWORK_READY_WAIT_SECS):
_LOGGER.info(
"network state: %d %s", NETWORK.state, NETWORK.state_str)
if NETWORK.state >= NETWORK.STATE_AWAKED:
_LOGGER.info("zwave ready after %d seconds", i)
break
time.sleep(1)
else:
_LOGGER.warning(
"zwave not ready after %d seconds, continuing anyway",
NETWORK_READY_WAIT_SECS)
_LOGGER.info(
"final network state: %d %s", NETWORK.state, NETWORK.state_str)
polling_interval = convert( polling_interval = convert(
config[DOMAIN].get(CONF_POLLING_INTERVAL), int) config[DOMAIN].get(CONF_POLLING_INTERVAL), int)
if polling_interval is not None: if polling_interval is not None:
NETWORK.set_poll_interval(polling_interval, False) NETWORK.set_poll_interval(polling_interval, False)
poll_interval = NETWORK.get_poll_interval()
_LOGGER.info("zwave polling interval set to %d ms", poll_interval)
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zwave) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zwave)
# Register add / remove node services for Z-Wave sticks without # Register add / remove node services for Z-Wave sticks without