Skip to content

Commit

Permalink
Fix integration stuck when SDK become unavailable (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaskivskyi authored Dec 26, 2023
1 parent 5f69b94 commit 797c3a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 6 additions & 3 deletions custom_components/chroma/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

from __future__ import annotations

import aiohttp
import logging
from typing import Any

import aiohttp
from aiochroma import AIOChroma, ChromaError
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import UpdateFailed

from aiochroma import AIOChroma
from .const import CONF_DEVICES, CONF_LAYOUT, DEFAULT_LAYOUT, SENSORS_TYPE_LIGHT

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -70,7 +70,10 @@ async def async_connect(self) -> None:
async def async_disconnect(self) -> None:
"""Disconnect from the device."""

await self._api.async_disconnect()
try:
await self._api.async_disconnect()
except ChromaError as ex:
pass

async def async_get_available_sensors(self) -> dict[str, dict[str, Any]]:
"""Get a dictionary of available sensors."""
Expand Down
11 changes: 8 additions & 3 deletions custom_components/chroma/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from datetime import timedelta
from typing import Any, Awaitable, Callable, TypeVar

from aiochroma import ChromaError, Color
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from aiochroma import Color
from .bridge import ChromaBridge
from .const import (
CONF_REQ_RELOAD,
Expand Down Expand Up @@ -115,8 +115,10 @@ async def setup(self) -> None:
)

try:
_LOGGER.debug("Connecting to Chroma SDK on %s", self._host)
await self._api.async_connect()
except OSError as ex:
_LOGGER.debug("Failed to connect to Chroma SDK on %s", self._host)
raise ConfigEntryNotReady from ex

# Services -->
Expand Down Expand Up @@ -184,8 +186,11 @@ async def init_sensors_coordinator(self) -> None:
async def close(self) -> None:
"""Close the connection."""

if self._api is not None:
await self._api.async_disconnect()
if self._api is not None and self._api._api.connected:
try:
await self._api.async_disconnect()
except ChromaError as ex:
pass
self._api = None

for func in self._on_close:
Expand Down

0 comments on commit 797c3a6

Please sign in to comment.