From ac263acb1cc9253ff6b0c3e267ecf96e207d647f Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Sun, 5 Dec 2021 23:29:39 +0100 Subject: [PATCH] Don't use ConfigEntry update listener for Fronius (#61017) * disable `async_setup_entry` in config_flow tests * don't use config_entry update listener * add `Final` to constants * assert that an updated entry causes a reload (unload) --- homeassistant/components/fronius/__init__.py | 13 ++------ .../components/fronius/config_flow.py | 11 +++---- homeassistant/components/fronius/sensor.py | 10 +++--- tests/components/fronius/test_config_flow.py | 33 ++++++++++++++----- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/fronius/__init__.py b/homeassistant/components/fronius/__init__.py index b7699ea7747..23e595b71ce 100644 --- a/homeassistant/components/fronius/__init__.py +++ b/homeassistant/components/fronius/__init__.py @@ -4,7 +4,7 @@ from __future__ import annotations import asyncio from collections.abc import Callable import logging -from typing import TypeVar +from typing import Final, TypeVar from pyfronius import Fronius, FroniusError @@ -27,8 +27,8 @@ from .coordinator import ( FroniusStorageUpdateCoordinator, ) -_LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [Platform.SENSOR] +_LOGGER: Final = logging.getLogger(__name__) +PLATFORMS: Final = [Platform.SENSOR] FroniusCoordinatorType = TypeVar("FroniusCoordinatorType", bound=FroniusCoordinatorBase) @@ -42,8 +42,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {})[entry.entry_id] = solar_net hass.config_entries.async_setup_platforms(entry, PLATFORMS) - # reload on config_entry update - entry.async_on_unload(entry.add_update_listener(async_update_entry)) return True @@ -58,11 +56,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return unload_ok -async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: - """Update a given config entry.""" - await hass.config_entries.async_reload(entry.entry_id) - - class FroniusSolarNet: """The FroniusSolarNet class routes received values to sensor entities.""" diff --git a/homeassistant/components/fronius/config_flow.py b/homeassistant/components/fronius/config_flow.py index 86654f00c36..ea590767359 100644 --- a/homeassistant/components/fronius/config_flow.py +++ b/homeassistant/components/fronius/config_flow.py @@ -18,7 +18,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN, FroniusConfigEntryData -_LOGGER = logging.getLogger(__name__) +_LOGGER: Final = logging.getLogger(__name__) DHCP_REQUEST_DELAY: Final = 60 @@ -102,9 +102,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" else: await self.async_set_unique_id(unique_id, raise_on_progress=False) - self._abort_if_unique_id_configured( - updates=dict(info), reload_on_update=False - ) + self._abort_if_unique_id_configured(updates=dict(info)) + return self.async_create_entry(title=create_title(info), data=info) return self.async_show_form( @@ -132,9 +131,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return self.async_abort(reason="invalid_host") await self.async_set_unique_id(unique_id, raise_on_progress=False) - self._abort_if_unique_id_configured( - updates=dict(self.info), reload_on_update=False - ) + self._abort_if_unique_id_configured(updates=dict(self.info)) return await self.async_step_confirm_discovery() diff --git a/homeassistant/components/fronius/sensor.py b/homeassistant/components/fronius/sensor.py index b2c6ecbb820..348ef91cd7d 100644 --- a/homeassistant/components/fronius/sensor.py +++ b/homeassistant/components/fronius/sensor.py @@ -2,7 +2,7 @@ from __future__ import annotations import logging -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Final import voluptuous as vol @@ -48,11 +48,11 @@ if TYPE_CHECKING: FroniusStorageUpdateCoordinator, ) -_LOGGER = logging.getLogger(__name__) +_LOGGER: Final = logging.getLogger(__name__) -ELECTRIC_CHARGE_AMPERE_HOURS = "Ah" -ENERGY_VOLT_AMPERE_REACTIVE_HOUR = "varh" -POWER_VOLT_AMPERE_REACTIVE = "var" +ELECTRIC_CHARGE_AMPERE_HOURS: Final = "Ah" +ENERGY_VOLT_AMPERE_REACTIVE_HOUR: Final = "varh" +POWER_VOLT_AMPERE_REACTIVE: Final = "var" PLATFORM_SCHEMA = vol.All( PLATFORM_SCHEMA.extend( diff --git a/tests/components/fronius/test_config_flow.py b/tests/components/fronius/test_config_flow.py index 427c8e4a163..c6f2f69ce5f 100644 --- a/tests/components/fronius/test_config_flow.py +++ b/tests/components/fronius/test_config_flow.py @@ -2,6 +2,7 @@ from unittest.mock import patch from pyfronius import FroniusError +import pytest from homeassistant import config_entries from homeassistant.components.dhcp import DhcpServiceInfo @@ -20,6 +21,17 @@ from . import MOCK_HOST, mock_responses from tests.common import MockConfigEntry + +@pytest.fixture(autouse=True) +def no_setup(): + """Disable setting up the whole integration in config_flow tests.""" + with patch( + "homeassistant.components.fronius.async_setup_entry", + return_value=True, + ): + yield + + INVERTER_INFO_RETURN_VALUE = { "inverters": [ { @@ -172,7 +184,7 @@ async def test_form_unexpected(hass: HomeAssistant) -> None: assert result2["errors"] == {"base": "unknown"} -async def test_form_already_existing(hass): +async def test_form_already_existing(hass: HomeAssistant) -> None: """Test existing entry.""" MockConfigEntry( domain=DOMAIN, @@ -224,17 +236,22 @@ async def test_form_updates_host(hass, aioclient_mock): ) mock_responses(aioclient_mock, host=new_host) - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "host": new_host, - }, - ) - await hass.async_block_till_done() + with patch( + "homeassistant.components.fronius.async_unload_entry", + return_value=True, + ) as mock_unload_entry: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + "host": new_host, + }, + ) + await hass.async_block_till_done() assert result2["type"] == RESULT_TYPE_ABORT assert result2["reason"] == "already_configured" + mock_unload_entry.assert_called_with(hass, entry) entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert entries[0].data == {