From ad9e1fe16629299dc5c0c1131509aaacf8109e91 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 Jun 2022 17:29:44 -1000 Subject: [PATCH] Fix reload race in yeelight when updating the ip address (#73390) --- .../components/yeelight/config_flow.py | 5 ++- tests/components/yeelight/test_config_flow.py | 31 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/yeelight/config_flow.py b/homeassistant/components/yeelight/config_flow.py index 8a3a5b41320..440b717fd8c 100644 --- a/homeassistant/components/yeelight/config_flow.py +++ b/homeassistant/components/yeelight/config_flow.py @@ -96,7 +96,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self.hass.config_entries.async_update_entry( entry, data={**entry.data, CONF_HOST: self._discovered_ip} ) - reload = True + reload = entry.state in ( + ConfigEntryState.SETUP_RETRY, + ConfigEntryState.LOADED, + ) if reload: self.hass.async_create_task( self.hass.config_entries.async_reload(entry.entry_id) diff --git a/tests/components/yeelight/test_config_flow.py b/tests/components/yeelight/test_config_flow.py index 80acaa6f10e..1c19a5e7dfd 100644 --- a/tests/components/yeelight/test_config_flow.py +++ b/tests/components/yeelight/test_config_flow.py @@ -739,7 +739,7 @@ async def test_discovered_zeroconf(hass): async def test_discovery_updates_ip(hass: HomeAssistant): - """Test discovery updtes ip.""" + """Test discovery updates ip.""" config_entry = MockConfigEntry( domain=DOMAIN, data={CONF_HOST: "1.2.2.3"}, unique_id=ID ) @@ -761,6 +761,35 @@ async def test_discovery_updates_ip(hass: HomeAssistant): assert config_entry.data[CONF_HOST] == IP_ADDRESS +async def test_discovery_updates_ip_no_reload_setup_in_progress(hass: HomeAssistant): + """Test discovery updates ip does not reload if setup is an an error state.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_HOST: "1.2.2.3"}, + unique_id=ID, + state=config_entries.ConfigEntryState.SETUP_ERROR, + ) + config_entry.add_to_hass(hass) + + mocked_bulb = _mocked_bulb() + with patch( + f"{MODULE}.async_setup_entry", return_value=True + ) as mock_setup_entry, _patch_discovery(), _patch_discovery_interval(), patch( + f"{MODULE_CONFIG_FLOW}.AsyncBulb", return_value=mocked_bulb + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=ZEROCONF_DATA, + ) + await hass.async_block_till_done() + + assert result["type"] == RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" + assert config_entry.data[CONF_HOST] == IP_ADDRESS + assert len(mock_setup_entry.mock_calls) == 0 + + async def test_discovery_adds_missing_ip_id_only(hass: HomeAssistant): """Test discovery adds missing ip.""" config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_ID: ID})