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)
This commit is contained in:
Matthias Alphart 2021-12-05 23:29:39 +01:00 committed by GitHub
parent ab75efda9a
commit ac263acb1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 30 deletions

View File

@ -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."""

View File

@ -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()

View File

@ -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(

View File

@ -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 == {