ha-ai-learner
A self-learning discovery tool + MCP server that turns your Home Assistant into knowledge an AI assistant can actually use.
README
HA-AI Learner
A self-learning discovery tool + MCP server that turns your Home Assistant into knowledge an AI assistant can actually use.
Magyarul: README.hu.md
AI assistants are great at reasoning about your smart home — but they know nothing about it. Which entity is the kitchen light? What does switch.sonoff_3 actually switch? What services can be called, and when do you usually turn things on?
HA-AI Learner answers this once, persistently, and keeps it fresh:
- Discovers everything through the Home Assistant REST + WebSocket APIs: entities, devices, areas/floors, labels, services, integrations, and installed HACS add-ons.
- Builds a knowledge base as human-readable Markdown (
knowledge.md) and machine-readable JSON (knowledge.json). - Tracks changes — every re-scan produces a diff (new/removed/renamed/moved entities, new HACS add-ons…) appended to
changes.log. - Asks when it doesn't understand — entities with generated names (
switch.sonoff_3), no area, or duplicate names get clarifying questions; your answers merge into the knowledge base permanently. - Learns usage patterns from history: active hours, typical switch-on times, and entities that activate together (candidate routines).
- Serves it all over MCP — Claude (Desktop / Code / Cowork) or any MCP client can search entities, inspect services, ask you the pending questions, trigger a re-scan, read live state, and (only if you enable it) control devices.
┌─────────────────┐ REST + WebSocket ┌──────────────────┐
│ Home Assistant │ ◄──────────────────► │ ha-learner │
│ (your home) │ │ scan · diff · │
└─────────────────┘ │ interview · │
│ patterns │
└────────┬─────────┘
│ writes
┌─────────────▼─────────────┐
│ ~/.ha-ai-learner/ │
│ knowledge.md / .json │
│ answers · questions │
│ patterns · changes.log │
└─────────────┬─────────────┘
│ serves (stdio)
┌────────▼─────────┐
│ MCP server │ ◄── Claude Desktop,
│ 13 tools │ Claude Code, …
└──────────────────┘
Installation
Runs on any machine that can reach your HA instance on the local network (including the HA host itself). Python 3.10+.
git clone https://github.com/buppy333/ha-ai-learner.git
cd ha-ai-learner
pip install -e .
Setup
- Create a Home Assistant long-lived access token: HA UI → your profile (bottom left) → Security tab → Long-lived access tokens → Create token.
- Copy
config.example.yamltoconfig.yaml(or~/.config/ha-ai-learner/config.yaml) and fill in your URL + token.
Environment variables work too: HA_URL, HA_TOKEN, HA_LEARNER_DATA_DIR, HA_LEARNER_ALLOW_CALLS.
Usage
ha-learner scan # one full discovery scan + knowledge base build
ha-learner scan --patterns # + learn usage patterns from history
ha-learner watch # periodic scanning (default: every 60 minutes)
ha-learner interview # interactive Q&A about unclear entities
ha-learner questions # list pending questions (JSON)
ha-learner answer switch.sonoff_3 --location "Kitchen" --purpose "coffee machine"
ha-learner find "living room lamp"
ha-learner summary
ha-learner mcp # run the MCP server (stdio)
The knowledge base is written to ~/.ha-ai-learner/ by default:
| File | Contents |
|---|---|
knowledge.md |
The whole home as readable Markdown — areas, entities, devices, services, integrations, HACS, patterns |
knowledge.json |
Same, machine-readable |
answers.json |
Your answers from the interview flow (permanent) |
questions.json |
Currently pending clarifying questions |
patterns.json |
Learned usage patterns |
changes.log |
Timestamped diff log of every scan |
Scheduled scanning
- On a Linux/HA box:
ha-learner watch(as a systemd service or under tmux), or cron:0 * * * * ha-learner scan --patterns - An AI client can also refresh on demand via the
rescanMCP tool.
Connecting to Claude Desktop
Add to claude_desktop_config.json (Settings → Developer → Edit Config) — see examples/claude_desktop_config.example.json:
{
"mcpServers": {
"ha-ai-learner": {
"command": "ha-learner",
"args": ["mcp"],
"env": {
"HA_URL": "http://homeassistant.local:8123",
"HA_TOKEN": "YOUR_TOKEN"
}
}
}
}
For Claude Code: claude mcp add ha-ai-learner -- ha-learner mcp
MCP tools exposed: home_summary, search_entities, get_entity, list_areas, list_services, get_service, usage_patterns, pending_questions, answer_question, rescan, recent_changes, get_live_state, call_service.
The "identify by asking" loop
This is the feature the project is named after. The learner flags entities it cannot confidently identify — no area, auto-generated name, duplicate names. Your AI assistant calls pending_questions, asks you in plain language ("Which room is switch.sonoff_3 in?"), and stores your reply with answer_question. From then on the knowledge base knows — permanently, across rescans.
Security
- Read-only by default. The
call_servicetool (device control) refuses to run unless you setallow_service_calls: truein the config (orHA_LEARNER_ALLOW_CALLS=1). - Your token lives in your local config file — never commit it.
config.yamlis in.gitignore. - The MCP server runs over stdio on your machine; nothing is exposed to the network beyond the calls to your own HA instance.
Testing
A full end-to-end test runs against a bundled mock HA server — no live Home Assistant needed:
python3 tests/test_e2e.py
It covers scanning, knowledge base generation, change detection, the interview flow, search, and pattern learning. The mock data intentionally uses accented Hungarian entity names to keep everything Unicode-safe.
How it works, briefly
scanner.pypulls states/services/config over REST and the entity/device/area/floor/label registries + config entries + HACS repositories over WebSocket, and normalizes them into one snapshot.miniws.pyis a minimal, dependency-free RFC 6455 WebSocket client — no extra WS library needed.knowledge.pypersists snapshots, computes human-readable diffs, merges in your answers and learned patterns, and renders the Markdown/JSON knowledge base.interview.pyscores entities for "unclarity" (regexes for generated names, area/duplicate checks) and manages the question/answer lifecycle.patterns.pymines history for per-entity activity histograms and 5-minute co-activation pairs → candidate routines.mcp_server.pyexposes it all as MCP tools via FastMCP.
Roadmap
- Entity-graph export (which automations touch which entities)
- Optional local vector index for semantic search
- HACS packaging for one-click install
- Logbook-based pattern mining (in addition to history)
Contributions welcome — see CONTRIBUTING.md.
License
MIT © Tamás Pénzes
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。