{"skill":{"slug":"ha-integration-patterns","displayName":"Ha Integration Patterns","summary":"Home Assistant custom integration patterns and architectural decisions. Use when building HACS integrations, custom components, or API bridges for Home Assistant. Covers service response data, HTTP views, storage APIs, and integration architecture.","description":"---\nname: ha-integration-patterns\ndescription: Home Assistant custom integration patterns and architectural decisions. Use when building HACS integrations, custom components, or API bridges for Home Assistant. Covers service response data, HTTP views, storage APIs, and integration architecture.\n---\n\n# Home Assistant Integration Patterns\n\n## Service Response Data Pattern\n\n### The Problem\nBy default, HA services are \"fire-and-forget\" and return empty arrays `[]`.\n\n### The Solution (HA 2023.7+)\nRegister service with `supports_response`:\n\n```python\nfrom homeassistant.helpers.service import SupportsResponse\n\nhass.services.async_register(\n    domain, \n    \"get_full_config\", \n    handle_get_full_config,\n    schema=GET_CONFIG_SCHEMA,\n    supports_response=SupportsResponse.ONLY,  # ← KEY PARAMETER\n)\n```\n\nCall with `?return_response` flag:\n```bash\ncurl -X POST \"$HA_URL/api/services/your_domain/get_full_config?return_response\"\n```\n\n### Response Handler\n```python\nasync def handle_get_full_config(hass: HomeAssistant, call: ServiceCall):\n    \"\"\"Handle the service call and return data.\"\"\"\n    # ... your logic ...\n    return {\"entities\": entity_data, \"automations\": automation_data}\n```\n\n---\n\n## HTTP View vs Service: When to Use Each\n\n| Use Case | Use | Don't Use |\n|----------|-----|-----------|\n| Return complex data | HTTP View | Service (without response support) |\n| Fire-and-forget actions | Service | HTTP View |\n| Trigger automations | Service | HTTP View |\n| Query state/config | HTTP View | Internal storage APIs |\n\n### HTTP View Pattern\nFor data retrieval APIs:\n\n```python\nfrom homeassistant.components.http import HomeAssistantView\n\nclass OpenClawConfigView(HomeAssistantView):\n    \"\"\"HTTP view for retrieving config.\"\"\"\n    url = \"/api/openclaw/config\"\n    name = \"api:openclaw:config\"\n    requires_auth = True\n\n    async def get(self, request):\n        hass = request.app[\"hass\"]\n        config = await get_config_data(hass)\n        return json_response(config)\n\n# Register in async_setup:\nhass.http.register_view(OpenClawConfigView())\n```\n\n---\n\n## Critical: Avoid Internal APIs\n\n**Never use underscore-prefixed APIs** — they're private and change between versions.\n\n❌ **Wrong:**\n```python\nstorage_collection = hass.data[\"_storage_collection\"]\n```\n\n✅ **Right:**\n```python\n# Use public APIs only\nfrom homeassistant.helpers.storage import Store\nstore = Store(hass, STORAGE_VERSION, STORAGE_KEY)\n```\n\n---\n\n## Storage Patterns\n\n### For Small Data (Settings, Cache)\n```python\nfrom homeassistant.helpers.storage import Store\n\nSTORAGE_KEY = \"your_domain.storage\"\nSTORAGE_VERSION = 1\n\nstore = Store(hass, STORAGE_VERSION, STORAGE_KEY)\n\n# Save\ndata = {\"entities\": modified_entities}\nawait store.async_save(data)\n\n# Load\ndata = await store.async_load()\n```\n\n### For Large Data (History, Logs)\nUse external database or file storage, not HA storage helpers.\n\n---\n\n## Breaking Changes to Watch\n\n| Change | Version | Migration |\n|--------|---------|-----------|\n| Conversation agents | 2025.x+ | Use `async_process` directly |\n| Service response data | 2023.7+ | Add `supports_response` param |\n| Config entry migration | 2022.x+ | Use `async_migrate_entry` |\n\n**Always check:** https://www.home-assistant.io/blog/ for your target version range.\n\n---\n\n## HACS Integration Structure\n\n```\ncustom_components/your_domain/\n├── __init__.py          # async_setup_entry\n├── config_flow.py       # UI configuration\n├── manifest.json        # Dependencies, version\n├── services.yaml        # Service definitions\n└── storage_services.py  # Your storage logic\n```\n\n### Minimal manifest.json\n```json\n{\n  \"domain\": \"your_domain\",\n  \"name\": \"Your Integration\",\n  \"codeowners\": [\"@yourusername\"],\n  \"config_flow\": true,\n  \"dependencies\": [],\n  \"requirements\": [],\n  \"version\": \"1.0.0\"\n}\n```\n\n---\n\n## Testing Checklist\n\n- [ ] Service calls return expected data (with `?return_response`)\n- [ ] HTTP views accessible with auth token\n- [ ] No underscore-prefixed API usage\n- [ ] Storage persists across restarts\n- [ ] Config flow creates config entry\n- [ ] Error handling returns meaningful messages\n\n---\n\n## Documentation Resources\n\n- Integration basics: `developers.home-assistant.io/docs/creating_integration_index`\n- Service calls: `developers.home-assistant.io/docs/dev_101_services`\n- HTTP views: `developers.home-assistant.io/docs/api/webserver`\n- Breaking changes: `home-assistant.io/blog/` (filter by version)\n- HACS guidelines: `hacs.xyz/docs/publish/start`\n\n---\n\n## Lesson Learned\n\nFrom HA-OpenClaw Bridge attempt:\n\n> *\"80% of our issues were discoverable with 30-60 minutes of upfront docs reading. We jumped straight to coding based on assumptions rather than reading how HA actually works.\"*\n\nUse `skills/pre-coding-research/` methodology before starting.\n","topics":["Home"],"tags":{"latest":"1.0.0"},"stats":{"comments":0,"downloads":1467,"installsAllTime":55,"installsCurrent":1,"stars":0,"versions":1},"createdAt":1770772297362,"updatedAt":1778487820503},"latestVersion":{"version":"1.0.0","createdAt":1770772297362,"changelog":"Initial release of Home Assistant Integration Patterns documentation.\n\n- Provides architectural guidance for Home Assistant custom integrations, HACS components, and API bridges.\n- Explains how to return data from services with the new `supports_response` parameter.\n- Details when to use HTTP views versus services, with code samples.\n- Highlights safe use of storage APIs, avoiding internal/private APIs.\n- Includes HACS integration file structure and minimal `manifest.json` example.\n- Features a practical testing checklist and documentation links.\n- Summarizes key lessons learned from real integration scenarios.","license":null},"metadata":null,"owner":{"handle":"usimic","userId":"s170s83vzx6bv581ecgev8a9g18855w6","displayName":"usimic","image":"https://avatars.githubusercontent.com/u/66952391?v=4"},"moderation":null}