Skip to content

Allow template in query in sql#150287

Merged
emontnemery merged 10 commits into
devfrom
gj-20250808-01
Nov 6, 2025
Merged

Allow template in query in sql#150287
emontnemery merged 10 commits into
devfrom
gj-20250808-01

Conversation

@gjohansson-ST
Copy link
Copy Markdown
Member

@gjohansson-ST gjohansson-ST commented Aug 8, 2025

Proposed change

Allow using templates in sql query in SQL integration.

Possible use cases

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Aug 8, 2025

Hey there @dougiteixeira, mind taking a look at this pull request as it has been labeled with an integration (sql) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sql can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sql Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@gjohansson-ST gjohansson-ST marked this pull request as ready for review August 8, 2025 22:17
Copilot AI review requested due to automatic review settings August 8, 2025 22:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces template support for SQL queries in the SQL integration, allowing dynamic query generation based on Home Assistant state and templates. The implementation adds query validation at both configuration time and runtime to ensure template rendering produces valid SQL.

Key changes:

  • Modified SQL query parameter from string to ValueTemplate to support Jinja2 templating
  • Added runtime query validation and rendering with proper error handling
  • Updated configuration flow to use template selector and validate templated queries

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
homeassistant/components/sql/util.py Added check_and_render_sql_query function for template validation and SQL parsing
homeassistant/components/sql/sensor.py Modified SQLSensor to handle ValueTemplate queries with runtime rendering
homeassistant/components/sql/config_flow.py Updated config flow to use template selector and new validation function
homeassistant/components/sql/init.py Updated YAML schema validation to handle Template objects
tests/components/sql/test_sensor.py Added comprehensive tests for template queries including error scenarios
tests/components/sql/test_config_flow.py Added tests for template query validation in config flow
tests/components/sql/test_init.py Updated validation tests to work with Template objects
tests/components/sql/init.py Added test configuration constants for template scenarios

Comment thread homeassistant/components/sql/sensor.py
Comment thread homeassistant/components/sql/util.py Outdated
Comment thread homeassistant/components/sql/util.py
@gjohansson-ST gjohansson-ST marked this pull request as draft August 8, 2025 22:27
@gjohansson-ST gjohansson-ST marked this pull request as ready for review August 9, 2025 10:57
Copy link
Copy Markdown
Contributor

@luuuis luuuis left a comment

Choose a reason for hiding this comment

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

Could this facilitate SQL injection attacks if we were to allow interpolating the state of arbitrary sensors into SQL statements (that might have been pulled off the internet, etc)?

What are the use cases and would adding bind parameter support not suffice for those?

Copy link
Copy Markdown
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

There are merge conflicts

@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant Bot marked this pull request as draft September 11, 2025 10:38
@gjohansson-ST gjohansson-ST marked this pull request as ready for review October 28, 2025 15:15
@home-assistant home-assistant Bot requested a review from joostlek October 28, 2025 15:15
@gjohansson-ST
Copy link
Copy Markdown
Member Author

Could this facilitate SQL injection attacks if we were to allow interpolating the state of arbitrary sensors into SQL statements (that might have been pulled off the internet, etc)?

We parse and validate the syntax after rendering so if the statement does something else than a regular select, it won't allow it.

That also ensures this even someone tries to use templating to bypass.

But the validation was already in place and has not changed.

@emontnemery
Copy link
Copy Markdown
Contributor

Please set the PR back to "Ready for review" when the merge conflicts are fixed

@emontnemery emontnemery marked this pull request as draft November 3, 2025 15:37
@gjohansson-ST gjohansson-ST marked this pull request as ready for review November 4, 2025 18:34
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Please update the PR description with a motivation for the new feature, with an example.
Also, the documentation PR should have an example.

@home-assistant home-assistant Bot marked this pull request as draft November 6, 2025 09:56
Comment thread homeassistant/components/sql/config_flow.py
@gjohansson-ST
Copy link
Copy Markdown
Member Author

gjohansson-ST commented Nov 6, 2025

Update the PR description with some related motivation why this should be done.

I'll update the doc PR shortly with an expanded example.

@gjohansson-ST gjohansson-ST marked this pull request as ready for review November 6, 2025 11:37
@home-assistant home-assistant Bot requested a review from emontnemery November 6, 2025 11:37
Comment thread homeassistant/components/sql/config_flow.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft November 6, 2025 12:15
@gjohansson-ST gjohansson-ST marked this pull request as ready for review November 6, 2025 13:55
@home-assistant home-assistant Bot requested a review from emontnemery November 6, 2025 13:55
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @gjohansson-ST

@emontnemery emontnemery merged commit 67ccdd3 into dev Nov 6, 2025
36 checks passed
@emontnemery emontnemery deleted the gj-20250808-01 branch November 6, 2025 16:11
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 7, 2025
Comment on lines +42 to +44
vol.Required(CONF_QUERY): vol.All(
cv.template, ValueTemplate.from_template, validate_sql_select
),
Copy link
Copy Markdown
Member

@arturpragacz arturpragacz Nov 10, 2025

Choose a reason for hiding this comment

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

This is a schema for a service. It should not contain templates, as template support for services is handled automatically by Core prior to this validation.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants