mirror of
https://github.com/home-assistant/core.git
synced 2025-11-13 13:00:11 +00:00
* Add new integration for WMS WebControl pro using local API Warema recently released a new local API for their WMS hub called "WebControl pro". This integration makes use of the new local API via a new dedicated Python library pywmspro. For now this integration only supports awnings as covers. But pywmspro is device-agnostic to ease future extensions. * Incorporated review feedback from joostlek Thanks a lot! * Incorporated more review feedback from joostlek Thanks a lot! * Incorporated more review feedback from joostlek Thanks a lot! * Fix * Follow-up fix * Improve handling of DHCP discovery * Further test improvements suggested by joostlek, thanks! --------- Co-authored-by: Joostlek <joostlek@outlook.com>
236 lines
6.8 KiB
Python
236 lines
6.8 KiB
Python
"""Test the wmspro config flow."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import aiohttp
|
|
|
|
from homeassistant.components.dhcp import DhcpServiceInfo
|
|
from homeassistant.components.wmspro.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
|
|
from homeassistant.const import CONF_HOST
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
|
|
async def test_config_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|
"""Test we can handle user-input to create a config entry."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_config_flow_from_dhcp(
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
|
) -> None:
|
|
"""Test we can handle DHCP discovery to create a config entry."""
|
|
info = DhcpServiceInfo(
|
|
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
|
|
)
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=info
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_config_flow_from_dhcp_add_mac(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
) -> None:
|
|
"""Test we can use DHCP discovery to add MAC address to a config entry."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id is None
|
|
|
|
info = DhcpServiceInfo(
|
|
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
|
|
)
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=info
|
|
)
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id == "00:11:22:33:44:55"
|
|
|
|
|
|
async def test_config_flow_ping_failed(
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
|
) -> None:
|
|
"""Test we handle ping failed error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=False,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_config_flow_cannot_connect(
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
|
) -> None:
|
|
"""Test we handle cannot connect error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
side_effect=aiohttp.ClientError,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_config_flow_unknown_error(
|
|
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
|
) -> None:
|
|
"""Test we handle an unknown error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
side_effect=RuntimeError,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": "unknown"}
|
|
|
|
with patch(
|
|
"wmspro.webcontrol.WebControlPro.ping",
|
|
return_value=True,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_HOST: "1.2.3.4",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "1.2.3.4"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|