hass-core/tests/components/rachio/test_config_flow.py
J. Nick Koston f5a6c3484d
Abort rachio homekit config when one is already setup (#33285)
* Abort rachio homekit config when one is already setup.

We can see rachio on the network to tell them to configure
it, but since the device will not give up the account it is
bound to and there can be multiple rachio controllers on a single
account, we avoid showing the device as discovered once
they already have one configured as they can always
add a new one via "+"

* doc string freshness

* doc string freshness

* Update tests/components/rachio/test_config_flow.py

Co-Authored-By: Franck Nijhof <git@frenck.dev>

Co-authored-by: Franck Nijhof <git@frenck.dev>
2020-03-26 17:14:52 -05:00

126 lines
4.1 KiB
Python

"""Test the Rachio config flow."""
from asynctest import patch
from asynctest.mock import MagicMock
from homeassistant import config_entries, setup
from homeassistant.components.rachio.const import (
CONF_CUSTOM_URL,
CONF_MANUAL_RUN_MINS,
DOMAIN,
)
from homeassistant.const import CONF_API_KEY
from tests.common import MockConfigEntry
def _mock_rachio_return_value(get=None, getInfo=None):
rachio_mock = MagicMock()
person_mock = MagicMock()
type(person_mock).get = MagicMock(return_value=get)
type(person_mock).getInfo = MagicMock(return_value=getInfo)
type(rachio_mock).person = person_mock
return rachio_mock
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
rachio_mock = _mock_rachio_return_value(
get=({"status": 200}, {"username": "myusername"}),
getInfo=({"status": 200}, {"id": "myid"}),
)
with patch(
"homeassistant.components.rachio.config_flow.Rachio", return_value=rachio_mock
), patch(
"homeassistant.components.rachio.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.rachio.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
CONF_CUSTOM_URL: "http://custom.url",
CONF_MANUAL_RUN_MINS: 5,
},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "myusername"
assert result2["data"] == {
CONF_API_KEY: "api_key",
CONF_CUSTOM_URL: "http://custom.url",
CONF_MANUAL_RUN_MINS: 5,
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_invalid_auth(hass):
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
rachio_mock = _mock_rachio_return_value(
get=({"status": 200}, {"username": "myusername"}),
getInfo=({"status": 412}, {"error": "auth fail"}),
)
with patch(
"homeassistant.components.rachio.config_flow.Rachio", return_value=rachio_mock
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_API_KEY: "api_key"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
rachio_mock = _mock_rachio_return_value(
get=({"status": 599}, {"username": "myusername"}),
getInfo=({"status": 200}, {"id": "myid"}),
)
with patch(
"homeassistant.components.rachio.config_flow.Rachio", return_value=rachio_mock
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_API_KEY: "api_key"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_homekit(hass):
"""Test that we abort from homekit if rachio is already setup."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "homekit"}
)
assert result["type"] == "form"
assert result["errors"] == {}
entry = MockConfigEntry(domain=DOMAIN, data={CONF_API_KEY: "api_key"})
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "homekit"}
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"