From dfde4039376274230aad5170b5c6959b94463404 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 16 Dec 2020 11:00:22 +0100 Subject: [PATCH] Remove Home Assistant Cast user when removing entry (#44228) * Remove Home Assistant Cast user when removing entry * Fix test * Fix test --- homeassistant/components/cast/__init__.py | 5 ++++ .../components/cast/home_assistant_cast.py | 11 +++++++ .../cast/test_home_assistant_cast.py | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/homeassistant/components/cast/__init__.py b/homeassistant/components/cast/__init__.py index 4dfb58ef3b7..49cec207764 100644 --- a/homeassistant/components/cast/__init__.py +++ b/homeassistant/components/cast/__init__.py @@ -29,3 +29,8 @@ async def async_setup_entry(hass, entry: config_entries.ConfigEntry): hass.config_entries.async_forward_entry_setup(entry, "media_player") ) return True + + +async def async_remove_entry(hass, entry): + """Remove Home Assistant Cast user.""" + await home_assistant_cast.async_remove_user(hass, entry) diff --git a/homeassistant/components/cast/home_assistant_cast.py b/homeassistant/components/cast/home_assistant_cast.py index 28672ef409c..3edc1ce2cde 100644 --- a/homeassistant/components/cast/home_assistant_cast.py +++ b/homeassistant/components/cast/home_assistant_cast.py @@ -72,3 +72,14 @@ async def async_setup_ha_cast( } ), ) + + +async def async_remove_user( + hass: core.HomeAssistant, entry: config_entries.ConfigEntry +): + """Remove Home Assistant Cast user.""" + user_id: Optional[str] = entry.data.get("user_id") + + if user_id is not None: + user = await hass.auth.async_get_user(user_id) + await hass.auth.async_remove_user(user) diff --git a/tests/components/cast/test_home_assistant_cast.py b/tests/components/cast/test_home_assistant_cast.py index 2fff760bb70..8ddb6e82eda 100644 --- a/tests/components/cast/test_home_assistant_cast.py +++ b/tests/components/cast/test_home_assistant_cast.py @@ -1,5 +1,6 @@ """Test Home Assistant Cast.""" +from homeassistant import config_entries from homeassistant.components.cast import home_assistant_cast from homeassistant.config import async_process_ha_core_config @@ -86,3 +87,32 @@ async def test_use_cloud_url(hass, mock_zeroconf): assert len(calls) == 1 controller = calls[0][0] assert controller.hass_url == "https://something.nabu.casa" + + +async def test_remove_entry(hass, mock_zeroconf): + """Test removing config entry removes user.""" + entry = MockConfigEntry( + connection_class=config_entries.CONN_CLASS_LOCAL_PUSH, + data={}, + domain="cast", + title="Google Cast", + ) + + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.cast.media_player._async_setup_platform" + ), patch( + "pychromecast.discovery.discover_chromecasts", return_value=(True, None) + ), patch( + "pychromecast.discovery.stop_discovery" + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + assert "cast" in hass.config.components + + user_id = entry.data.get("user_id") + assert await hass.auth.async_get_user(user_id) + + assert await hass.config_entries.async_remove(entry.entry_id) + assert not await hass.auth.async_get_user(user_id)