Skip to content

Commit

Permalink
Improve error handling on startup (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl authored Oct 5, 2020
1 parent 85c10ed commit 4ba6694
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
25 changes: 15 additions & 10 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"""The TaHoma integration."""
import asyncio
from asyncio import TimeoutError
from collections import defaultdict
from datetime import timedelta
import logging

from aiohttp import ClientError, ServerDisconnectedError
from pyhoma.client import TahomaClient
from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException
from pyhoma.models import Command
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.scene import DOMAIN as SCENE
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_EXCLUDE, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, config_validation as cv

from .const import (
Expand Down Expand Up @@ -63,9 +65,7 @@ async def async_setup(hass: HomeAssistant, config: dict):

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=configuration,
DOMAIN, context={"source": SOURCE_IMPORT}, data=configuration,
)
)

Expand All @@ -84,12 +84,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

try:
await client.login()
except TooManyRequestsException:
_LOGGER.error("too_many_requests")
return False
devices = await client.get_devices()
scenarios = await client.get_scenarios()
except BadCredentialsException:
_LOGGER.error("invalid_auth")
return False
except TooManyRequestsException as exception:
_LOGGER.error("too_many_requests")
raise ConfigEntryNotReady from exception
except (TimeoutError, ClientError, ServerDisconnectedError) as exception:
_LOGGER.error("cannot_connect")
raise ConfigEntryNotReady from exception
except Exception as exception: # pylint: disable=broad-except
_LOGGER.exception(exception)
return False
Expand All @@ -101,14 +106,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
_LOGGER,
name="TaHoma Event Fetcher",
client=client,
devices=await client.get_devices(),
devices=devices,
update_interval=timedelta(seconds=update_interval),
)

await tahoma_coordinator.async_refresh()

entities = defaultdict(list)
entities[SCENE] = await client.get_scenarios()
entities[SCENE] = scenarios

hass.data[DOMAIN][entry.entry_id] = {
"entities": entities,
Expand Down
14 changes: 11 additions & 3 deletions custom_components/tahoma/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from aiohttp import ServerDisconnectedError
from pyhoma.client import TahomaClient
from pyhoma.enums import EventName, ExecutionState
from pyhoma.exceptions import NotAuthenticatedException
from pyhoma.exceptions import (
BadCredentialsException,
NotAuthenticatedException,
TooManyRequestsException,
)
from pyhoma.models import DataType, Device, State

from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -53,13 +57,17 @@ async def _async_update_data(self) -> Dict[str, Device]:
"""Fetch TaHoma data via event listener."""
try:
events = await self.client.fetch_events()
except (ServerDisconnectedError, NotAuthenticatedException) as exception:
_LOGGER.debug(exception)
except BadCredentialsException:
raise UpdateFailed("invalid_auth")
except TooManyRequestsException:
raise UpdateFailed("too_many_requests")
except (ServerDisconnectedError, NotAuthenticatedException):
self.executions = {}
await self.client.login()
self.devices = await self._get_devices()
return self.devices
except Exception as exception:
_LOGGER.debug(exception)
raise UpdateFailed(exception)

for event in events:
Expand Down

0 comments on commit 4ba6694

Please sign in to comment.