From f045c0512b063569a18d7d40fa7c36ac757b3beb Mon Sep 17 00:00:00 2001 From: Tom Harris Date: Sat, 20 Feb 2021 18:00:18 -0500 Subject: [PATCH] Fix Insteon config flow with add X10 and device override (#45854) --- homeassistant/components/insteon/schemas.py | 48 ++++++++++---------- tests/components/insteon/test_config_flow.py | 8 +++- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/insteon/schemas.py b/homeassistant/components/insteon/schemas.py index adc7e945eba..8698a358b21 100644 --- a/homeassistant/components/insteon/schemas.py +++ b/homeassistant/components/insteon/schemas.py @@ -187,47 +187,47 @@ def add_device_override(config_data, new_override): except ValueError as err: raise ValueError("Incorrect values") from err - overrides = config_data.get(CONF_OVERRIDE, []) + overrides = [] + + for override in config_data.get(CONF_OVERRIDE, []): + if override[CONF_ADDRESS] != address: + overrides.append(override) + curr_override = {} - - # If this address has an override defined, remove it - for override in overrides: - if override[CONF_ADDRESS] == address: - curr_override = override - break - if curr_override: - overrides.remove(curr_override) - curr_override[CONF_ADDRESS] = address curr_override[CONF_CAT] = cat curr_override[CONF_SUBCAT] = subcat overrides.append(curr_override) - config_data[CONF_OVERRIDE] = overrides - return config_data + + new_config = {} + if config_data.get(CONF_X10): + new_config[CONF_X10] = config_data[CONF_X10] + new_config[CONF_OVERRIDE] = overrides + return new_config def add_x10_device(config_data, new_x10): """Add a new X10 device to X10 device list.""" - curr_device = {} - x10_devices = config_data.get(CONF_X10, []) - for x10_device in x10_devices: + x10_devices = [] + for x10_device in config_data.get(CONF_X10, []): if ( - x10_device[CONF_HOUSECODE] == new_x10[CONF_HOUSECODE] - and x10_device[CONF_UNITCODE] == new_x10[CONF_UNITCODE] + x10_device[CONF_HOUSECODE] != new_x10[CONF_HOUSECODE] + or x10_device[CONF_UNITCODE] != new_x10[CONF_UNITCODE] ): - curr_device = x10_device - break - - if curr_device: - x10_devices.remove(curr_device) + x10_devices.append(x10_device) + curr_device = {} curr_device[CONF_HOUSECODE] = new_x10[CONF_HOUSECODE] curr_device[CONF_UNITCODE] = new_x10[CONF_UNITCODE] curr_device[CONF_PLATFORM] = new_x10[CONF_PLATFORM] curr_device[CONF_DIM_STEPS] = new_x10[CONF_DIM_STEPS] x10_devices.append(curr_device) - config_data[CONF_X10] = x10_devices - return config_data + + new_config = {} + if config_data.get(CONF_OVERRIDE): + new_config[CONF_OVERRIDE] = config_data[CONF_OVERRIDE] + new_config[CONF_X10] = x10_devices + return new_config def build_device_override_schema( diff --git a/tests/components/insteon/test_config_flow.py b/tests/components/insteon/test_config_flow.py index f1940b1eb39..1b08317ca30 100644 --- a/tests/components/insteon/test_config_flow.py +++ b/tests/components/insteon/test_config_flow.py @@ -369,13 +369,16 @@ async def test_options_add_device_override(hass: HomeAssistantType): CONF_CAT: "05", CONF_SUBCAT: "bb", } - await _options_form(hass, result2["flow_id"], user_input) + result3, _ = await _options_form(hass, result2["flow_id"], user_input) assert len(config_entry.options[CONF_OVERRIDE]) == 2 assert config_entry.options[CONF_OVERRIDE][1][CONF_ADDRESS] == "4D.5E.6F" assert config_entry.options[CONF_OVERRIDE][1][CONF_CAT] == 5 assert config_entry.options[CONF_OVERRIDE][1][CONF_SUBCAT] == 187 + # If result1 eq result2 the changes will not save + assert result["data"] != result3["data"] + async def test_options_remove_device_override(hass: HomeAssistantType): """Test removing a device override.""" @@ -477,6 +480,9 @@ async def test_options_add_x10_device(hass: HomeAssistantType): assert config_entry.options[CONF_X10][1][CONF_PLATFORM] == "binary_sensor" assert config_entry.options[CONF_X10][1][CONF_DIM_STEPS] == 15 + # If result2 eq result3 the changes will not save + assert result2["data"] != result3["data"] + async def test_options_remove_x10_device(hass: HomeAssistantType): """Test removing an X10 device."""