mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Add reauthentication flow to Telldus Live (#194006)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
53e8a6df6a
commit
1b0245fb64
@@ -10,7 +10,7 @@ from tellduslive import DIM, TURNON, UP, Session
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryError
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
@@ -55,7 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: TelldusLiveConfigEntry)
|
||||
)
|
||||
|
||||
if not session.is_authorized:
|
||||
raise ConfigEntryError(
|
||||
raise ConfigEntryAuthFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="authentication_error",
|
||||
)
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
"""Config flow for Tellduslive."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
import probatio
|
||||
from tellduslive import Session, supports_local_api
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_HOST
|
||||
|
||||
from .const import (
|
||||
@@ -83,6 +84,10 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
KEY_TOKEN: self._session.access_token,
|
||||
KEY_TOKEN_SECRET: self._session.access_token_secret,
|
||||
}
|
||||
if self.source == SOURCE_REAUTH:
|
||||
return self.async_update_reload_and_abort(
|
||||
self._get_reauth_entry(), data_updates={KEY_SESSION: session}
|
||||
)
|
||||
return self.async_create_entry(
|
||||
title=host,
|
||||
data={
|
||||
@@ -114,6 +119,21 @@ class FlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
},
|
||||
)
|
||||
|
||||
async def async_step_reauth(
|
||||
self, entry_data: Mapping[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reauthentication."""
|
||||
self._host = entry_data[KEY_SESSION].get(CONF_HOST)
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Confirm reauthentication."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(step_id="reauth_confirm")
|
||||
return await self.async_step_auth()
|
||||
|
||||
@override
|
||||
async def async_step_discovery(
|
||||
self,
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
"description": "To link your Telldus Live account:\n1. Open the link below\n2. Log in to Telldus Live\n3. Authorize **{app_name}** (select **Yes**).\n4. Come back here and select **Submit**.\n\n[Link Telldus Live account]({auth_url})",
|
||||
"title": "Authenticate with Telldus Live"
|
||||
},
|
||||
"reauth_confirm": {
|
||||
"description": "Home Assistant can no longer access your Telldus Live account. Select **Submit** to authorize **Home Assistant** again.",
|
||||
"title": "[%key:common::config_flow::title::reauth%]"
|
||||
},
|
||||
"user": {
|
||||
"data": {
|
||||
"host": "[%key:common::config_flow::data::host%]"
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
"""Tests for the TelldusLive config flow."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
from homeassistant.components.tellduslive import APPLICATION_NAME, DOMAIN
|
||||
from homeassistant.components.tellduslive.const import KEY_SCAN_INTERVAL, KEY_SESSION
|
||||
from homeassistant.config_entries import SOURCE_DISCOVERY, SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
@@ -199,3 +201,87 @@ async def test_discovery_already_configured(
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entry_data", "expected_session"),
|
||||
[
|
||||
pytest.param(
|
||||
{
|
||||
"host": "Cloud API",
|
||||
KEY_SCAN_INTERVAL: 60,
|
||||
KEY_SESSION: {"token": "old", "token_secret": "old_secret"},
|
||||
},
|
||||
{"token": "token", "token_secret": "token_secret"},
|
||||
id="cloud",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"host": "localhost",
|
||||
KEY_SCAN_INTERVAL: 60,
|
||||
KEY_SESSION: {"host": "localhost", "token": "old"},
|
||||
},
|
||||
{"host": "localhost", "token": "token"},
|
||||
id="local",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_tellduslive: tuple[MagicMock, MagicMock],
|
||||
mock_setup_entry: AsyncMock,
|
||||
entry_data: dict[str, Any],
|
||||
expected_session: dict[str, str],
|
||||
) -> None:
|
||||
"""Test reauthentication updates the session and reloads the entry."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=entry_data)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "auth"
|
||||
assert result["description_placeholders"]["auth_url"] == "https://example.com"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert entry.data[KEY_SESSION] == expected_session
|
||||
assert entry.data["host"] == entry_data["host"]
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("authorize", [False])
|
||||
async def test_reauth_flow_invalid_auth(
|
||||
hass: HomeAssistant,
|
||||
mock_tellduslive: tuple[MagicMock, MagicMock],
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test reauthentication recovers after a failed authorization."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
"host": "Cloud API",
|
||||
KEY_SCAN_INTERVAL: 60,
|
||||
KEY_SESSION: {"token": "old", "token_secret": "old_secret"},
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await entry.start_reauth_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "auth"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
session_cls, _ = mock_tellduslive
|
||||
session_cls.return_value.authorize.return_value = True
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert entry.data[KEY_SESSION] == {"token": "token", "token_secret": "token_secret"}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
@@ -6,7 +6,7 @@ from unittest.mock import MagicMock
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
from homeassistant.components.tellduslive.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
@@ -85,7 +85,7 @@ async def test_setup_not_authorized(
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tellduslive: MagicMock,
|
||||
) -> None:
|
||||
"""Test setup fails when the session is not authorized."""
|
||||
"""Test setup starts a reauth flow when the session is not authorized."""
|
||||
mock_tellduslive.is_authorized = False
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
@@ -95,6 +95,11 @@ async def test_setup_not_authorized(
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
assert mock_config_entry.reason == "Authentication with Telldus Live failed"
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
||||
assert flows[0]["context"]["entry_id"] == mock_config_entry.entry_id
|
||||
|
||||
|
||||
async def test_no_polling_after_unload(
|
||||
hass: HomeAssistant,
|
||||
|
||||
Reference in New Issue
Block a user