Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions custom_components/dahua/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from datetime import timedelta

from aiohttp import ClientError, ClientResponseError, ClientSession, TCPConnector
from aiohttp import ClientError, ClientResponseError, ClientConnectorError, ClientSession, TCPConnector
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady, PlatformNotReady
Expand Down Expand Up @@ -70,6 +70,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await coordinator.async_config_entry_first_refresh()

if not coordinator.last_update_success:
_LOGGER.warning("dahua async_setup_entry for init, data not ready")
raise ConfigEntryNotReady

hass.data[DOMAIN][entry.entry_id] = coordinator
Expand Down Expand Up @@ -179,6 +180,16 @@ async def _close_session(self) -> None:
_LOGGER.exception("serverConnect - failed to close session")

async def _async_update_data(self):
try:
data = await self._async_update_data_int()
return data
except Exception as ex:
# If we let an exception bubble up, it seems to result in self being
# deleted. So clean up first.
await self._close_session()
raise

async def _async_update_data_int(self):
"""Reload the camera information"""
data = {}

Expand Down Expand Up @@ -280,9 +291,15 @@ async def _async_update_data(self):
await self.async_start_vto_event_listener()

self.initialized = True
except (ClientConnectorError, asyncio.TimeoutError) as exception:
_LOGGER.warning(exception)
# Pass the exception on up. Our caller
# homeassistant/helpers/update_coordinator.py:_async_refresh()
# gracefully handles some common errors like timeout and connection errors.
raise
except Exception as exception:
_LOGGER.error("Failed to initialize device at %s", self._address, exc_info=exception)
raise PlatformNotReady("Dahua device at " + self._address + " isn't fully initialized yet")
raise PlatformNotReady("Dahua device at " + self._address + " isn't fully initialized yet") from exception

# This is the event loop code that's called every n seconds
try:
Expand Down