Improve sense timeout exception handling (#33127)

asyncio.Timeout needs to be trapped as well
pull/33176/head
J. Nick Koston 2020-03-22 23:41:59 -05:00 committed by GitHub
parent 0b2a8bf79a
commit fa60e9b03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 11 deletions

View File

@ -26,6 +26,7 @@ from .const import (
SENSE_DEVICE_UPDATE,
SENSE_DEVICES_DATA,
SENSE_DISCOVERED_DEVICES_DATA,
SENSE_TIMEOUT_EXCEPTIONS,
)
_LOGGER = logging.getLogger(__name__)
@ -101,7 +102,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
except SenseAuthenticationException:
_LOGGER.error("Could not authenticate with sense server")
return False
except SenseAPITimeoutException:
except SENSE_TIMEOUT_EXCEPTIONS:
raise ConfigEntryNotReady
sense_devices_data = SenseDevicesData()

View File

@ -1,17 +1,13 @@
"""Config flow for Sense integration."""
import logging
from sense_energy import (
ASyncSenseable,
SenseAPITimeoutException,
SenseAuthenticationException,
)
from sense_energy import ASyncSenseable, SenseAuthenticationException
import voluptuous as vol
from homeassistant import config_entries, core
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
from .const import ACTIVE_UPDATE_RATE, DEFAULT_TIMEOUT
from .const import ACTIVE_UPDATE_RATE, DEFAULT_TIMEOUT, SENSE_TIMEOUT_EXCEPTIONS
from .const import DOMAIN # pylint:disable=unused-import; pylint:disable=unused-import
@ -55,7 +51,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
info = await validate_input(self.hass, user_input)
await self.async_set_unique_id(user_input[CONF_EMAIL])
return self.async_create_entry(title=info["title"], data=user_input)
except SenseAPITimeoutException:
except SENSE_TIMEOUT_EXCEPTIONS:
errors["base"] = "cannot_connect"
except SenseAuthenticationException:
errors["base"] = "invalid_auth"

View File

@ -1,4 +1,9 @@
"""Constants for monitoring a Sense energy sensor."""
import asyncio
from sense_energy import SenseAPITimeoutException
DOMAIN = "sense"
DEFAULT_TIMEOUT = 10
ACTIVE_UPDATE_RATE = 60
@ -18,6 +23,8 @@ PRODUCTION_ID = "production"
ICON = "mdi:flash"
SENSE_TIMEOUT_EXCEPTIONS = (asyncio.TimeoutError, SenseAPITimeoutException)
MDI_ICONS = {
"ac": "air-conditioner",
"aquarium": "fish",

View File

@ -2,8 +2,6 @@
from datetime import timedelta
import logging
from sense_energy import SenseAPITimeoutException
from homeassistant.const import DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, POWER_WATT
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -24,6 +22,7 @@ from .const import (
SENSE_DEVICE_UPDATE,
SENSE_DEVICES_DATA,
SENSE_DISCOVERED_DEVICES_DATA,
SENSE_TIMEOUT_EXCEPTIONS,
)
MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=300)
@ -256,7 +255,7 @@ class SenseTrendsSensor(Entity):
try:
await self.update_sensor()
except SenseAPITimeoutException:
except SENSE_TIMEOUT_EXCEPTIONS:
_LOGGER.error("Timeout retrieving data")
return