Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions homeassistant/components/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
from .const import PLATFORMS


async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener for options."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up SQL from a config entry."""
entry.async_on_unload(entry.add_update_listener(async_update_listener))

hass.config_entries.async_setup_platforms(entry, PLATFORMS)

Expand Down
9 changes: 8 additions & 1 deletion homeassistant/components/sql/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ async def async_step_init(
except ValueError:
errors["query"] = "query_invalid"
else:
return self.async_create_entry(title="", data=user_input)
return self.async_create_entry(
title="",
data={
CONF_NAME: self.entry.title,
**self.entry.options,
**user_input,
},
)

return self.async_show_form(
step_id="init",
Expand Down
55 changes: 55 additions & 0 deletions tests/components/sql/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,62 @@ async def test_options_flow(hass: HomeAssistant) -> None:

assert result["type"] == RESULT_TYPE_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",
}


async def test_options_flow_name_previously_removed(hass: HomeAssistant) -> None:
"""Test options config flow where the name was missing."""
entry = MockConfigEntry(
domain=DOMAIN,
data={},
options={
"db_url": "sqlite://",
"query": "SELECT 5 as value",
"column": "value",
"unit_of_measurement": "MiB",
"value_template": None,
},
title="Get Value Title",
)
entry.add_to_hass(hass)

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"] == RESULT_TYPE_FORM
assert result["step_id"] == "init"

with patch(
"homeassistant.components.sql.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"db_url": "sqlite://",
"query": "SELECT 5 as size",
"column": "size",
"unit_of_measurement": "MiB",
},
)
await hass.async_block_till_done()

assert len(mock_setup_entry.mock_calls) == 1
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
"name": "Get Value Title",
"db_url": "sqlite://",
"query": "SELECT 5 as size",
"column": "size",
"value_template": None,
"unit_of_measurement": "MiB",
}

Expand Down Expand Up @@ -312,6 +365,8 @@ async def test_options_flow_fails_invalid_query(

assert result4["type"] == RESULT_TYPE_CREATE_ENTRY
assert result4["data"] == {
"name": "Get Value",
"value_template": None,
"db_url": "sqlite://",
"query": "SELECT 5 as size",
"column": "size",
Expand Down