Fix Insteon config flow with add X10 and device override (#45854)
This commit is contained in:
parent
115fe26642
commit
f045c0512b
2 changed files with 31 additions and 25 deletions
|
@ -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(
|
||||
|
|
|
@ -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."""
|
||||
|
|
Loading…
Add table
Reference in a new issue