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)
This commit is contained in:
J. Nick Koston 2023-07-16 20:58:12 -10:00 committed by GitHub
parent 260e00ffb4
commit 085eebc903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -373,7 +373,7 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator):
except InvalidFolder as ex: except InvalidFolder as ex:
_LOGGER.warning("Selected mailbox folder is invalid") _LOGGER.warning("Selected mailbox folder is invalid")
await self._cleanup() await self._cleanup()
self.config_entry.async_set_state( self.config_entry._async_set_state( # pylint: disable=protected-access
self.hass, self.hass,
ConfigEntryState.SETUP_ERROR, ConfigEntryState.SETUP_ERROR,
"Selected mailbox folder is invalid.", "Selected mailbox folder is invalid.",

View File

@ -338,7 +338,7 @@ class ConfigEntry:
# Only store setup result as state if it was not forwarded. # Only store setup result as state if it was not forwarded.
if self.domain == integration.domain: 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: if self.supports_unload is None:
self.supports_unload = await support_entry_unload(hass, self.domain) self.supports_unload = await support_entry_unload(hass, self.domain)
@ -357,7 +357,9 @@ class ConfigEntry:
err, err,
) )
if self.domain == integration.domain: 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 return
if self.domain == integration.domain: if self.domain == integration.domain:
@ -373,12 +375,14 @@ class ConfigEntry:
self.domain, self.domain,
err, err,
) )
self.async_set_state(hass, ConfigEntryState.SETUP_ERROR, "Import error") self._async_set_state(
hass, ConfigEntryState.SETUP_ERROR, "Import error"
)
return return
# Perform migration # Perform migration
if not await self.async_migrate(hass): 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 return
error_reason = None error_reason = None
@ -418,7 +422,7 @@ class ConfigEntry:
self.async_start_reauth(hass) self.async_start_reauth(hass)
result = False result = False
except ConfigEntryNotReady as ex: 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 + ( wait_time = 2 ** min(tries, 4) * 5 + (
randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000 randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000
) )
@ -479,9 +483,9 @@ class ConfigEntry:
return return
if result: if result:
self.async_set_state(hass, ConfigEntryState.LOADED, None) self._async_set_state(hass, ConfigEntryState.LOADED, None)
else: 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: async def async_shutdown(self) -> None:
"""Call when Home Assistant is stopping.""" """Call when Home Assistant is stopping."""
@ -502,7 +506,7 @@ class ConfigEntry:
Returns if unload is possible and was successful. Returns if unload is possible and was successful.
""" """
if self.source == SOURCE_IGNORE: 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 return True
if self.state == ConfigEntryState.NOT_LOADED: if self.state == ConfigEntryState.NOT_LOADED:
@ -516,7 +520,7 @@ class ConfigEntry:
# that was uninstalled, or an integration # that was uninstalled, or an integration
# that has been renamed without removing the config # that has been renamed without removing the config
# entry. # entry.
self.async_set_state(hass, ConfigEntryState.NOT_LOADED, None) self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None)
return True return True
component = integration.get_component() component = integration.get_component()
@ -527,14 +531,14 @@ class ConfigEntry:
if self.state is not ConfigEntryState.LOADED: if self.state is not ConfigEntryState.LOADED:
self.async_cancel_retry_setup() 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 return True
supports_unload = hasattr(component, "async_unload_entry") supports_unload = hasattr(component, "async_unload_entry")
if not supports_unload: if not supports_unload:
if integration.domain == self.domain: if integration.domain == self.domain:
self.async_set_state( self._async_set_state(
hass, ConfigEntryState.FAILED_UNLOAD, "Unload not supported" hass, ConfigEntryState.FAILED_UNLOAD, "Unload not supported"
) )
return False return False
@ -546,7 +550,7 @@ class ConfigEntry:
# Only adjust state if we unloaded the component # Only adjust state if we unloaded the component
if result and integration.domain == self.domain: 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) await self._async_process_on_unload(hass)
@ -556,7 +560,7 @@ class ConfigEntry:
"Error unloading entry %s for %s", self.title, integration.domain "Error unloading entry %s for %s", self.title, integration.domain
) )
if integration.domain == self.domain: if integration.domain == self.domain:
self.async_set_state( self._async_set_state(
hass, ConfigEntryState.FAILED_UNLOAD, str(ex) or "Unknown error" hass, ConfigEntryState.FAILED_UNLOAD, str(ex) or "Unknown error"
) )
return False return False
@ -588,7 +592,7 @@ class ConfigEntry:
) )
@callback @callback
def async_set_state( def _async_set_state(
self, hass: HomeAssistant, state: ConfigEntryState, reason: str | None self, hass: HomeAssistant, state: ConfigEntryState, reason: str | None
) -> None: ) -> None:
"""Set the state of the config entry.""" """Set the state of the config entry."""