Skip to content

Commit 09e49fc

Browse files
dawidclaude
andcommitted
Add comprehensive Claude Code Addon for Home Assistant
FEATURES: - πŸ€– Modern web interface for Claude chat directly in HA - πŸ”— Automatic MCP server integration for file access - πŸ“± Responsive design with glassmorphism UI - ⚑ Real-time WebSocket communication - πŸ›‘οΈ Enterprise security (rate limiting, validation, helmet) - 🎨 Quick action buttons for common HA tasks TECHNICAL: - Express.js + Socket.io server - Anthropic SDK integration - Home Assistant Ingress support - Container-optimized with health checks - Comprehensive error handling and logging - Token usage tracking and optimization STRUCTURE: - config.yaml - HA addon configuration - Dockerfile - Multi-stage container build - src/server.js - Main Node.js application - src/public/index.html - Modern chat interface - run.sh - Startup script with validation - README.md - Complete documentation USAGE: 1. Install addon in HA 2. Configure Anthropic API key 3. Access via Ingress: HA β†’ Add-ons β†’ Claude Code CLI 4. Chat with Claude about HA configuration The addon automatically connects to the existing MCP server for direct Home Assistant file system access. πŸ€– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 596b18a commit 09e49fc

File tree

7 files changed

+1168
-0
lines changed

7 files changed

+1168
-0
lines changed

