diff --git a/homeassistant/components/wiz/__init__.py b/homeassistant/components/wiz/__init__.py index ef6ffeb58b5..104ecb6f0c5 100644 --- a/homeassistant/components/wiz/__init__.py +++ b/homeassistant/components/wiz/__init__.py @@ -54,6 +54,11 @@ async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool: return True +async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Handle options update.""" + await hass.config_entries.async_reload(entry.entry_id) + + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up the wiz integration from a config entry.""" ip_address = entry.data[CONF_HOST] @@ -123,6 +128,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator=coordinator, bulb=bulb, scenes=scenes ) hass.config_entries.async_setup_platforms(entry, PLATFORMS) + + entry.async_on_unload(entry.add_update_listener(_async_update_listener)) return True diff --git a/tests/components/wiz/test_init.py b/tests/components/wiz/test_init.py index 58afb5c944a..30340f78e49 100644 --- a/tests/components/wiz/test_init.py +++ b/tests/components/wiz/test_init.py @@ -3,7 +3,7 @@ import datetime from unittest.mock import AsyncMock from homeassistant import config_entries -from homeassistant.const import EVENT_HOMEASSISTANT_STOP +from homeassistant.const import ATTR_FRIENDLY_NAME, EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant from homeassistant.util.dt import utcnow @@ -24,20 +24,20 @@ async def test_setup_retry(hass: HomeAssistant) -> None: bulb = _mocked_wizlight(None, None, FAKE_SOCKET) bulb.getMac = AsyncMock(side_effect=OSError) _, entry = await async_setup_integration(hass, wizlight=bulb) - assert entry.state == config_entries.ConfigEntryState.SETUP_RETRY + assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY bulb.getMac = AsyncMock(return_value=FAKE_MAC) with _patch_discovery(), _patch_wizlight(device=bulb): async_fire_time_changed(hass, utcnow() + datetime.timedelta(minutes=15)) await hass.async_block_till_done() - assert entry.state == config_entries.ConfigEntryState.LOADED + assert entry.state is config_entries.ConfigEntryState.LOADED async def test_cleanup_on_shutdown(hass: HomeAssistant) -> None: """Test the socket is cleaned up on shutdown.""" bulb = _mocked_wizlight(None, None, FAKE_SOCKET) _, entry = await async_setup_integration(hass, wizlight=bulb) - assert entry.state == config_entries.ConfigEntryState.LOADED + assert entry.state is config_entries.ConfigEntryState.LOADED hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done() bulb.async_close.assert_called_once() @@ -48,7 +48,7 @@ async def test_cleanup_on_failed_first_update(hass: HomeAssistant) -> None: bulb = _mocked_wizlight(None, None, FAKE_SOCKET) bulb.updateState = AsyncMock(side_effect=OSError) _, entry = await async_setup_integration(hass, wizlight=bulb) - assert entry.state == config_entries.ConfigEntryState.SETUP_RETRY + assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY bulb.async_close.assert_called_once() @@ -57,4 +57,22 @@ async def test_wrong_device_now_has_our_ip(hass: HomeAssistant) -> None: bulb = _mocked_wizlight(None, None, FAKE_SOCKET) bulb.mac = "dddddddddddd" _, entry = await async_setup_integration(hass, wizlight=bulb) - assert entry.state == config_entries.ConfigEntryState.SETUP_RETRY + assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY + + +async def test_reload_on_title_change(hass: HomeAssistant) -> None: + """Test the integration gets reloaded when the title is updated.""" + bulb = _mocked_wizlight(None, None, FAKE_SOCKET) + _, entry = await async_setup_integration(hass, wizlight=bulb) + assert entry.state is config_entries.ConfigEntryState.LOADED + await hass.async_block_till_done() + + with _patch_discovery(), _patch_wizlight(device=bulb): + hass.config_entries.async_update_entry(entry, title="Shop Switch") + assert entry.title == "Shop Switch" + await hass.async_block_till_done() + + assert ( + hass.states.get("switch.mock_title").attributes[ATTR_FRIENDLY_NAME] + == "Shop Switch" + )