Skip to content

Commit 5f56cf5

Browse files
committed
Add code to migrate configuration
1 parent 9f83105 commit 5f56cf5

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

custom_components/zha_toolkit/__init__.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import homeassistant.helpers.config_validation as cv
66
import voluptuous as vol
7+
from homeassistant.config_entries import ConfigEntry
78

89
try:
910
from homeassistant.components.zha import Gateway as ZHAGateway
@@ -16,11 +17,10 @@
1617

1718
from . import params as PARDEFS
1819
from . import utils as u
20+
from .const import DOMAIN
1921

2022
DEPENDENCIES = ["zha"]
2123

22-
DOMAIN = "zha_toolkit"
23-
2424
# Legacy parameters
2525
ATTR_COMMAND = "command"
2626
ATTR_COMMAND_DATA = "command_data"
@@ -935,3 +935,51 @@ async def command_handler_register_services(
935935
app, listener, ieee, cmd, data, service, params, event_data
936936
):
937937
await _register_services(u.get_hass(listener))
938+
939+
940+
# For migrating from one version to another.
941+
# Added to migrate from a configuration.yaml entry to UI configuration
942+
# Example migration function
943+
# Return True when migration is successful
944+
#
945+
async def async_migrate_entry(hass, config_entry: ConfigEntry):
946+
"""Migrate old entry."""
947+
LOGGER.debug(
948+
"Migrating configuration from version %s.%s",
949+
config_entry.version,
950+
config_entry.minor_version,
951+
)
952+
953+
from .config_flow import ZhaToolkitCustomConfigFlow as CF
954+
955+
new_data = {**config_entry.data}
956+
957+
# Sample code to test version migration and update it
958+
# if config_entry.version > 1:
959+
# # This means the user has downgraded from a future version
960+
# return False
961+
962+
# if config_entry.version == 1:
963+
964+
# new_data = {**config_entry.data}
965+
# if config_entry.minor_version < 2:
966+
# # TODO: modify Config Entry data with changes in version 1.2
967+
# pass
968+
# if config_entry.minor_version < 3:
969+
# # TODO: modify Config Entry data with changes in version 1.3
970+
# pass
971+
972+
hass.config_entries.async_update_entry(
973+
config_entry,
974+
data=new_data,
975+
minor_version=CF.MIN_VERSION,
976+
version=CF.VERSION,
977+
)
978+
979+
LOGGER.debug(
980+
"Migration to configuration version %s.%s successful",
981+
config_entry.version,
982+
config_entry.minor_version,
983+
)
984+
985+
return True

custom_components/zha_toolkit/config_flow.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from homeassistant import config_entries
88

9-
from . import DOMAIN
109
from . import utils as u
10+
from .const import DOMAIN
1111

1212
# from homeassistant.const import CONF_ACCESS_TOKEN,CONF_NAME
1313
# from homeassistant.const import CONF_PATH,CONF_URL
@@ -40,10 +40,13 @@
4040
class ZhaToolkitCustomConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
4141
"""Zha Toolkit Custom config flow."""
4242

43+
VERSION = 0
44+
MINOR_VERSION = 1
45+
4346
data: Optional[dict[str, Any]]
4447

4548
async def my_async_create_entry(self):
46-
self.data["VERSION"] = u.getVersion()
49+
self.data["VERSION"] = await u.getVersion()
4750
# Create the configuration entry
4851
return self.async_create_entry(title="ZHA Toolkit", data=self.data)
4952

0 commit comments

Comments
 (0)