From 6947912fa99d3b900d8f55c65852d0b8e23d6fa2 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sat, 18 Sep 2021 08:57:27 +0200 Subject: [PATCH] Modbus entity update does not occur until after scan_interval (#56221) * Secure update is called when integration is started. * Review comments. * Update homeassistant/components/modbus/base_platform.py Co-authored-by: Paulus Schoutsen * Update homeassistant/components/modbus/base_platform.py Co-authored-by: Paulus Schoutsen Co-authored-by: Paulus Schoutsen --- .../components/modbus/base_platform.py | 30 ++++++++++++------- homeassistant/components/modbus/const.py | 4 +-- tests/components/modbus/test_sensor.py | 1 + 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 3167313fae8..64b7de1976c 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -28,6 +28,7 @@ from homeassistant.helpers.event import async_call_later, async_track_time_inter from homeassistant.helpers.restore_state import RestoreEntity from .const import ( + ACTIVE_SCAN_INTERVAL, CALL_TYPE_COIL, CALL_TYPE_DISCRETE, CALL_TYPE_REGISTER_HOLDING, @@ -78,6 +79,7 @@ class BasePlatform(Entity): self._scan_interval = int(entry[CONF_SCAN_INTERVAL]) self._call_active = False self._cancel_timer: Callable[[], None] | None = None + self._cancel_call: Callable[[], None] | None = None self._attr_name = entry[CONF_NAME] self._attr_should_poll = False @@ -92,11 +94,11 @@ class BasePlatform(Entity): """Virtual function to be overwritten.""" @callback - def async_remote_start(self) -> None: + def async_run(self) -> None: """Remote start entity.""" - if self._cancel_timer: - self._cancel_timer() - self._cancel_timer = None + self.async_hold(update=False) + if self._scan_interval == 0 or self._scan_interval > ACTIVE_SCAN_INTERVAL: + self._cancel_call = async_call_later(self.hass, 1, 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) @@ -105,20 +107,26 @@ class BasePlatform(Entity): self.async_write_ha_state() @callback - def async_remote_stop(self) -> None: + def async_hold(self, update=True) -> None: """Remote stop entity.""" + if self._cancel_call: + self._cancel_call() + self._cancel_call = None if self._cancel_timer: self._cancel_timer() self._cancel_timer = None - self._attr_available = False - self.async_write_ha_state() + if update: + self._attr_available = False + self.async_write_ha_state() async def async_base_added_to_hass(self): """Handle entity which will be added.""" - self.async_remote_start() - async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_remote_stop) - async_dispatcher_connect( - self.hass, SIGNAL_START_ENTITY, self.async_remote_start + self.async_run() + self.async_on_remove( + async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_hold) + ) + self.async_on_remove( + async_dispatcher_connect(self.hass, SIGNAL_START_ENTITY, self.async_run) ) diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index f2c3b7dd19c..d3240565982 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -118,11 +118,11 @@ DEFAULT_HUB = "modbus_hub" DEFAULT_SCAN_INTERVAL = 15 # seconds DEFAULT_SLAVE = 1 DEFAULT_STRUCTURE_PREFIX = ">f" - - DEFAULT_TEMP_UNIT = "C" MODBUS_DOMAIN = "modbus" +ACTIVE_SCAN_INTERVAL = 2 # limit to force an extra update + PLATFORMS = ( (BINARY_SENSOR_DOMAIN, CONF_BINARY_SENSORS), (CLIMATE_DOMAIN, CONF_CLIMATES), diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index 5227be835db..e2435d6acc8 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -246,6 +246,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl { CONF_NAME: TEST_ENTITY_NAME, CONF_ADDRESS: 51, + CONF_SCAN_INTERVAL: 1, }, ], },