From 344cd0d71fb947e57b0c74aec23a6caaf5b3dd51 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 1 Dec 2021 16:59:33 -0700 Subject: [PATCH] Only unload RainMachine services if the last config entry is loaded (#60805) --- homeassistant/components/rainmachine/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/rainmachine/__init__.py b/homeassistant/components/rainmachine/__init__.py index 76a02fa94a8..9f7f014f13d 100644 --- a/homeassistant/components/rainmachine/__init__.py +++ b/homeassistant/components/rainmachine/__init__.py @@ -11,7 +11,7 @@ from regenmaschine.controller import Controller from regenmaschine.errors import RainMachineError import voluptuous as vol -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import ( CONF_DEVICE_ID, CONF_IP_ADDRESS, @@ -313,9 +313,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) - if len(hass.config_entries.async_entries(DOMAIN)) == 1: - # If this is the last instance of RainMachine, deregister any services defined - # during integration setup: + loaded_entries = [ + entry + for entry in hass.config_entries.async_entries(DOMAIN) + if entry.state == ConfigEntryState.LOADED + ] + if len(loaded_entries) == 1: + # If this is the last loaded instance of RainMachine, deregister any services + # defined during integration setup: for service_name in ( SERVICE_NAME_PAUSE_WATERING, SERVICE_NAME_PUSH_WEATHER_DATA,