From ac12d2a4637144a604b84854ec0569a58b586aef Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Sat, 4 May 2024 20:16:58 +0200 Subject: [PATCH] fix UnboundLocalError on modified_statistic_ids in compile_statistics (#116795) Co-authored-by: J. Nick Koston --- homeassistant/components/recorder/statistics.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 41cf4e22b53..572731a9fed 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -485,6 +485,12 @@ def compile_statistics(instance: Recorder, start: datetime, fire_events: bool) - The actual calculation is delegated to the platforms. """ + # Define modified_statistic_ids outside of the "with" statement as + # _compile_statistics may raise and be trapped by + # filter_unique_constraint_integrity_error which would make + # modified_statistic_ids unbound. + modified_statistic_ids: set[str] | None = None + # Return if we already have 5-minute statistics for the requested period with session_scope( session=instance.get_session(),