Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make modbus retry fast on read errors #99576

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion homeassistant/components/modbus/base_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ 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)
self._cancel_call = 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)
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/modbus/sensor.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -106,12 +107,16 @@ async def async_update(self, now: datetime | None = None) -> None:
"""Update the state of the sensor."""
# remark "now" is a dummy parameter to avoid problems with
# async_track_time_interval
self._cancel_call = None
raw_result = await self._hub.async_pb_call(
self._slave, self._address, self._count, self._input_type
)
if raw_result is None:
janiversen marked this conversation as resolved.
Show resolved Hide resolved
if self._lazy_errors:
self._lazy_errors -= 1
self._cancel_call = async_call_later(
self.hass, timedelta(seconds=1), self.async_update
)
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
Expand Down
12 changes: 4 additions & 8 deletions tests/components/modbus/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,27 +946,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(
Expand Down