From 56bda80e0a799404001efe309f52ea1f8a20f479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 3 Jan 2022 13:30:40 +0100 Subject: [PATCH] Address late review comments for version integration (#63283) * Address late review comments for version integration * 2 leftover persistent_notification setup --- homeassistant/components/version/config_flow.py | 17 +++++------------ homeassistant/components/version/coordinator.py | 3 +-- homeassistant/components/version/sensor.py | 2 +- tests/components/version/common.py | 2 -- tests/components/version/test_config_flow.py | 14 +++++--------- tests/components/version/test_sensor.py | 2 -- 6 files changed, 12 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/version/config_flow.py b/homeassistant/components/version/config_flow.py index 5a501b4a97d..30f03663de1 100644 --- a/homeassistant/components/version/config_flow.py +++ b/homeassistant/components/version/config_flow.py @@ -9,7 +9,6 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_NAME, CONF_SOURCE from homeassistant.data_entry_flow import FlowResult -from homeassistant.util import slugify from .const import ( ATTR_VERSION_SOURCE, @@ -47,10 +46,12 @@ from .const import ( class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Version.""" - _entry_data: dict[str, Any] = DEFAULT_CONFIGURATION.copy() - VERSION = 1 + def __init__(self) -> None: + """Initialize the Version config flow.""" + self._entry_data: dict[str, Any] = DEFAULT_CONFIGURATION.copy() + async def async_step_user( self, user_input: dict[str, Any] | None = None, @@ -141,9 +142,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Import a config entry from configuration.yaml.""" self._entry_data = _convert_imported_configuration(import_config) - for entry in self._async_current_entries(): - if _fingerprint(entry.data) == _fingerprint(self._entry_data): - return self.async_abort(reason="already_configured") + self._async_abort_entries_match({**DEFAULT_CONFIGURATION, **self._entry_data}) return self.async_create_entry( title=self._config_entry_name, data=self._entry_data @@ -163,12 +162,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return name -def _fingerprint(data) -> str: - """Return a fingerprint of the configuration.""" - configuration = {**DEFAULT_CONFIGURATION, **data} - return slugify("_".join(configuration.values())) - - def _convert_imported_configuration(config: dict[str, Any]) -> Any: """Convert a key from the imported configuration.""" data = DEFAULT_CONFIGURATION.copy() diff --git a/homeassistant/components/version/coordinator.py b/homeassistant/components/version/coordinator.py index d99fb531da2..635f0fb3bb2 100644 --- a/homeassistant/components/version/coordinator.py +++ b/homeassistant/components/version/coordinator.py @@ -29,7 +29,6 @@ class VersionDataUpdateCoordinator(DataUpdateCoordinator): hass=hass, logger=LOGGER, name=DOMAIN, - update_method=self._async_update_version_data, update_interval=UPDATE_COORDINATOR_UPDATE_INTERVAL, ) self._api = api @@ -46,7 +45,7 @@ class VersionDataUpdateCoordinator(DataUpdateCoordinator): """Return the version data.""" return self._version_data or {} - async def _async_update_version_data(self) -> None: + async def _async_update_data(self) -> None: """Update version data.""" try: self._version, self._version_data = await self._api.get_version() diff --git a/homeassistant/components/version/sensor.py b/homeassistant/components/version/sensor.py index 1cd1aecb053..6a6a2b88879 100644 --- a/homeassistant/components/version/sensor.py +++ b/homeassistant/components/version/sensor.py @@ -113,7 +113,7 @@ class VersionSensorEntity(CoordinatorEntity, SensorEntity): super().__init__(coordinator) self.entity_description = entity_description self._attr_unique_id = ( - f"{coordinator.config_entry.unique_id}_{entity_description.key}" + f"{coordinator.config_entry.entry_id}_{entity_description.key}" ) @property diff --git a/tests/components/version/common.py b/tests/components/version/common.py index 489d1d435bf..17d72d6de72 100644 --- a/tests/components/version/common.py +++ b/tests/components/version/common.py @@ -14,7 +14,6 @@ from homeassistant.components.version.const import ( ) from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from homeassistant.util import dt from tests.common import MockConfigEntry, async_fire_time_changed @@ -55,7 +54,6 @@ async def mock_get_version_update( async def setup_version_integration(hass: HomeAssistant) -> MockConfigEntry: """Set up the Version integration.""" - await async_setup_component(hass, "persistent_notification", {}) mock_entry = MockConfigEntry(**MOCK_VERSION_CONFIG_ENTRY_DATA) mock_entry.add_to_hass(hass) diff --git a/tests/components/version/test_config_flow.py b/tests/components/version/test_config_flow.py index f45ff1764f2..757afeac93d 100644 --- a/tests/components/version/test_config_flow.py +++ b/tests/components/version/test_config_flow.py @@ -3,7 +3,7 @@ from unittest.mock import patch from pyhaversion.consts import HaVersionChannel, HaVersionSource -from homeassistant import config_entries, setup +from homeassistant import config_entries from homeassistant.components.version.const import ( CONF_BETA, CONF_BOARD, @@ -34,9 +34,10 @@ from tests.components.version.common import ( ) -async def test_reload(hass: HomeAssistant): - """Test the Version sensor with different sources.""" +async def test_reload_config_entry(hass: HomeAssistant): + """Test reloading the config entry.""" config_entry = await setup_version_integration(hass) + assert config_entry.state == config_entries.ConfigEntryState.LOADED with patch( "pyhaversion.HaVersion.get_version", @@ -48,12 +49,10 @@ async def test_reload(hass: HomeAssistant): entry = hass.config_entries.async_get_entry(config_entry.entry_id) assert entry.state == config_entries.ConfigEntryState.LOADED - assert hass.states.get("sensor.local_installation").state == MOCK_VERSION async def test_basic_form(hass: HomeAssistant) -> None: - """Test we get the form.""" - await setup.async_setup_component(hass, "persistent_notification", {}) + """Test that we get the form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER, "show_advanced_options": False}, @@ -82,7 +81,6 @@ async def test_basic_form(hass: HomeAssistant) -> None: async def test_advanced_form_pypi(hass: HomeAssistant) -> None: """Show advanced form when pypi is selected.""" - await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER, "show_advanced_options": True}, @@ -124,7 +122,6 @@ async def test_advanced_form_pypi(hass: HomeAssistant) -> None: async def test_advanced_form_container(hass: HomeAssistant) -> None: """Show advanced form when container source is selected.""" - await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER, "show_advanced_options": True}, @@ -166,7 +163,6 @@ async def test_advanced_form_container(hass: HomeAssistant) -> None: async def test_advanced_form_supervisor(hass: HomeAssistant) -> None: """Show advanced form when docker source is selected.""" - await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER, "show_advanced_options": True}, diff --git a/tests/components/version/test_sensor.py b/tests/components/version/test_sensor.py index 6a37fd58b3a..1dead6ab40b 100644 --- a/tests/components/version/test_sensor.py +++ b/tests/components/version/test_sensor.py @@ -36,7 +36,6 @@ async def async_setup_sensor_wrapper( hass: HomeAssistant, config: dict[str, Any] ) -> ConfigEntry: """Set up the Version sensor platform.""" - await async_setup_component(hass, "persistent_notification", {}) with patch( "pyhaversion.HaVersion.get_version", return_value=(MOCK_VERSION, MOCK_VERSION_DATA), @@ -47,7 +46,6 @@ async def async_setup_sensor_wrapper( await hass.async_block_till_done() config_entries = hass.config_entries.async_entries(DOMAIN) - print(config_entries) config_entry = config_entries[-1] assert config_entry.source == "import" return config_entry