From 74c4553bb03ecfcb894972a702457aefc92fd490 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 16 Apr 2025 14:02:27 +0200 Subject: [PATCH] Increase uptime deviation for Shelly (#142996) * Increase uptime deviation for Shelly * fix test * make troubleshooting easy * change deviation interval * increase deviation to 1m --- homeassistant/components/shelly/const.py | 2 +- homeassistant/components/shelly/utils.py | 12 +++++++++++- tests/components/shelly/test_utils.py | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index 0c64df52409..cc3ec564b3f 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -209,7 +209,7 @@ KELVIN_MIN_VALUE_COLOR: Final = 3000 BLOCK_WRONG_SLEEP_PERIOD = 21600 BLOCK_EXPECTED_SLEEP_PERIOD = 43200 -UPTIME_DEVIATION: Final = 5 +UPTIME_DEVIATION: Final = 60 # Time to wait before reloading entry upon device config change ENTRY_RELOAD_COOLDOWN = 60 diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index a5e08faf0e0..9284afdd567 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -200,8 +200,18 @@ def get_device_uptime(uptime: float, last_uptime: datetime | None) -> datetime: if ( not last_uptime - or abs((delta_uptime - last_uptime).total_seconds()) > UPTIME_DEVIATION + or (diff := abs((delta_uptime - last_uptime).total_seconds())) + > UPTIME_DEVIATION ): + if last_uptime: + LOGGER.debug( + "Time deviation %s > %s: uptime=%s, last_uptime=%s, delta_uptime=%s", + diff, + UPTIME_DEVIATION, + uptime, + last_uptime, + delta_uptime, + ) return delta_uptime return last_uptime diff --git a/tests/components/shelly/test_utils.py b/tests/components/shelly/test_utils.py index b7c3dff10f6..ae3caa93825 100644 --- a/tests/components/shelly/test_utils.py +++ b/tests/components/shelly/test_utils.py @@ -21,6 +21,7 @@ from homeassistant.components.shelly.const import ( GEN1_RELEASE_URL, GEN2_BETA_RELEASE_URL, GEN2_RELEASE_URL, + UPTIME_DEVIATION, ) from homeassistant.components.shelly.utils import ( get_block_channel_name, @@ -188,8 +189,9 @@ async def test_get_device_uptime() -> None: ) == dt_util.as_utc(dt_util.parse_datetime("2019-01-10 18:42:00+00:00")) assert get_device_uptime( - 50, dt_util.as_utc(dt_util.parse_datetime("2019-01-10 18:42:00+00:00")) - ) == dt_util.as_utc(dt_util.parse_datetime("2019-01-10 18:42:10+00:00")) + 55 - UPTIME_DEVIATION, + dt_util.as_utc(dt_util.parse_datetime("2019-01-10 18:42:00+00:00")), + ) == dt_util.as_utc(dt_util.parse_datetime("2019-01-10 18:43:05+00:00")) async def test_get_block_input_triggers(