Add communication data attributes to Zwave node_entity (#6813)
* Add quality attribute to node * Move quality to node_entity * adjustments * Line lenght * flake8 * Cleanup and add each entry from getNodeStatistics as attribute * resolve conflict * Move NETWORK to hass.data * Test Done * Cleanup from review * Resolve conflicts * Review changes * Long lines.... * blank line * import-error-disable * Update tests Part1... Again * Hound * Argh! * Argh! * YABBADABBADOOOOOOO! * Enhance tests * hound * Resolve * resolve tests...
This commit is contained in:
1 parent
f68542ba0d
commit
d63028e44a
8 files changed
+240
-148
No files matched your search
@@ -20,13 +20,13 @@ _LOGGER = logging.getLogger(__name__)
|
||||
SUPPORT_GARAGE = SUPPORT_OPEN | SUPPORT_CLOSE
|
||||
|
||||
|
||||
def get_device(values, node_config, **kwargs):
|
||||
def get_device(hass, values, node_config, **kwargs):
|
||||
"""Create zwave entity device."""
|
||||
invert_buttons = node_config.get(zwave.CONF_INVERT_OPENCLOSE_BUTTONS)
|
||||
if (values.primary.command_class ==
|
||||
zwave.const.COMMAND_CLASS_SWITCH_MULTILEVEL
|
||||
and values.primary.index == 0):
|
||||
return ZwaveRollershutter(values, invert_buttons)
|
||||
return ZwaveRollershutter(hass, values, invert_buttons)
|
||||
elif (values.primary.command_class in [
|
||||
zwave.const.COMMAND_CLASS_SWITCH_BINARY,
|
||||
zwave.const.COMMAND_CLASS_BARRIER_OPERATOR]):
|
||||
@@ -37,10 +37,11 @@ def get_device(values, node_config, **kwargs):
|
||||
class ZwaveRollershutter(zwave.ZWaveDeviceEntity, CoverDevice):
|
||||
"""Representation of an Zwave roller shutter."""
|
||||
|
||||
def __init__(self, values, invert_buttons):
|
||||
def __init__(self, hass, values, invert_buttons):
|
||||
"""Initialize the zwave rollershutter."""
|
||||
ZWaveDeviceEntity.__init__(self, values, DOMAIN)
|
||||
# pylint: disable=no-member
|
||||
self._network = hass.data[zwave.ZWAVE_NETWORK]
|
||||
self._open_id = None
|
||||
self._close_id = None
|
||||
self._current_position = None
|
||||
@@ -90,11 +91,11 @@ class ZwaveRollershutter(zwave.ZWaveDeviceEntity, CoverDevice):
|
||||
|
||||
def open_cover(self, **kwargs):
|
||||
"""Move the roller shutter up."""
|
||||
zwave.NETWORK.manager.pressButton(self._open_id)
|
||||
self._network.manager.pressButton(self._open_id)
|
||||
|
||||
def close_cover(self, **kwargs):
|
||||
"""Move the roller shutter down."""
|
||||
zwave.NETWORK.manager.pressButton(self._close_id)
|
||||
self._network.manager.pressButton(self._close_id)
|
||||
|
||||
def set_cover_position(self, position, **kwargs):
|
||||
"""Move the roller shutter to a specific position."""
|
||||
@@ -102,7 +103,7 @@ class ZwaveRollershutter(zwave.ZWaveDeviceEntity, CoverDevice):
|
||||
|
||||
def stop_cover(self, **kwargs):
|
||||
"""Stop the roller shutter."""
|
||||
zwave.NETWORK.manager.releaseButton(self._open_id)
|
||||
self._network.manager.releaseButton(self._open_id)
|
||||
|
||||
|
||||
class ZwaveGarageDoor(zwave.ZWaveDeviceEntity, CoverDevice):
|
||||
|
||||
@@ -128,11 +128,12 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
|
||||
descriptions = load_yaml_config_file(
|
||||
path.join(path.dirname(__file__), 'services.yaml'))
|
||||
network = hass.data[zwave.ZWAVE_NETWORK]
|
||||
|
||||
def set_usercode(service):
|
||||
"""Set the usercode to index X on the lock."""
|
||||
node_id = service.data.get(zwave.const.ATTR_NODE_ID)
|
||||
lock_node = zwave.NETWORK.nodes[node_id]
|
||||
lock_node = network.nodes[node_id]
|
||||
code_slot = service.data.get(ATTR_CODE_SLOT)
|
||||
usercode = service.data.get(ATTR_USERCODE)
|
||||
|
||||
@@ -151,7 +152,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
def get_usercode(service):
|
||||
"""Get a usercode at index X on the lock."""
|
||||
node_id = service.data.get(zwave.const.ATTR_NODE_ID)
|
||||
lock_node = zwave.NETWORK.nodes[node_id]
|
||||
lock_node = network.nodes[node_id]
|
||||
code_slot = service.data.get(ATTR_CODE_SLOT)
|
||||
|
||||
for value in lock_node.get_values(
|
||||
@@ -164,7 +165,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
def clear_usercode(service):
|
||||
"""Set usercode to slot X on the lock."""
|
||||
node_id = service.data.get(zwave.const.ATTR_NODE_ID)
|
||||
lock_node = zwave.NETWORK.nodes[node_id]
|
||||
lock_node = network.nodes[node_id]
|
||||
code_slot = service.data.get(ATTR_CODE_SLOT)
|
||||
data = ''
|
||||
|
||||
|
||||
@@ -65,8 +65,7 @@ DEFAULT_CONF_REFRESH_VALUE = False
|
||||
DEFAULT_CONF_REFRESH_DELAY = 5
|
||||
|
||||
DATA_ZWAVE_DICT = 'zwave_devices'
|
||||
|
||||
NETWORK = None
|
||||
ZWAVE_NETWORK = 'zwave_network'
|
||||
|
||||
RENAME_NODE_SCHEMA = vol.Schema({
|
||||
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
|
||||
@@ -200,7 +199,7 @@ def get_config_value(node, value_index, tries=5):
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
"""Generic Z-Wave platform setup."""
|
||||
if discovery_info is None or NETWORK is None:
|
||||
if discovery_info is None or ZWAVE_NETWORK not in hass.data:
|
||||
return False
|
||||
|
||||
device = hass.data[DATA_ZWAVE_DICT].pop(
|
||||
@@ -218,9 +217,6 @@ def setup(hass, config):
|
||||
|
||||
Will automatically load components to support devices found on the network.
|
||||
"""
|
||||
# pylint: disable=global-statement, import-error
|
||||
global NETWORK
|
||||
|
||||
descriptions = conf_util.load_yaml_config_file(
|
||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||
|
||||
@@ -232,6 +228,7 @@ def setup(hass, config):
|
||||
"https://home-assistant.io/components/zwave/")
|
||||
return False
|
||||
from pydispatch import dispatcher
|
||||
# pylint: disable=import-error
|
||||
from openzwave.option import ZWaveOption
|
||||
from openzwave.network import ZWaveNetwork
|
||||
from openzwave.group import ZWaveGroup
|
||||
@@ -257,7 +254,7 @@ def setup(hass, config):
|
||||
options.set_console_output(use_debug)
|
||||
options.lock()
|
||||
|
||||
NETWORK = ZWaveNetwork(options, autostart=False)
|
||||
network = hass.data[ZWAVE_NETWORK] = ZWaveNetwork(options, autostart=False)
|
||||
hass.data[DATA_ZWAVE_DICT] = {}
|
||||
|
||||
if use_debug: # pragma: no cover
|
||||
@@ -301,7 +298,7 @@ def setup(hass, config):
|
||||
|
||||
def node_added(node):
|
||||
"""Called when a node is added on the network."""
|
||||
entity = ZWaveNodeEntity(node)
|
||||
entity = ZWaveNodeEntity(node, network)
|
||||
node_config = device_config.get(entity.entity_id)
|
||||
if node_config.get(CONF_IGNORED):
|
||||
_LOGGER.info(
|
||||
@@ -354,49 +351,49 @@ def setup(hass, config):
|
||||
def add_node(service):
|
||||
"""Switch into inclusion mode."""
|
||||
_LOGGER.info("Zwave add_node have been initialized.")
|
||||
NETWORK.controller.add_node()
|
||||
network.controller.add_node()
|
||||
|
||||
def add_node_secure(service):
|
||||
"""Switch into secure inclusion mode."""
|
||||
_LOGGER.info("Zwave add_node_secure have been initialized.")
|
||||
NETWORK.controller.add_node(True)
|
||||
network.controller.add_node(True)
|
||||
|
||||
def remove_node(service):
|
||||
"""Switch into exclusion mode."""
|
||||
_LOGGER.info("Zwave remove_node have been initialized.")
|
||||
NETWORK.controller.remove_node()
|
||||
network.controller.remove_node()
|
||||
|
||||
def cancel_command(service):
|
||||
"""Cancel a running controller command."""
|
||||
_LOGGER.info("Cancel running ZWave command.")
|
||||
NETWORK.controller.cancel_command()
|
||||
network.controller.cancel_command()
|
||||
|
||||
def heal_network(service):
|
||||
"""Heal the network."""
|
||||
_LOGGER.info("ZWave heal running.")
|
||||
NETWORK.heal()
|
||||
network.heal()
|
||||
|
||||
def soft_reset(service):
|
||||
"""Soft reset the controller."""
|
||||
_LOGGER.info("Zwave soft_reset have been initialized.")
|
||||
NETWORK.controller.soft_reset()
|
||||
network.controller.soft_reset()
|
||||
|
||||
def test_network(service):
|
||||
"""Test the network by sending commands to all the nodes."""
|
||||
_LOGGER.info("Zwave test_network have been initialized.")
|
||||
NETWORK.test()
|
||||
network.test()
|
||||
|
||||
def stop_network(_service_or_event):
|
||||
"""Stop Z-Wave network."""
|
||||
_LOGGER.info("Stopping ZWave network.")
|
||||
NETWORK.stop()
|
||||
network.stop()
|
||||
if hass.state == CoreState.running:
|
||||
hass.bus.fire(const.EVENT_NETWORK_STOP)
|
||||
|
||||
def rename_node(service):
|
||||
"""Rename a node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = hass.data[ZWAVE_NETWORK].nodes[node_id]
|
||||
name = service.data.get(const.ATTR_NAME)
|
||||
node.name = name
|
||||
_LOGGER.info(
|
||||
@@ -406,18 +403,18 @@ def setup(hass, config):
|
||||
"""Remove failed node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
_LOGGER.info('Trying to remove zwave node %d', node_id)
|
||||
NETWORK.controller.remove_failed_node(node_id)
|
||||
network.controller.remove_failed_node(node_id)
|
||||
|
||||
def replace_failed_node(service):
|
||||
"""Replace failed node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
_LOGGER.info('Trying to replace zwave node %d', node_id)
|
||||
NETWORK.controller.replace_failed_node(node_id)
|
||||
network.controller.replace_failed_node(node_id)
|
||||
|
||||
def set_config_parameter(service):
|
||||
"""Set a config parameter to a node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = network.nodes[node_id]
|
||||
param = service.data.get(const.ATTR_CONFIG_PARAMETER)
|
||||
selection = service.data.get(const.ATTR_CONFIG_VALUE)
|
||||
size = service.data.get(const.ATTR_CONFIG_SIZE, 2)
|
||||
@@ -446,7 +443,7 @@ def setup(hass, config):
|
||||
def print_config_parameter(service):
|
||||
"""Print a config parameter from a node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = network.nodes[node_id]
|
||||
param = service.data.get(const.ATTR_CONFIG_PARAMETER)
|
||||
_LOGGER.info("Config parameter %s on Node %s : %s",
|
||||
param, node_id, get_config_value(node, param))
|
||||
@@ -454,13 +451,13 @@ def setup(hass, config):
|
||||
def print_node(service):
|
||||
"""Print all information about z-wave node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = network.nodes[node_id]
|
||||
nice_print_node(node)
|
||||
|
||||
def set_wakeup(service):
|
||||
"""Set wake-up interval of a node."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = network.nodes[node_id]
|
||||
value = service.data.get(const.ATTR_CONFIG_VALUE)
|
||||
if node.can_wake_up():
|
||||
for value_id in node.get_values(
|
||||
@@ -478,7 +475,7 @@ def setup(hass, config):
|
||||
group = service.data.get(const.ATTR_GROUP)
|
||||
instance = service.data.get(const.ATTR_INSTANCE)
|
||||
|
||||
node = ZWaveGroup(group, NETWORK, node_id)
|
||||
node = ZWaveGroup(group, network, node_id)
|
||||
if association_type == 'add':
|
||||
node.add_association(target_node_id, instance)
|
||||
_LOGGER.info("Adding association for node:%s in group:%s "
|
||||
@@ -500,13 +497,13 @@ def setup(hass, config):
|
||||
def refresh_node(service):
|
||||
"""Refresh all node info."""
|
||||
node_id = service.data.get(const.ATTR_NODE_ID)
|
||||
node = NETWORK.nodes[node_id]
|
||||
node = network.nodes[node_id]
|
||||
node.refresh_info()
|
||||
|
||||
def start_zwave(_service_or_event):
|
||||
"""Startup Z-Wave network."""
|
||||
_LOGGER.info("Starting ZWave network.")
|
||||
NETWORK.start()
|
||||
network.start()
|
||||
hass.bus.fire(const.EVENT_NETWORK_START)
|
||||
|
||||
# Need to be in STATE_AWAKED before talking to nodes.
|
||||
@@ -514,8 +511,9 @@ def setup(hass, config):
|
||||
# to be ready.
|
||||
for i in range(const.NETWORK_READY_WAIT_SECS):
|
||||
_LOGGER.debug(
|
||||
"network state: %d %s", NETWORK.state, NETWORK.state_str)
|
||||
if NETWORK.state >= NETWORK.STATE_AWAKED:
|
||||
"network state: %d %s", hass.data[ZWAVE_NETWORK].state,
|
||||
network.state_str)
|
||||
if network.state >= network.STATE_AWAKED:
|
||||
_LOGGER.info("zwave ready after %d seconds", i)
|
||||
break
|
||||
time.sleep(1)
|
||||
@@ -524,14 +522,15 @@ def setup(hass, config):
|
||||
"zwave not ready after %d seconds, continuing anyway",
|
||||
const.NETWORK_READY_WAIT_SECS)
|
||||
_LOGGER.info(
|
||||
"final network state: %d %s", NETWORK.state, NETWORK.state_str)
|
||||
"final network state: %d %s", network.state,
|
||||
network.state_str)
|
||||
|
||||
polling_interval = convert(
|
||||
config[DOMAIN].get(CONF_POLLING_INTERVAL), int)
|
||||
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()
|
||||
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_network)
|
||||
|
||||
@@ -22,6 +22,10 @@ _REQUIRED_ATTRIBUTES = [
|
||||
ATTR_QUERY_STAGE, ATTR_AWAKE, ATTR_READY, ATTR_FAILED,
|
||||
'is_info_received', 'max_baud_rate', 'is_zwave_plus']
|
||||
_OPTIONAL_ATTRIBUTES = ['capabilities', 'neighbors', 'location']
|
||||
_COMM_ATTRIBUTES = [
|
||||
'sentCnt', 'sentFailed', 'retries', 'receivedCnt', 'receivedDups',
|
||||
'receivedUnsolicited', 'sentTS', 'receivedTS', 'lastRequestRTT',
|
||||
'averageRequestRTT', 'lastResponseRTT', 'averageResponseRTT']
|
||||
ATTRIBUTES = _REQUIRED_ATTRIBUTES + _OPTIONAL_ATTRIBUTES
|
||||
|
||||
|
||||
@@ -65,12 +69,13 @@ def sub_status(status, stage):
|
||||
class ZWaveNodeEntity(ZWaveBaseEntity):
|
||||
"""Representation of a Z-Wave node."""
|
||||
|
||||
def __init__(self, node):
|
||||
def __init__(self, node, network):
|
||||
"""Initialize node."""
|
||||
# pylint: disable=import-error
|
||||
super().__init__()
|
||||
from openzwave.network import ZWaveNetwork
|
||||
from pydispatch import dispatcher
|
||||
self._network = network
|
||||
self.node = node
|
||||
self.node_id = self.node.node_id
|
||||
self._name = node_name(self.node)
|
||||
@@ -95,13 +100,22 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
|
||||
return
|
||||
self.node_changed()
|
||||
|
||||
def get_node_statistics(self):
|
||||
"""Retrieve statistics from the node."""
|
||||
return self._network.manager.getNodeStatistics(self._network.home_id,
|
||||
self.node_id)
|
||||
|
||||
def node_changed(self):
|
||||
"""Update node properties."""
|
||||
self._attributes = {}
|
||||
stats = self.get_node_statistics()
|
||||
|
||||
for attr in ATTRIBUTES:
|
||||
value = getattr(self.node, attr)
|
||||
if attr in _REQUIRED_ATTRIBUTES or value:
|
||||
self._attributes[attr] = value
|
||||
for attr in _COMM_ATTRIBUTES:
|
||||
self._attributes[attr] = stats[attr]
|
||||
|
||||
if self.node.can_wake_up():
|
||||
for value in self.node.get_values(COMMAND_CLASS_WAKE_UP).values():
|
||||
@@ -152,4 +166,5 @@ class ZWaveNodeEntity(ZWaveBaseEntity):
|
||||
attrs[ATTR_BATTERY_LEVEL] = self.battery_level
|
||||
if self.wakeup_interval is not None:
|
||||
attrs[ATTR_WAKEUP] = self.wakeup_interval
|
||||
|
||||
return attrs
|
||||
Reference in new issue
Block a user