Allow template in query in sql#150287
Conversation
|
Hey there @dougiteixeira, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this comment.
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 |
luuuis
left a comment
There was a problem hiding this comment.
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?
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
5dbf50e to
6a8ada5
Compare
6b2fc57 to
36d5488
Compare
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. |
|
Please set the PR back to "Ready for review" when the merge conflicts are fixed |
7be0c3a to
2693e5b
Compare
emontnemery
left a comment
There was a problem hiding this comment.
Please update the PR description with a motivation for the new feature, with an example.
Also, the documentation PR should have an example.
|
Update the PR description with some related motivation why this should be done. I'll update the doc PR shortly with an expanded example. |
emontnemery
left a comment
There was a problem hiding this comment.
LGTM, thanks @gjohansson-ST
| vol.Required(CONF_QUERY): vol.All( | ||
| cv.template, ValueTemplate.from_template, validate_sql_select | ||
| ), |
There was a problem hiding this comment.
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.
Proposed change
Allow using templates in sql query in SQL integration.
Possible use cases
Some is mentioned here https://community.home-assistant.io/t/add-support-for-query-template-to-sql-integration/188760
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: