Add lazy_error_count to modbus (#54412)

* Add lazy_error_count.

* Use -=

* Review comments.
This commit is contained in:
jan iversen 2021-08-21 15:49:50 +02:00 committed by GitHub
parent 51434c5faa
commit 33f660118f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 47 additions and 1 deletions

View file

@ -36,6 +36,7 @@ from .const import (
CALL_TYPE_X_REGISTER_HOLDINGS,
CONF_DATA_TYPE,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_PRECISION,
CONF_SCALE,
CONF_STATE_OFF,
@ -76,6 +77,8 @@ class BasePlatform(Entity):
self._attr_device_class = entry.get(CONF_DEVICE_CLASS)
self._attr_available = True
self._attr_unit_of_measurement = None
self._lazy_error_count = entry[CONF_LAZY_ERROR]
self._lazy_errors = self._lazy_error_count
@abstractmethod
async def async_update(self, now=None):
@ -245,10 +248,15 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
)
self._call_active = False
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self.async_write_ha_state()
return
self._lazy_errors = self._lazy_error_count
self._attr_available = True
if self._verify_type == CALL_TYPE_COIL:
self._attr_is_on = bool(result.bits[0] & 1)