Add laundrify integration (#65090)
* First version of laundrify integration * Code cleanup * Code cleanup after review #2 * Move coordinator to its own file * Save devices as dict and implement available prop as fn * Validate token on init, abort if already configured * Some more cleanup after review * Add strict type hints * Minor changes after code review * Remove OptionsFlow (use default poll interval instead) * Fix CODEOWNERS to pass hassfest job * Fix formatting to pass prettier job * Fix mypy typing error * Update internal device property after fetching data * Call parental update handler and remove obsolete code * Add coordinator tests and fix some config flow tests * Refactor tests * Refactor fixtures * Device unavailable if polling fails
This commit is contained in:
parent
3f8c896cb2
commit
abf9aab18f
22 changed files with 709 additions and 0 deletions
129
tests/components/laundrify/test_config_flow.py
Normal file
129
tests/components/laundrify/test_config_flow.py
Normal file
|
@ -0,0 +1,129 @@
|
|||
"""Test the laundrify config flow."""
|
||||
|
||||
from laundrify_aio import exceptions
|
||||
|
||||
from homeassistant.components.laundrify.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CODE, 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 create_entry
|
||||
from .const import VALID_ACCESS_TOKEN, VALID_AUTH_CODE, VALID_USER_INPUT
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, laundrify_setup_entry) -> None:
|
||||
"""Test we get the form."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=VALID_USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == DOMAIN
|
||||
assert result["data"] == {
|
||||
CONF_ACCESS_TOKEN: VALID_ACCESS_TOKEN,
|
||||
}
|
||||
assert len(laundrify_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_format(
|
||||
hass: HomeAssistant, laundrify_exchange_code
|
||||
) -> None:
|
||||
"""Test we handle invalid format."""
|
||||
laundrify_exchange_code.side_effect = exceptions.InvalidFormat
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_CODE: "invalidFormat"},
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_CODE: "invalid_format"}
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass: HomeAssistant, laundrify_exchange_code) -> None:
|
||||
"""Test we handle invalid auth."""
|
||||
laundrify_exchange_code.side_effect = exceptions.UnknownAuthCode
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data=VALID_USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_CODE: "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistant, laundrify_exchange_code):
|
||||
"""Test we handle cannot connect error."""
|
||||
laundrify_exchange_code.side_effect = exceptions.ApiConnectionException
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data=VALID_USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unkown_exception(hass: HomeAssistant, laundrify_exchange_code):
|
||||
"""Test we handle all other errors."""
|
||||
laundrify_exchange_code.side_effect = Exception
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data=VALID_USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_step_reauth(hass: HomeAssistant) -> None:
|
||||
"""Test the reauth form is shown."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_REAUTH}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
|
||||
|
||||
async def test_integration_already_exists(hass: HomeAssistant):
|
||||
"""Test we only allow a single config flow."""
|
||||
create_entry(hass)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_CODE: VALID_AUTH_CODE,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
Loading…
Add table
Add a link
Reference in a new issue