Solve modbus binary slave problem (#82338)

* Solve modbus binary slave problem.
This commit is contained in:
jan iversen 2022-11-26 11:11:50 +01:00 committed by GitHub
parent e1a0f8314e
commit 8ed4ce64c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 113 deletions

View file

@ -60,7 +60,7 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
"""Initialize the Modbus binary sensor."""
self._count = slave_count + 1
self._coordinator: DataUpdateCoordinator[Any] | None = None
self._result = None
self._result: list = []
super().__init__(hub, entry)
async def async_setup_slaves(
@ -106,15 +106,15 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self._result = None
self._result = []
else:
self._lazy_errors = self._lazy_error_count
self._attr_available = True
self._result = result
if self._input_type in (CALL_TYPE_COIL, CALL_TYPE_DISCRETE):
self._attr_is_on = bool(result.bits[0] & 1)
self._result = result.bits
else:
self._attr_is_on = bool(result.registers[0] & 1)
self._result = result.registers
self._attr_is_on = bool(self._result[0] & 1)
self.async_write_ha_state()
if self._coordinator:
@ -132,8 +132,7 @@ class SlaveSensor(CoordinatorEntity, RestoreEntity, BinarySensorEntity):
self._attr_name = f"{entry[CONF_NAME]} {idx}"
self._attr_device_class = entry.get(CONF_DEVICE_CLASS)
self._attr_available = False
self._result_inx = int(idx / 8)
self._result_bit = 2 ** (idx % 8)
self._result_inx = idx
super().__init__(coordinator)
async def async_added_to_hass(self) -> None:
@ -148,5 +147,5 @@ class SlaveSensor(CoordinatorEntity, RestoreEntity, BinarySensorEntity):
"""Handle updated data from the coordinator."""
result = self.coordinator.data
if result:
self._attr_is_on = result.bits[self._result_inx] & self._result_bit
self._attr_is_on = bool(result[self._result_inx] & 1)
super()._handle_coordinator_update()