Skip to content

Commit

Permalink
Improve error handling again
Browse files Browse the repository at this point in the history
  • Loading branch information
planbnet committed Feb 22, 2024
1 parent 251f6e3 commit 3c61679
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
14 changes: 12 additions & 2 deletions custom_components/livisi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from typing import Final

from homeassistant import core
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er

from .livisi_errors import WrongCredentialException


from .const import CONF_HOST, DOMAIN, LOGGER, SWITCH_DEVICE_TYPES
from .coordinator import LivisiDataUpdateCoordinator
Expand All @@ -32,7 +35,14 @@
async def async_setup_entry(hass: core.HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Livisi Smart Home from a config entry."""
coordinator = LivisiDataUpdateCoordinator(hass, entry)
await coordinator.async_setup()
try:
await coordinator.async_setup()
except Exception as exception:
LOGGER.error(exception, exc_info=True)
# no need to retry if the credentials are wrong
if isinstance(exception, WrongCredentialException):
return False
raise ConfigEntryNotReady(exception) from exception

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
device_registry = dr.async_get(hass)
Expand Down Expand Up @@ -93,7 +103,7 @@ async def async_migrate_entry(hass, config_entry):
await coordinator.async_setup()
devices = await coordinator.async_get_devices()
except Exception as exception:
LOGGER.error(exception)
LOGGER.error(exception, exc_info=True)
return False
finally:
if coordinator.aiolivisi is not None:
Expand Down
23 changes: 4 additions & 19 deletions custom_components/livisi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from aiohttp import ClientConnectorError

from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_send
Expand All @@ -17,13 +16,6 @@
from .livisi_connector import LivisiConnection, connect as livisi_connect
from .livisi_websocket import LivisiWebsocketEvent

from .livisi_errors import (
IncorrectIpAddressException,
ShcUnreachableException,
WrongCredentialException,
)


from .const import (
CONF_HOST,
CONF_PASSWORD,
Expand Down Expand Up @@ -66,17 +58,10 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:

async def async_setup(self) -> None:
"""Set up the Livisi Smart Home Controller."""
try:
self.aiolivisi = await livisi_connect(
self.config_entry.data[CONF_HOST],
self.config_entry.data[CONF_PASSWORD],
)
except ShcUnreachableException as exception:
raise ConfigEntryNotReady from exception
except WrongCredentialException as exception:
raise ConfigEntryNotReady from exception
except IncorrectIpAddressException as exception:
raise ConfigEntryNotReady from exception
self.aiolivisi = await livisi_connect(
self.config_entry.data[CONF_HOST],
self.config_entry.data[CONF_PASSWORD],
)

async def _async_update_data(self) -> list[LivisiDevice]:
"""Get device configuration from LIVISI."""
Expand Down
5 changes: 2 additions & 3 deletions custom_components/livisi/livisi_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ async def connect(self, host: str, password: str):
self._password = password
try:
await self._async_retrieve_token()
except Exception as connection_error:
LOGGER.error(connection_error)
except:
await self.close()
raise connection_error
raise

self.controller = await self._async_get_controller()
if self.controller.is_v2:
Expand Down

0 comments on commit 3c61679

Please sign in to comment.