Skip to content

Commit 9d702fa

Browse files
dzerikclaude
andcommitted
feat: custom sidebar panel + deploy_automation service
SmartChain AI panel — sidebar page with two tabs: - Generate Automation: describe in natural language → preview YAML → deploy - Analyze Camera: select camera → enter prompt → get AI analysis New service `smartchain.deploy_automation`: - Accepts raw automation YAML - Writes to .storage/automations + triggers reload - Used by panel for deploying generated automations Panel uses HA custom panel API (web component with hass object). Registration is graceful — panel is skipped if frontend is unavailable. 128 tests (+3 new for deploy_automation service), lint clean. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d1c0483 commit 9d702fa

7 files changed

Lines changed: 550 additions & 3 deletions

File tree

custom_components/smartchain/__init__.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import base64
44
import logging
55
import uuid
6+
from pathlib import Path
67

78
import voluptuous as vol
89
import yaml
910
from homeassistant.components.camera import async_get_image
11+
from homeassistant.components.frontend import async_register_built_in_panel
1012
from homeassistant.config_entries import ConfigEntry
1113
from homeassistant.const import Platform
1214
from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, SupportsResponse
@@ -93,6 +95,13 @@ def _resolve_client_args(options: dict) -> dict:
9395
}
9496
)
9597

98+
SERVICE_DEPLOY_AUTOMATION = "deploy_automation"
99+
SERVICE_DEPLOY_AUTOMATION_SCHEMA = vol.Schema(
100+
{
101+
vol.Required("automation_yaml"): str,
102+
}
103+
)
104+
96105
AUTOMATIONS_STORAGE_KEY = "automations"
97106
AUTOMATIONS_STORAGE_VERSION = 1
98107

@@ -281,6 +290,31 @@ async def _handle_generate_automation(call: ServiceCall) -> ServiceResponse:
281290

282291
return response
283292

293+
# Register sidebar panel (graceful — skip if frontend not available)
294+
try:
295+
panel_dir = Path(__file__).parent / "panel"
296+
from homeassistant.components.http import StaticPathConfig
297+
298+
panel_path = str(panel_dir / "smartchain-panel.js")
299+
await hass.http.async_register_static_paths(
300+
[StaticPathConfig("/smartchain/panel.js", panel_path, False)]
301+
)
302+
async_register_built_in_panel(
303+
hass,
304+
component_name="custom",
305+
sidebar_title="SmartChain AI",
306+
sidebar_icon="mdi:robot",
307+
frontend_url_path="smartchain",
308+
config={
309+
"_panel_custom": {
310+
"name": "smartchain-panel",
311+
"module_url": "/smartchain/panel.js",
312+
}
313+
},
314+
)
315+
except Exception:
316+
LOGGER.debug("Could not register SmartChain panel (frontend not available)")
317+
284318
# Store helpers for use by Options Flow wizard
285319
hass.data.setdefault(DOMAIN, {})
286320
hass.data[DOMAIN]["generate_yaml"] = _generate_automation_yaml
@@ -308,6 +342,24 @@ async def _handle_generate_automation(call: ServiceCall) -> ServiceResponse:
308342
schema=SERVICE_GENERATE_AUTOMATION_SCHEMA,
309343
supports_response=SupportsResponse.ONLY,
310344
)
345+
346+
async def _handle_deploy_automation(call: ServiceCall) -> ServiceResponse:
347+
"""Handle smartchain.deploy_automation — deploy raw YAML to HA."""
348+
yaml_text = call.data["automation_yaml"]
349+
try:
350+
result = await _deploy_automation_to_ha(yaml_text)
351+
return result
352+
except Exception as err:
353+
LOGGER.exception("SmartChain deploy_automation error: %s", err)
354+
return {"error": str(err)}
355+
356+
hass.services.async_register(
357+
DOMAIN,
358+
SERVICE_DEPLOY_AUTOMATION,
359+
_handle_deploy_automation,
360+
schema=SERVICE_DEPLOY_AUTOMATION_SCHEMA,
361+
supports_response=SupportsResponse.ONLY,
362+
)
311363
return True
312364

313365

custom_components/smartchain/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"langchain-ollama>=0.3.0,<1",
2020
"yandexcloud==0.295.0"
2121
],
22-
"version": "2.3.0"
22+
"version": "2.4.0"
2323
}

0 commit comments

Comments
 (0)