From d4b548b16969b7b2728d91aafd9ac5c9cdf246a8 Mon Sep 17 00:00:00 2001 From: Robin Lintermann Date: Thu, 26 Jun 2025 15:30:03 +0200 Subject: [PATCH] Fixed issue when tests (should) fail in Smarla (#146102) * Fixed issue when tests (should) fail * Use usefixture decorator * Throw ConfigEntryError instead of AuthFailed --- homeassistant/components/smarla/__init__.py | 4 ++-- tests/components/smarla/test_config_flow.py | 20 ++++++++++---------- tests/components/smarla/test_init.py | 3 +++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/smarla/__init__.py b/homeassistant/components/smarla/__init__.py index 2de3fcfa242..533acb3375b 100644 --- a/homeassistant/components/smarla/__init__.py +++ b/homeassistant/components/smarla/__init__.py @@ -5,7 +5,7 @@ from pysmarlaapi import Connection, Federwiege from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.exceptions import ConfigEntryError from .const import HOST, PLATFORMS @@ -18,7 +18,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) - # Check if token still has access if not await connection.refresh_token(): - raise ConfigEntryAuthFailed("Invalid authentication") + raise ConfigEntryError("Invalid authentication") federwiege = Federwiege(hass.loop, connection) federwiege.register() diff --git a/tests/components/smarla/test_config_flow.py b/tests/components/smarla/test_config_flow.py index a2bd5b36fc0..beccf6e4b95 100644 --- a/tests/components/smarla/test_config_flow.py +++ b/tests/components/smarla/test_config_flow.py @@ -2,6 +2,8 @@ from unittest.mock import AsyncMock, MagicMock, patch +import pytest + from homeassistant.components.smarla.const import DOMAIN from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant @@ -12,9 +14,8 @@ from .const import MOCK_SERIAL_NUMBER, MOCK_USER_INPUT from tests.common import MockConfigEntry -async def test_config_flow( - hass: HomeAssistant, mock_setup_entry, mock_connection: MagicMock -) -> None: +@pytest.mark.usefixtures("mock_setup_entry", "mock_connection") +async def test_config_flow(hass: HomeAssistant) -> None: """Test creating a config entry.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -35,9 +36,8 @@ async def test_config_flow( assert result["result"].unique_id == MOCK_SERIAL_NUMBER -async def test_malformed_token( - hass: HomeAssistant, mock_setup_entry, mock_connection: MagicMock -) -> None: +@pytest.mark.usefixtures("mock_setup_entry", "mock_connection") +async def test_malformed_token(hass: HomeAssistant) -> None: """Test we show user form on malformed token input.""" with patch( "homeassistant.components.smarla.config_flow.Connection", side_effect=ValueError @@ -60,9 +60,8 @@ async def test_malformed_token( assert result["type"] is FlowResultType.CREATE_ENTRY -async def test_invalid_auth( - hass: HomeAssistant, mock_setup_entry, mock_connection: MagicMock -) -> None: +@pytest.mark.usefixtures("mock_setup_entry") +async def test_invalid_auth(hass: HomeAssistant, mock_connection: MagicMock) -> None: """Test we show user form on invalid auth.""" with patch.object( mock_connection, "refresh_token", new=AsyncMock(return_value=False) @@ -85,8 +84,9 @@ async def test_invalid_auth( assert result["type"] is FlowResultType.CREATE_ENTRY +@pytest.mark.usefixtures("mock_setup_entry", "mock_connection") async def test_device_exists_abort( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_connection: MagicMock + hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: """Test we abort config flow if Smarla device already configured.""" mock_config_entry.add_to_hass(hass) diff --git a/tests/components/smarla/test_init.py b/tests/components/smarla/test_init.py index b9d291f582d..9523772d914 100644 --- a/tests/components/smarla/test_init.py +++ b/tests/components/smarla/test_init.py @@ -2,6 +2,8 @@ from unittest.mock import MagicMock +import pytest + from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant @@ -10,6 +12,7 @@ from . import setup_integration from tests.common import MockConfigEntry +@pytest.mark.usefixtures("mock_federwiege") async def test_init_invalid_auth( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_connection: MagicMock ) -> None: