diff --git a/homeassistant/components/braviatv/config_flow.py b/homeassistant/components/braviatv/config_flow.py index 45a2bad0036..183e13a19e8 100644 --- a/homeassistant/components/braviatv/config_flow.py +++ b/homeassistant/components/braviatv/config_flow.py @@ -245,22 +245,17 @@ class BraviaTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return uuid, f"{NICKNAME_PREFIX} {uuid[:6]}" -class BraviaTVOptionsFlowHandler(config_entries.OptionsFlow): +class BraviaTVOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry): """Config flow options for Bravia TV.""" - def __init__(self, config_entry: ConfigEntry) -> None: - """Initialize Bravia TV options flow.""" - self.config_entry = config_entry - self.ignored_sources = config_entry.options.get(CONF_IGNORED_SOURCES) - self.source_list: list[str] = [] + data_schema: vol.Schema async def async_step_init( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Manage the options.""" - coordinator: BraviaTVCoordinator = self.hass.data[DOMAIN][ - self.config_entry.entry_id - ] + coordinator: BraviaTVCoordinator + coordinator = self.hass.data[DOMAIN][self.config_entry.entry_id] try: await coordinator.async_update_sources() @@ -268,7 +263,13 @@ class BraviaTVOptionsFlowHandler(config_entries.OptionsFlow): return self.async_abort(reason="failed_update") sources = coordinator.source_map.values() - self.source_list = [item["title"] for item in sources] + source_list = [item["title"] for item in sources] + self.data_schema = vol.Schema( + { + vol.Optional(CONF_IGNORED_SOURCES): cv.multi_select(source_list), + } + ) + return await self.async_step_user() async def async_step_user( @@ -280,11 +281,7 @@ class BraviaTVOptionsFlowHandler(config_entries.OptionsFlow): return self.async_show_form( step_id="user", - data_schema=vol.Schema( - { - vol.Optional( - CONF_IGNORED_SOURCES, default=self.ignored_sources - ): cv.multi_select(self.source_list) - } + data_schema=self.add_suggested_values_to_schema( + self.data_schema, self.options ), ) diff --git a/tests/components/braviatv/test_config_flow.py b/tests/components/braviatv/test_config_flow.py index 58e684a1378..ee835493e66 100644 --- a/tests/components/braviatv/test_config_flow.py +++ b/tests/components/braviatv/test_config_flow.py @@ -21,6 +21,7 @@ from homeassistant.components.braviatv.const import ( ) from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PIN +from homeassistant.core import HomeAssistant from homeassistant.helpers import instance_id from tests.common import MockConfigEntry @@ -381,7 +382,7 @@ async def test_create_entry_psk(hass): } -async def test_options_flow(hass): +async def test_options_flow(hass: HomeAssistant) -> None: """Test config flow options.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -422,6 +423,43 @@ async def test_options_flow(hass): assert config_entry.options == {CONF_IGNORED_SOURCES: ["HDMI 1", "HDMI 2"]} +async def test_options_flow_error(hass: HomeAssistant) -> None: + """Test config flow options.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + unique_id="very_unique_string", + data={ + CONF_HOST: "bravia-host", + CONF_PIN: "1234", + CONF_MAC: "AA:BB:CC:DD:EE:FF", + }, + title="TV-Model", + ) + config_entry.add_to_hass(hass) + + with patch("pybravia.BraviaTV.connect"), patch( + "pybravia.BraviaTV.get_power_status", + return_value="active", + ), patch( + "pybravia.BraviaTV.get_external_status", + return_value=BRAVIA_SOURCES, + ), patch( + "pybravia.BraviaTV.send_rest_req", + return_value={}, + ): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + with patch( + "pybravia.BraviaTV.send_rest_req", + side_effect=BraviaTVError, + ): + result = await hass.config_entries.options.async_init(config_entry.entry_id) + + assert result["type"] == data_entry_flow.FlowResultType.ABORT + assert result["reason"] == "failed_update" + + @pytest.mark.parametrize( "user_input", [{CONF_PIN: "mypsk", CONF_USE_PSK: True}, {CONF_PIN: "1234", CONF_USE_PSK: False}],