Skip to content

feat: expose category on automation, script, and helper config tools - #850

Merged
kingpanther13 merged 13 commits into
homeassistant-ai:masterfrom
kingpanther13:feat/config-tool-categories
Apr 5, 2026
Merged

feat: expose category on automation, script, and helper config tools#850
kingpanther13 merged 13 commits into
homeassistant-ai:masterfrom
kingpanther13:feat/config-tool-categories

Conversation

@kingpanther13

@kingpanther13 kingpanther13 commented Apr 1, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Adds category parameter to domain-specific config tools, building on #677's category CRUD foundation. This is the follow-up described in #702 (comment).

Changes

ha_config_get_automation — now includes category in response when set:

  • Resolves entity_id (handles both entity_id and unique_id lookups)
  • Fetches category from entity registry via config/entity_registry/get
  • Best-effort: failure doesn't break the config response

ha_config_set_automation — new category parameter:

  • Extracts category from config dict before REST API call (prevents extra keys not allowed error)
  • Applies category via config/entity_registry/update after successful creation
  • Parameter takes precedence over config dict value
  • Failure to set category warns but doesn't fail the automation creation

ha_config_get_script / ha_config_set_script — same pattern as automations

ha_config_set_helper — adds category alongside existing area_id and labels in the entity registry update

Background

Categories are stored in the entity registry, not in automation/script YAML config. The HA REST API (/api/config/automation/config/{id}) rejects unknown keys like category, which is why these tools need to:

  1. Strip category from the config before sending to the REST API
  2. Apply it separately via a WebSocket config/entity_registry/update call

