Config Entry migrations (#20888)
* Updated per review feedback. * Fixed line length * Review comments and lint error * Fixed mypy typeing error * Moved migration logic to setup * Use new migration error state * Fix bug and ignore mypy type error * Removed SmartThings example and added unit tests. * Fixed test comments.
This commit is contained in:
1 parent
1130ccb325
commit
383813bfe6
3 files changed
+180
-8
No files matched your search
@@ -7,7 +7,11 @@ component.
|
||||
During startup, Home Assistant will setup the entries during the normal setup
|
||||
of a component. It will first call the normal setup and then call the method
|
||||
`async_setup_entry(hass, entry)` for each entry. The same method is called when
|
||||
Home Assistant is running while a config entry is created.
|
||||
Home Assistant is running while a config entry is created. If the version of
|
||||
the config entry does not match that of the flow handler, setup will
|
||||
call the method `async_migrate_entry(hass, entry)` with the expectation that
|
||||
the entry be brought to the current version. Return `True` to indicate
|
||||
migration was successful, otherwise `False`.
|
||||
|
||||
## Config Flows
|
||||
|
||||
@@ -116,6 +120,7 @@ If the result of the step is to show a form, the user will be able to continue
|
||||
the flow from the config panel.
|
||||
"""
|
||||
import logging
|
||||
import functools
|
||||
import uuid
|
||||
from typing import Set, Optional, List, Dict # noqa pylint: disable=unused-import
|
||||
|
||||
@@ -188,6 +193,8 @@ SAVE_DELAY = 1
|
||||
ENTRY_STATE_LOADED = 'loaded'
|
||||
# There was an error while trying to set up this config entry
|
||||
ENTRY_STATE_SETUP_ERROR = 'setup_error'
|
||||
# There was an error while trying to migrate the config entry to a new version
|
||||
ENTRY_STATE_MIGRATION_ERROR = 'migration_error'
|
||||
# The config entry was not ready to be set up yet, but might be later
|
||||
ENTRY_STATE_SETUP_RETRY = 'setup_retry'
|
||||
# The config entry has not been loaded
|
||||
@@ -256,6 +263,12 @@ class ConfigEntry:
|
||||
if component is None:
|
||||
component = getattr(hass.components, self.domain)
|
||||
|
||||
# Perform migration
|
||||
if component.DOMAIN == self.domain:
|
||||
if not await self.async_migrate(hass):
|
||||
self.state = ENTRY_STATE_MIGRATION_ERROR
|
||||
return
|
||||
|
||||
try:
|
||||
result = await component.async_setup_entry(hass, self)
|
||||
|
||||
@@ -332,6 +345,45 @@ class ConfigEntry:
|
||||
self.state = ENTRY_STATE_FAILED_UNLOAD
|
||||
return False
|
||||
|
||||
async def async_migrate(self, hass: HomeAssistant) -> bool:
|
||||
"""Migrate an entry.
|
||||
|
||||
Returns True if config entry is up-to-date or has been migrated.
|
||||
"""
|
||||
handler = HANDLERS.get(self.domain)
|
||||
if handler is None:
|
||||
_LOGGER.error("Flow handler not found for entry %s for %s",
|
||||
self.title, self.domain)
|
||||
return False
|
||||
# Handler may be a partial
|
||||
while isinstance(handler, functools.partial):
|
||||
handler = handler.func
|
||||
|
||||
if self.version == handler.VERSION:
|
||||
return True
|
||||
|
||||
component = getattr(hass.components, self.domain)
|
||||
supports_migrate = hasattr(component, 'async_migrate_entry')
|
||||
if not supports_migrate:
|
||||
_LOGGER.error("Migration handler not found for entry %s for %s",
|
||||
self.title, self.domain)
|
||||
return False
|
||||
|
||||
try:
|
||||
result = await component.async_migrate_entry(hass, self)
|
||||
if not isinstance(result, bool):
|
||||
_LOGGER.error('%s.async_migrate_entry did not return boolean',
|
||||
self.domain)
|
||||
return False
|
||||
if result:
|
||||
# pylint: disable=protected-access
|
||||
hass.config_entries._async_schedule_save() # type: ignore
|
||||
return result
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception('Error migrating entry %s for %s',
|
||||
self.title, component.DOMAIN)
|
||||
return False
|
||||
|
||||
def as_dict(self):
|
||||
"""Return dictionary version of this entry."""
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user