Close hue api if setup fails (#112164)

fixes #109722
pull/112676/head
J. Nick Koston 2024-03-07 18:39:44 -10:00 committed by GitHub
parent a12fa0383b
commit 0382d628a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 1 deletions

View File

@ -71,10 +71,11 @@ class HueBridge:
async def async_initialize_bridge(self) -> bool: async def async_initialize_bridge(self) -> bool:
"""Initialize Connection with the Hue API.""" """Initialize Connection with the Hue API."""
setup_ok = False
try: try:
async with asyncio.timeout(10): async with asyncio.timeout(10):
await self.api.initialize() await self.api.initialize()
setup_ok = True
except (LinkButtonNotPressed, Unauthorized): except (LinkButtonNotPressed, Unauthorized):
# Usernames can become invalid if hub is reset or user removed. # Usernames can become invalid if hub is reset or user removed.
# We are going to fail the config entry setup and initiate a new # 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 except Exception: # pylint: disable=broad-except
self.logger.exception("Unknown error connecting to Hue bridge") self.logger.exception("Unknown error connecting to Hue bridge")
return False return False
finally:
if not setup_ok:
await self.api.close()
# v1 specific initialization/setup code here # v1 specific initialization/setup code here
if self.api_version == 1: if self.api_version == 1: