async def test_state_ready(hass, mock_openzwave):
    """Test state property."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode(
        query_stage="Dynamic",
        is_awake=True,
        is_ready=False,
        is_failed=False,
        is_info_received=True,
    )
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)

    node.query_stage = "Complete"
    node.is_ready = True
    entity.node_changed()
    await hass.async_block_till_done()
    assert entity.state == "ready"

    node.is_failed = True
    entity.node_changed()
    assert entity.state == "dead"

    node.is_failed = False
    node.is_awake = False
    entity.node_changed()
    assert entity.state == "sleeping"
Ejemplo n.º 2
0
async def test_application_version(hass, mock_openzwave):
    """Test application version."""
    mock_receivers = {}

    signal_mocks = [
        mock_zwave.MockNetwork.SIGNAL_VALUE_CHANGED,
        mock_zwave.MockNetwork.SIGNAL_VALUE_ADDED,
    ]

    def mock_connect(receiver, signal, *args, **kwargs):
        if signal in signal_mocks:
            mock_receivers[signal] = receiver

    node = mock_zwave.MockNode(node_id=11)

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

    for signal_mock in signal_mocks:
        assert signal_mock in mock_receivers.keys()

    events = []

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

    # Make sure application version isn't set before
    assert (node_entity.ATTR_APPLICATION_VERSION
            not in entity.device_state_attributes.keys())

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

    # Fire off an added value
    value = mock_zwave.MockValue(
        command_class=const.COMMAND_CLASS_VERSION,
        label="Application Version",
        data="5.10",
    )
    hass.async_add_job(
        mock_receivers[mock_zwave.MockNetwork.SIGNAL_VALUE_ADDED], node, value)
    await hass.async_block_till_done()

    assert (entity.device_state_attributes[
        node_entity.ATTR_APPLICATION_VERSION] == "5.10")

    # Fire off a changed
    value = mock_zwave.MockValue(
        command_class=const.COMMAND_CLASS_VERSION,
        label="Application Version",
        data="4.14",
    )
    hass.async_add_job(
        mock_receivers[mock_zwave.MockNetwork.SIGNAL_VALUE_CHANGED], node,
        value)
    await hass.async_block_till_done()

    assert (entity.device_state_attributes[
        node_entity.ATTR_APPLICATION_VERSION] == "4.14")
Ejemplo n.º 3
0
    def test_unique_id_missing_data(self):
        """Test unique_id."""
        self.node.manufacturer_name = None
        self.node.name = None
        entity = node_entity.ZWaveNodeEntity(self.node, self.zwave_network)

        assert entity.unique_id is None
async def test_network_node_changed_from_notification(hass, mock_openzwave):
    """Test for network_node_changed."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    with patch.object(entity, "maybe_schedule_update") as mock:
        mock_zwave.notification(node_id=node.node_id)
        mock.assert_called_once_with()
Ejemplo n.º 5
0
 def setUp(self):
     """Initialize values for this testcase class."""
     self.zwave_network = MagicMock()
     self.node = mock_zwave.MockNode(
         query_stage='Dynamic', is_awake=True, is_ready=False,
         is_failed=False, is_info_received=True, max_baud_rate=40000,
         is_zwave_plus=False, capabilities=[], neighbors=[], location=None)
     self.entity = node_entity.ZWaveNodeEntity(self.node,
                                               self.zwave_network)
async def test_network_node_changed_from_another_node(hass, mock_openzwave):
    """Test for network_node_changed."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    with patch.object(entity, "maybe_schedule_update") as mock:
        another_node = mock_zwave.MockNode(node_id=1024)
        mock_zwave.node_changed(another_node)
        assert not mock.called
async def test_unique_id_missing_data(hass, mock_openzwave):
    """Test unique_id."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    node.manufacturer_name = None
    node.name = None
    node.is_ready = False
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)

    assert entity.unique_id is None
Ejemplo n.º 8
0
async 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)

    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)
    await 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)
    await 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
