diff --git a/homeassistant/components/google_sheets/__init__.py b/homeassistant/components/google_sheets/__init__.py index a4c10da7f239..ea96288371c2 100644 --- a/homeassistant/components/google_sheets/__init__.py +++ b/homeassistant/components/google_sheets/__init__.py @@ -10,7 +10,7 @@ from google.oauth2.credentials import Credentials from gspread import Client import voluptuous as vol -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady @@ -69,6 +69,15 @@ def async_entry_has_scopes(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" hass.data[DOMAIN].pop(entry.entry_id) + loaded_entries = [ + entry + for entry in hass.config_entries.async_entries(DOMAIN) + if entry.state == ConfigEntryState.LOADED + ] + if len(loaded_entries) == 1: + for service_name in hass.services.async_services()[DOMAIN]: + hass.services.async_remove(DOMAIN, service_name) + return True diff --git a/tests/components/google_sheets/test_init.py b/tests/components/google_sheets/test_init.py index d060e01bac26..c32eb3455348 100644 --- a/tests/components/google_sheets/test_init.py +++ b/tests/components/google_sheets/test_init.py @@ -80,6 +80,7 @@ async def mock_setup_integration( assert len(entries) == 1 await hass.config_entries.async_unload(entries[0].entry_id) await hass.async_block_till_done() + assert not len(hass.services.async_services().get(DOMAIN, {})) assert not hass.data.get(DOMAIN) assert entries[0].state is ConfigEntryState.NOT_LOADED