mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Add reauth flow to homee (#147258)
This commit is contained in:
parent
1b21c986e8
commit
f1698cdb75
@ -7,7 +7,7 @@ from pyHomee import Homee, HomeeAuthFailedException, HomeeConnectionFailedExcept
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .const import DOMAIN
|
||||
@ -53,12 +53,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: HomeeConfigEntry) -> boo
|
||||
try:
|
||||
await homee.get_access_token()
|
||||
except HomeeConnectionFailedException as exc:
|
||||
raise ConfigEntryNotReady(
|
||||
f"Connection to Homee failed: {exc.__cause__}"
|
||||
) from exc
|
||||
raise ConfigEntryNotReady(f"Connection to Homee failed: {exc.reason}") from exc
|
||||
except HomeeAuthFailedException as exc:
|
||||
raise ConfigEntryNotReady(
|
||||
f"Authentication to Homee failed: {exc.__cause__}"
|
||||
raise ConfigEntryAuthFailed(
|
||||
f"Authentication to Homee failed: {exc.reason}"
|
||||
) from exc
|
||||
|
||||
hass.loop.create_task(homee.run())
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""Config flow for homee integration."""
|
||||
|
||||
from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@ -32,6 +33,8 @@ class HomeeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
|
||||
homee: Homee
|
||||
_reauth_host: str
|
||||
_reauth_username: str
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
@ -84,6 +87,63 @@ class HomeeConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_reauth(
|
||||
self, entry_data: Mapping[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Perform reauthentication upon an API authentication error."""
|
||||
self._reauth_host = entry_data[CONF_HOST]
|
||||
self._reauth_username = entry_data[CONF_USERNAME]
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Confirm reauthentication dialog."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input:
|
||||
self.homee = Homee(
|
||||
self._reauth_host, user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
|
||||
)
|
||||
try:
|
||||
await self.homee.get_access_token()
|
||||
except HomeeConnectionFailedException:
|
||||
errors["base"] = "cannot_connect"
|
||||
except HomeeAuthenticationFailedException:
|
||||
errors["base"] = "invalid_auth"
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
self.hass.loop.create_task(self.homee.run())
|
||||
await self.homee.wait_until_connected()
|
||||
self.homee.disconnect()
|
||||
await self.homee.wait_until_disconnected()
|
||||
|
||||
await self.async_set_unique_id(self.homee.settings.uid)
|
||||
self._abort_if_unique_id_mismatch(reason="wrong_hub")
|
||||
|
||||
_LOGGER.debug(
|
||||
"Reauthenticated homee entry with ID %s", self.homee.settings.uid
|
||||
)
|
||||
return self.async_update_reload_and_abort(
|
||||
self._get_reauth_entry(), data_updates=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_USERNAME, default=self._reauth_username): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
}
|
||||
),
|
||||
description_placeholders={
|
||||
"host": self._reauth_host,
|
||||
},
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
|
@ -3,8 +3,9 @@
|
||||
"flow_title": "homee {name} ({host})",
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
|
||||
"wrong_hub": "Address belongs to a different homee."
|
||||
"wrong_hub": "IP-Address belongs to a different homee than the configured one."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
@ -25,6 +26,17 @@
|
||||
"password": "The password for your homee."
|
||||
}
|
||||
},
|
||||
"reauth_confirm": {
|
||||
"title": "[%key:common::config_flow::title::reauth%]",
|
||||
"data": {
|
||||
"username": "[%key:common::config_flow::data::username%]",
|
||||
"password": "[%key:common::config_flow::data::password%]"
|
||||
},
|
||||
"data_description": {
|
||||
"username": "[%key:component::homee::config::step::user::data_description::username%]",
|
||||
"password": "[%key:component::homee::config::step::user::data_description::password%]"
|
||||
}
|
||||
},
|
||||
"reconfigure": {
|
||||
"title": "Reconfigure homee {name}",
|
||||
"description": "Reconfigure the IP address of your homee.",
|
||||
@ -32,7 +44,7 @@
|
||||
"host": "[%key:common::config_flow::data::host%]"
|
||||
},
|
||||
"data_description": {
|
||||
"host": "The IP address of your homee."
|
||||
"host": "[%key:component::homee::config::step::user::data_description::host%]"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,9 @@ HOMEE_IP = "192.168.1.11"
|
||||
NEW_HOMEE_IP = "192.168.1.12"
|
||||
HOMEE_NAME = "TestHomee"
|
||||
TESTUSER = "testuser"
|
||||
NEW_TESTUSER = "testuser2"
|
||||
TESTPASS = "testpass"
|
||||
NEW_TESTPASS = "testpass2"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -11,7 +11,16 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import HOMEE_ID, HOMEE_IP, HOMEE_NAME, NEW_HOMEE_IP, TESTPASS, TESTUSER
|
||||
from .conftest import (
|
||||
HOMEE_ID,
|
||||
HOMEE_IP,
|
||||
HOMEE_NAME,
|
||||
NEW_HOMEE_IP,
|
||||
NEW_TESTPASS,
|
||||
NEW_TESTUSER,
|
||||
TESTPASS,
|
||||
TESTUSER,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@ -113,7 +122,6 @@ async def test_flow_already_configured(
|
||||
) -> None:
|
||||
"""Test config flow aborts when already configured."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
@ -132,6 +140,130 @@ async def test_flow_already_configured(
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_homee", "mock_setup_entry")
|
||||
async def test_reauth_success(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the reauth flow."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
assert result["handler"] == DOMAIN
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_USERNAME: NEW_TESTUSER,
|
||||
CONF_PASSWORD: NEW_TESTPASS,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "reauth_successful"
|
||||
|
||||
# Confirm that the config entry has been updated
|
||||
assert mock_config_entry.data[CONF_HOST] == HOMEE_IP
|
||||
assert mock_config_entry.data[CONF_USERNAME] == NEW_TESTUSER
|
||||
assert mock_config_entry.data[CONF_PASSWORD] == NEW_TESTPASS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_eff", "error"),
|
||||
[
|
||||
(
|
||||
HomeeConnectionFailedException("connection timed out"),
|
||||
{"base": "cannot_connect"},
|
||||
),
|
||||
(
|
||||
HomeeAuthFailedException("wrong username or password"),
|
||||
{"base": "invalid_auth"},
|
||||
),
|
||||
(
|
||||
Exception,
|
||||
{"base": "unknown"},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reauth_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_homee: AsyncMock,
|
||||
side_eff: Exception,
|
||||
error: dict[str, str],
|
||||
) -> None:
|
||||
"""Test reconfigure flow errors."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
mock_homee.get_access_token.side_effect = side_eff
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_USERNAME: NEW_TESTUSER,
|
||||
CONF_PASSWORD: NEW_TESTPASS,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == error
|
||||
|
||||
# Confirm that the config entry is unchanged
|
||||
assert mock_config_entry.data[CONF_USERNAME] == TESTUSER
|
||||
assert mock_config_entry.data[CONF_PASSWORD] == TESTPASS
|
||||
|
||||
mock_homee.get_access_token.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_USERNAME: NEW_TESTUSER,
|
||||
CONF_PASSWORD: NEW_TESTPASS,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
# Confirm that the config entry has been updated
|
||||
assert mock_config_entry.data[CONF_HOST] == HOMEE_IP
|
||||
assert mock_config_entry.data[CONF_USERNAME] == NEW_TESTUSER
|
||||
assert mock_config_entry.data[CONF_PASSWORD] == NEW_TESTPASS
|
||||
|
||||
|
||||
async def test_reauth_wrong_uid(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_homee: AsyncMock,
|
||||
) -> None:
|
||||
"""Test reauth flow with wrong UID."""
|
||||
mock_homee.settings.uid = "wrong_uid"
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_USERNAME: NEW_TESTUSER,
|
||||
CONF_PASSWORD: NEW_TESTPASS,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "wrong_hub"
|
||||
|
||||
# Confirm that the config entry is unchanged
|
||||
assert mock_config_entry.data[CONF_HOST] == HOMEE_IP
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_reconfigure_success(
|
||||
hass: HomeAssistant,
|
||||
|
Loading…
x
Reference in New Issue
Block a user