From 0382d628a3b470f3c07bd8dd200448fc3ae392de Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 7 Mar 2024 18:39:44 -1000 Subject: [PATCH] Close hue api if setup fails (#112164) fixes #109722 --- homeassistant/components/hue/bridge.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/hue/bridge.py b/homeassistant/components/hue/bridge.py index abf91cf4577..86db9c4021c 100644 --- a/homeassistant/components/hue/bridge.py +++ b/homeassistant/components/hue/bridge.py @@ -71,10 +71,11 @@ class HueBridge: async def async_initialize_bridge(self) -> bool: """Initialize Connection with the Hue API.""" + setup_ok = False try: async with asyncio.timeout(10): await self.api.initialize() - + setup_ok = True except (LinkButtonNotPressed, Unauthorized): # Usernames can become invalid if hub is reset or user removed. # We are going to fail the config entry setup and initiate a new @@ -95,6 +96,9 @@ class HueBridge: except Exception: # pylint: disable=broad-except self.logger.exception("Unknown error connecting to Hue bridge") return False + finally: + if not setup_ok: + await self.api.close() # v1 specific initialization/setup code here if self.api_version == 1: