diff --git a/custom_components/adaptive_lighting/__init__.py b/custom_components/adaptive_lighting/__init__.py index be8868f5..fb68b80f 100755 --- a/custom_components/adaptive_lighting/__init__.py +++ b/custom_components/adaptive_lighting/__init__.py @@ -100,12 +100,16 @@ async def async_unload_entry(hass, config_entry: ConfigEntry) -> bool: ) data = hass.data[DOMAIN] data[config_entry.entry_id][UNDO_UPDATE_LISTENER]() - if len(data) == 1: # no more config_entries + if unload_ok: + data.pop(config_entry.entry_id) + + if len(data) == 1 and ATTR_TURN_ON_OFF_LISTENER in data: + # no more config_entries turn_on_off_listener = data.pop(ATTR_TURN_ON_OFF_LISTENER) turn_on_off_listener.remove_listener() turn_on_off_listener.remove_listener2() - if unload_ok: - data.pop(config_entry.entry_id) + if not data: + hass.data.pop(DOMAIN) return unload_ok diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 23d460e8..5aa91f7d 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -213,6 +213,7 @@ async def async_setup_entry( ): """Set up the AdaptiveLighting switch.""" data = hass.data[DOMAIN] + assert config_entry.entry_id in data if ATTR_TURN_ON_OFF_LISTENER not in data: data[ATTR_TURN_ON_OFF_LISTENER] = TurnOnOffListener(hass) @@ -880,16 +881,12 @@ def get_sun_events(self, date: datetime.datetime) -> Dict[str, float]: def _replace_time(date: datetime.datetime, key: str) -> datetime.datetime: time = getattr(self, f"{key}_time") - date_time = datetime.datetime.combine(datetime.date.today(), time) + date_time = datetime.datetime.combine(date, time) utc_time = self.time_zone.localize(date_time).astimezone(dt_util.UTC) - return date.replace( - hour=utc_time.hour, - minute=utc_time.minute, - second=utc_time.second, - microsecond=utc_time.microsecond, - ) + return utc_time location = self.astral_location + sunrise = ( location.sunrise(date, local=False) if self.sunrise_time is None