Skip to content

Commit

Permalink
even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Dec 18, 2020
1 parent 92e487f commit d9d3f19
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions tests/components/kmtronic/test_config_flow.py
@@ -1,9 +1,12 @@
"""Test the kmtronic config flow."""
from aiohttp import ClientConnectorError, ClientResponseError

from homeassistant import config_entries, setup
from homeassistant.components.kmtronic.config_flow import CannotConnect, InvalidAuth
from homeassistant.components.kmtronic.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED

from tests.async_mock import Mock, patch
from tests.common import MockConfigEntry


async def test_form(hass):
Expand Down Expand Up @@ -53,7 +56,7 @@ async def test_form_invalid_auth(hass):

with patch(
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
side_effect=InvalidAuth,
side_effect=ClientResponseError(None, None, status=401),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
Expand All @@ -76,7 +79,7 @@ async def test_form_cannot_connect(hass):

with patch(
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
side_effect=CannotConnect,
side_effect=ClientConnectorError(None, Mock()),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
Expand All @@ -89,3 +92,53 @@ async def test_form_cannot_connect(hass):

assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}


async def test_form_unknown_error(hass):
"""Test we handle unknown errors."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

with patch(
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
side_effect=Exception(),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
},
)

assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"}


async def test_unload_config_entry(hass, aioclient_mock):
"""Test entry unloading."""

config_entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.1.1.1", "username": "admin", "password": "admin"},
)
config_entry.add_to_hass(hass)

aioclient_mock.get(
"http://1.1.1.1/status.xml",
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

config_entries = hass.config_entries.async_entries(DOMAIN)
assert len(config_entries) == 1
assert config_entries[0] is config_entry
assert config_entry.state == ENTRY_STATE_LOADED

await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()

assert config_entry.state == ENTRY_STATE_NOT_LOADED

0 comments on commit d9d3f19

Please sign in to comment.