Skip to content

Commit

Permalink
Update Z-Wave Tests asyncio/yield from -> async/await (#18599)
Browse files Browse the repository at this point in the history
* Update lock tests

* Update binary sensor

* Update zwave component tests
  • Loading branch information
cgarwood committed Nov 20, 2018
1 parent 8742750 commit b774299
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 127 deletions.
8 changes: 3 additions & 5 deletions tests/components/binary_sensor/test_zwave.py
@@ -1,5 +1,4 @@
"""Test Z-Wave binary sensors."""
import asyncio
import datetime

from unittest.mock import patch
Expand Down Expand Up @@ -71,8 +70,7 @@ def test_binary_sensor_value_changed(mock_openzwave):
assert device.is_on


@asyncio.coroutine
def test_trigger_sensor_value_changed(hass, mock_openzwave):
async def test_trigger_sensor_value_changed(hass, mock_openzwave):
"""Test value changed for trigger sensor."""
node = MockNode(
manufacturer_id='013c', product_type='0002', product_id='0002')
Expand All @@ -84,13 +82,13 @@ def test_trigger_sensor_value_changed(hass, mock_openzwave):
assert not device.is_on

value.data = True
yield from hass.async_add_job(value_changed, value)
await hass.async_add_job(value_changed, value)
assert device.invalidate_after is None

device.hass = hass

value.data = True
yield from hass.async_add_job(value_changed, value)
await hass.async_add_job(value_changed, value)
assert device.is_on

test_time = device.invalidate_after - datetime.timedelta(seconds=1)
Expand Down
48 changes: 21 additions & 27 deletions tests/components/lock/test_zwave.py
@@ -1,6 +1,4 @@
"""Test Z-Wave locks."""
import asyncio

from unittest.mock import patch, MagicMock

from homeassistant import config_entries
Expand Down Expand Up @@ -185,21 +183,19 @@ def test_lock_alarm_level(mock_openzwave):
'Tamper Alarm: Too many keypresses'


@asyncio.coroutine
def setup_ozw(hass, mock_openzwave):
async def setup_ozw(hass, mock_openzwave):
"""Set up the mock ZWave config entry."""
hass.config.components.add('zwave')
config_entry = config_entries.ConfigEntry(1, 'zwave', 'Mock Title', {
'usb_path': 'mock-path',
'network_key': 'mock-key'
}, 'test', config_entries.CONN_CLASS_LOCAL_PUSH)
yield from hass.config_entries.async_forward_entry_setup(config_entry,
'lock')
yield from hass.async_block_till_done()
await hass.config_entries.async_forward_entry_setup(config_entry,
'lock')
await hass.async_block_till_done()


@asyncio.coroutine
def test_lock_set_usercode_service(hass, mock_openzwave):
async def test_lock_set_usercode_service(hass, mock_openzwave):
"""Test the zwave lock set_usercode service."""
mock_network = hass.data[zwave.zwave.DATA_NETWORK] = MagicMock()

Expand All @@ -216,35 +212,34 @@ def test_lock_set_usercode_service(hass, mock_openzwave):
node.node_id: node
}

yield from setup_ozw(hass, mock_openzwave)
yield from hass.async_block_till_done()
await setup_ozw(hass, mock_openzwave)
await hass.async_block_till_done()

yield from hass.services.async_call(
await hass.services.async_call(
zwave.DOMAIN, zwave.SERVICE_SET_USERCODE, {
const.ATTR_NODE_ID: node.node_id,
zwave.ATTR_USERCODE: '1234',
zwave.ATTR_CODE_SLOT: 1,
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()

assert value1.data == '1234'

mock_network.nodes = {
node.node_id: node
}
yield from hass.services.async_call(
await hass.services.async_call(
zwave.DOMAIN, zwave.SERVICE_SET_USERCODE, {
const.ATTR_NODE_ID: node.node_id,
zwave.ATTR_USERCODE: '123',
zwave.ATTR_CODE_SLOT: 1,
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()

assert value1.data == '1234'


@asyncio.coroutine
def test_lock_get_usercode_service(hass, mock_openzwave):
async def test_lock_get_usercode_service(hass, mock_openzwave):
"""Test the zwave lock get_usercode service."""
mock_network = hass.data[zwave.zwave.DATA_NETWORK] = MagicMock()
node = MockNode(node_id=12)
Expand All @@ -256,25 +251,24 @@ def test_lock_get_usercode_service(hass, mock_openzwave):
value1.value_id: value1,
}

yield from setup_ozw(hass, mock_openzwave)
yield from hass.async_block_till_done()
await setup_ozw(hass, mock_openzwave)
await hass.async_block_till_done()

with patch.object(zwave, '_LOGGER') as mock_logger:
mock_network.nodes = {node.node_id: node}
yield from hass.services.async_call(
await hass.services.async_call(
zwave.DOMAIN, zwave.SERVICE_GET_USERCODE, {
const.ATTR_NODE_ID: node.node_id,
zwave.ATTR_CODE_SLOT: 1,
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
# This service only seems to write to the log
assert mock_logger.info.called
assert len(mock_logger.info.mock_calls) == 1
assert mock_logger.info.mock_calls[0][1][2] == '1234'


@asyncio.coroutine
def test_lock_clear_usercode_service(hass, mock_openzwave):
async def test_lock_clear_usercode_service(hass, mock_openzwave):
"""Test the zwave lock clear_usercode service."""
mock_network = hass.data[zwave.zwave.DATA_NETWORK] = MagicMock()
node = MockNode(node_id=12)
Expand All @@ -290,14 +284,14 @@ def test_lock_clear_usercode_service(hass, mock_openzwave):
node.node_id: node
}

yield from setup_ozw(hass, mock_openzwave)
yield from hass.async_block_till_done()
await setup_ozw(hass, mock_openzwave)
await hass.async_block_till_done()

yield from hass.services.async_call(
await hass.services.async_call(
zwave.DOMAIN, zwave.SERVICE_CLEAR_USERCODE, {
const.ATTR_NODE_ID: node.node_id,
zwave.ATTR_CODE_SLOT: 1
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()

assert value1.data == '\0\0\0'

0 comments on commit b774299

Please sign in to comment.