From 12ac66645900b3227af29b62f7f35ea722932589 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 13 Jul 2021 21:45:42 +0200 Subject: [PATCH] only allow one active call in each platform. (#52823) --- .coveragerc | 3 +++ homeassistant/components/modbus/base_platform.py | 6 ++++++ homeassistant/components/modbus/binary_sensor.py | 6 ++++++ homeassistant/components/modbus/climate.py | 7 ++++++- homeassistant/components/modbus/cover.py | 5 +++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.coveragerc b/.coveragerc index bcfa7fc8e4f9..6f6adee207a4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -634,6 +634,9 @@ omit = homeassistant/components/mitemp_bt/sensor.py homeassistant/components/mjpeg/camera.py homeassistant/components/mochad/* + homeassistant/components/modbus/base_platform.py + homeassistant/components/modbus/binary_sensor.py + homeassistant/components/modbus/cover.py homeassistant/components/modbus/climate.py homeassistant/components/modbus/modbus.py homeassistant/components/modbus/validators.py diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 9c21ba3970cd..c382923e15dc 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -56,6 +56,7 @@ class BasePlatform(Entity): self._value = None self._available = True self._scan_interval = int(entry[CONF_SCAN_INTERVAL]) + self._call_active = False @abstractmethod async def async_update(self, now=None): @@ -174,9 +175,14 @@ class BaseSwitch(BasePlatform, RestoreEntity): self.async_write_ha_state() return + # do not allow multiple active calls to the same platform + if self._call_active: + return + self._call_active = True result = await self._hub.async_pymodbus_call( self._slave, self._verify_address, 1, self._verify_type ) + self._call_active = False if result is None: self._available = False self.async_write_ha_state() diff --git a/homeassistant/components/modbus/binary_sensor.py b/homeassistant/components/modbus/binary_sensor.py index bc586e2f24dc..0188210be8ab 100644 --- a/homeassistant/components/modbus/binary_sensor.py +++ b/homeassistant/components/modbus/binary_sensor.py @@ -54,9 +54,15 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity): async def async_update(self, now=None): """Update the state of the sensor.""" + + # do not allow multiple active calls to the same platform + if self._call_active: + return + self._call_active = True result = await self._hub.async_pymodbus_call( self._slave, self._address, 1, self._input_type ) + self._call_active = False if result is None: self._available = False self.async_write_ha_state() diff --git a/homeassistant/components/modbus/climate.py b/homeassistant/components/modbus/climate.py index 54894a4227ce..dbec27c3af67 100644 --- a/homeassistant/components/modbus/climate.py +++ b/homeassistant/components/modbus/climate.py @@ -194,13 +194,18 @@ class ModbusThermostat(BasePlatform, RestoreEntity, ClimateEntity): """Update Target & Current Temperature.""" # remark "now" is a dummy parameter to avoid problems with # async_track_time_interval + + # do not allow multiple active calls to the same platform + if self._call_active: + return + self._call_active = True self._target_temperature = await self._async_read_register( CALL_TYPE_REGISTER_HOLDING, self._target_temperature_register ) self._current_temperature = await self._async_read_register( self._input_type, self._address ) - + self._call_active = False self.async_write_ha_state() async def _async_read_register(self, register_type, register) -> float | None: diff --git a/homeassistant/components/modbus/cover.py b/homeassistant/components/modbus/cover.py index 88c8fd77ae85..bd150434dc15 100644 --- a/homeassistant/components/modbus/cover.py +++ b/homeassistant/components/modbus/cover.py @@ -149,9 +149,14 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity): """Update the state of the cover.""" # remark "now" is a dummy parameter to avoid problems with # async_track_time_interval + # do not allow multiple active calls to the same platform + if self._call_active: + return + self._call_active = True result = await self._hub.async_pymodbus_call( self._slave, self._address, 1, self._input_type ) + self._call_active = False if result is None: self._available = False self.async_write_ha_state()