From fa60e9b03b1770b8b95c87463fc56d6c7f8bbdff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Mar 2020 23:41:59 -0500 Subject: [PATCH] Improve sense timeout exception handling (#33127) asyncio.Timeout needs to be trapped as well --- homeassistant/components/sense/__init__.py | 3 ++- homeassistant/components/sense/config_flow.py | 10 +++------- homeassistant/components/sense/const.py | 7 +++++++ homeassistant/components/sense/sensor.py | 5 ++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/sense/__init__.py b/homeassistant/components/sense/__init__.py index d7887f7ab01..13452c97088 100644 --- a/homeassistant/components/sense/__init__.py +++ b/homeassistant/components/sense/__init__.py @@ -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() diff --git a/homeassistant/components/sense/config_flow.py b/homeassistant/components/sense/config_flow.py index 68bbb9ed932..7a5b04229a1 100644 --- a/homeassistant/components/sense/config_flow.py +++ b/homeassistant/components/sense/config_flow.py @@ -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" diff --git a/homeassistant/components/sense/const.py b/homeassistant/components/sense/const.py index 619956903f2..882c3c9d79f 100644 --- a/homeassistant/components/sense/const.py +++ b/homeassistant/components/sense/const.py @@ -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", diff --git a/homeassistant/components/sense/sensor.py b/homeassistant/components/sense/sensor.py index 6fe7b59c46c..06cfb90d2b5 100644 --- a/homeassistant/components/sense/sensor.py +++ b/homeassistant/components/sense/sensor.py @@ -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