hass-core/tests/components/anthemav/test_config_flow.py
Alex Henry f5c6a6be3a
Add config flow to AnthemAV integration (#53268)
* Add config flow to AnthemAV integration

* Add importing of existing configuration

* Change setting to optional and add default value

* Use entity attribute

* Reduce changes by removing additional media player properties

* Remove title from translation

* Refactor config flow and fix PR comments

* Fix a failing test because of wrong renaming

* Add typing and use existing class and enum

* Bump dependency to v1.3.1

* Remove unecessary async_reload_entry

* Fix requirements_test_all after rebase

* Add const for timeout and remove async_block in test

* Reapply CodeOwner and configflow after rebase

* Remove name from configflow

* Fix manifest prettier failure

* Simplify code and avoid catching broad exception

* Removed unused strings and translations

* Avoid asserting hass.data
2022-06-30 07:13:08 -07:00

115 lines
3.4 KiB
Python

"""Test the Anthem A/V Receivers config flow."""
from unittest.mock import AsyncMock, patch
from anthemav.device_error import DeviceError
from homeassistant import config_entries
from homeassistant.components.anthemav.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
async def test_form_with_valid_connection(
hass: HomeAssistant, mock_connection_create: AsyncMock, mock_anthemav: AsyncMock
) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] is None
with patch(
"homeassistant.components.anthemav.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"port": 14999,
},
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
assert result2["data"] == {
"host": "1.1.1.1",
"port": 14999,
"name": "Anthem AV",
"mac": "00:00:00:00:00:01",
"model": "MRX 520",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_device_info_error(hass: HomeAssistant) -> None:
"""Test we handle DeviceError from library."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"anthemav.Connection.create",
side_effect=DeviceError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"port": 14999,
},
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_receive_deviceinfo"}
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"anthemav.Connection.create",
side_effect=OSError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"port": 14999,
},
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_import_configuration(
hass: HomeAssistant, mock_connection_create: AsyncMock, mock_anthemav: AsyncMock
) -> None:
"""Test we import existing configuration."""
config = {
"host": "1.1.1.1",
"port": 14999,
"name": "Anthem Av Import",
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=config
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
"host": "1.1.1.1",
"port": 14999,
"name": "Anthem Av Import",
"mac": "00:00:00:00:00:01",
"model": "MRX 520",
}