2019-10-23 19:47:00 +00:00
|
|
|
"""Test the Coolmaster config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-08-09 18:08:07 +00:00
|
|
|
from homeassistant import config_entries
|
2022-04-26 16:57:45 +00:00
|
|
|
from homeassistant.components.coolmaster.config_flow import AVAILABLE_MODES
|
|
|
|
from homeassistant.components.coolmaster.const import DOMAIN
|
2023-02-08 17:08:43 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-03 07:21:17 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2019-10-23 19:47:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _flow_data():
|
|
|
|
options = {"host": "1.1.1.1"}
|
|
|
|
for mode in AVAILABLE_MODES:
|
|
|
|
options[mode] = True
|
2023-01-03 18:21:11 +00:00
|
|
|
options["swing_support"] = False
|
2019-10-23 19:47:00 +00:00
|
|
|
return options
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form(hass: HomeAssistant) -> None:
|
2019-10-23 19:47:00 +00:00
|
|
|
"""Test we get the form."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-23 19:47:00 +00:00
|
|
|
assert result["errors"] is None
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.coolmaster.config_flow.CoolMasterNet.status",
|
|
|
|
return_value={"test_id": "test_unit"},
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.coolmaster.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2019-10-23 19:47:00 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], _flow_data()
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-10-23 19:47:00 +00:00
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2019-10-23 19:47:00 +00:00
|
|
|
assert result2["title"] == "1.1.1.1"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"port": 10102,
|
|
|
|
"supported_modes": AVAILABLE_MODES,
|
2023-01-03 18:21:11 +00:00
|
|
|
"swing_support": False,
|
2019-10-23 19:47:00 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_timeout(hass: HomeAssistant) -> None:
|
2019-10-23 19:47:00 +00:00
|
|
|
"""Test we handle a connection timeout."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2020-08-09 18:08:07 +00:00
|
|
|
"homeassistant.components.coolmaster.config_flow.CoolMasterNet.status",
|
2019-10-23 19:47:00 +00:00
|
|
|
side_effect=TimeoutError(),
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], _flow_data()
|
|
|
|
)
|
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-10-02 19:17:32 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
2019-10-23 19:47:00 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_connection_refused(hass: HomeAssistant) -> None:
|
2019-10-23 19:47:00 +00:00
|
|
|
"""Test we handle a connection error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2020-08-09 18:08:07 +00:00
|
|
|
"homeassistant.components.coolmaster.config_flow.CoolMasterNet.status",
|
2019-10-23 19:47:00 +00:00
|
|
|
side_effect=ConnectionRefusedError(),
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], _flow_data()
|
|
|
|
)
|
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-10-02 19:17:32 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
2019-10-23 19:47:00 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_no_units(hass: HomeAssistant) -> None:
|
2019-10-23 19:47:00 +00:00
|
|
|
"""Test we handle no units found."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2020-08-09 18:08:07 +00:00
|
|
|
"homeassistant.components.coolmaster.config_flow.CoolMasterNet.status",
|
|
|
|
return_value={},
|
2019-10-23 19:47:00 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], _flow_data()
|
|
|
|
)
|
|
|
|
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2019-10-23 19:47:00 +00:00
|
|
|
assert result2["errors"] == {"base": "no_units"}
|