Skip to content

Commit dd555d6

Browse files
committed
fix access to config data now that we are using config flow
1 parent e4317d8 commit dd555d6

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

custom_components/pyscript/__init__.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import voluptuous as vol
99

10-
from homeassistant.config import async_hass_config_yaml, async_process_component_config
10+
from homeassistant.config import async_hass_config_yaml
1111
from homeassistant.config_entries import SOURCE_IMPORT
1212
from homeassistant.const import (
1313
EVENT_HOMEASSISTANT_STARTED,
@@ -17,7 +17,7 @@
1717
)
1818
from homeassistant.exceptions import HomeAssistantError
1919
import homeassistant.helpers.config_validation as cv
20-
from homeassistant.loader import async_get_integration, bind_hass
20+
from homeassistant.loader import bind_hass
2121

2222
from .const import CONF_ALLOW_ALL_IMPORTS, DOMAIN, FOLDER, LOGGER_PATH, SERVICE_JUPYTER_KERNEL_START
2323
from .eval import AstEval
@@ -81,11 +81,14 @@ async def reload_scripts_handler(call):
8181
_LOGGER.error(err)
8282
return
8383

84-
integration = await async_get_integration(hass, DOMAIN)
85-
86-
config = await async_process_component_config(hass, conf, integration)
84+
# If data in config doesn't match config entry, trigger a config import
85+
# so that the config entry can get updated
86+
if DOMAIN in conf and conf[DOMAIN] != config_entry.data:
87+
await hass.config_entries.flow.async_init(
88+
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf[DOMAIN]
89+
)
8790

88-
State.set_pyscript_config(config.get(DOMAIN, {}))
91+
State.set_pyscript_config(config_entry.data)
8992

9093
ctx_delete = {}
9194
for global_ctx_name, global_ctx in GlobalContextMgr.items():
@@ -97,7 +100,7 @@ async def reload_scripts_handler(call):
97100
for global_ctx_name, global_ctx in ctx_delete.items():
98101
await GlobalContextMgr.delete(global_ctx_name)
99102

100-
await load_scripts(hass, config)
103+
await load_scripts(hass, config_entry.data)
101104

102105
for global_ctx_name, global_ctx in GlobalContextMgr.items():
103106
idx = global_ctx_name.find(".")
@@ -188,14 +191,14 @@ async def async_unload_entry(hass, config_entry):
188191

189192

190193
@bind_hass
191-
async def load_scripts(hass, config):
194+
async def load_scripts(hass, data):
192195
"""Load all python scripts in FOLDER."""
193196

194197
pyscript_dir = hass.config.path(FOLDER)
195198

196-
def glob_files(load_paths, config):
199+
def glob_files(load_paths, data):
197200
source_files = []
198-
apps_config = config.get(DOMAIN, {}).get("apps", None)
201+
apps_config = data.get("apps", None)
199202
for path, match, check_config in load_paths:
200203
for this_path in sorted(glob.glob(os.path.join(pyscript_dir, path, match))):
201204
rel_import_path = None
@@ -227,7 +230,7 @@ def glob_files(load_paths, config):
227230
["", "*.py", False],
228231
]
229232

230-
source_files = await hass.async_add_executor_job(glob_files, load_paths, config)
233+
source_files = await hass.async_add_executor_job(glob_files, load_paths, data)
231234
for global_ctx_name, source_file, rel_import_path, fq_mod_name in source_files:
232235
global_ctx = GlobalContext(
233236
global_ctx_name,

custom_components/pyscript/config_flow.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ async def async_step_import(self, import_config: Dict[str, Any] = None) -> None:
3636
entries = self.hass.config_entries.async_entries(DOMAIN)
3737
if entries:
3838
entry = entries[0]
39+
updated_data = entry.data.copy()
40+
41+
# Update "allow_all_imports" if it has been changed
3942
if entry.data.get(CONF_ALLOW_ALL_IMPORTS, False) != import_config.get(
4043
CONF_ALLOW_ALL_IMPORTS, False
4144
):
42-
updated_data = entry.data.copy()
4345
updated_data[CONF_ALLOW_ALL_IMPORTS] = import_config.get(CONF_ALLOW_ALL_IMPORTS, False)
46+
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
55+
if updated_data != entry.data:
4456
self.hass.config_entries.async_update_entry(entry=entry, data=updated_data)
4557
await self.hass.config_entries.async_reload(entry.entry_id)
4658
return self.async_abort(reason="updated_entry")

0 commit comments

Comments
 (0)