Skip to content

Commit 5da1d6b

Browse files
committed
make config update logic more generic
1 parent c29b124 commit 5da1d6b

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

custom_components/pyscript/config_flow.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from homeassistant import config_entries
77

8-
from .const import CONF_ALLOW_ALL_IMPORTS, DOMAIN
8+
from .const import CONF_ALL_KEYS, CONF_ALLOW_ALL_IMPORTS, DOMAIN
99

1010
PYSCRIPT_SCHEMA = vol.Schema(
1111
{vol.Optional(CONF_ALLOW_ALL_IMPORTS, default=False): bool}, extra=vol.ALLOW_EXTRA,
@@ -38,20 +38,15 @@ async def async_step_import(self, import_config: Dict[str, Any] = None) -> None:
3838
entry = entries[0]
3939
updated_data = entry.data.copy()
4040

41-
# Update "allow_all_imports" if it has been changed
42-
if entry.data.get(CONF_ALLOW_ALL_IMPORTS, False) != import_config.get(
43-
CONF_ALLOW_ALL_IMPORTS, False
44-
):
45-
updated_data[CONF_ALLOW_ALL_IMPORTS] = import_config.get(CONF_ALLOW_ALL_IMPORTS, False)
41+
# Update key if it's value has been changed
42+
for key in CONF_ALL_KEYS:
43+
if entry.data.get(key) != import_config.get(key):
44+
if import_config.get(key) is not None:
45+
updated_data[key] = import_config[key]
46+
else:
47+
updated_data.pop(key)
4648

47-
# Update "apps" if it has been changed
48-
if entry.data.get("apps") != import_config.get("apps"):
49-
if import_config.get("apps"):
50-
updated_data["apps"] = import_config["apps"]
51-
else:
52-
updated_data.pop("apps")
53-
54-
# Update and reload entry
49+
# Update and reload entry if data needs to be updated
5550
if updated_data != entry.data:
5651
self.hass.config_entries.async_update_entry(entry=entry, data=updated_data)
5752
await self.hass.config_entries.async_reload(entry.entry_id)

custom_components/pyscript/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
FOLDER = "pyscript"
66

77
CONF_ALLOW_ALL_IMPORTS = "allow_all_imports"
8+
CONF_ALL_KEYS = [CONF_ALLOW_ALL_IMPORTS, "apps"]
89

910
SERVICE_JUPYTER_KERNEL_START = "jupyter_kernel_start"
1011

0 commit comments

Comments
 (0)