mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 10:47:51 +00:00
Address post-merge coolmaster config flow code review (#28163)
* Address post-merge code review comments * Use component path for 3rd party lib
This commit is contained in:
parent
43c7b57d1e
commit
0656f0c62b
@ -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")
|
||||
|
@ -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):
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user