From 2c278da442bad44bb9ad2b83d591fe941a757f95 Mon Sep 17 00:00:00 2001 From: samuelspagl Date: Tue, 12 Mar 2024 15:04:30 +0100 Subject: [PATCH] Add `ConfigEntryNotReady` exception when the update during setup fails --- CHANGELOG.md | 7 +++++++ custom_components/samsung_soundbar/__init__.py | 9 ++++++++- custom_components/samsung_soundbar/manifest.json | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b48de..74fb511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [0.3.1] Let HomeAssistant schedule failing setup of integration + +### Added + +- Add a `try / catch` mechanism to the first update of the client while setup and throw a + `ConfigEntryNotReady` exception when the update fails + ## [0.3.0] Icons and Chore ### Added diff --git a/custom_components/samsung_soundbar/__init__.py b/custom_components/samsung_soundbar/__init__.py index aa03b87..2685918 100644 --- a/custom_components/samsung_soundbar/__init__.py +++ b/custom_components/samsung_soundbar/__init__.py @@ -1,7 +1,9 @@ import logging +from aiohttp import ClientResponseError from homeassistant.config_entries import ConfigEntry from homeassistant.core import DOMAIN, HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession from pysmartthings import SmartThings @@ -49,7 +51,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.data.get(CONF_ENTRY_MAX_VOLUME), entry.data.get(CONF_ENTRY_DEVICE_NAME), ) - await soundbar_device.update() + try: + await soundbar_device.update() + except ClientResponseError as excp: + raise ConfigEntryNotReady("An error occurred while setting up the soundbar device. " + "Please recheck whether the device has power or is connected to the internet.")\ + from excp domain_config.devices[entry.data.get(CONF_ENTRY_DEVICE_ID)] = DeviceConfig( entry.data, soundbar_device ) diff --git a/custom_components/samsung_soundbar/manifest.json b/custom_components/samsung_soundbar/manifest.json index df644b0..b3363ff 100644 --- a/custom_components/samsung_soundbar/manifest.json +++ b/custom_components/samsung_soundbar/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/samuelspagl/ha_samsung_soundbar/issues", "requirements": ["pysmartthings"], - "version": "0.3.0" + "version": "0.3.1" }