hass-core/tests/components/rachio/test_config_flow.py
J. Nick Koston 7737387efe
Add config flow for rachio (#32757)
* Do not fail when a user has a controller with shared access on their account

* Add config flow for rachio

Also discoverable via homekit

* Update homeassistant/components/rachio/switch.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Split setting the default run time to an options flow

Ensue the run time coming from yaml gets imported into the option flow

Only get the schedule once at setup instead of each zone (was hitting rate limits)

Add the config entry id to the end of the webhook so there is a unique hook per config entry

Breakout the slew of exceptions rachiopy can throw into RachioAPIExceptions

Remove the base url override as an option for the config flow

Switch identifer for device_info to serial number

Add connections to device_info (mac address)

* rename to make pylint happy

* Fix import of custom_url

* claim rachio

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
2020-03-13 22:46:17 -07:00

104 lines
3.4 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
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"}