From 320dc52bb3811621cbabbc95aae34a83bac3823e Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 2 Jan 2020 00:16:27 +0100 Subject: [PATCH] Migrate config tests from coroutine to async/await (#30366) --- .../components/config/test_config_entries.py | 85 +++++------- tests/components/config/test_customize.py | 43 +++---- tests/components/config/test_group.py | 52 ++++---- tests/components/config/test_init.py | 20 ++- tests/components/config/test_zwave.py | 121 ++++++++---------- 5 files changed, 133 insertions(+), 188 deletions(-) diff --git a/tests/components/config/test_config_entries.py b/tests/components/config/test_config_entries.py index 6631bbf8fbf..ccd41eeb3a5 100644 --- a/tests/components/config/test_config_entries.py +++ b/tests/components/config/test_config_entries.py @@ -1,6 +1,5 @@ """Test config entries API.""" -import asyncio from collections import OrderedDict from unittest.mock import patch @@ -94,16 +93,15 @@ async def test_get_entries(hass, client): ] -@asyncio.coroutine -def test_remove_entry(hass, client): +async def test_remove_entry(hass, client): """Test removing an entry via the API.""" entry = MockConfigEntry(domain="demo", state=core_ce.ENTRY_STATE_LOADED) entry.add_to_hass(hass) - resp = yield from client.delete( + resp = await client.delete( "/api/config/config_entries/entry/{}".format(entry.entry_id) ) assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() assert data == {"require_restart": True} assert len(hass.config_entries.async_entries()) == 0 @@ -120,13 +118,12 @@ async def test_remove_entry_unauth(hass, client, hass_admin_user): assert len(hass.config_entries.async_entries()) == 1 -@asyncio.coroutine -def test_available_flows(hass, client): +async def test_available_flows(hass, client): """Test querying the available flows.""" with patch.object(config_flows, "FLOWS", ["hello", "world"]): - resp = yield from client.get("/api/config/config_entries/flow_handlers") + resp = await client.get("/api/config/config_entries/flow_handlers") assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() assert set(data) == set(["hello", "world"]) @@ -135,14 +132,12 @@ def test_available_flows(hass, client): ############################ -@asyncio.coroutine -def test_initialize_flow(hass, client): +async def test_initialize_flow(hass, client): """Test we can initialize a flow.""" mock_entity_platform(hass, "config_flow.test", None) class TestFlow(core_ce.ConfigFlow): - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): schema = OrderedDict() schema[vol.Required("username")] = str schema[vol.Required("password")] = str @@ -155,12 +150,12 @@ def test_initialize_flow(hass, client): ) with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow", json={"handler": "test"} ) assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() data.pop("flow_id") @@ -182,8 +177,7 @@ async def test_initialize_flow_unauth(hass, client, hass_admin_user): hass_admin_user.groups = [] class TestFlow(core_ce.ConfigFlow): - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): schema = OrderedDict() schema[vol.Required("username")] = str schema[vol.Required("password")] = str @@ -203,23 +197,21 @@ async def test_initialize_flow_unauth(hass, client, hass_admin_user): assert resp.status == 401 -@asyncio.coroutine -def test_abort(hass, client): +async def test_abort(hass, client): """Test a flow that aborts.""" mock_entity_platform(hass, "config_flow.test", None) class TestFlow(core_ce.ConfigFlow): - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): return self.async_abort(reason="bla") with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow", json={"handler": "test"} ) assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() data.pop("flow_id") assert data == { "description_placeholders": None, @@ -229,8 +221,7 @@ def test_abort(hass, client): } -@asyncio.coroutine -def test_create_account(hass, client): +async def test_create_account(hass, client): """Test a flow that creates an account.""" mock_entity_platform(hass, "config_flow.test", None) @@ -239,14 +230,13 @@ def test_create_account(hass, client): class TestFlow(core_ce.ConfigFlow): VERSION = 1 - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): return self.async_create_entry( title="Test Entry", data={"secret": "account_token"} ) with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow", json={"handler": "test"} ) @@ -255,7 +245,7 @@ def test_create_account(hass, client): entries = hass.config_entries.async_entries("test") assert len(entries) == 1 - data = yield from resp.json() + data = await resp.json() data.pop("flow_id") assert data == { "handler": "test", @@ -268,8 +258,7 @@ def test_create_account(hass, client): } -@asyncio.coroutine -def test_two_step_flow(hass, client): +async def test_two_step_flow(hass, client): """Test we can finish a two step flow.""" mock_integration(hass, MockModule("test", async_setup_entry=mock_coro_func(True))) mock_entity_platform(hass, "config_flow.test", None) @@ -277,24 +266,22 @@ def test_two_step_flow(hass, client): class TestFlow(core_ce.ConfigFlow): VERSION = 1 - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): return self.async_show_form( step_id="account", data_schema=vol.Schema({"user_title": str}) ) - @asyncio.coroutine - def async_step_account(self, user_input=None): + async def async_step_account(self, user_input=None): return self.async_create_entry( title=user_input["user_title"], data={"secret": "account_token"} ) with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow", json={"handler": "test"} ) assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() flow_id = data.pop("flow_id") assert data == { "type": "form", @@ -306,7 +293,7 @@ def test_two_step_flow(hass, client): } with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow/{}".format(flow_id), json={"user_title": "user-title"}, ) @@ -315,7 +302,7 @@ def test_two_step_flow(hass, client): entries = hass.config_entries.async_entries("test") assert len(entries) == 1 - data = yield from resp.json() + data = await resp.json() data.pop("flow_id") assert data == { "handler": "test", @@ -336,14 +323,12 @@ async def test_continue_flow_unauth(hass, client, hass_admin_user): class TestFlow(core_ce.ConfigFlow): VERSION = 1 - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): return self.async_show_form( step_id="account", data_schema=vol.Schema({"user_title": str}) ) - @asyncio.coroutine - def async_step_account(self, user_input=None): + async def async_step_account(self, user_input=None): return self.async_create_entry( title=user_input["user_title"], data={"secret": "account_token"} ) @@ -415,14 +400,12 @@ async def test_get_progress_index_unauth(hass, hass_ws_client, hass_admin_user): assert response["error"]["code"] == "unauthorized" -@asyncio.coroutine -def test_get_progress_flow(hass, client): +async def test_get_progress_flow(hass, client): """Test we can query the API for same result as we get from init a flow.""" mock_entity_platform(hass, "config_flow.test", None) class TestFlow(core_ce.ConfigFlow): - @asyncio.coroutine - def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None): schema = OrderedDict() schema[vol.Required("username")] = str schema[vol.Required("password")] = str @@ -434,19 +417,19 @@ def test_get_progress_flow(hass, client): ) with patch.dict(HANDLERS, {"test": TestFlow}): - resp = yield from client.post( + resp = await client.post( "/api/config/config_entries/flow", json={"handler": "test"} ) assert resp.status == 200 - data = yield from resp.json() + data = await resp.json() - resp2 = yield from client.get( + resp2 = await client.get( "/api/config/config_entries/flow/{}".format(data["flow_id"]) ) assert resp2.status == 200 - data2 = yield from resp2.json() + data2 = await resp2.json() assert data == data2 diff --git a/tests/components/config/test_customize.py b/tests/components/config/test_customize.py index f6a678ce8f0..45c1f40d4ad 100644 --- a/tests/components/config/test_customize.py +++ b/tests/components/config/test_customize.py @@ -1,5 +1,4 @@ """Test Customize config panel.""" -import asyncio import json from unittest.mock import patch @@ -8,13 +7,12 @@ from homeassistant.components import config from homeassistant.config import DATA_CUSTOMIZE -@asyncio.coroutine -def test_get_entity(hass, hass_client): +async def test_get_entity(hass, hass_client): """Test getting entity.""" with patch.object(config, "SECTIONS", ["customize"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() def mock_read(path): """Mock reading data.""" @@ -22,21 +20,20 @@ def test_get_entity(hass, hass_client): hass.data[DATA_CUSTOMIZE] = {"hello.beer": {"cold": "beer"}} with patch("homeassistant.components.config._read", mock_read): - resp = yield from client.get("/api/config/customize/config/hello.beer") + resp = await client.get("/api/config/customize/config/hello.beer") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"local": {"free": "beer"}, "global": {"cold": "beer"}} -@asyncio.coroutine -def test_update_entity(hass, hass_client): +async def test_update_entity(hass, hass_client): """Test updating entity.""" with patch.object(config, "SECTIONS", ["customize"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() orig_data = { "hello.beer": {"ignored": True}, @@ -57,7 +54,7 @@ def test_update_entity(hass, hass_client): with patch("homeassistant.components.config._read", mock_read), patch( "homeassistant.components.config._write", mock_write ): - resp = yield from client.post( + resp = await client.post( "/api/config/customize/config/hello.world", data=json.dumps( {"name": "Beer", "entities": ["light.top", "light.bottom"]} @@ -65,7 +62,7 @@ def test_update_entity(hass, hass_client): ) assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"result": "ok"} state = hass.states.get("hello.world") @@ -82,31 +79,27 @@ def test_update_entity(hass, hass_client): assert written[0] == orig_data -@asyncio.coroutine -def test_update_entity_invalid_key(hass, hass_client): +async def test_update_entity_invalid_key(hass, hass_client): """Test updating entity.""" with patch.object(config, "SECTIONS", ["customize"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() - resp = yield from client.post( + resp = await client.post( "/api/config/customize/config/not_entity", data=json.dumps({"name": "YO"}) ) assert resp.status == 400 -@asyncio.coroutine -def test_update_entity_invalid_json(hass, hass_client): +async def test_update_entity_invalid_json(hass, hass_client): """Test updating entity.""" with patch.object(config, "SECTIONS", ["customize"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() - resp = yield from client.post( - "/api/config/customize/config/hello.beer", data="not json" - ) + resp = await client.post("/api/config/customize/config/hello.beer", data="not json") assert resp.status == 400 diff --git a/tests/components/config/test_group.py b/tests/components/config/test_group.py index 3a4a145105a..1b79f30a5b6 100644 --- a/tests/components/config/test_group.py +++ b/tests/components/config/test_group.py @@ -1,5 +1,4 @@ """Test Group config panel.""" -import asyncio import json from unittest.mock import MagicMock, patch @@ -9,34 +8,32 @@ from homeassistant.components import config VIEW_NAME = "api:config:group:config" -@asyncio.coroutine -def test_get_device_config(hass, hass_client): +async def test_get_device_config(hass, hass_client): """Test getting device config.""" with patch.object(config, "SECTIONS", ["group"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() def mock_read(path): """Mock reading data.""" return {"hello.beer": {"free": "beer"}, "other.entity": {"do": "something"}} with patch("homeassistant.components.config._read", mock_read): - resp = yield from client.get("/api/config/group/config/hello.beer") + resp = await client.get("/api/config/group/config/hello.beer") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"free": "beer"} -@asyncio.coroutine -def test_update_device_config(hass, hass_client): +async def test_update_device_config(hass, hass_client): """Test updating device config.""" with patch.object(config, "SECTIONS", ["group"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() orig_data = { "hello.beer": {"ignored": True}, @@ -58,7 +55,7 @@ def test_update_device_config(hass, hass_client): with patch("homeassistant.components.config._read", mock_read), patch( "homeassistant.components.config._write", mock_write ), patch.object(hass.services, "async_call", mock_call): - resp = yield from client.post( + resp = await client.post( "/api/config/group/config/hello_beer", data=json.dumps( {"name": "Beer", "entities": ["light.top", "light.bottom"]} @@ -66,7 +63,7 @@ def test_update_device_config(hass, hass_client): ) assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"result": "ok"} orig_data["hello_beer"]["name"] = "Beer" @@ -76,46 +73,41 @@ def test_update_device_config(hass, hass_client): mock_call.assert_called_once_with("group", "reload") -@asyncio.coroutine -def test_update_device_config_invalid_key(hass, hass_client): +async def test_update_device_config_invalid_key(hass, hass_client): """Test updating device config.""" with patch.object(config, "SECTIONS", ["group"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() - resp = yield from client.post( + resp = await client.post( "/api/config/group/config/not a slug", data=json.dumps({"name": "YO"}) ) assert resp.status == 400 -@asyncio.coroutine -def test_update_device_config_invalid_data(hass, hass_client): +async def test_update_device_config_invalid_data(hass, hass_client): """Test updating device config.""" with patch.object(config, "SECTIONS", ["group"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() - resp = yield from client.post( + resp = await client.post( "/api/config/group/config/hello_beer", data=json.dumps({"invalid_option": 2}) ) assert resp.status == 400 -@asyncio.coroutine -def test_update_device_config_invalid_json(hass, hass_client): +async def test_update_device_config_invalid_json(hass, hass_client): """Test updating device config.""" with patch.object(config, "SECTIONS", ["group"]): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - client = yield from hass_client() + client = await hass_client() - resp = yield from client.post( - "/api/config/group/config/hello_beer", data="not json" - ) + resp = await client.post("/api/config/group/config/hello_beer", data="not json") assert resp.status == 400 diff --git a/tests/components/config/test_init.py b/tests/components/config/test_init.py index 21af45a1203..7f9b62d71f6 100644 --- a/tests/components/config/test_init.py +++ b/tests/components/config/test_init.py @@ -1,5 +1,4 @@ """Test config init.""" -import asyncio from unittest.mock import patch from homeassistant.components import config @@ -9,15 +8,13 @@ from homeassistant.setup import ATTR_COMPONENT, async_setup_component from tests.common import mock_component, mock_coro -@asyncio.coroutine -def test_config_setup(hass, loop): +async def test_config_setup(hass, loop): """Test it sets up hassbian.""" - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) assert "config" in hass.config.components -@asyncio.coroutine -def test_load_on_demand_already_loaded(hass, aiohttp_client): +async def test_load_on_demand_already_loaded(hass, aiohttp_client): """Test getting suites.""" mock_component(hass, "zwave") @@ -26,25 +23,24 @@ def test_load_on_demand_already_loaded(hass, aiohttp_client): ), patch("homeassistant.components.config.zwave.async_setup") as stp: stp.return_value = mock_coro(True) - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert stp.called -@asyncio.coroutine -def test_load_on_demand_on_load(hass, aiohttp_client): +async def test_load_on_demand_on_load(hass, aiohttp_client): """Test getting suites.""" with patch.object(config, "SECTIONS", []), patch.object( config, "ON_DEMAND", ["zwave"] ): - yield from async_setup_component(hass, "config", {}) + await async_setup_component(hass, "config", {}) assert "config.zwave" not in hass.config.components with patch("homeassistant.components.config.zwave.async_setup") as stp: stp.return_value = mock_coro(True) hass.bus.async_fire(EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: "zwave"}) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert stp.called diff --git a/tests/components/config/test_zwave.py b/tests/components/config/test_zwave.py index c2490de23ea..267c57717f9 100644 --- a/tests/components/config/test_zwave.py +++ b/tests/components/config/test_zwave.py @@ -1,5 +1,4 @@ """Test Z-Wave config panel.""" -import asyncio import json from unittest.mock import MagicMock, patch @@ -23,8 +22,7 @@ def client(loop, hass, hass_client): return loop.run_until_complete(hass_client()) -@asyncio.coroutine -def test_get_device_config(client): +async def test_get_device_config(client): """Test getting device config.""" def mock_read(path): @@ -32,16 +30,15 @@ def test_get_device_config(client): return {"hello.beer": {"free": "beer"}, "other.entity": {"do": "something"}} with patch("homeassistant.components.config._read", mock_read): - resp = yield from client.get("/api/config/zwave/device_config/hello.beer") + resp = await client.get("/api/config/zwave/device_config/hello.beer") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"free": "beer"} -@asyncio.coroutine -def test_update_device_config(client): +async def test_update_device_config(client): """Test updating device config.""" orig_data = { "hello.beer": {"ignored": True}, @@ -61,13 +58,13 @@ def test_update_device_config(client): with patch("homeassistant.components.config._read", mock_read), patch( "homeassistant.components.config._write", mock_write ): - resp = yield from client.post( + resp = await client.post( "/api/config/zwave/device_config/hello.beer", data=json.dumps({"polling_intensity": 2}), ) assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"result": "ok"} orig_data["hello.beer"]["polling_intensity"] = 2 @@ -75,10 +72,9 @@ def test_update_device_config(client): assert written[0] == orig_data -@asyncio.coroutine -def test_update_device_config_invalid_key(client): +async def test_update_device_config_invalid_key(client): """Test updating device config.""" - resp = yield from client.post( + resp = await client.post( "/api/config/zwave/device_config/invalid_entity", data=json.dumps({"polling_intensity": 2}), ) @@ -86,10 +82,9 @@ def test_update_device_config_invalid_key(client): assert resp.status == 400 -@asyncio.coroutine -def test_update_device_config_invalid_data(client): +async def test_update_device_config_invalid_data(client): """Test updating device config.""" - resp = yield from client.post( + resp = await client.post( "/api/config/zwave/device_config/hello.beer", data=json.dumps({"invalid_option": 2}), ) @@ -97,18 +92,16 @@ def test_update_device_config_invalid_data(client): assert resp.status == 400 -@asyncio.coroutine -def test_update_device_config_invalid_json(client): +async def test_update_device_config_invalid_json(client): """Test updating device config.""" - resp = yield from client.post( + resp = await client.post( "/api/config/zwave/device_config/hello.beer", data="not json" ) assert resp.status == 400 -@asyncio.coroutine -def test_get_values(hass, client): +async def test_get_values(hass, client): """Test getting values on node.""" node = MockNode(node_id=1) value = MockValue( @@ -125,10 +118,10 @@ def test_get_values(hass, client): values2 = MockEntityValues(primary=value2) hass.data[const.DATA_ENTITY_VALUES] = [values, values2] - resp = yield from client.get("/api/zwave/values/1") + resp = await client.get("/api/zwave/values/1") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == { "123456": { @@ -140,8 +133,7 @@ def test_get_values(hass, client): } -@asyncio.coroutine -def test_get_groups(hass, client): +async def test_get_groups(hass, client): """Test getting groupdata on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) @@ -152,10 +144,10 @@ def test_get_groups(hass, client): node.groups = {1: node.groups} network.nodes = {2: node} - resp = yield from client.get("/api/zwave/groups/2") + resp = await client.get("/api/zwave/groups/2") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == { "1": { @@ -167,38 +159,35 @@ def test_get_groups(hass, client): } -@asyncio.coroutine -def test_get_groups_nogroups(hass, client): +async def test_get_groups_nogroups(hass, client): """Test getting groupdata on node with no groups.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) network.nodes = {2: node} - resp = yield from client.get("/api/zwave/groups/2") + resp = await client.get("/api/zwave/groups/2") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {} -@asyncio.coroutine -def test_get_groups_nonode(hass, client): +async def test_get_groups_nonode(hass, client): """Test getting groupdata on nonexisting node.""" network = hass.data[DATA_NETWORK] = MagicMock() network.nodes = {1: 1, 5: 5} - resp = yield from client.get("/api/zwave/groups/2") + resp = await client.get("/api/zwave/groups/2") assert resp.status == 404 - result = yield from resp.json() + result = await resp.json() assert result == {"message": "Node not found"} -@asyncio.coroutine -def test_get_config(hass, client): +async def test_get_config(hass, client): """Test getting config on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) @@ -214,10 +203,10 @@ def test_get_config(hass, client): network.nodes = {2: node} node.get_values.return_value = node.values - resp = yield from client.get("/api/zwave/config/2") + resp = await client.get("/api/zwave/config/2") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == { "12": { @@ -232,8 +221,7 @@ def test_get_config(hass, client): } -@asyncio.coroutine -def test_get_config_noconfig_node(hass, client): +async def test_get_config_noconfig_node(hass, client): """Test getting config on node without config.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) @@ -241,44 +229,41 @@ def test_get_config_noconfig_node(hass, client): network.nodes = {2: node} node.get_values.return_value = node.values - resp = yield from client.get("/api/zwave/config/2") + resp = await client.get("/api/zwave/config/2") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {} -@asyncio.coroutine -def test_get_config_nonode(hass, client): +async def test_get_config_nonode(hass, client): """Test getting config on nonexisting node.""" network = hass.data[DATA_NETWORK] = MagicMock() network.nodes = {1: 1, 5: 5} - resp = yield from client.get("/api/zwave/config/2") + resp = await client.get("/api/zwave/config/2") assert resp.status == 404 - result = yield from resp.json() + result = await resp.json() assert result == {"message": "Node not found"} -@asyncio.coroutine -def test_get_usercodes_nonode(hass, client): +async def test_get_usercodes_nonode(hass, client): """Test getting usercodes on nonexisting node.""" network = hass.data[DATA_NETWORK] = MagicMock() network.nodes = {1: 1, 5: 5} - resp = yield from client.get("/api/zwave/usercodes/2") + resp = await client.get("/api/zwave/usercodes/2") assert resp.status == 404 - result = yield from resp.json() + result = await resp.json() assert result == {"message": "Node not found"} -@asyncio.coroutine -def test_get_usercodes(hass, client): +async def test_get_usercodes(hass, client): """Test getting usercodes on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE]) @@ -290,16 +275,15 @@ def test_get_usercodes(hass, client): network.nodes = {18: node} node.get_values.return_value = node.values - resp = yield from client.get("/api/zwave/usercodes/18") + resp = await client.get("/api/zwave/usercodes/18") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {"0": {"code": "1234", "label": "label", "length": 4}} -@asyncio.coroutine -def test_get_usercode_nousercode_node(hass, client): +async def test_get_usercode_nousercode_node(hass, client): """Test getting usercodes on node without usercodes.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18) @@ -307,16 +291,15 @@ def test_get_usercode_nousercode_node(hass, client): network.nodes = {18: node} node.get_values.return_value = node.values - resp = yield from client.get("/api/zwave/usercodes/18") + resp = await client.get("/api/zwave/usercodes/18") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {} -@asyncio.coroutine -def test_get_usercodes_no_genreuser(hass, client): +async def test_get_usercodes_no_genreuser(hass, client): """Test getting usercodes on node missing genre user.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE]) @@ -328,33 +311,31 @@ def test_get_usercodes_no_genreuser(hass, client): network.nodes = {18: node} node.get_values.return_value = node.values - resp = yield from client.get("/api/zwave/usercodes/18") + resp = await client.get("/api/zwave/usercodes/18") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert result == {} -@asyncio.coroutine -def test_save_config_no_network(hass, client): +async def test_save_config_no_network(hass, client): """Test saving configuration without network data.""" - resp = yield from client.post("/api/zwave/saveconfig") + resp = await client.post("/api/zwave/saveconfig") assert resp.status == 404 - result = yield from resp.json() + result = await resp.json() assert result == {"message": "No Z-Wave network data found"} -@asyncio.coroutine -def test_save_config(hass, client): +async def test_save_config(hass, client): """Test saving configuration.""" network = hass.data[DATA_NETWORK] = MagicMock() - resp = yield from client.post("/api/zwave/saveconfig") + resp = await client.post("/api/zwave/saveconfig") assert resp.status == 200 - result = yield from resp.json() + result = await resp.json() assert network.write_config.called assert result == {"message": "Z-Wave configuration saved to file."}