Allow empty db in SQL options flow (#77777)
This commit is contained in:
parent
8280b8422c
commit
76006ce9d7
2 changed files with 71 additions and 11 deletions
|
@ -147,9 +147,12 @@ class SQLOptionsFlowHandler(config_entries.OptionsFlow):
|
|||
) -> FlowResult:
|
||||
"""Manage SQL options."""
|
||||
errors = {}
|
||||
db_url_default = DEFAULT_URL.format(
|
||||
hass_config_path=self.hass.config.path(DEFAULT_DB_FILE)
|
||||
)
|
||||
|
||||
if user_input is not None:
|
||||
db_url = user_input[CONF_DB_URL]
|
||||
db_url = user_input.get(CONF_DB_URL, db_url_default)
|
||||
query = user_input[CONF_QUERY]
|
||||
column = user_input[CONF_COLUMN_NAME]
|
||||
|
||||
|
@ -176,7 +179,7 @@ class SQLOptionsFlowHandler(config_entries.OptionsFlow):
|
|||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(
|
||||
vol.Optional(
|
||||
CONF_DB_URL,
|
||||
description={
|
||||
"suggested_value": self.entry.options[CONF_DB_URL]
|
||||
|
|
|
@ -20,7 +20,7 @@ from . import (
|
|||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
async def test_form(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -52,7 +52,7 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_flow_success(hass: HomeAssistant) -> None:
|
||||
async def test_import_flow_success(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test a successful import of yaml."""
|
||||
|
||||
with patch(
|
||||
|
@ -79,7 +79,7 @@ async def test_import_flow_success(hass: HomeAssistant) -> None:
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
|
||||
async def test_import_flow_already_exist(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test import of yaml already exist."""
|
||||
|
||||
MockConfigEntry(
|
||||
|
@ -102,7 +102,7 @@ async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
|
|||
assert result3["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_flow_fails_db_url(hass: HomeAssistant) -> None:
|
||||
async def test_flow_fails_db_url(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test config flow fails incorrect db url."""
|
||||
result4 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
|
@ -123,7 +123,7 @@ async def test_flow_fails_db_url(hass: HomeAssistant) -> None:
|
|||
assert result4["errors"] == {"db_url": "db_url_invalid"}
|
||||
|
||||
|
||||
async def test_flow_fails_invalid_query(hass: HomeAssistant) -> None:
|
||||
async def test_flow_fails_invalid_query(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test config flow fails incorrect db url."""
|
||||
result4 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
|
@ -169,7 +169,7 @@ async def test_flow_fails_invalid_query(hass: HomeAssistant) -> None:
|
|||
}
|
||||
|
||||
|
||||
async def test_options_flow(hass: HomeAssistant) -> None:
|
||||
async def test_options_flow(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test options config flow."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
|
@ -218,7 +218,9 @@ async def test_options_flow(hass: HomeAssistant) -> None:
|
|||
}
|
||||
|
||||
|
||||
async def test_options_flow_name_previously_removed(hass: HomeAssistant) -> None:
|
||||
async def test_options_flow_name_previously_removed(
|
||||
hass: HomeAssistant, recorder_mock
|
||||
) -> None:
|
||||
"""Test options config flow where the name was missing."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
|
@ -269,7 +271,7 @@ async def test_options_flow_name_previously_removed(hass: HomeAssistant) -> None
|
|||
}
|
||||
|
||||
|
||||
async def test_options_flow_fails_db_url(hass: HomeAssistant) -> None:
|
||||
async def test_options_flow_fails_db_url(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test options flow fails incorrect db url."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
|
@ -312,7 +314,7 @@ async def test_options_flow_fails_db_url(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
async def test_options_flow_fails_invalid_query(
|
||||
hass: HomeAssistant,
|
||||
hass: HomeAssistant, recorder_mock
|
||||
) -> None:
|
||||
"""Test options flow fails incorrect query and template."""
|
||||
entry = MockConfigEntry(
|
||||
|
@ -367,3 +369,58 @@ async def test_options_flow_fails_invalid_query(
|
|||
"column": "size",
|
||||
"unit_of_measurement": "MiB",
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow_db_url_empty(hass: HomeAssistant, recorder_mock) -> None:
|
||||
"""Test options config flow with leaving db_url empty."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={
|
||||
"db_url": "sqlite://",
|
||||
"name": "Get Value",
|
||||
"query": "SELECT 5 as value",
|
||||
"column": "value",
|
||||
"unit_of_measurement": "MiB",
|
||||
"value_template": None,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sql.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sql.async_setup_entry",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.sql.config_flow.sqlalchemy.create_engine",
|
||||
):
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
"query": "SELECT 5 as size",
|
||||
"column": "size",
|
||||
"unit_of_measurement": "MiB",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
"name": "Get Value",
|
||||
"db_url": "sqlite://",
|
||||
"query": "SELECT 5 as size",
|
||||
"column": "size",
|
||||
"value_template": None,
|
||||
"unit_of_measurement": "MiB",
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue