From 76006ce9d7d056674826142bf45e5eef252bb31b Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 5 Sep 2022 21:50:47 +0200 Subject: [PATCH] Allow empty db in SQL options flow (#77777) --- homeassistant/components/sql/config_flow.py | 7 +- tests/components/sql/test_config_flow.py | 75 ++++++++++++++++++--- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/sql/config_flow.py b/homeassistant/components/sql/config_flow.py index dc3a839ef1d..bcbece9f7f6 100644 --- a/homeassistant/components/sql/config_flow.py +++ b/homeassistant/components/sql/config_flow.py @@ -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] diff --git a/tests/components/sql/test_config_flow.py b/tests/components/sql/test_config_flow.py index 7c3571f8f19..96402e1bc7a 100644 --- a/tests/components/sql/test_config_flow.py +++ b/tests/components/sql/test_config_flow.py @@ -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", + }