Ejemplo n.º 9
0
async def test_scene_activated(hass, mock_openzwave):
    """Test scene activated event."""
    mock_receivers = []

    def mock_connect(receiver, signal, *args, **kwargs):
        if signal == mock_zwave.MockNetwork.SIGNAL_SCENE_EVENT:
            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)

    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 = 123
    hass.async_add_job(mock_receivers[0], node, scene_id)
    await hass.async_block_till_done()
    assert len(events) == 0

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

    scene_id = 123
    hass.async_add_job(mock_receivers[0], node, scene_id)
    await 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
Ejemplo n.º 10
0
def test_node_event_activated(hass, mock_openzwave):
    """Test Node event activated event."""
    mock_receivers = []

    def mock_connect(receiver, signal, *args, **kwargs):
        if signal == mock_zwave.MockNetwork.SIGNAL_NODE_EVENT:
            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)

    assert len(mock_receivers) == 1

    events = []

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

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

    # Test event before entity added to hass
    value = 234
    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'

    value = 234
    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_BASIC_LEVEL] == value
async def test_node_changed(hass, mock_openzwave):
    """Test node_changed function."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode(
        query_stage="Dynamic",
        is_awake=True,
        is_ready=False,
        is_failed=False,
        is_info_received=True,
        max_baud_rate=40000,
        is_zwave_plus=False,
        capabilities=[],
        neighbors=[],
        location=None,
    )
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)

    assert {
        "node_id": node.node_id,
        "node_name": "Mock Node",
        "manufacturer_name": "Test Manufacturer",
        "product_name": "Test Product",
    } == entity.device_state_attributes

    node.get_values.return_value = {1: mock_zwave.MockValue(data=1800)}
    zwave_network.manager.getNodeStatistics.return_value = {
        "receivedCnt":
        4,
        "ccData": [
            {
                "receivedCnt": 0,
                "commandClassId": 134,
                "sentCnt": 0
            },
            {
                "receivedCnt": 1,
                "commandClassId": 133,
                "sentCnt": 1
            },
            {
                "receivedCnt": 1,
                "commandClassId": 115,
                "sentCnt": 1
            },
            {
                "receivedCnt": 0,
                "commandClassId": 114,
                "sentCnt": 0
            },
            {
                "receivedCnt": 0,
                "commandClassId": 112,
                "sentCnt": 0
            },
            {
                "receivedCnt": 1,
                "commandClassId": 32,
                "sentCnt": 1
            },
            {
                "receivedCnt": 0,
                "commandClassId": 0,
                "sentCnt": 0
            },
        ],
        "receivedUnsolicited":
        0,
        "sentTS":
        "2017-03-27 15:38:15:620 ",
        "averageRequestRTT":
        2462,
        "lastResponseRTT":
        3679,
        "retries":
        0,
        "sentFailed":
        1,
        "sentCnt":
        7,
        "quality":
        0,
        "lastRequestRTT":
        1591,
        "lastReceivedMessage": [
            0,
            4,
            0,
            15,
            3,
            32,
            3,
            0,
            221,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        ],
        "receivedDups":
        1,
        "averageResponseRTT":
        2443,
        "receivedTS":
        "2017-03-27 15:38:19:298 ",
    }
    entity.node_changed()
    assert {
        "node_id": node.node_id,
        "node_name": "Mock Node",
        "manufacturer_name": "Test Manufacturer",
        "product_name": "Test Product",
        "query_stage": "Dynamic",
        "is_awake": True,
        "is_ready": False,
        "is_failed": False,
        "is_info_received": True,
        "max_baud_rate": 40000,
        "is_zwave_plus": False,
        "battery_level": 42,
        "wake_up_interval": 1800,
        "averageRequestRTT": 2462,
        "averageResponseRTT": 2443,
        "lastRequestRTT": 1591,
        "lastResponseRTT": 3679,
        "receivedCnt": 4,
        "receivedDups": 1,
        "receivedTS": "2017-03-27 15:38:19:298 ",
        "receivedUnsolicited": 0,
        "retries": 0,
        "sentCnt": 7,
        "sentFailed": 1,
        "sentTS": "2017-03-27 15:38:15:620 ",
    } == entity.device_state_attributes

    node.can_wake_up_value = False
    entity.node_changed()

    assert "wake_up_interval" not in entity.device_state_attributes
Ejemplo n.º 12
0
async def test_update_usercodes_using_zwave(hass, mock_openzwave, caplog):
    """Test handling usercode updates using zwave"""

    mock_receivers = {}

    def mock_connect(receiver, signal, *args, **kwargs):
        mock_receivers[signal] = receiver

    with patch("pydispatch.dispatcher.connect", new=mock_connect):
        await async_setup_component(hass, "zwave", {"zwave": {}})
        await hass.async_block_till_done()

    # Setup zwave mock
    hass.data[DATA_NETWORK] = mock_openzwave
    node = MockNode(node_id=12)
    value0 = MockValue(data="12345678", node=node, index=1)
    value1 = MockValue(data="******", node=node, index=2)

    node.get_values.return_value = {
        value0.value_id: value0,
        value1.value_id: value1,
    }

    mock_openzwave.nodes = {node.node_id: node}
    entity = node_entity.ZWaveNodeEntity(node, mock_openzwave)

    # Setup the zwave integration
    await setup_zwave(hass, mock_openzwave)
    await hass.async_block_till_done()

    # Set the zwave network as ready
    hass.data[DATA_NETWORK].state = MockNetwork.STATE_READY

    assert mock_receivers

    await hass.async_add_executor_job(
        mock_receivers[MockNetwork.SIGNAL_ALL_NODES_QUERIED])

    # Create the entities
    hass.states.async_set(
        "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_level", 1)
    await hass.async_block_till_done()
    hass.states.async_set(
        "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_type", 22)
    await hass.async_block_till_done()
    hass.states.async_set(
        "lock.smartcode_10_touchpad_electronic_deadbolt_locked",
        "locked",
        {"node_id": 12},
    )
    await hass.async_block_till_done()

    # Load the integration
    with patch("custom_components.keymaster.binary_sensor.using_zwave",
               return_value=True), patch(
                   "custom_components.keymaster.using_zwave",
                   return_value=True):
        entry = MockConfigEntry(domain=DOMAIN,
                                title="frontdoor",
                                data=CONFIG_DATA_REAL,
                                version=2)
        entry.add_to_hass(hass)
        assert await hass.config_entries.async_setup(entry.entry_id)
        await hass.async_block_till_done()

    # Fire the event
    hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
    await hass.async_block_till_done()

    assert hass.states.get(NETWORK_READY_ENTITY)
    assert hass.states.get(NETWORK_READY_ENTITY).state == "on"

    assert hass.states.get("sensor.frontdoor_code_slot_1").state == "12345678"
    assert "Work around code in use." in caplog.text
async def test_name(hass, mock_openzwave):
    """Test name property."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    assert entity.name == "Mock Node"
