mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Start SFR Box reauthentication on button authentication failure (#186684)
This commit is contained in:
@@ -6,7 +6,7 @@ from functools import wraps
|
||||
from typing import Any, Concatenate, override
|
||||
|
||||
from sfrbox_api.bridge import SFRBox
|
||||
from sfrbox_api.exceptions import SFRBoxError
|
||||
from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError
|
||||
from sfrbox_api.models import SystemInfo
|
||||
|
||||
from homeassistant.components.button import (
|
||||
@@ -42,6 +42,12 @@ def with_error_wrapping[**_P, _R](
|
||||
"""Catch SFRBoxError errors and raise HomeAssistantError."""
|
||||
try:
|
||||
return await func(self, *args, **kwargs)
|
||||
except SFRBoxAuthenticationError as err:
|
||||
self._entry.async_start_reauth(self.hass)
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_credentials",
|
||||
) from err
|
||||
except SFRBoxError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
@@ -83,7 +89,7 @@ async def async_setup_entry(
|
||||
system_info = data.system.data
|
||||
|
||||
entities = [
|
||||
SFRBoxButton(data.box, description, system_info) for description in BUTTON_TYPES
|
||||
SFRBoxButton(entry, description, system_info) for description in BUTTON_TYPES
|
||||
]
|
||||
async_add_entities(entities)
|
||||
|
||||
@@ -95,13 +101,14 @@ class SFRBoxButton(SFREntity, ButtonEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
box: SFRBox,
|
||||
entry: SFRConfigEntry,
|
||||
description: SFRBoxButtonEntityDescription,
|
||||
system_info: SystemInfo,
|
||||
) -> None:
|
||||
"""Initialize the button."""
|
||||
super().__init__(description, system_info)
|
||||
self._box = box
|
||||
self._entry = entry
|
||||
self._box = entry.runtime_data.box
|
||||
|
||||
@with_error_wrapping
|
||||
@override
|
||||
|
||||
@@ -4,11 +4,11 @@ from collections.abc import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sfrbox_api.exceptions import SFRBoxError
|
||||
from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
@@ -92,3 +92,32 @@ async def test_reboot(hass: HomeAssistant, config_entry_with_auth: ConfigEntry)
|
||||
|
||||
assert len(mock_action.mock_calls) == 1
|
||||
assert mock_action.mock_calls[0][1] == ()
|
||||
|
||||
|
||||
async def test_reboot_invalid_auth(
|
||||
hass: HomeAssistant, config_entry_with_auth: ConfigEntry
|
||||
) -> None:
|
||||
"""Test reauthentication is started when credentials fail on press."""
|
||||
await hass.config_entries.async_setup(config_entry_with_auth.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.sfr_box.button.SFRBox.system_reboot",
|
||||
side_effect=SFRBoxAuthenticationError,
|
||||
),
|
||||
pytest.raises(HomeAssistantError) as exc_info,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
service_data={ATTR_ENTITY_ID: "button.sfr_box_restart"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert exc_info.value.translation_key == "invalid_credentials"
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
||||
assert flows[0]["context"]["entry_id"] == config_entry_with_auth.entry_id
|
||||
|
||||
Reference in New Issue
Block a user