From 4c6cbadc11bbab816562cf281253474aee0ccbea Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Wed, 26 Jun 2024 21:53:02 +0300 Subject: [PATCH] Align Shelly sleeping devices timeout with non-sleeping (#118969) --- homeassistant/components/shelly/const.py | 4 +--- homeassistant/components/shelly/coordinator.py | 9 ++++----- tests/components/shelly/test_binary_sensor.py | 4 ++-- tests/components/shelly/test_coordinator.py | 9 ++++----- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index fcc7cc44af9..c5bdb88bbd1 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -83,11 +83,9 @@ REST_SENSORS_UPDATE_INTERVAL: Final = 60 # Refresh interval for RPC polling sensors RPC_SENSORS_POLLING_INTERVAL: Final = 60 -# Multiplier used to calculate the "update_interval" for sleeping devices. -SLEEP_PERIOD_MULTIPLIER: Final = 1.2 CONF_SLEEP_PERIOD: Final = "sleep_period" -# Multiplier used to calculate the "update_interval" for non-sleeping devices. +# Multiplier used to calculate the "update_interval" for shelly devices. UPDATE_PERIOD_MULTIPLIER: Final = 2.2 # Reconnect interval for GEN2 devices diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index fd5cfaa1542..02feef3633b 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -54,7 +54,6 @@ from .const import ( RPC_RECONNECT_INTERVAL, RPC_SENSORS_POLLING_INTERVAL, SHBTN_MODELS, - SLEEP_PERIOD_MULTIPLIER, UPDATE_PERIOD_MULTIPLIER, BLEScannerMode, ) @@ -229,7 +228,7 @@ class ShellyBlockCoordinator(ShellyCoordinatorBase[BlockDevice]): """Initialize the Shelly block device coordinator.""" self.entry = entry if self.sleep_period: - update_interval = SLEEP_PERIOD_MULTIPLIER * self.sleep_period + update_interval = UPDATE_PERIOD_MULTIPLIER * self.sleep_period else: update_interval = ( UPDATE_PERIOD_MULTIPLIER * device.settings["coiot"]["update_period"] @@ -429,7 +428,7 @@ class ShellyRestCoordinator(ShellyCoordinatorBase[BlockDevice]): in BATTERY_DEVICES_WITH_PERMANENT_CONNECTION ): update_interval = ( - SLEEP_PERIOD_MULTIPLIER * device.settings["coiot"]["update_period"] + UPDATE_PERIOD_MULTIPLIER * device.settings["coiot"]["update_period"] ) super().__init__(hass, entry, device, update_interval) @@ -459,7 +458,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): """Initialize the Shelly RPC device coordinator.""" self.entry = entry if self.sleep_period: - update_interval = SLEEP_PERIOD_MULTIPLIER * self.sleep_period + update_interval = UPDATE_PERIOD_MULTIPLIER * self.sleep_period else: update_interval = RPC_RECONNECT_INTERVAL super().__init__(hass, entry, device, update_interval) @@ -486,7 +485,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): data[CONF_SLEEP_PERIOD] = wakeup_period self.hass.config_entries.async_update_entry(self.entry, data=data) - update_interval = SLEEP_PERIOD_MULTIPLIER * wakeup_period + update_interval = UPDATE_PERIOD_MULTIPLIER * wakeup_period self.update_interval = timedelta(seconds=update_interval) return True diff --git a/tests/components/shelly/test_binary_sensor.py b/tests/components/shelly/test_binary_sensor.py index 026a7041863..3bfbf350f7e 100644 --- a/tests/components/shelly/test_binary_sensor.py +++ b/tests/components/shelly/test_binary_sensor.py @@ -7,7 +7,7 @@ from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN -from homeassistant.components.shelly.const import SLEEP_PERIOD_MULTIPLIER +from homeassistant.components.shelly.const import UPDATE_PERIOD_MULTIPLIER from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN from homeassistant.core import HomeAssistant, State from homeassistant.helpers.device_registry import DeviceRegistry @@ -122,7 +122,7 @@ async def test_block_rest_binary_sensor_connected_battery_devices( assert hass.states.get(entity_id).state == STATE_OFF # Verify update on slow intervals - await mock_rest_update(hass, freezer, seconds=SLEEP_PERIOD_MULTIPLIER * 3600) + await mock_rest_update(hass, freezer, seconds=UPDATE_PERIOD_MULTIPLIER * 3600) assert hass.states.get(entity_id).state == STATE_ON entry = entity_registry.async_get(entity_id) diff --git a/tests/components/shelly/test_coordinator.py b/tests/components/shelly/test_coordinator.py index 1e0af115c9e..35123a2db91 100644 --- a/tests/components/shelly/test_coordinator.py +++ b/tests/components/shelly/test_coordinator.py @@ -20,7 +20,6 @@ from homeassistant.components.shelly.const import ( ENTRY_RELOAD_COOLDOWN, MAX_PUSH_UPDATE_FAILURES, RPC_RECONNECT_INTERVAL, - SLEEP_PERIOD_MULTIPLIER, UPDATE_PERIOD_MULTIPLIER, BLEScannerMode, ) @@ -564,7 +563,7 @@ async def test_rpc_update_entry_sleep_period( # Move time to generate sleep period update monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 3600) - freezer.tick(timedelta(seconds=600 * SLEEP_PERIOD_MULTIPLIER)) + freezer.tick(timedelta(seconds=600 * UPDATE_PERIOD_MULTIPLIER)) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) @@ -596,7 +595,7 @@ async def test_rpc_sleeping_device_no_periodic_updates( assert get_entity_state(hass, entity_id) == "22.9" # Move time to generate polling - freezer.tick(timedelta(seconds=SLEEP_PERIOD_MULTIPLIER * 1000)) + freezer.tick(timedelta(seconds=UPDATE_PERIOD_MULTIPLIER * 1000)) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) @@ -889,7 +888,7 @@ async def test_block_sleeping_device_connection_error( assert get_entity_state(hass, entity_id) == STATE_ON # Move time to generate sleep period update - freezer.tick(timedelta(seconds=sleep_period * SLEEP_PERIOD_MULTIPLIER)) + freezer.tick(timedelta(seconds=sleep_period * UPDATE_PERIOD_MULTIPLIER)) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) @@ -934,7 +933,7 @@ async def test_rpc_sleeping_device_connection_error( assert get_entity_state(hass, entity_id) == STATE_ON # Move time to generate sleep period update - freezer.tick(timedelta(seconds=sleep_period * SLEEP_PERIOD_MULTIPLIER)) + freezer.tick(timedelta(seconds=sleep_period * UPDATE_PERIOD_MULTIPLIER)) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True)