From 7cb0ce0eec6c1883d0f8d9307d4cc9a4d1a52cd8 Mon Sep 17 00:00:00 2001 From: Simon Hansen <67142049+DurgNomis-drol@users.noreply.github.com> Date: Mon, 14 Feb 2022 21:07:50 +0100 Subject: [PATCH] Move config option to OptionsFlow in iss (#65303) Co-authored-by: Franck Nijhof --- homeassistant/components/iss/__init__.py | 7 +++ homeassistant/components/iss/binary_sensor.py | 4 +- homeassistant/components/iss/config_flow.py | 50 +++++++++++++++---- homeassistant/components/iss/strings.json | 14 ++++-- .../components/iss/translations/en.json | 12 +++-- tests/components/iss/test_config_flow.py | 42 ++++++++++++---- 6 files changed, 102 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/iss/__init__.py b/homeassistant/components/iss/__init__.py index af44e621a7f..29d6ced184b 100644 --- a/homeassistant/components/iss/__init__.py +++ b/homeassistant/components/iss/__init__.py @@ -15,6 +15,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {}) + entry.async_on_unload(entry.add_update_listener(update_listener)) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True @@ -25,3 +27,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): del hass.data[DOMAIN] return unload_ok + + +async def update_listener(hass: HomeAssistant, entry: ConfigEntry): + """Handle options update.""" + await hass.config_entries.async_reload(entry.entry_id) diff --git a/homeassistant/components/iss/binary_sensor.py b/homeassistant/components/iss/binary_sensor.py index 5272053a517..196bdfd055e 100644 --- a/homeassistant/components/iss/binary_sensor.py +++ b/homeassistant/components/iss/binary_sensor.py @@ -72,8 +72,8 @@ async def async_setup_entry( ) -> None: """Set up the sensor platform.""" - name = entry.data.get(CONF_NAME, DEFAULT_NAME) - show_on_map = entry.data.get(CONF_SHOW_ON_MAP, False) + name = entry.title + show_on_map = entry.options.get(CONF_SHOW_ON_MAP, False) try: iss_data = IssData(hass.config.latitude, hass.config.longitude) diff --git a/homeassistant/components/iss/config_flow.py b/homeassistant/components/iss/config_flow.py index e1703b54acb..dc80126bd14 100644 --- a/homeassistant/components/iss/config_flow.py +++ b/homeassistant/components/iss/config_flow.py @@ -4,8 +4,10 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_NAME, CONF_SHOW_ON_MAP +from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult +from .binary_sensor import DEFAULT_NAME from .const import DOMAIN @@ -14,6 +16,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 + @staticmethod + @callback + def async_get_options_flow( + config_entry: config_entries.ConfigEntry, + ) -> config_entries.OptionsFlow: + """Get the options flow for this handler.""" + return OptionsFlowHandler(config_entry) + async def async_step_user(self, user_input=None) -> FlowResult: """Handle a flow initialized by the user.""" # Check if already configured @@ -26,17 +36,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: return self.async_create_entry( - title="International Space Station", data=user_input + title=user_input.get(CONF_NAME, DEFAULT_NAME), + data={}, + options={CONF_SHOW_ON_MAP: user_input.get(CONF_SHOW_ON_MAP, False)}, ) - return self.async_show_form( - step_id="user", - data_schema=vol.Schema( - { - vol.Optional(CONF_SHOW_ON_MAP, default=False): bool, - } - ), - ) + return self.async_show_form(step_id="user") async def async_step_import(self, conf: dict) -> FlowResult: """Import a configuration from configuration.yaml.""" @@ -46,3 +51,30 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): CONF_SHOW_ON_MAP: conf[CONF_SHOW_ON_MAP], } ) + + +class OptionsFlowHandler(config_entries.OptionsFlow): + """Config flow options handler for iss.""" + + def __init__(self, config_entry: config_entries.ConfigEntry) -> None: + """Initialize options flow.""" + self.config_entry = config_entry + self.options = dict(config_entry.options) + + async def async_step_init(self, user_input=None) -> FlowResult: + """Manage the options.""" + if user_input is not None: + self.options.update(user_input) + return self.async_create_entry(title="", data=self.options) + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Optional( + CONF_SHOW_ON_MAP, + default=self.config_entry.options.get(CONF_SHOW_ON_MAP, False), + ): bool, + } + ), + ) diff --git a/homeassistant/components/iss/strings.json b/homeassistant/components/iss/strings.json index b9dd7c374d0..4a2da5f6556 100644 --- a/homeassistant/components/iss/strings.json +++ b/homeassistant/components/iss/strings.json @@ -2,15 +2,21 @@ "config": { "step": { "user": { - "description": "Do you want to configure the International Space Station?", - "data": { - "show_on_map": "Show on map?" - } + "description": "Do you want to configure International Space Station (ISS)?" } }, "abort": { "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]", "latitude_longitude_not_defined": "Latitude and longitude are not defined in Home Assistant." } + }, + "options": { + "step": { + "init": { + "data": { + "show_on_map": "Show on map" + } + } + } } } diff --git a/homeassistant/components/iss/translations/en.json b/homeassistant/components/iss/translations/en.json index f8ef8d27cd7..b90ff56964a 100644 --- a/homeassistant/components/iss/translations/en.json +++ b/homeassistant/components/iss/translations/en.json @@ -6,10 +6,16 @@ }, "step": { "user": { + "description": "Do you want to configure International Space Station (ISS)?" + } + } + }, + "options": { + "step": { + "init": { "data": { - "show_on_map": "Show on map?" - }, - "description": "Do you want to configure the International Space Station?" + "show_on_map": "Show on map" + } } } } diff --git a/tests/components/iss/test_config_flow.py b/tests/components/iss/test_config_flow.py index a20a8729f55..e47d977b96f 100644 --- a/tests/components/iss/test_config_flow.py +++ b/tests/components/iss/test_config_flow.py @@ -7,11 +7,12 @@ from homeassistant.components.iss.const import DOMAIN from homeassistant.config import async_process_ha_core_config from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import CONF_NAME, CONF_SHOW_ON_MAP +from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry -async def test_import(hass): +async def test_import(hass: HomeAssistant): """Test entry will be imported.""" imported_config = {CONF_NAME: DEFAULT_NAME, CONF_SHOW_ON_MAP: False} @@ -22,10 +23,11 @@ async def test_import(hass): DOMAIN, context={"source": SOURCE_IMPORT}, data=imported_config ) assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result.get("result").data == imported_config + assert result.get("result").title == DEFAULT_NAME + assert result.get("result").options == {CONF_SHOW_ON_MAP: False} -async def test_create_entry(hass): +async def test_create_entry(hass: HomeAssistant): """Test we can finish a config flow.""" result = await hass.config_entries.flow.async_init( @@ -39,14 +41,14 @@ async def test_create_entry(hass): result = await hass.config_entries.flow.async_configure( result["flow_id"], - {CONF_SHOW_ON_MAP: True}, + {}, ) assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result.get("result").data[CONF_SHOW_ON_MAP] is True + assert result.get("result").data == {} -async def test_integration_already_exists(hass): +async def test_integration_already_exists(hass: HomeAssistant): """Test we only allow a single config flow.""" MockConfigEntry( @@ -55,14 +57,14 @@ async def test_integration_already_exists(hass): ).add_to_hass(hass) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={CONF_SHOW_ON_MAP: False} + DOMAIN, context={"source": SOURCE_USER}, data={} ) assert result.get("type") == data_entry_flow.RESULT_TYPE_ABORT assert result.get("reason") == "single_instance_allowed" -async def test_abort_no_home(hass): +async def test_abort_no_home(hass: HomeAssistant): """Test we don't create an entry if no coordinates are set.""" await async_process_ha_core_config( @@ -71,8 +73,30 @@ async def test_abort_no_home(hass): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={CONF_SHOW_ON_MAP: False} + DOMAIN, context={"source": SOURCE_USER}, data={} ) assert result.get("type") == data_entry_flow.RESULT_TYPE_ABORT assert result.get("reason") == "latitude_longitude_not_defined" + + +async def test_options(hass: HomeAssistant): + """Test options flow.""" + + config_entry = MockConfigEntry( + domain=DOMAIN, + data={}, + ) + + config_entry.add_to_hass(hass) + + optionflow = await hass.config_entries.options.async_init(config_entry.entry_id) + + configured = await hass.config_entries.options.async_configure( + optionflow["flow_id"], + user_input={ + CONF_SHOW_ON_MAP: True, + }, + ) + + assert configured.get("type") == "create_entry"