β€Žclaude-code-addon/Dockerfileβ€Ž

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
ARG BUILD_FROM
2+
FROM $BUILD_FROM
3+
4+
# Install Node.js, npm and dependencies
5+
RUN apk add --no-cache \
6+
nodejs \
7+
npm \
8+
bash \
9+
python3 \
10+
py3-yaml \
11+
curl \
12+
git \
13+
&& rm -rf /var/cache/apk/*
14+
15+
# Create app user
16+
RUN addgroup -g 1000 appuser && \
17+
adduser -D -s /bin/bash -u 1000 -G appuser appuser
18+
19+
# Create app directory
20+
WORKDIR /app
21+
22+
# Copy package.json and install dependencies
23+
COPY src/package.json /app/package.json
24+
RUN npm install --production && \
25+
chown -R appuser:appuser /app
26+
27+
# Copy application files
28+
COPY src/ /app/
29+
COPY run.sh /run.sh
30+
31+
# Set permissions
32+
RUN chmod +x /run.sh && \
33+
chown -R appuser:appuser /app
34+
35+
# Labels
36+
ARG BUILD_ARCH
37+
ARG BUILD_DATE
38+
ARG BUILD_DESCRIPTION
39+
ARG BUILD_NAME
40+
ARG BUILD_REF
41+
ARG BUILD_REPOSITORY
42+
ARG BUILD_VERSION
43+
44+
LABEL \
45+
io.hass.name="${BUILD_NAME}" \
46+
io.hass.description="${BUILD_DESCRIPTION}" \
47+
io.hass.arch="${BUILD_ARCH}" \
48+
io.hass.type="addon" \
49+
io.hass.version="${BUILD_VERSION}" \
50+
maintainer="DawidSu" \
51+
org.opencontainers.image.title="${BUILD_NAME}" \
52+
org.opencontainers.image.description="${BUILD_DESCRIPTION}" \
53+
org.opencontainers.image.vendor="Home Assistant Community Add-ons" \
54+
org.opencontainers.image.authors="DawidSu" \
55+
org.opencontainers.image.licenses="MIT" \
56+
org.opencontainers.image.url="https://github.com/DawidSu/ha-mcp-server" \
57+
org.opencontainers.image.source="https://github.com/DawidSu/ha-mcp-server" \
58+
org.opencontainers.image.documentation="https://github.com/DawidSu/ha-mcp-server/blob/main/README.md" \
59+
org.opencontainers.image.created="${BUILD_DATE}" \
60+
org.opencontainers.image.revision="${BUILD_REF}" \
61+
org.opencontainers.image.version="${BUILD_VERSION}"
62+
63+
# Health check
64+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
65+
CMD curl -f http://localhost:8080/health || exit 1
66+
67+
# Switch to app user
68+
USER appuser
69+
70+
# Expose port
71+
EXPOSE 8080
72+
73+
CMD [ "/run.sh" ]

β€Žclaude-code-addon/README.mdβ€Ž

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Claude Code Addon fΓΌr Home Assistant
2+
3+
Ein vollwertiges **Claude Code Web-Interface** direkt in Home Assistant! Chatte mit Claude ΓΌber eine moderne Web-OberflΓ€che und nutze dabei die volle Power des MCP (Model Context Protocol) Servers fΓΌr direkten Zugriff auf deine Home Assistant Konfiguration.
4+
5+
## 🌟 Features
6+
7+
- **πŸ€– Claude Chat Interface** - Moderne Web-OberflΓ€che fΓΌr Claude Conversations
8+
- **πŸ”— MCP Integration** - Automatische Verbindung zum MCP Server fΓΌr Dateizugriff
9+
- **🏠 Home Assistant Integration** - LÀuft als Addon mit Ingress-Unterstützung
10+
- **πŸ“± Responsive Design** - Funktioniert auf Desktop, Tablet und Smartphone
11+
- **⚑ Echtzeit-Features** - WebSocket-Verbindung für Live-Status
12+
- **πŸ›‘οΈ Sicherheit** - Rate Limiting, Input Validation, Helmet Security Headers
13+
- **🎨 Moderne UI** - Gradient-Design mit Glassmorphism-Effekten
14+
15+
## πŸ“‹ Installation
16+
17+
### 1. Voraussetzungen
18+
- **Claude MCP Server Addon** muss installiert und aktiv sein
19+
- **Anthropic API Key** (kostenlos bei anthropic.com)
20+
21+
### 2. Addon Installation
22+
1. FΓΌge das Repository zu Home Assistant hinzu
23+
2. Installiere das "Claude Code CLI" Addon
24+
3. Konfiguriere den Anthropic API Key
25+
4. Starte das Addon
26+
27+
### 3. Konfiguration
28+
29+
```yaml
30+
anthropic_api_key: "sk-ant-api03-..." # Dein API Key von anthropic.com
31+
model: "claude-3-5-sonnet-20241022" # Claude Modell
32+
max_tokens: 4096 # Maximale Token pro Response
33+
auto_connect_mcp: true # Automatische MCP Verbindung
34+
mcp_server_host: "localhost" # MCP Server Host
35+
mcp_server_port: 3000 # MCP Server Port
36+
log_level: "info" # Log Level
37+
```
38+
39+
## πŸš€ Nutzung
40+
41+
### Web-Interface
42+
1. Γ–ffne das Addon ΓΌber **Home Assistant β†’ Einstellungen β†’ Add-ons β†’ Claude Code CLI**
43+
2. Klicke auf "OPEN WEB UI" oder nutze das Ingress-Interface
44+
3. Beginne zu chatten!
45+
46+
### Beispiel-Prompts
47+
```
48+
"Zeige mir meine Home Assistant Konfiguration"
49+
"Erstelle eine Automation fΓΌr den Flur-Bewegungsmelder"
50+
"Analysiere meine Automationen auf Fehler"
51+
"Schreibe ein Script fΓΌr den Gute-Nacht-Modus"
52+
"Optimiere meine configuration.yaml"
53+
```
54+
55+
### Quick Actions
56+
Das Interface bietet vorgefertigte Buttons fΓΌr hΓ€ufige Aufgaben:
57+
- πŸ“ **Konfiguration anzeigen**
58+
- ⚑ **Automation erstellen**
59+
- πŸ” **Fehlercheck**
60+
- πŸŒ™ **Script erstellen**
61+
62+
## πŸ”§ Erweiterte Konfiguration
63+
64+
### API Key erhalten
65+
1. Gehe zu [anthropic.com](https://console.anthropic.com)
66+
2. Erstelle einen Account
67+
3. Generiere einen API Key
68+
4. Key beginnt mit `sk-ant-api03-...`
69+
70+
### MCP Server Verbindung
71+
Das Addon verbindet sich automatisch mit dem Claude MCP Server:
72+
- **Host**: `localhost` (Standard)
73+
- **Port**: `3000` (Standard)
74+
- **Auto-Connect**: `true` (Standard)
75+
76+
### Modell-Auswahl
77+
VerfΓΌgbare Claude Modelle:
78+
- `claude-3-5-sonnet-20241022` (Empfohlen - Balance aus Speed und QualitΓ€t)
79+
- `claude-3-5-haiku-20241022` (Schnell und gΓΌnstig)
80+
- `claude-3-opus-20240229` (HΓΆchste QualitΓ€t, langsamer)
81+
82+
## πŸ›‘οΈ Sicherheit
83+
84+
- **Rate Limiting**: 10 Requests pro Minute pro IP
85+
- **Input Validation**: NachrichtenlΓ€nge und Format werden validiert
86+
- **Helmet Security**: HTTP Security Headers aktiviert
87+
- **API Key Schutz**: Keys werden nicht in Logs angezeigt
88+
- **CORS Schutz**: Nur erlaubte Origins kΓΆnnen zugreifen
89+
90+
## πŸ” Troubleshooting
91+
92+
### Claude nicht verfΓΌgbar
93+
```
94+
Problem: "Claude ist nicht verfΓΌgbar"
95+
LΓΆsung: API Key in Addon-Einstellungen prΓΌfen
96+
```
97+
98+
### MCP Verbindung fehlgeschlagen
99+
```
100+
Problem: "MCP server not reachable"
101+
LΓΆsung: Claude MCP Server Addon starten
102+
```
103+
104+
### Performance-Probleme
105+
```
106+
Problem: Langsame Responses
107+
LΓΆsung:
108+
- Zu claude-3-5-haiku wechseln
109+
- max_tokens reduzieren
110+
- Conversation History kΓΌrzen
111+
```
112+
113+
### Container startet nicht
114+
```
115+
bash
116+
# Logs prΓΌfen:
117+
docker logs addon_local_claude-code-cli
118+
119+
# HΓ€ufige Ursachen:
120+
- UngΓΌltiger API Key Format
121+
- Port 8080 bereits belegt
122+
- Node.js Dependencies fehlen
123+
```
124+
125+
## πŸ“Š Status & Monitoring
126+
127+
Das Interface zeigt Live-Status an:
128+
- **🟒 Claude**: Anthropic API verfügbar
129+
- **🟒 MCP Server**: Verbindung zum MCP Server
130+
- **Model Info**: Aktuell verwendetes Claude Modell
131+
132+
## πŸ”„ Updates
133+
134+
Das Addon updated sich automatisch ΓΌber Home Assistant:
135+
1. Neue Version wird in Add-ons angezeigt
136+
2. "Update" Button klicken
137+
3. Restart erfolgt automatisch
138+
139+
## πŸ’‘ Tipps & Tricks
140+
141+
### Effiziente Prompts
142+
- **Spezifisch sein**: "Erstelle Automation fΓΌr KΓΌche" statt "Hilfe"
143+
- **Kontext geben**: "Analysiere automation.yaml auf Syntaxfehler"
144+
- **Schrittweise**: Große Aufgaben in kleine Teile aufteilen
145+
146+
### Token sparen
147+
- **Kurze Messages**: Lange Conversations verbrauchen mehr Token
148+
- **Clear Context**: "Vergiss vorherige Conversation" fΓΌr neue Themen
149+
- **Haiku Model**: FΓΌr einfache Fragen claude-3-5-haiku nutzen
150+
151+
### Bessere Results
152+
- **Beispiele geben**: "Erstelle Automation wie diese: [YAML]"
153+
- **Format spezifizieren**: "Als YAML Code ausgeben"
154+
- **Validation**: "PrΓΌfe den Code auf Syntax-Fehler"
155+
156+
## πŸ†˜ Support
157+
158+
Bei Problemen:
159+
1. **Logs checken**: Home Assistant β†’ Add-ons β†’ Claude Code CLI β†’ Logs
160+
2. **Issue erstellen**: [GitHub Issues](https://github.com/DawidSu/ha-mcp-server/issues)
161+
3. **Community**: Home Assistant Community Forum
162+
163+
---
164+
165+
**Viel Spaß mit Claude Code in Home Assistant! πŸŽ‰**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Claude Code CLI
2+
version: "1.0.0"
3+
slug: claude-code-cli
4+
description: Claude Code CLI direkt in Home Assistant - Chat mit Claude ΓΌber Web-Interface
5+
url: https://github.com/DawidSu/ha-mcp-server
6+
arch:
7+
- armhf
8+
- armv7
9+
- aarch64
10+
- amd64
11+
- i386
12+
startup: services
13+
boot: manual
14+
init: false
15+
hassio_api: true
16+
hassio_role: manager
17+
ingress: true
18+
ingress_port: 8080
19+
panel_icon: mdi:robot
20+
panel_title: Claude Code
21+
ports:
22+
8080/tcp: 8080
23+
ports_description:
24+
8080/tcp: Claude Code Web Interface
25+
map:
26+
- config:rw
27+
- addons_local:rw
28+
options:
29+
anthropic_api_key: ""
30+
model: "claude-3-5-sonnet-20241022"
31+
max_tokens: 4096
32+
auto_connect_mcp: true
33+
mcp_server_host: "localhost"
34+
mcp_server_port: 3000
35+
log_level: "info"
36+
schema:
37+
anthropic_api_key: password
38+
model: list(claude-3-5-sonnet-20241022|claude-3-5-haiku-20241022|claude-3-opus-20240229)?
39+
max_tokens: int(1000,8000)?
40+
auto_connect_mcp: bool?
41+
mcp_server_host: str?
42+
mcp_server_port: port?
43+
log_level: list(debug|info|warning|error)?

β€Žclaude-code-addon/run.shβ€Ž

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/with-contenv bashio
2+
set -euo pipefail
3+
4+
# Get configuration from addon options
5+
API_KEY=$(bashio::config 'anthropic_api_key')
6+
MODEL=$(bashio::config 'model')
7+
MAX_TOKENS=$(bashio::config 'max_tokens')
8+
AUTO_CONNECT_MCP=$(bashio::config 'auto_connect_mcp')
9+
MCP_HOST=$(bashio::config 'mcp_server_host')
10+
MCP_PORT=$(bashio::config 'mcp_server_port')
11+
LOG_LEVEL=$(bashio::config 'log_level')
12+
13+
# Validate configuration
14+
if [ -z "$API_KEY" ]; then
15+
bashio::log.warning "Kein Anthropic API Key konfiguriert!"
16+
bashio::log.warning "Bitte API Key in den Addon-Einstellungen hinzufΓΌgen."
17+
bashio::log.warning "Claude FunktionalitΓ€t ist deaktiviert bis API Key gesetzt ist."
18+
fi
19+
20+
# Export environment variables for Node.js app
21+
export ANTHROPIC_API_KEY="$API_KEY"
22+
export ADDON_OPTIONS=$(bashio::addon.options)
23+
export NODE_ENV=production
24+
export PORT=8080
25+
26+
# Log configuration (without exposing API key)
27+
bashio::log.info "Starting Claude Code Addon..."
28+
bashio::log.info "Model: $MODEL"
29+
bashio::log.info "Max Tokens: $MAX_TOKENS"
30+
bashio::log.info "MCP Auto Connect: $AUTO_CONNECT_MCP"
31+
bashio::log.info "MCP Server: $MCP_HOST:$MCP_PORT"
32+
bashio::log.info "Log Level: $LOG_LEVEL"
33+
34+
# Validate API key format (without logging it)
35+
if [ -n "$API_KEY" ]; then
36+
if echo "$API_KEY" | grep -q "^sk-ant-"; then
37+
bashio::log.info "βœ… API Key format appears valid"
38+
else
39+
bashio::log.warning "⚠️ API Key format may be invalid (should start with 'sk-ant-')"
40+
fi
41+
fi
42+
43+
# Check if MCP server is reachable if auto-connect is enabled
44+
if [ "$AUTO_CONNECT_MCP" = "true" ]; then
45+
bashio::log.info "Testing MCP server connection..."
46+
if timeout 5 bash -c "</dev/tcp/$MCP_HOST/$MCP_PORT" 2>/dev/null; then
47+
bashio::log.info "βœ… MCP server is reachable at $MCP_HOST:$MCP_PORT"
48+
else
49+
bashio::log.warning "⚠️ MCP server not reachable at $MCP_HOST:$MCP_PORT"
50+
bashio::log.warning "Make sure the Claude MCP Server addon is running"
51+
fi
52+
fi
53+
54+
# Change to app directory
55+
cd /app
56+
57+
# Check if dependencies are installed
58+
if [ ! -d "node_modules" ]; then
59+
bashio::log.error "Node.js dependencies not found!"
60+
bashio::log.error "Container may not have built correctly"
61+
exit 1
62+
fi
63+
64+
# Verify Node.js
65+
if ! node --version >/dev/null 2>&1; then
66+
bashio::log.error "Node.js not available!"
67+
exit 1
68+
fi
69+
70+
bashio::log.info "Node.js version: $(node --version)"
71+
72+
# Set up signal handling for graceful shutdown
73+
cleanup() {
74+
bashio::log.info "Shutting down Claude Code Addon..."
75+
# Kill any background processes
76+
pkill -f "node.*server.js" 2>/dev/null || true
77+
exit 0
78+
}
79+
80+
trap cleanup SIGTERM SIGINT
81+
82+
# Start the application
83+
bashio::log.info "Starting Claude Code web interface on port 8080..."
84+
bashio::log.info "Access via Home Assistant Ingress"
85+
86+
# Run the Node.js server
87+
exec node server.js

0 commit comments

Comments
Β (0)