Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ZWave CentralScene activation handler. #9178

Merged
merged 4 commits into from Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/zwave/const.py
Expand Up @@ -10,6 +10,7 @@
ATTR_OBJECT_ID = "object_id"
ATTR_NAME = "name"
ATTR_SCENE_ID = "scene_id"
ATTR_SCENE_DATA = "scene_data"
ATTR_BASIC_LEVEL = "basic_level"
ATTR_CONFIG_PARAMETER = "parameter"
ATTR_CONFIG_SIZE = "size"
Expand Down
25 changes: 22 additions & 3 deletions homeassistant/components/zwave/node_entity.py
Expand Up @@ -7,8 +7,9 @@
from homeassistant.util import slugify

from .const import (
ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_BASIC_LEVEL,
EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED, DOMAIN)
ATTR_NODE_ID, COMMAND_CLASS_WAKE_UP, ATTR_SCENE_ID, ATTR_SCENE_DATA,
ATTR_BASIC_LEVEL, EVENT_NODE_EVENT, EVENT_SCENE_ACTIVATED, DOMAIN,
COMMAND_CLASS_CENTRAL_SCENE)
from .util import node_name

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -107,13 +108,19 @@ def __init__(self, node, network, new_entity_ids):
dispatcher.connect(
self.network_scene_activated, ZWaveNetwork.SIGNAL_SCENE_EVENT)

def network_node_changed(self, node=None, args=None):
def network_node_changed(self, node=None, value=None, args=None):
"""Handle a changed node on the network."""
if node and node.node_id != self.node_id:
return
if args is not None and 'nodeId' in args and \
args['nodeId'] != self.node_id:
return

# Process central scene activation
if (value is not None and
value.command_class == COMMAND_CLASS_CENTRAL_SCENE):
self.central_scene_activated(value.index, value.data)

self.node_changed()

def get_node_statistics(self):
Expand Down Expand Up @@ -177,6 +184,18 @@ def scene_activated(self, scene_id):
ATTR_SCENE_ID: scene_id
})

def central_scene_activated(self, scene_id, scene_data):
"""Handle an activated central scene for this node."""
if self.hass is None:
return

self.hass.bus.fire(EVENT_SCENE_ACTIVATED, {
ATTR_ENTITY_ID: self.entity_id,
ATTR_NODE_ID: self.node_id,
ATTR_SCENE_ID: scene_id,
ATTR_SCENE_DATA: scene_data
})

@property
def state(self):
"""Return the state."""
Expand Down
54 changes: 54 additions & 0 deletions tests/components/zwave/test_node_entity.py
Expand Up @@ -117,6 +117,60 @@ def listener(event):
assert events[0].data[const.ATTR_SCENE_ID] == scene_id


@asyncio.coroutine
def test_central_scene_activated(hass, mock_openzwave):
"""Test central scene activated event."""
mock_receivers = []

def mock_connect(receiver, signal, *args, **kwargs):
if signal == mock_zwave.MockNetwork.SIGNAL_VALUE_CHANGED:
mock_receivers.append(receiver)

node = mock_zwave.MockNode(node_id=11)

with patch('pydispatch.dispatcher.connect', new=mock_connect):
entity = node_entity.ZWaveNodeEntity(node, mock_openzwave, True)

assert len(mock_receivers) == 1

events = []

def listener(event):
events.append(event)

hass.bus.async_listen(const.EVENT_SCENE_ACTIVATED, listener)

# Test event before entity added to hass
scene_id = 1
scene_data = 3
value = mock_zwave.MockValue(
command_class=const.COMMAND_CLASS_CENTRAL_SCENE,
index=scene_id,
data=scene_data)
hass.async_add_job(mock_receivers[0], node, value)
yield from hass.async_block_till_done()
assert len(events) == 0

# Add entity to hass
entity.hass = hass
entity.entity_id = 'zwave.mock_node'

scene_id = 1
scene_data = 3
value = mock_zwave.MockValue(
command_class=const.COMMAND_CLASS_CENTRAL_SCENE,
index=scene_id,
data=scene_data)
hass.async_add_job(mock_receivers[0], node, value)
yield from hass.async_block_till_done()

assert len(events) == 1
assert events[0].data[ATTR_ENTITY_ID] == "zwave.mock_node"
assert events[0].data[const.ATTR_NODE_ID] == 11
assert events[0].data[const.ATTR_SCENE_ID] == scene_id
assert events[0].data[const.ATTR_SCENE_DATA] == scene_data


@pytest.mark.usefixtures('mock_openzwave')
class TestZWaveNodeEntity(unittest.TestCase):
"""Class to test ZWaveNodeEntity."""
Expand Down