This follows the same pattern already used by ha_set_entity (added in #677) and ha_config_set_helper (for area_id/labels).

Closes #702

Type of change

  • ✨ New feature

Testing

  • I have tested these changes with a LLM agent
  • All automated tests pass (uv run pytest)
  • Code follows style guidelines (uv run ruff check)

Checklist

  • I have updated documentation if needed

🤖 Generated with Claude Code

Add category parameter to domain-specific config tools, building on
homeassistant-ai#677's category CRUD foundation:

- ha_config_get_automation: includes category from entity registry
- ha_config_set_automation: accepts category parameter, applies via
  entity registry update after creation (also extracts from config
  dict to prevent REST API rejection)
- ha_config_get_script: includes category from entity registry
- ha_config_set_script: same pattern as automations
- ha_config_set_helper: adds category alongside existing area_id
  and labels entity registry updates

Categories are stored in the entity registry (not YAML config), so
GET tools make a secondary WebSocket call to fetch them, and SET
tools apply them via entity_registry/update after the primary
create/update succeeds.

Closes homeassistant-ai#702

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request extends the Home Assistant configuration tools by introducing support for entity categories. By decoupling the category management from the primary configuration payload, the changes ensure compatibility with the Home Assistant REST API while enabling users to organize automations, scripts, and helpers more effectively through the entity registry.

Highlights

  • Category Parameter Integration: Added a new 'category' parameter to 'ha_config_set_automation', 'ha_config_set_script', and 'ha_config_set_helper' tools to allow better organization of Home Assistant entities.
  • REST API Compatibility: Implemented logic to strip the 'category' field from configuration dictionaries before sending them to the Home Assistant REST API, which does not support this field, and instead applies it via the 'config/entity_registry/update' WebSocket call.
  • Enhanced Retrieval: Updated 'ha_config_get_automation' and 'ha_config_get_script' to fetch and include the 'category' from the entity registry in their responses.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces category management for Home Assistant automations, scripts, and helpers by updating the respective 'get' and 'set' tools to interact with the entity registry. Key feedback includes ensuring the category parameter is supported in the update path for helpers and using the correct 'helpers' scope for helper categories. Additionally, several broad exception blocks should be replaced with debug logging to improve maintainability, and the efficiency of resolving automation entity IDs from unique IDs should be considered for large installations.

Comment thread src/ha_mcp/tools/tools_config_helpers.py
Comment thread src/ha_mcp/tools/tools_config_helpers.py Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
Comment thread src/ha_mcp/tools/tools_config_scripts.py Outdated
Fixes from Gemini review:
- Use "helpers" scope for helper categories (not helper_type)
- Add category support to helper UPDATE path (was only on create)
- Replace bare except:pass with logger.debug for debuggability

Add E2E tests:
- test_automation_set_and_get_category: full round-trip
- test_script_set_and_get_category: full round-trip
- test_automation_category_in_config_dict: category extraction

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kingpanther13

Copy link
Copy Markdown
Member Author

/Gemini review , made changes, do one more review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces category support for automations, scripts, and helpers across the Home Assistant MCP tools. It updates the configuration tools to allow setting and retrieving categories via the entity registry and includes comprehensive end-to-end tests to verify this functionality. I have provided feedback regarding the use of overly broad exception handling and suggested refactoring the duplicated entity ID resolution logic to improve maintainability.

Comment thread site/src/data/tools.json Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
kingpanther13 and others added 4 commits April 1, 2026 11:54
Include scope='helpers' and mention ha_config_set_category() for
consistency across all category-aware config tools.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Deduplicate the entity_id-from-unique_id resolution logic used by
both ha_config_get_automation and ha_config_remove_automation into
a single helper function.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
state["entity_id"] returns Any from untyped dict; wrap in str()
to match the declared str | None return type.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kingpanther13

Copy link
Copy Markdown
Member Author

/Gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces category support for automations, scripts, and helpers, allowing users to organize these entities via the Home Assistant entity registry. The changes include updates to the configuration tools to handle category assignment during creation and retrieval, along with comprehensive end-to-end tests to verify the new functionality. I have provided feedback regarding code duplication in category handling and the naming convention for the removal tool.

Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py
Comment thread src/ha_mcp/tools/tools_config_automations.py
kingpanther13 and others added 2 commits April 1, 2026 19:20
- Extract fetch_entity_category() and apply_entity_category() into
  util_helpers.py, replacing duplicate inline logic in automations
  and scripts config tools
- Restore warning log in ha_config_remove_automation when entity_id
  resolution fails (was lost during _resolve helper extraction)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
categories.get(scope) returns Any from untyped dict; cast to
str to match declared str | None return type.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kingpanther13
kingpanther13 marked this pull request as ready for review April 1, 2026 23:57
@kingpanther13
kingpanther13 requested a review from a team April 1, 2026 23:57
@kingpanther13
kingpanther13 enabled auto-merge (squash) April 1, 2026 23:57
kingpanther13 added a commit to kingpanther13/ha-mcp-fork that referenced this pull request Apr 2, 2026
kingpanther13 added a commit to kingpanther13/ha-mcp-fork that referenced this pull request Apr 2, 2026
kingpanther13 added a commit to kingpanther13/ha-mcp-fork that referenced this pull request Apr 3, 2026
kingpanther13 added a commit to kingpanther13/ha-mcp-fork that referenced this pull request Apr 3, 2026
Comment thread src/ha_mcp/tools/util_helpers.py Outdated
Comment thread src/ha_mcp/tools/util_helpers.py Outdated
Comment thread src/ha_mcp/tools/tools_config_helpers.py
Comment thread src/ha_mcp/tools/tools_config_helpers.py Outdated
Comment thread src/ha_mcp/tools/tools_config_automations.py Outdated
Comment thread tests/src/e2e/workflows/categories/test_config_tool_categories.py
Comment thread tests/src/e2e/workflows/categories/test_config_tool_categories.py Outdated
Comment thread tests/src/e2e/workflows/categories/test_config_tool_categories.py
Comment thread src/ha_mcp/tools/tools_config_helpers.py Outdated
kingpanther13 and others added 3 commits April 4, 2026 12:05
- Check WebSocket success in apply_entity_category before reporting
- Upgrade fetch_entity_category error logging from debug to warning
- Add else branch for helper create registry update failure
- Capture and check config-store-types registry update result
- Use None check instead of falsy check for category precedence
- Add helper category test (input_boolean with "helpers" scope)
- Use wait_for_tool_result polling in GET-after-create tests
- Add tests: param precedence, category on update, script config-dict

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Automation update path: entity_id is None in upsert result, so
  apply_entity_category was silently skipped. Now falls back to
  identifier when it's an entity_id format.
- Helper test: removed invalid `action` param (tool uses helper_id
  presence to decide create vs update), use ha_config_remove_helper
  for cleanup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Some helper types (e.g., input_boolean) don't return entity_id in
the WebSocket create result. Derive it from helper_type + result id
so that wait_for_entity_registered and category application work.
Test updated to handle the same fallback.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/ha_mcp/tools/util_helpers.py Outdated
Comment thread src/ha_mcp/tools/tools_config_helpers.py Outdated
…helpers

- apply_entity_category: add isinstance guard before .get() on error
  value, matching pattern in tools_config_helpers.py
- helpers: replace three inline category constructions with shared
  apply_entity_category (create path, config-store-types update,
  standard update), consistent with automations/scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Patch76

Patch76 commented Apr 4, 2026

Copy link
Copy Markdown
Member

We're running 8 automation categories in production and use
ha_config_get_category with scope="automation" regularly.

The scope fix for helpers — "helpers" as the registry key rather than
the domain type — is consistent with how ha_config_get_category behaves
on our instance. Good to see this covered by the E2E test suite.

@sergeykad sergeykad left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All review issues addressed. CI green.

@kingpanther13
kingpanther13 merged commit 1029558 into homeassistant-ai:master Apr 5, 2026
22 of 23 checks passed
@github-actions

github-actions Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

🧪 Your changes are now in the dev channel!

Your PR has been merged to master and is available for testing in the dev channel.

Test your changes before the next stable release (biweekly Wednesday):
📖 Dev Channel Documentation

Quick start

# Run dev version
uvx ha-mcp-dev

# Check version
uvx ha-mcp-dev --version

Docker:

docker pull ghcr.io/homeassistant-ai/ha-mcp:dev
docker run --rm -i \
  -e HOMEASSISTANT_URL=http://your-ha:8123 \
  -e HOMEASSISTANT_TOKEN=your_token \
  ghcr.io/homeassistant-ai/ha-mcp:dev

Found an issue? Please open a new bug report and mention this PR for context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose automation category in ha_config_set_automation and ha_config_get_automation

3 participants