From 20dd81695e94ff848caf740d83aa91b964410480 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sun, 3 Sep 2023 18:19:47 +0200 Subject: [PATCH] Fast retry on read errors. --- homeassistant/components/modbus/base_platform.py | 2 +- homeassistant/components/modbus/sensor.py | 4 +++- tests/components/modbus/test_sensor.py | 12 ++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index b71f8c2021584c..5e3d009ae02936 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -115,7 +115,7 @@ async def async_update(self, now: datetime | None = None) -> None: def async_run(self) -> None: """Remote start entity.""" self.async_hold(update=False) - self._cancel_call = async_call_later(self.hass, 1, self.async_update) + async_call_later(self.hass, timedelta(milliseconds=100), self.async_update) if self._scan_interval > 0: self._cancel_timer = async_track_time_interval( self.hass, self.async_update, timedelta(seconds=self._scan_interval) diff --git a/homeassistant/components/modbus/sensor.py b/homeassistant/components/modbus/sensor.py index fe2d4bc415d88a..e9d0160237d37e 100644 --- a/homeassistant/components/modbus/sensor.py +++ b/homeassistant/components/modbus/sensor.py @@ -1,7 +1,7 @@ """Support for Modbus Register sensors.""" from __future__ import annotations -from datetime import datetime +from datetime import datetime, timedelta import logging from typing import Any @@ -19,6 +19,7 @@ ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.event import async_call_later from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, @@ -112,6 +113,7 @@ async def async_update(self, now: datetime | None = None) -> None: if raw_result is None: if self._lazy_errors: self._lazy_errors -= 1 + async_call_later(self.hass, timedelta(seconds=1), self.async_update) return self._lazy_errors = self._lazy_error_count self._attr_available = False diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index a746bcda3ba53b..f2edf005863c98 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -914,27 +914,23 @@ async def test_wrong_unpack(hass: HomeAssistant, mock_do_cycle) -> None: ], ) @pytest.mark.parametrize( - ("register_words", "do_exception", "start_expect", "end_expect"), + ("register_words", "do_exception"), [ ( [0x8000], True, - "17", - STATE_UNAVAILABLE, ), ], ) async def test_lazy_error_sensor( - hass: HomeAssistant, mock_do_cycle: FrozenDateTimeFactory, start_expect, end_expect + hass: HomeAssistant, mock_do_cycle: FrozenDateTimeFactory ) -> None: """Run test for sensor.""" hass.states.async_set(ENTITY_ID, 17) await hass.async_block_till_done() - assert hass.states.get(ENTITY_ID).state == start_expect + assert hass.states.get(ENTITY_ID).state == "17" await do_next_cycle(hass, mock_do_cycle, 5) - assert hass.states.get(ENTITY_ID).state == start_expect - await do_next_cycle(hass, mock_do_cycle, 11) - assert hass.states.get(ENTITY_ID).state == end_expect + assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE @pytest.mark.parametrize(