From 085eebc903f842dd76000c8673fead77feaf14c1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 16 Jul 2023 20:58:12 -1000 Subject: [PATCH] Make async_set_state in ConfigEntry a protected method (#96727) I added this in #77803 but I never designed it to be called externally. External usage may break at any time because the class is not designed for this. I should have made it protected in the original PR but I did not think it would get called externally (my mistake) --- homeassistant/components/imap/coordinator.py | 2 +- homeassistant/config_entries.py | 32 +++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/imap/coordinator.py b/homeassistant/components/imap/coordinator.py index bf7f173e647..b13a861fa79 100644 --- a/homeassistant/components/imap/coordinator.py +++ b/homeassistant/components/imap/coordinator.py @@ -373,7 +373,7 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator): except InvalidFolder as ex: _LOGGER.warning("Selected mailbox folder is invalid") await self._cleanup() - self.config_entry.async_set_state( + self.config_entry._async_set_state( # pylint: disable=protected-access self.hass, ConfigEntryState.SETUP_ERROR, "Selected mailbox folder is invalid.", diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 9e27f6efb3e..825064e5410 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -338,7 +338,7 @@ class ConfigEntry: # Only store setup result as state if it was not forwarded. if self.domain == integration.domain: - self.async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None) + self._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None) if self.supports_unload is None: self.supports_unload = await support_entry_unload(hass, self.domain) @@ -357,7 +357,9 @@ class ConfigEntry: err, ) if self.domain == integration.domain: - self.async_set_state(hass, ConfigEntryState.SETUP_ERROR, "Import error") + self._async_set_state( + hass, ConfigEntryState.SETUP_ERROR, "Import error" + ) return if self.domain == integration.domain: @@ -373,12 +375,14 @@ class ConfigEntry: self.domain, err, ) - self.async_set_state(hass, ConfigEntryState.SETUP_ERROR, "Import error") + self._async_set_state( + hass, ConfigEntryState.SETUP_ERROR, "Import error" + ) return # Perform migration if not await self.async_migrate(hass): - self.async_set_state(hass, ConfigEntryState.MIGRATION_ERROR, None) + self._async_set_state(hass, ConfigEntryState.MIGRATION_ERROR, None) return error_reason = None @@ -418,7 +422,7 @@ class ConfigEntry: self.async_start_reauth(hass) result = False except ConfigEntryNotReady as ex: - self.async_set_state(hass, ConfigEntryState.SETUP_RETRY, str(ex) or None) + self._async_set_state(hass, ConfigEntryState.SETUP_RETRY, str(ex) or None) wait_time = 2 ** min(tries, 4) * 5 + ( randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000 ) @@ -479,9 +483,9 @@ class ConfigEntry: return if result: - self.async_set_state(hass, ConfigEntryState.LOADED, None) + self._async_set_state(hass, ConfigEntryState.LOADED, None) else: - self.async_set_state(hass, ConfigEntryState.SETUP_ERROR, error_reason) + self._async_set_state(hass, ConfigEntryState.SETUP_ERROR, error_reason) async def async_shutdown(self) -> None: """Call when Home Assistant is stopping.""" @@ -502,7 +506,7 @@ class ConfigEntry: Returns if unload is possible and was successful. """ if self.source == SOURCE_IGNORE: - self.async_set_state(hass, ConfigEntryState.NOT_LOADED, None) + self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) return True if self.state == ConfigEntryState.NOT_LOADED: @@ -516,7 +520,7 @@ class ConfigEntry: # that was uninstalled, or an integration # that has been renamed without removing the config # entry. - self.async_set_state(hass, ConfigEntryState.NOT_LOADED, None) + self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) return True component = integration.get_component() @@ -527,14 +531,14 @@ class ConfigEntry: if self.state is not ConfigEntryState.LOADED: self.async_cancel_retry_setup() - self.async_set_state(hass, ConfigEntryState.NOT_LOADED, None) + self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) return True supports_unload = hasattr(component, "async_unload_entry") if not supports_unload: if integration.domain == self.domain: - self.async_set_state( + self._async_set_state( hass, ConfigEntryState.FAILED_UNLOAD, "Unload not supported" ) return False @@ -546,7 +550,7 @@ class ConfigEntry: # Only adjust state if we unloaded the component if result and integration.domain == self.domain: - self.async_set_state(hass, ConfigEntryState.NOT_LOADED, None) + self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) await self._async_process_on_unload(hass) @@ -556,7 +560,7 @@ class ConfigEntry: "Error unloading entry %s for %s", self.title, integration.domain ) if integration.domain == self.domain: - self.async_set_state( + self._async_set_state( hass, ConfigEntryState.FAILED_UNLOAD, str(ex) or "Unknown error" ) return False @@ -588,7 +592,7 @@ class ConfigEntry: ) @callback - def async_set_state( + def _async_set_state( self, hass: HomeAssistant, state: ConfigEntryState, reason: str | None ) -> None: """Set the state of the config entry."""