Skip to content

Commit

Permalink
Merge pull request #60 from grimmpp/feature-branch
Browse files Browse the repository at this point in the history
Migration support for gateway names added
  • Loading branch information
grimmpp authored Feb 2, 2024
2 parents a968200 + c22a740 commit 2a8f421
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions custom_components/eltako/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CONF_ENABLE_TEACH_IN_BUTTONS: Final = "enable_teach_in_buttons"
CONF_FAST_STATUS_CHANGE: Final = "fast_status_change"
GATEWAY_DEFAULT_NAME: Final = "EnOcean Gateway"
OLD_GATEWAY_DEFAULT_NAME: Final = "EnOcean ESP2 Gateway"
CONF_GATEWAY: Final = "gateway"
CONF_GATEWAY_ID: Final = "gateway_id"
CONF_GATEWAY_DESCRIPTION: Final = "gateway_description"
Expand Down
35 changes: 33 additions & 2 deletions custom_components/eltako/eltako_integration_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from . import config_helpers
from .gateway import *

LOG_PREFIX = "Eltako Integration Setup"

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Eltako component."""
Expand All @@ -25,17 +26,42 @@ def print_config_entry(config_entry: ConfigEntry) -> None:
for k in config_entry.data.keys():
LOGGER.debug("- data %s - %s", k, config_entry.data.get(k, ''))

# relevant for higher than v.1.3.4: removed 'ESP2' from GATEWAY_DEFAULT_NAME which is still in OLD_GATEWAY_DEFAULT_NAME
def migrate_old_gateway_descriptions(hass: HomeAssistant):
LOGGER.debug(f"[{LOG_PREFIX}] Provide new and old gateway descriptions/id for smooth version upgrades.")
migration_dict:dict = {}
for key in hass.data[DATA_ELTAKO].keys():
# LOGGER.debug(f"[{LOG_PREFIX}] Check description: {key}")
if GATEWAY_DEFAULT_NAME in key:
old_key = key.replace(GATEWAY_DEFAULT_NAME, OLD_GATEWAY_DEFAULT_NAME)
LOGGER.info(f"[{LOG_PREFIX}] Support downwards compatibility => from new gatewy description '{key}' to old description '{old_key}'")
migration_dict[old_key] = hass.data[DATA_ELTAKO][key]
# del hass.data[DATA_ELTAKO][key]
if OLD_GATEWAY_DEFAULT_NAME in key:
new_key = key.replace(OLD_GATEWAY_DEFAULT_NAME, GATEWAY_DEFAULT_NAME)
LOGGER.info(f"[{LOG_PREFIX}] Migrate gatewy from old description '{key}' to new description '{new_key}'")
migration_dict[new_key] = hass.data[DATA_ELTAKO][key]
# prvide either new or old key in parallel
for key in migration_dict:
hass.data[DATA_ELTAKO][key] = migration_dict[key]

def get_gateway_from_hass(hass: HomeAssistant, config_entry: ConfigEntry) -> EnOceanGateway:

# Migrage existing gateway configs / ESP2 was removed in the name
migrate_old_gateway_descriptions(hass)

return hass.data[DATA_ELTAKO][config_entry.data[CONF_GATEWAY_DESCRIPTION]]

def set_gateway_to_hass(hass: HomeAssistant, gateway_enity: EnOceanGateway) -> None:

# Migrage existing gateway configs / ESP2 was removed in the name
migrate_old_gateway_descriptions(hass)

hass.data[DATA_ELTAKO][gateway_enity.dev_name] = gateway_enity

def get_device_config_for_gateway(hass: HomeAssistant, config_entry: ConfigEntry, gateway: EnOceanGateway) -> ConfigType:
return config_helpers.get_device_config(hass.data[DATA_ELTAKO][ELTAKO_CONFIG], gateway.dev_id)

LOG_PREFIX = "Eltako Integration Setup"

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up an Eltako gateway for the given entry."""
LOGGER.info(f"[{LOG_PREFIX}] Start gateway setup.")
Expand All @@ -53,12 +79,16 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if not config_helpers.config_check_gateway(config):
raise Exception("Gateway Ids are not unique.")


# set config for global access
eltako_data = hass.data.setdefault(DATA_ELTAKO, {})
eltako_data[ELTAKO_CONFIG] = config
# print whole eltako configuration
LOGGER.debug(f"config: {config}\n")

# Migrage existing gateway configs / ESP2 was removed in the name
migrate_old_gateway_descriptions(hass)

general_settings = config_helpers.get_general_settings_from_configuration(hass)
# Initialise the gateway
# get base_id from user input
Expand Down Expand Up @@ -106,6 +136,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload Eltako config entry."""

gateway = get_gateway_from_hass(hass, config_entry)

LOGGER.info("Unload %s and all its supported devices!", gateway.dev_name)
Expand Down

0 comments on commit 2a8f421

Please sign in to comment.