Get the FREE Ultimate OpenClaw Setup Guide →
u

Ha Integration Patterns

Verified

@usimic

npx machina-cli add skill @usimic/ha-integration-patterns --openclaw
Files (1)
SKILL.md
4.7 KB

Home Assistant Integration Patterns

Service Response Data Pattern

The Problem

By default, HA services are "fire-and-forget" and return empty arrays [].

The Solution (HA 2023.7+)

Register service with supports_response:

from homeassistant.helpers.service import SupportsResponse

hass.services.async_register(
    domain, 
    "get_full_config", 
    handle_get_full_config,
    schema=GET_CONFIG_SCHEMA,
    supports_response=SupportsResponse.ONLY,  # ← KEY PARAMETER
)

Call with ?return_response flag:

curl -X POST "$HA_URL/api/services/your_domain/get_full_config?return_response"

Response Handler

async def handle_get_full_config(hass: HomeAssistant, call: ServiceCall):
    """Handle the service call and return data."""
    # ... your logic ...
    return {"entities": entity_data, "automations": automation_data}

HTTP View vs Service: When to Use Each

Use CaseUseDon't Use
Return complex dataHTTP ViewService (without response support)
Fire-and-forget actionsServiceHTTP View
Trigger automationsServiceHTTP View
Query state/configHTTP ViewInternal storage APIs

HTTP View Pattern

For data retrieval APIs:

from homeassistant.components.http import HomeAssistantView

class OpenClawConfigView(HomeAssistantView):
    """HTTP view for retrieving config."""
    url = "/api/openclaw/config"
    name = "api:openclaw:config"
    requires_auth = True

    async def get(self, request):
        hass = request.app["hass"]
        config = await get_config_data(hass)
        return json_response(config)

# Register in async_setup:
hass.http.register_view(OpenClawConfigView())

Critical: Avoid Internal APIs

Never use underscore-prefixed APIs — they're private and change between versions.

Wrong:

storage_collection = hass.data["_storage_collection"]

Right:

# Use public APIs only
from homeassistant.helpers.storage import Store
store = Store(hass, STORAGE_VERSION, STORAGE_KEY)

Storage Patterns

For Small Data (Settings, Cache)

from homeassistant.helpers.storage import Store

STORAGE_KEY = "your_domain.storage"
STORAGE_VERSION = 1

store = Store(hass, STORAGE_VERSION, STORAGE_KEY)

# Save
data = {"entities": modified_entities}
await store.async_save(data)

# Load
data = await store.async_load()

For Large Data (History, Logs)

Use external database or file storage, not HA storage helpers.


Breaking Changes to Watch

ChangeVersionMigration
Conversation agents2025.x+Use async_process directly
Service response data2023.7+Add supports_response param
Config entry migration2022.x+Use async_migrate_entry

Always check: https://www.home-assistant.io/blog/ for your target version range.


HACS Integration Structure

custom_components/your_domain/
├── __init__.py          # async_setup_entry
├── config_flow.py       # UI configuration
├── manifest.json        # Dependencies, version
├── services.yaml        # Service definitions
└── storage_services.py  # Your storage logic

Minimal manifest.json

{
  "domain": "your_domain",
  "name": "Your Integration",
  "codeowners": ["@yourusername"],
  "config_flow": true,
  "dependencies": [],
  "requirements": [],
  "version": "1.0.0"
}

Testing Checklist

  • Service calls return expected data (with ?return_response)
  • HTTP views accessible with auth token
  • No underscore-prefixed API usage
  • Storage persists across restarts
  • Config flow creates config entry
  • Error handling returns meaningful messages

Documentation Resources

  • Integration basics: developers.home-assistant.io/docs/creating_integration_index
  • Service calls: developers.home-assistant.io/docs/dev_101_services
  • HTTP views: developers.home-assistant.io/docs/api/webserver
  • Breaking changes: home-assistant.io/blog/ (filter by version)
  • HACS guidelines: hacs.xyz/docs/publish/start

Lesson Learned

From HA-OpenClaw Bridge attempt:

"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."

Use skills/pre-coding-research/ methodology before starting.

Source

git clone https://clawhub.ai/usimic/ha-integration-patternsView on GitHub

Overview

Learn reusable patterns for building Home Assistant custom integrations, including service response handling, HTTP views, and storage strategies. It also covers architecture decisions for HACS integrations and API bridges, helping you create maintainable and upgrade-safe components.

How This Skill Works

It summarizes concrete patterns with practical code touchpoints: register services with supports_response for returns, decide when to expose HTTP views for data retrieval, and choose Store for small data while avoiding private internal APIs. It also points to typical project structure and migration notes to keep integrations robust across HA versions.

When to Use It

  • To return structured data from a service using supports_response and the ?return_response flag
  • To expose data retrieval or config data via an HTTP view rather than a fire-and-forget service
  • To persist small settings or caches locally using the Store API
  • To ensure you avoid underscore-prefixed internal APIs and rely on public Home Assistant APIs
  • To plan for breaking changes and migrations when upgrading or evolving a custom integration

Quick Start

  1. Step 1: Identify which data your integration should return from a service and decide if an HTTP view is needed for retrieval
  2. Step 2: Register the service with supports_response and implement the response handler; add an HTTP view if appropriate
  3. Step 3: Add small data storage with Store for settings/cache and review public APIs to avoid private/private APIs

Best Practices

  • Register services with supports_response when you need to return data and test the ?return_response workflow
  • Prefer HTTP views for complex data retrieval and use services for actions that do not return data
  • Avoid internal/private APIs (underscore-prefixed); use public APIs like Store for storage
  • Use Store for small data; for large data consider external databases or file storage
  • Track breaking changes and migrations (e.g., 2023.7+, 2022.x+) and test compatibility across HA versions

Example Use Cases

  • A custom integration that exposes a get_full_config service with return_response demonstrating the Service Response Data Pattern
  • An HTTP view at /api/openclaw/config used to retrieve configuration data via an authenticated HTTP GET
  • Storing user preferences or cache data locally with Store for quick access across restarts
  • A HACS-friendly integration layout showing __init__.py, config_flow.py, manifest.json, and storage_services.py
  • Migration guidance snippet updating a config entry with async_migrate_entry during breaking changes

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers