Fix uncaught exception in sense and retry later (#58623)

pull/58319/head
J. Nick Koston 2021-10-28 14:27:19 -05:00 committed by GitHub
parent 05353f8e13
commit 5f36fd2a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -3,11 +3,7 @@ import asyncio
from datetime import timedelta
import logging
from sense_energy import (
ASyncSenseable,
SenseAPITimeoutException,
SenseAuthenticationException,
)
from sense_energy import ASyncSenseable, SenseAuthenticationException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -30,6 +26,7 @@ from .const import (
SENSE_DEVICE_UPDATE,
SENSE_DEVICES_DATA,
SENSE_DISCOVERED_DEVICES_DATA,
SENSE_EXCEPTIONS,
SENSE_TIMEOUT_EXCEPTIONS,
SENSE_TRENDS_COORDINATOR,
)
@ -76,14 +73,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.error("Could not authenticate with sense server")
return False
except SENSE_TIMEOUT_EXCEPTIONS as err:
raise ConfigEntryNotReady from err
raise ConfigEntryNotReady(
str(err) or "Timed out during authentication"
) from err
except SENSE_EXCEPTIONS as err:
raise ConfigEntryNotReady(str(err) or "Error during authentication") from err
sense_devices_data = SenseDevicesData()
try:
sense_discovered_devices = await gateway.get_discovered_device_data()
await gateway.update_realtime()
except SENSE_TIMEOUT_EXCEPTIONS as err:
raise ConfigEntryNotReady from err
raise ConfigEntryNotReady(
str(err) or "Timed out during realtime update"
) from err
except SENSE_EXCEPTIONS as err:
raise ConfigEntryNotReady(str(err) or "Error during realtime update") from err
trends_coordinator = DataUpdateCoordinator(
hass,
@ -114,8 +119,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Retrieve latest state."""
try:
await gateway.update_realtime()
except SenseAPITimeoutException:
_LOGGER.error("Timeout retrieving data")
except SENSE_TIMEOUT_EXCEPTIONS as ex:
_LOGGER.error("Timeout retrieving data: %s", ex)
except SENSE_EXCEPTIONS as ex:
_LOGGER.error("Failed to update data: %s", ex)
data = gateway.get_realtime()
if "devices" in data:

View File

@ -1,8 +1,10 @@
"""Constants for monitoring a Sense energy sensor."""
import asyncio
import socket
from sense_energy import SenseAPITimeoutException
from sense_energy.sense_exceptions import SenseWebsocketException
DOMAIN = "sense"
DEFAULT_TIMEOUT = 10
@ -37,6 +39,7 @@ SOLAR_POWERED_ID = "solar_powered"
ICON = "mdi:flash"
SENSE_TIMEOUT_EXCEPTIONS = (asyncio.TimeoutError, SenseAPITimeoutException)
SENSE_EXCEPTIONS = (socket.gaierror, SenseWebsocketException)
MDI_ICONS = {
"ac": "air-conditioner",