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 import asyncio
from collections.abc import Callable from collections.abc import Callable
import logging import logging
from typing import TypeVar from typing import Final, TypeVar
from pyfronius import Fronius, FroniusError from pyfronius import Fronius, FroniusError
@ -27,8 +27,8 @@ from .coordinator import (
FroniusStorageUpdateCoordinator, FroniusStorageUpdateCoordinator,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER: Final = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: Final = [Platform.SENSOR]
FroniusCoordinatorType = TypeVar("FroniusCoordinatorType", bound=FroniusCoordinatorBase) 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.data.setdefault(DOMAIN, {})[entry.entry_id] = solar_net
hass.config_entries.async_setup_platforms(entry, PLATFORMS) 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 return True
@ -58,11 +56,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok 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: class FroniusSolarNet:
"""The FroniusSolarNet class routes received values to sensor entities.""" """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 from .const import DOMAIN, FroniusConfigEntryData
_LOGGER = logging.getLogger(__name__) _LOGGER: Final = logging.getLogger(__name__)
DHCP_REQUEST_DELAY: Final = 60 DHCP_REQUEST_DELAY: Final = 60
@ -102,9 +102,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown" errors["base"] = "unknown"
else: else:
await self.async_set_unique_id(unique_id, raise_on_progress=False) await self.async_set_unique_id(unique_id, raise_on_progress=False)
self._abort_if_unique_id_configured( self._abort_if_unique_id_configured(updates=dict(info))
updates=dict(info), reload_on_update=False
)
return self.async_create_entry(title=create_title(info), data=info) return self.async_create_entry(title=create_title(info), data=info)
return self.async_show_form( return self.async_show_form(
@ -132,9 +131,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="invalid_host") return self.async_abort(reason="invalid_host")
await self.async_set_unique_id(unique_id, raise_on_progress=False) await self.async_set_unique_id(unique_id, raise_on_progress=False)
self._abort_if_unique_id_configured( self._abort_if_unique_id_configured(updates=dict(self.info))
updates=dict(self.info), reload_on_update=False
)
return await self.async_step_confirm_discovery() return await self.async_step_confirm_discovery()

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, Final
import voluptuous as vol import voluptuous as vol
@ -48,11 +48,11 @@ if TYPE_CHECKING:
FroniusStorageUpdateCoordinator, FroniusStorageUpdateCoordinator,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER: Final = logging.getLogger(__name__)
ELECTRIC_CHARGE_AMPERE_HOURS = "Ah" ELECTRIC_CHARGE_AMPERE_HOURS: Final = "Ah"
ENERGY_VOLT_AMPERE_REACTIVE_HOUR = "varh" ENERGY_VOLT_AMPERE_REACTIVE_HOUR: Final = "varh"
POWER_VOLT_AMPERE_REACTIVE = "var" POWER_VOLT_AMPERE_REACTIVE: Final = "var"
PLATFORM_SCHEMA = vol.All( PLATFORM_SCHEMA = vol.All(
PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA.extend(

View File

@ -2,6 +2,7 @@
from unittest.mock import patch from unittest.mock import patch
from pyfronius import FroniusError from pyfronius import FroniusError
import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.dhcp import DhcpServiceInfo from homeassistant.components.dhcp import DhcpServiceInfo
@ -20,6 +21,17 @@ from . import MOCK_HOST, mock_responses
from tests.common import MockConfigEntry 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 = { INVERTER_INFO_RETURN_VALUE = {
"inverters": [ "inverters": [
{ {
@ -172,7 +184,7 @@ async def test_form_unexpected(hass: HomeAssistant) -> None:
assert result2["errors"] == {"base": "unknown"} assert result2["errors"] == {"base": "unknown"}
async def test_form_already_existing(hass): async def test_form_already_existing(hass: HomeAssistant) -> None:
"""Test existing entry.""" """Test existing entry."""
MockConfigEntry( MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -224,17 +236,22 @@ async def test_form_updates_host(hass, aioclient_mock):
) )
mock_responses(aioclient_mock, host=new_host) mock_responses(aioclient_mock, host=new_host)
result2 = await hass.config_entries.flow.async_configure( with patch(
result["flow_id"], "homeassistant.components.fronius.async_unload_entry",
{ return_value=True,
"host": new_host, ) as mock_unload_entry:
}, result2 = await hass.config_entries.flow.async_configure(
) result["flow_id"],
await hass.async_block_till_done() {
"host": new_host,
},
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_ABORT assert result2["type"] == RESULT_TYPE_ABORT
assert result2["reason"] == "already_configured" assert result2["reason"] == "already_configured"
mock_unload_entry.assert_called_with(hass, entry)
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1 assert len(entries) == 1
assert entries[0].data == { assert entries[0].data == {