const convertLegacyBackup = (data) => { // 1.x to 2.0.0: rename legacy settings keys throughout the backup. const aliases = { alwaysSendIndoorTemp: 'always_send_indoor_temp', antiStuckInterval: 'antistuck_interval', antiStuckTime: 'antistuck_time', autoDiagReset: 'auto_diag_reset', autoFaultReset: 'auto_fault_reset', cascadeControl: 'cascade_control', ch2AlwaysEnabled: 'ch2_always_enabled', ch2DisablingAction: 'ch2_disabling_action', coolingSupport: 'cooling_support', dhwBlocking: 'dhw_blocking', dhwDisablingAction: 'dhw_disabling_action', dhwStateAsDhwBlocking: 'dhw_state_as_dhw_blocking', dhwSupport: 'dhw_support', dhwToCh2: 'dhw_to_ch2', externalPump: 'ext_pump', filteringFactor: 'filtering_factor', freezeProtection: 'freeze_protection', getMinMaxTemp: 'get_min_max_temp', heatingDisablingAction: 'heating_disabling_action', heatingStateToSummerWinterMode: 'heating_state_to_summer_winter_mode', heatingToCh2: 'heating_to_ch2', highTemp: 'high_temp', homeAssistantDiscovery: 'ha_discovery', homeAssistantDiscoveryPrefix: 'ha_discovery_prefix', ignoreDiagState: 'ignore_diag_state', immergasFix: 'immergas_fix', indoorTempAvgType: 'indoor_temp_avg_type', inGpio: 'in_gpio', invertState: 'invert_state', logLevel: 'loglevel', lowTemp: 'low_temp', maxModulation: 'max_modulation', maxPower: 'max_power', maxTemp: 'max_temp', maxTempSyncWithTargetTemp: 'max_temp_sync_with_target_temp', memberId: 'member_id', minPower: 'min_power', minTemp: 'min_temp', nativeOTC: 'native_otc', onEnabledHeating: 'on_enabled_heating', onFault: 'on_fault', onLossConnection: 'on_loss_connection', outdoorTempAvgType: 'outdoor_temp_avg_type', outGpio: 'out_gpio', overheatProtection: 'overheat_protection', postCirculationTime: 'post_circulation_time', rxLedGpio: 'rx_led_gpio', setDateAndTime: 'set_date_and_time', staticConfig: 'static_config', statusLedGpio: 'status_led_gpio', summerWinterMode: 'summer_winter_mode', targetDiffFactor: 'target_diff_factor', thresholdHigh: 'threshold_high', thresholdLow: 'threshold_low', thresholdTime: 'threshold_time', tresholdTime: 'threshold_time', turboFactor: 'turbo_factor', unitSystem: 'unit_system', useDhcp: 'use_dhcp', webSerial: 'web_serial' }; // 1.x to 2.0.0: apply the key aliases recursively, including objects inside arrays. const convert = (value) => { if (Array.isArray(value)) { return value.map(convert); } if (value === null || typeof value !== 'object') { return value; } const result = Object.create(null); for (const [key, field] of Object.entries(value)) { const convertedKey = Object.prototype.hasOwnProperty.call(aliases, key) ? aliases[key] : key; result[convertedKey] = convert(field); } return result; }; // 1.x to 2.0.0: map TinyLogger levels to ESP-IDF levels before renaming logLevel. const logLevels = [0, 1, 1, 2, 3, 3, 4, 5]; const legacySystem = data?.settings?.system; if (legacySystem !== null && typeof legacySystem === 'object' && Object.prototype.hasOwnProperty.call(legacySystem, 'logLevel')) { const system = {...legacySystem}; if (Object.prototype.hasOwnProperty.call(system, 'loglevel')) { delete system.logLevel; } else if (Number.isInteger(system.logLevel) && system.logLevel >= 0 && system.logLevel < logLevels.length) { system.logLevel = logLevels[system.logLevel]; } data = { ...data, settings: {...data.settings, system} }; } const result = convert(data); // 1.x to 2.0.0: settings.system.unit_system became the sole unit setting. const opentherm = result?.settings?.opentherm; if (opentherm !== null && typeof opentherm === 'object') { delete opentherm.unit_system; } const network = result?.network; if (network !== null && typeof network === 'object' && !Array.isArray(network)) { // 1.x to 2.0.0: move network.ap and network.sta under network.wifi. const hasLegacyAp = Object.prototype.hasOwnProperty.call(network, 'ap'); const hasLegacySta = Object.prototype.hasOwnProperty.call(network, 'sta'); if (hasLegacyAp || hasLegacySta) { const wifi = network.wifi !== null && typeof network.wifi === 'object' && !Array.isArray(network.wifi) ? network.wifi : Object.create(null); if (hasLegacyAp && !Object.prototype.hasOwnProperty.call(wifi, 'ap')) { wifi.ap = network.ap; } if (hasLegacySta && !Object.prototype.hasOwnProperty.call(wifi, 'sta')) { wifi.sta = network.sta; } network.wifi = wifi; delete network.ap; delete network.sta; } // 1.x to 2.0.0: derive network.type from the settings present in the backup. if (!Object.prototype.hasOwnProperty.call(network, 'type')) { if (typeof network.ethernet?.enabled === 'boolean') { // Legacy builds with W5500 after the 1.6.0 tag (#286) stored ethernet.enabled. network.type = network.ethernet.enabled ? 3 : 2; } else if (hasLegacyAp || hasLegacySta) { // Tagged 1.x releases only supported Wi-Fi. network.type = 2; } } const ethernet = network.ethernet; if (ethernet !== null && typeof ethernet === 'object' && !Array.isArray(ethernet)) { // Post-1.6.0 legacy (#286) to 2.0.0: rename ethernet.pins to ethernet.gpio. if (Object.prototype.hasOwnProperty.call(ethernet, 'pins') && !Object.prototype.hasOwnProperty.call(ethernet, 'gpio')) { ethernet.gpio = ethernet.pins; } delete ethernet.pins; delete ethernet.enabled; // 2.0.0 and 2.0.1 to the per-controller format: ethernet.gpio belonged to W5500. const gpio = ethernet.gpio; if (gpio !== null && typeof gpio === 'object' && !Array.isArray(gpio)) { if (!Object.prototype.hasOwnProperty.call(ethernet, 'w5500')) { ethernet.w5500 = gpio; } if (!Object.prototype.hasOwnProperty.call(ethernet, 'type')) { ethernet.type = 1; // W5500 } } delete ethernet.gpio; } } return result; };