104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
"""Test Efergy config flow."""
|
|
from unittest.mock import patch
|
|
|
|
from pyefergy import exceptions
|
|
|
|
from homeassistant.components.efergy.const import DEFAULT_NAME, DOMAIN
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
|
from homeassistant.const import CONF_API_KEY, CONF_SOURCE
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import (
|
|
RESULT_TYPE_ABORT,
|
|
RESULT_TYPE_CREATE_ENTRY,
|
|
RESULT_TYPE_FORM,
|
|
)
|
|
|
|
from . import CONF_DATA, HID, _patch_efergy, _patch_efergy_status, create_entry
|
|
|
|
|
|
def _patch_setup():
|
|
return patch("homeassistant.components.efergy.async_setup_entry")
|
|
|
|
|
|
async def test_flow_user(hass: HomeAssistant):
|
|
"""Test user initialized flow."""
|
|
with _patch_efergy(), _patch_setup():
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={CONF_SOURCE: SOURCE_USER},
|
|
)
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input=CONF_DATA,
|
|
)
|
|
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
|
assert result["title"] == DEFAULT_NAME
|
|
assert result["data"] == CONF_DATA
|
|
assert result["result"].unique_id == HID
|
|
|
|
|
|
async def test_flow_user_cannot_connect(hass: HomeAssistant):
|
|
"""Test user initialized flow with unreachable service."""
|
|
with _patch_efergy_status() as efergymock:
|
|
efergymock.side_effect = exceptions.ConnectError
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={CONF_SOURCE: SOURCE_USER}, data=CONF_DATA
|
|
)
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"]["base"] == "cannot_connect"
|
|
|
|
|
|
async def test_flow_user_invalid_auth(hass: HomeAssistant):
|
|
"""Test user initialized flow with invalid authentication."""
|
|
with _patch_efergy_status() as efergymock:
|
|
efergymock.side_effect = exceptions.InvalidAuth
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={CONF_SOURCE: SOURCE_USER}, data=CONF_DATA
|
|
)
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"]["base"] == "invalid_auth"
|
|
|
|
|
|
async def test_flow_user_unknown(hass: HomeAssistant):
|
|
"""Test user initialized flow with unknown error."""
|
|
with _patch_efergy_status() as efergymock:
|
|
efergymock.side_effect = Exception
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={CONF_SOURCE: SOURCE_USER}, data=CONF_DATA
|
|
)
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"]["base"] == "unknown"
|
|
|
|
|
|
async def test_flow_reauth(hass: HomeAssistant):
|
|
"""Test reauth step."""
|
|
entry = create_entry(hass)
|
|
with _patch_efergy(), _patch_setup():
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={
|
|
CONF_SOURCE: SOURCE_REAUTH,
|
|
"entry_id": entry.entry_id,
|
|
"unique_id": entry.unique_id,
|
|
},
|
|
data=CONF_DATA,
|
|
)
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
new_conf = {CONF_API_KEY: "1234567890"}
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input=new_conf,
|
|
)
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert entry.data == new_conf
|