From e32074c74e412f2b9957f46d165a95b929b98173 Mon Sep 17 00:00:00 2001 From: Artem Draft Date: Tue, 13 Dec 2022 10:45:04 +0300 Subject: [PATCH] Fix saving options with missing ignored sources in BraviaTV (#83891) fix https://github.com/home-assistant/core/issues/83217 fixes undefined --- homeassistant/components/braviatv/config_flow.py | 6 ++++++ tests/components/braviatv/test_config_flow.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/homeassistant/components/braviatv/config_flow.py b/homeassistant/components/braviatv/config_flow.py index 183e13a19e8..369aae374cf 100644 --- a/homeassistant/components/braviatv/config_flow.py +++ b/homeassistant/components/braviatv/config_flow.py @@ -264,6 +264,12 @@ class BraviaTVOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry): sources = coordinator.source_map.values() source_list = [item["title"] for item in sources] + ignored_sources = self.options.get(CONF_IGNORED_SOURCES, []) + + for item in ignored_sources: + if item not in source_list: + source_list.append(item) + self.data_schema = vol.Schema( { vol.Optional(CONF_IGNORED_SOURCES): cv.multi_select(source_list), diff --git a/tests/components/braviatv/test_config_flow.py b/tests/components/braviatv/test_config_flow.py index ee835493e66..18576207a30 100644 --- a/tests/components/braviatv/test_config_flow.py +++ b/tests/components/braviatv/test_config_flow.py @@ -422,6 +422,19 @@ async def test_options_flow(hass: HomeAssistant) -> None: assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert config_entry.options == {CONF_IGNORED_SOURCES: ["HDMI 1", "HDMI 2"]} + # Test that saving with missing sources is ok + with patch( + "pybravia.BraviaTV.get_external_status", + return_value=BRAVIA_SOURCES[1:], + ): + result = await hass.config_entries.options.async_init(config_entry.entry_id) + result = await hass.config_entries.options.async_configure( + result["flow_id"], user_input={CONF_IGNORED_SOURCES: ["HDMI 1"]} + ) + await hass.async_block_till_done() + assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY + assert config_entry.options == {CONF_IGNORED_SOURCES: ["HDMI 1"]} + async def test_options_flow_error(hass: HomeAssistant) -> None: """Test config flow options."""