Add reauth to SleepIQ (#67321)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
1cca317294
commit
bafa99fe3e
6 changed files with 110 additions and 16 deletions
|
@ -1,4 +1,6 @@
|
|||
"""Common methods for SleepIQ."""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
from asyncsleepiq import SleepIQBed, SleepIQSleeper
|
||||
|
@ -20,6 +22,12 @@ SLEEPER_L_NAME_LOWER = SLEEPER_L_NAME.lower().replace(" ", "_")
|
|||
SLEEPER_R_NAME_LOWER = SLEEPER_R_NAME.lower().replace(" ", "_")
|
||||
|
||||
|
||||
SLEEPIQ_CONFIG = {
|
||||
CONF_USERNAME: "user@email.com",
|
||||
CONF_PASSWORD: "password",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_asyncsleepiq():
|
||||
"""Mock an AsyncSleepIQ object."""
|
||||
|
@ -49,14 +57,14 @@ def mock_asyncsleepiq():
|
|||
yield client
|
||||
|
||||
|
||||
async def setup_platform(hass: HomeAssistant, platform) -> MockConfigEntry:
|
||||
async def setup_platform(
|
||||
hass: HomeAssistant, platform: str | None = None
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the SleepIQ platform."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_USERNAME: "user@email.com",
|
||||
CONF_PASSWORD: "password",
|
||||
},
|
||||
data=SLEEPIQ_CONFIG,
|
||||
unique_id=SLEEPIQ_CONFIG[CONF_USERNAME].lower(),
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
|
|
|
@ -9,10 +9,7 @@ from homeassistant.components.sleepiq.const import DOMAIN
|
|||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
SLEEPIQ_CONFIG = {
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
}
|
||||
from tests.components.sleepiq.conftest import SLEEPIQ_CONFIG, setup_platform
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistant) -> None:
|
||||
|
@ -97,3 +94,36 @@ async def test_success(hass: HomeAssistant) -> None:
|
|||
assert result2["data"][CONF_USERNAME] == SLEEPIQ_CONFIG[CONF_USERNAME]
|
||||
assert result2["data"][CONF_PASSWORD] == SLEEPIQ_CONFIG[CONF_PASSWORD]
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth_password(hass):
|
||||
"""Test reauth form."""
|
||||
|
||||
# set up initially
|
||||
entry = await setup_platform(hass)
|
||||
with patch(
|
||||
"homeassistant.components.sleepiq.config_flow.AsyncSleepIQ.login",
|
||||
side_effect=SleepIQLoginException,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": entry.entry_id,
|
||||
"unique_id": entry.unique_id,
|
||||
},
|
||||
data=entry.data,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sleepiq.config_flow.AsyncSleepIQ.login",
|
||||
return_value=True,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"password": "password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result2["reason"] == "reauth_successful"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue