Skip to content

Commit 68736f3

Browse files
jrx-codeclaude
andcommitted
fix: v0.20.2 — sync addon config env vars to settings.json (fixes OpenRouter 401)
init_from_env() now syncs SANDBOX_* env vars (API key, provider, model, URL) from run.sh into settings.json on startup. Previously, addon config UI values were exported as env vars but never persisted to settings.json, causing _get_ai_config() to return empty defaults and OpenRouter API calls to fail with 401 Forbidden. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 143d78b commit 68736f3

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

ha-sandbox/app/settings.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ def _apply_to_runtime(data: dict) -> None:
111111

112112

113113
def init_from_env() -> None:
114-
"""On startup, merge env vars into saved settings (env takes precedence for secrets)."""
114+
"""On startup, merge env vars into saved settings (env takes precedence for secrets).
115+
116+
The HA addon config UI sets options that run.sh exports as SANDBOX_* env vars.
117+
These must be synced into settings.json so that settings.load() returns them.
118+
Without this, _get_ai_config() would return empty defaults and API calls fail (401).
119+
"""
115120
import os
116121
data = load()
117122
env_token = os.environ.get("HA_TOKEN", "")
@@ -120,4 +125,22 @@ def init_from_env() -> None:
120125
env_mqtt_pass = os.environ.get("MQTT_PASS", "")
121126
if env_mqtt_pass and not data.get("mqtt_pass"):
122127
data["mqtt_pass"] = env_mqtt_pass
128+
129+
# Sync addon config options (SANDBOX_* env vars from run.sh) into settings.json.
130+
# Env takes precedence over saved settings for these fields, because the user
131+
# sets them in the HA addon config UI and expects them to take effect immediately.
132+
env_map = {
133+
"SANDBOX_AI_PROVIDER": "ai_provider",
134+
"SANDBOX_PUBLIC_PROVIDER": "public_provider",
135+
"SANDBOX_PUBLIC_API_KEY": "public_api_key",
136+
"SANDBOX_PUBLIC_MODEL": "public_model",
137+
"SANDBOX_PUBLIC_URL": "public_url",
138+
"SANDBOX_OLLAMA_URL": "ollama_url",
139+
"SANDBOX_OLLAMA_MODEL": "ollama_model",
140+
}
141+
for env_var, setting_key in env_map.items():
142+
val = os.environ.get(env_var, "")
143+
if val:
144+
data[setting_key] = val
145+
123146
save(data)

ha-sandbox/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "HA Security Sandbox"
2-
version: "0.20.1"
2+
version: "0.20.2"
33
slug: ha_security_sandbox
44
description: "Security scanner for Home Assistant custom components — static analysis + AI review"
55
url: "https://github.com/jrx-code/ha-security-sandbox"

0 commit comments

Comments
 (0)