async def test_state_before_update(hass, mock_openzwave):
    """Test state before update was called."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    assert entity.state is None
Ejemplo n.º 15
0
async def test_clear_code(hass, lock_data, sent_messages, mock_openzwave,
                          caplog):
    """Test refresh_codes"""
    entry = MockConfigEntry(domain=DOMAIN,
                            title="frontdoor",
                            data=CONFIG_DATA,
                            version=2)

    entry.add_to_hass(hass)
    assert await hass.config_entries.async_setup(entry.entry_id)
    await hass.async_block_till_done()

    # Test ZWaveIntegrationNotConfiguredError
    servicedata = {
        "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor",
        "code_slot": 1,
    }
    await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
    await hass.async_block_till_done()

    assert (
        "A Z-Wave integration has not been configured for this Home Assistant instance"
        in caplog.text)

    # Mock using_zwave
    with patch("custom_components.keymaster.services.async_using_zwave",
               return_value=True):
        servicedata = {
            "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor",
            "code_slot": 1,
        }
        await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
        await hass.async_block_till_done()
        assert (
            "Problem retrieving node_id from entity lock.kwikset_touchpad_electronic_deadbolt_frontdoor"
            in caplog.text)

    with patch("custom_components.keymaster.services.async_using_zwave",
               return_value=True), patch(
                   "custom_components.keymaster.services.get_node_id",
                   return_value="14"):
        servicedata = {
            "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor",
            "code_slot": 1,
        }
        await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
        await hass.async_block_till_done()
        assert (
            "Error calling lock.set_usercode service call: Unable to find service lock.set_usercode"
            in caplog.text)

    with patch("custom_components.keymaster.services.async_using_zwave",
               return_value=True):
        # Setup zwave mock
        hass.data[DATA_NETWORK] = mock_openzwave
        node = MockNode(node_id=12)
        value0 = MockValue(data="12345678", node=node, index=1)
        value1 = MockValue(data="******", node=node, index=2)

        node.get_values.return_value = {
            value0.value_id: value0,
            value1.value_id: value1,
        }

        mock_openzwave.nodes = {node.node_id: node}
        entity = node_entity.ZWaveNodeEntity(node, mock_openzwave)

        # Setup the zwave integration
        await setup_zwave(hass, mock_openzwave)
        await hass.async_block_till_done()

        # Set the zwave network as ready
        hass.data[DATA_NETWORK].state = MockNetwork.STATE_READY

        # Create the entities
        hass.states.async_set(
            "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_level_2",
            1)
        await hass.async_block_till_done()
        hass.states.async_set(
            "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_type_2",
            22)
        await hass.async_block_till_done()
        hass.states.async_set(
            "lock.smartcode_10_touchpad_electronic_deadbolt_locked_2",
            "locked",
            {"node_id": 12},
        )
        await hass.async_block_till_done()
        servicedata = {
            "entity_id":
            "lock.smartcode_10_touchpad_electronic_deadbolt_locked_2",
            "code_slot": 1,
        }
        await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
        await hass.async_block_till_done()

        assert "Usercode at slot 1 is cleared" in caplog.text

    # Bring OZW up
    await setup_ozw(hass, fixture=lock_data)

    with patch("custom_components.keymaster.services.async_using_ozw",
               return_value=True):
        state = hass.states.get(
            "lock.smartcode_10_touchpad_electronic_deadbolt_locked")
        assert state is not None
        assert state.state == "locked"
        assert state.attributes["node_id"] == 14

        entry = MockConfigEntry(domain=DOMAIN,
                                title="frontdoor",
                                data=CONFIG_DATA,
                                version=2)

        entry.add_to_hass(hass)
        assert await hass.config_entries.async_setup(entry.entry_id)
        await hass.async_block_till_done()

        servicedata = {
            "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor",
            "code_slot": 1,
        }
        await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
        await hass.async_block_till_done()

        assert (
            "Unable to find referenced entities lock.kwikset_touchpad_electronic_deadbolt_frontdoor"
            in caplog.text)

        servicedata = {
            "entity_id":
            "lock.smartcode_10_touchpad_electronic_deadbolt_locked",
            "code_slot": 1,
        }
        await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata)
        await hass.async_block_till_done()

        assert len(sent_messages) == 4
        msg = sent_messages[3]
        assert msg["topic"] == "OpenZWave/1/command/setvalue/"
        assert msg["payload"] == {"Value": 1, "ValueIDKey": 72057594287013910}
async def test_not_polled(hass, mock_openzwave):
    """Test should_poll property."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    assert not entity.should_poll
async def test_unique_id(hass, mock_openzwave):
    """Test unique_id."""
    zwave_network = MagicMock()
    node = mock_zwave.MockNode()
    entity = node_entity.ZWaveNodeEntity(node, zwave_network)
    assert entity.unique_id == "node-567"