From 0656f0c62b980d6f47af8b47eace45524e848acd Mon Sep 17 00:00:00 2001 From: On Freund Date: Fri, 25 Oct 2019 19:39:16 +0300 Subject: [PATCH] Address post-merge coolmaster config flow code review (#28163) * Address post-merge code review comments * Use component path for 3rd party lib --- homeassistant/components/coolmaster/__init__.py | 10 ++++------ homeassistant/components/coolmaster/config_flow.py | 7 +++---- tests/components/coolmaster/test_config_flow.py | 14 ++++++-------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/coolmaster/__init__.py b/homeassistant/components/coolmaster/__init__.py index 530427d33ad..c666c39cfb3 100644 --- a/homeassistant/components/coolmaster/__init__.py +++ b/homeassistant/components/coolmaster/__init__.py @@ -8,15 +8,13 @@ async def async_setup(hass, config): async def async_setup_entry(hass, entry): """Set up Coolmaster from a config entry.""" - hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry, "climate")) + hass.async_create_task( + hass.config_entries.async_forward_entry_setup(entry, "climate") + ) return True async def async_unload_entry(hass, entry): """Unload a Coolmaster config entry.""" - await hass.async_add_job( - hass.config_entries.async_forward_entry_unload(entry, "climate") - ) - - return True + return await hass.config_entries.async_forward_entry_unload(entry, "climate") diff --git a/homeassistant/components/coolmaster/config_flow.py b/homeassistant/components/coolmaster/config_flow.py index 543b4c239c8..fe52ea17b28 100644 --- a/homeassistant/components/coolmaster/config_flow.py +++ b/homeassistant/components/coolmaster/config_flow.py @@ -14,11 +14,10 @@ MODES_SCHEMA = {vol.Required(mode, default=True): bool for mode in AVAILABLE_MOD DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str, **MODES_SCHEMA}) -async def validate_connection(hass: core.HomeAssistant, host): - """Validate that we can connect to the Coolmaster instance.""" +async def _validate_connection(hass: core.HomeAssistant, host): cool = CoolMasterNet(host, port=DEFAULT_PORT) devices = await hass.async_add_executor_job(cool.devices) - return len(devices) > 0 + return bool(devices) class CoolmasterConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): @@ -50,7 +49,7 @@ class CoolmasterConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): host = user_input[CONF_HOST] try: - result = await validate_connection(self.hass, host) + result = await _validate_connection(self.hass, host) if not result: errors["base"] = "no_units" except (ConnectionRefusedError, TimeoutError): diff --git a/tests/components/coolmaster/test_config_flow.py b/tests/components/coolmaster/test_config_flow.py index d49858fcf05..d0126ff2cb6 100644 --- a/tests/components/coolmaster/test_config_flow.py +++ b/tests/components/coolmaster/test_config_flow.py @@ -4,8 +4,6 @@ from unittest.mock import patch from homeassistant import config_entries, setup from homeassistant.components.coolmaster.const import DOMAIN, AVAILABLE_MODES -# from homeassistant.components.coolmaster.config_flow import validate_connection - from tests.common import mock_coro @@ -26,8 +24,8 @@ async def test_form(hass): assert result["errors"] is None with patch( - "homeassistant.components.coolmaster.config_flow.validate_connection", - return_value=mock_coro(True), + "homeassistant.components.coolmaster.config_flow.CoolMasterNet.devices", + return_value=[1], ), patch( "homeassistant.components.coolmaster.async_setup", return_value=mock_coro(True) ) as mock_setup, patch( @@ -57,7 +55,7 @@ async def test_form_timeout(hass): ) with patch( - "homeassistant.components.coolmaster.config_flow.validate_connection", + "homeassistant.components.coolmaster.config_flow.CoolMasterNet.devices", side_effect=TimeoutError(), ): result2 = await hass.config_entries.flow.async_configure( @@ -75,7 +73,7 @@ async def test_form_connection_refused(hass): ) with patch( - "homeassistant.components.coolmaster.config_flow.validate_connection", + "homeassistant.components.coolmaster.config_flow.CoolMasterNet.devices", side_effect=ConnectionRefusedError(), ): result2 = await hass.config_entries.flow.async_configure( @@ -93,8 +91,8 @@ async def test_form_no_units(hass): ) with patch( - "homeassistant.components.coolmaster.config_flow.validate_connection", - return_value=mock_coro(False), + "homeassistant.components.coolmaster.config_flow.CoolMasterNet.devices", + return_value=[], ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], _flow_data()