Skip to content

Commit

Permalink
Migrating data configEntry to options
Browse files Browse the repository at this point in the history
  • Loading branch information
WouterTuinstra committed Jun 2, 2024
1 parent 924a414 commit 5c4150b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
35 changes: 32 additions & 3 deletions custom_components/growatt_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from homeassistant.helpers.sun import get_astral_event_next
from homeassistant.util import dt as dt_util
from homeassistant.const import (
CONF_NAME,
CONF_ADDRESS,
CONF_IP_ADDRESS,
CONF_MODEL,
Expand Down Expand Up @@ -95,9 +96,9 @@ async def async_setup_entry(
coordinator = GrowattLocalCoordinator(
hass,
device,
timedelta(seconds=entry.data[CONF_SCAN_INTERVAL]),
timedelta(seconds=entry.data[CONF_POWER_SCAN_INTERVAL])
if entry.data[CONF_POWER_SCAN_ENABLED]
timedelta(seconds=entry.options[CONF_SCAN_INTERVAL]),
timedelta(seconds=entry.options[CONF_POWER_SCAN_INTERVAL])
if entry.options[CONF_POWER_SCAN_ENABLED]
else None,
)

Expand All @@ -107,8 +108,36 @@ async def async_setup_entry(
entry.async_on_unload(entry.add_update_listener(async_update_options))
return True


async def async_migrate_entry(hass, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug("Migrating configuration from version %s.%s", config_entry.version, config_entry.minor_version)

if config_entry.version > 1:
# This means the user has downgraded from a future version
return False

if config_entry.version == 1:

new_data = {**config_entry.data}
new_options = {**config_entry.options}
# Migration of ConfigEntry data to ConfigEntry options to support the OptionsFlow
if config_entry.minor_version < 2:
items_to_move = (CONF_NAME, CONF_SCAN_INTERVAL, CONF_POWER_SCAN_ENABLED, CONF_POWER_SCAN_INTERVAL)

for item in items_to_move:
new_options[item] = new_data.pop(item)

hass.config_entries.async_update_entry(config_entry, data=new_data, options=new_options, minor_version=2, version=1)

_LOGGER.debug("Migration to configuration version %s.%s successful", config_entry.version, config_entry.minor_version)

return True


async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update options."""
_LOGGER.debug("Reloading intergration")
await hass.config_entries.async_reload(entry.entry_id)

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
43 changes: 20 additions & 23 deletions custom_components/growatt_local/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class GrowattLocalConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow class."""

VERSION = 1
MINOR_VERSION = 2

def __init__(self):
"""Initialise growatt server flow."""
Expand All @@ -82,6 +83,14 @@ def __init__(self):
self.data: dict[str, Any] = {}
self.force_next_page = False

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return GrowattLocalOptionsFlow(config_entry)

@callback
def _async_show_selection_form(self, errors=None):
"""Show the initial form to the user to select protocol."""
Expand Down Expand Up @@ -463,10 +472,19 @@ async def async_step_device(self, user_input=None) -> FlowResult:
self.data[CONF_SERIAL_NUMBER] = device_info.serial_number
self.data[CONF_FIRMWARE] = device_info.firmware

options = {
CONF_NAME: user_input.pop(CONF_NAME, ""),
CONF_SCAN_INTERVAL: user_input.pop(CONF_SCAN_INTERVAL, 60),
CONF_POWER_SCAN_ENABLED: user_input.pop(CONF_POWER_SCAN_ENABLED, False),
CONF_POWER_SCAN_INTERVAL: user_input.pop(CONF_POWER_SCAN_INTERVAL, 5)
}

self.data.update(user_input)

return self.async_create_entry(
title=f"Growatt {self.data[CONF_MODEL]}", data=self.data
title=f"Growatt {self.data[CONF_MODEL]}",
data=self.data,
options=options
)

class GrowattLocalOptionsFlow(config_entries.OptionsFlow):
Expand All @@ -481,31 +499,10 @@ def _async_show_options_form(self, errors=None):
data_schema=vol.Schema(
{
vol.Required(CONF_NAME, default=self.config_entry.options.get(CONF_NAME)): str,
vol.Required(CONF_MODEL, default=self.config_entry.options.get(CONF_MODEL)): str,
vol.Required(CONF_TYPE, default=self.config_entry.options.get(CONF_TYPE),): selector.SelectSelector(
selector.SelectSelectorConfig(
options=DEVICETYPES_OPTION
),
),
vol.Required(CONF_DC_STRING, default=self.config_entry.options.get(CONF_DC_STRING)): selector.NumberSelector(
selector.NumberSelectorConfig(
min=1,
max=8,
mode=selector.NumberSelectorMode.BOX,
),
),
vol.Required(CONF_AC_PHASES, default=self.config_entry.options.get(CONF_AC_PHASES)): selector.NumberSelector(
selector.NumberSelectorConfig(
min=1,
max=3,
step=2,
mode=selector.NumberSelectorMode.BOX,
),
),
vol.Required(CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(CONF_SCAN_INTERVAL)): int,
vol.Required(CONF_POWER_SCAN_ENABLED,
default=self.config_entry.options.get(CONF_POWER_SCAN_INTERVAL)): bool,
default=self.config_entry.options.get(CONF_POWER_SCAN_ENABLED)): bool,
vol.Optional(CONF_POWER_SCAN_INTERVAL,
default=self.config_entry.options.get(CONF_POWER_SCAN_INTERVAL)): int
}
Expand Down
4 changes: 2 additions & 2 deletions custom_components/growatt_local/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def async_setup_entry(

coordinator.get_keys_by_name({sensor.key for sensor in sensor_descriptions}, True)

if config_entry.data[CONF_POWER_SCAN_ENABLED]:
if config_entry.options[CONF_POWER_SCAN_ENABLED]:
power_keys = coordinator.get_keys_by_name(power_sensor)

coordinator.p_keys.update(power_keys)
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(self, coordinator, description, entry):
manufacturer="Growatt",
model=entry.data[CONF_MODEL],
sw_version=entry.data[CONF_FIRMWARE],
name=entry.data[CONF_NAME],
name=entry.options[CONF_NAME],
)

async def async_added_to_hass(self) -> None:
Expand Down

0 comments on commit 5c4150b

Please sign in to comment.