From 8b998365a46a3a3db5b46dddfce9b65025d91c46 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sun, 8 Nov 2020 22:49:41 +0200 Subject: [PATCH] Increase update timeouts used in Shelly integration (#42937) Co-authored-by: Paulus Schoutsen --- homeassistant/components/shelly/__init__.py | 25 ++++++++++++++++----- homeassistant/components/shelly/const.py | 12 ++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 8696a0a23b5..1f6ecdbd031 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -23,7 +23,14 @@ from homeassistant.helpers import ( update_coordinator, ) -from .const import DATA_CONFIG_ENTRY, DOMAIN +from .const import ( + DATA_CONFIG_ENTRY, + DOMAIN, + POLLING_TIMEOUT_MULTIPLIER, + SETUP_ENTRY_TIMEOUT_SEC, + SLEEP_PERIOD_MULTIPLIER, + UPDATE_PERIOD_MULTIPLIER, +) PLATFORMS = ["binary_sensor", "cover", "light", "sensor", "switch"] _LOGGER = logging.getLogger(__name__) @@ -66,7 +73,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): coap_context = await get_coap_context(hass) try: - async with async_timeout.timeout(5): + async with async_timeout.timeout(SETUP_ENTRY_TIMEOUT_SEC): device = await aioshelly.Device.create( aiohttp_client.async_get_clientsession(hass), coap_context, @@ -100,9 +107,13 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): if sleep_mode["unit"] == "h": sleep_period *= 60 # hours to minutes - update_interval = 1.2 * sleep_period * 60 # minutes to seconds + update_interval = ( + SLEEP_PERIOD_MULTIPLIER * sleep_period * 60 + ) # minutes to seconds else: - update_interval = 2 * device.settings["coiot"]["update_period"] + update_interval = ( + UPDATE_PERIOD_MULTIPLIER * device.settings["coiot"]["update_period"] + ) super().__init__( hass, @@ -118,8 +129,12 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): async def _async_update_data(self): """Fetch data.""" + _LOGGER.debug("Polling Shelly Device - %s", self.name) try: - async with async_timeout.timeout(5): + async with async_timeout.timeout( + POLLING_TIMEOUT_MULTIPLIER + * self.device.settings["coiot"]["update_period"] + ): return await self.device.update() except OSError as err: raise update_coordinator.UpdateFailed("Error fetching data") from err diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index c17fc28c378..50af82c2b7d 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -2,3 +2,15 @@ DATA_CONFIG_ENTRY = "config_entry" DOMAIN = "shelly" + +# Used to calculate the timeout in "_async_update_data" used for polling data from devices. +POLLING_TIMEOUT_MULTIPLIER = 1.2 + +# Timeout used for initial entry setup in "async_setup_entry". +SETUP_ENTRY_TIMEOUT_SEC = 10 + +# Multiplier used to calculate the "update_interval" for sleeping devices. +SLEEP_PERIOD_MULTIPLIER = 1.2 + +# Multiplier used to calculate the "update_interval" for non-sleeping devices. +UPDATE_PERIOD_MULTIPLIER = 2.2