hass-core/tests/components/guardian/test_config_flow.py
Aaron Bach 369745c4cf
Add support for Elexa Guardian water valve controllers (#34627)
* Add support for Elexa Guardian water valve controllers

* Zeroconf + cleanup

* Sensors and services

* API registration

* Service bug fixes

* Fix bug in cleanup

* Tests and coverage

* Fix incorrect service description

* Bump aioguardian

* Bump aioguardian to 0.2.2

* Bump aioguardian to 0.2.3

* Proper entity inheritance

* Give device a proper name

* Code review
2020-05-26 08:47:25 -05:00

124 lines
4.3 KiB
Python

"""Define tests for the Elexa Guardian config flow."""
from aioguardian.errors import GuardianError
from asynctest import patch
from homeassistant import data_entry_flow
from homeassistant.components.guardian import CONF_UID, DOMAIN
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from tests.common import MockConfigEntry
async def test_duplicate_error(hass):
"""Test that errors are shown when duplicate entries are added."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
MockConfigEntry(domain=DOMAIN, unique_id="192.168.1.100", data=conf).add_to_hass(
hass
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_connect_error(hass):
"""Test that the config entry errors out if the device cannot connect."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
with patch(
"aioguardian.client.Client.connect", side_effect=GuardianError,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
async def test_step_user(hass, ping_client):
"""Test the user step."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Elexa Guardian (192.168.1.100)"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PORT: 7777,
CONF_UID: "ABCDEF123456",
}
async def test_step_zeroconf(hass, ping_client):
"""Test the zeroconf step."""
zeroconf_data = {
"host": "192.168.1.100",
"port": 7777,
"hostname": "GVC1-ABCD.local.",
"type": "_api._udp.local.",
"name": "Guardian Valve Controller API._api._udp.local.",
"properties": {"_raw": {}},
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=zeroconf_data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "zeroconf_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Elexa Guardian (192.168.1.100)"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PORT: 7777,
CONF_UID: "ABCDEF123456",
}
async def test_step_zeroconf_already_in_progress(hass):
"""Test the zeroconf step aborting because it's already in progress."""
zeroconf_data = {
"host": "192.168.1.100",
"port": 7777,
"hostname": "GVC1-ABCD.local.",
"type": "_api._udp.local.",
"name": "Guardian Valve Controller API._api._udp.local.",
"properties": {"_raw": {}},
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=zeroconf_data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "zeroconf_confirm"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=zeroconf_data
)
assert result["type"] == "abort"
assert result["reason"] == "already_in_progress"
async def test_step_zeroconf_no_discovery_info(hass):
"""Test the zeroconf step aborting because no discovery info came along."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "connection_error"