mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Validate SleepIQ connection/credentials for the import step (#67292)
This commit is contained in:
parent
e65670fef4
commit
858c09060d
@ -1,6 +1,7 @@
|
|||||||
"""Config flow to configure SleepIQ component."""
|
"""Config flow to configure SleepIQ component."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from asyncsleepiq import AsyncSleepIQ, SleepIQLoginException, SleepIQTimeoutException
|
from asyncsleepiq import AsyncSleepIQ, SleepIQLoginException, SleepIQTimeoutException
|
||||||
@ -14,6 +15,8 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a SleepIQ config flow."""
|
"""Handle a SleepIQ config flow."""
|
||||||
@ -28,6 +31,10 @@ class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(import_config[CONF_USERNAME].lower())
|
await self.async_set_unique_id(import_config[CONF_USERNAME].lower())
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
|
if error := await try_connection(self.hass, import_config):
|
||||||
|
_LOGGER.error("Could not authenticate with SleepIQ server: %s", error)
|
||||||
|
return self.async_abort(reason=error)
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=import_config[CONF_USERNAME], data=import_config
|
title=import_config[CONF_USERNAME], data=import_config
|
||||||
)
|
)
|
||||||
@ -43,26 +50,23 @@ class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
try:
|
if error := await try_connection(self.hass, user_input):
|
||||||
await try_connection(self.hass, user_input)
|
errors["base"] = error
|
||||||
except SleepIQLoginException:
|
|
||||||
errors["base"] = "invalid_auth"
|
|
||||||
except SleepIQTimeoutException:
|
|
||||||
errors["base"] = "cannot_connect"
|
|
||||||
else:
|
else:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_USERNAME], data=user_input
|
title=user_input[CONF_USERNAME], data=user_input
|
||||||
)
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
user_input = {}
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema(
|
data_schema=vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
default=user_input.get(CONF_USERNAME)
|
default=user_input.get(CONF_USERNAME),
|
||||||
if user_input is not None
|
|
||||||
else "",
|
|
||||||
): str,
|
): str,
|
||||||
vol.Required(CONF_PASSWORD): str,
|
vol.Required(CONF_PASSWORD): str,
|
||||||
}
|
}
|
||||||
@ -72,10 +76,17 @@ class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def try_connection(hass: HomeAssistant, user_input: dict[str, Any]) -> None:
|
async def try_connection(hass: HomeAssistant, user_input: dict[str, Any]) -> str | None:
|
||||||
"""Test if the given credentials can successfully login to SleepIQ."""
|
"""Test if the given credentials can successfully login to SleepIQ."""
|
||||||
|
|
||||||
client_session = async_get_clientsession(hass)
|
client_session = async_get_clientsession(hass)
|
||||||
|
|
||||||
gateway = AsyncSleepIQ(client_session=client_session)
|
gateway = AsyncSleepIQ(client_session=client_session)
|
||||||
|
try:
|
||||||
await gateway.login(user_input[CONF_USERNAME], user_input[CONF_PASSWORD])
|
await gateway.login(user_input[CONF_USERNAME], user_input[CONF_PASSWORD])
|
||||||
|
except SleepIQLoginException:
|
||||||
|
return "invalid_auth"
|
||||||
|
except SleepIQTimeoutException:
|
||||||
|
return "cannot_connect"
|
||||||
|
|
||||||
|
return None
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from asyncsleepiq import SleepIQLoginException, SleepIQTimeoutException
|
from asyncsleepiq import SleepIQLoginException, SleepIQTimeoutException
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow, setup
|
from homeassistant import config_entries, data_entry_flow, setup
|
||||||
from homeassistant.components.sleepiq.const import DOMAIN
|
from homeassistant.components.sleepiq.const import DOMAIN
|
||||||
@ -26,6 +27,21 @@ async def test_import(hass: HomeAssistant) -> None:
|
|||||||
assert entry.data[CONF_PASSWORD] == SLEEPIQ_CONFIG[CONF_PASSWORD]
|
assert entry.data[CONF_PASSWORD] == SLEEPIQ_CONFIG[CONF_PASSWORD]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"side_effect", [SleepIQLoginException, SleepIQTimeoutException]
|
||||||
|
)
|
||||||
|
async def test_import_failure(hass: HomeAssistant, side_effect) -> None:
|
||||||
|
"""Test that we won't import a config entry on login failure."""
|
||||||
|
with patch(
|
||||||
|
"asyncsleepiq.AsyncSleepIQ.login",
|
||||||
|
side_effect=side_effect,
|
||||||
|
):
|
||||||
|
assert await setup.async_setup_component(hass, DOMAIN, {DOMAIN: SLEEPIQ_CONFIG})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_show_set_form(hass: HomeAssistant) -> None:
|
async def test_show_set_form(hass: HomeAssistant) -> None:
|
||||||
"""Test that the setup form is served."""
|
"""Test that the setup form is served."""
|
||||||
with patch("asyncsleepiq.AsyncSleepIQ.login"):
|
with patch("asyncsleepiq.AsyncSleepIQ.login"):
|
||||||
@ -37,11 +53,18 @@ async def test_show_set_form(hass: HomeAssistant) -> None:
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
|
||||||
async def test_login_invalid_auth(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test we show user form with appropriate error on login failure."""
|
"side_effect,error",
|
||||||
|
[
|
||||||
|
(SleepIQLoginException, "invalid_auth"),
|
||||||
|
(SleepIQTimeoutException, "cannot_connect"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_login_failure(hass: HomeAssistant, side_effect, error) -> None:
|
||||||
|
"""Test that we show user form with appropriate error on login failure."""
|
||||||
with patch(
|
with patch(
|
||||||
"asyncsleepiq.AsyncSleepIQ.login",
|
"asyncsleepiq.AsyncSleepIQ.login",
|
||||||
side_effect=SleepIQLoginException,
|
side_effect=side_effect,
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=SLEEPIQ_CONFIG
|
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=SLEEPIQ_CONFIG
|
||||||
@ -49,22 +72,7 @@ async def test_login_invalid_auth(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {"base": "invalid_auth"}
|
assert result["errors"] == {"base": error}
|
||||||
|
|
||||||
|
|
||||||
async def test_login_cannot_connect(hass: HomeAssistant) -> None:
|
|
||||||
"""Test we show user form with appropriate error on login failure."""
|
|
||||||
with patch(
|
|
||||||
"asyncsleepiq.AsyncSleepIQ.login",
|
|
||||||
side_effect=SleepIQTimeoutException,
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=SLEEPIQ_CONFIG
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
||||||
assert result["step_id"] == "user"
|
|
||||||
assert result["errors"] == {"base": "cannot_connect"}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_success(hass: HomeAssistant) -> None:
|
async def test_success(hass: HomeAssistant) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user