Skip to content

feat: add fields= projection to ha_search_entities#1212

Closed
SealKan wants to merge 2 commits into
homeassistant-ai:masterfrom
SealKan:feat/1199-search-fields
Closed

feat: add fields= projection to ha_search_entities#1212
SealKan wants to merge 2 commits into
homeassistant-ai:masterfrom
SealKan:feat/1199-search-fields

Conversation

@SealKan

@SealKan SealKan commented May 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an optional fields parameter to ha_search_entities that trims the response
to only the requested top-level keys, reducing payload size by up to 80%.

  • fields=["results"] cuts the typical 5 KB response to ~1.2 KB
  • success is always retained regardless of what fields lists
  • Unknown keys in fields are silently dropped
  • Projection happens before add_timezone_metadata so the outer
    {"data": ..., "metadata": ...} wrapper shape is always preserved

A module-level _project_fields helper centralizes the logic and is
called at all 5 return paths inside ha_search_entities.

Part of #1199 (alongside ha_config_list_areas PR #1210 and
ha_get_history PR #1211).

Changes

  • src/ha_mcp/tools/tools_search.py — adds _project_fields helper +
    fields parameter with all 5 return paths updated
  • tests/src/unit/test_search_fields_projection.py — 9 unit tests for
    _project_fields covering: no-op (None), single/multiple fields,
    success-always-retained, unknown-key drop, empty list, no-mutation

Test plan

  • uv run pytest tests/src/unit/test_search_fields_projection.py — 9/9 pass
  • uv run mypy src/ — no issues
  • CI (pr.yml — lint + type check)

Adds an optional `fields` parameter that filters the top-level response
keys before timezone metadata is added, reducing response size for
callers that only need specific fields (e.g. ["results"]).

A `_project_fields` module-level helper handles the projection:
- `success` is always retained regardless of what fields lists
- unknown keys are silently dropped
- projection happens before `add_timezone_metadata` so the outer
  {"data": ..., "metadata": ...} wrapper shape is always preserved

Covers all 5 return paths in ha_search_entities (exact, area, empty
area, domain listing, fuzzy/smart). Adds 9 unit tests for
_project_fields.

Closes homeassistant-ai#1199 (search entities)
@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 introduces a field projection mechanism for the ha_search_entities tool. By allowing users to specify a subset of fields to return, the implementation reduces unnecessary data transfer and improves performance. The changes are applied uniformly across all search return paths while maintaining backward compatibility and structural integrity of the response.

Highlights

  • Field Projection Support: Added an optional fields parameter to ha_search_entities to allow callers to request specific top-level keys, significantly reducing response payload sizes.
  • Centralized Logic: Introduced a _project_fields helper function to handle the projection logic consistently across all return paths in the search tool.
  • Safety and Integrity: Ensured the success key is always retained and that projection occurs before timezone metadata is added to preserve the expected outer response structure.
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 the 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 counterproductive. 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 field projection to the ha_search_entities tool, allowing users to specify which top-level keys to return to reduce response size. Feedback suggests moving the projection logic to a shared utility module to improve reusability, enhancing the helper to handle comma-separated string inputs from AI agents, and correcting the tool's documentation regarding available response keys.

Comment thread src/ha_mcp/tools/tools_search.py Outdated
Comment thread src/ha_mcp/tools/tools_search.py
Comment thread tests/src/unit/test_search_fields_projection.py
- Move _project_fields from tools_search.py to util_helpers.py as the
  public project_fields() function so it can be shared across modules
- Accept str | list[str] | None for fields; parse_string_list_param
  normalises CSV and JSON-array strings from AI clients
- Remove time_zone from ha_search_entities fields= description (it is
  metadata, not part of the inner data dict)
- Add test cases for CSV string and JSON-array string inputs

@SealKan SealKan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed all Gemini review feedback in bcb2ab5:

  • Moved _project_fields to util_helpers.py as the public project_fields() shared across modules
  • Updated fields type to str | list[str] | None; the helper calls parse_string_list_param internally to normalise CSV/JSON-array strings
  • Removed time_zone from the available keys description (it is in the metadata wrapper, not the inner data dict)
  • Added string input test cases (test_csv_string_input_parsed_correctly and test_json_array_string_input_parsed_correctly)

@SealKan

SealKan commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1225 which consolidates all six fields= projection PRs into a single implementation.

@SealKan SealKan closed this May 11, 2026
@SealKan SealKan deleted the feat/1199-search-fields branch May 11, 2026 12:53
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.

2 participants