Simplify calls to pymodbus (#50717)

* simplify pymodbus_call.

Do not call with a function object and a check attribute, call instead
with CALL_TYPE*.

Avoid if <type> call x else call y.
Call async_pymodbus_call directly, instead of unpacking/packing.

* Declare call type in __init__.

* Modbus.py back to 100% test coverage.
This commit is contained in:
jan iversen 2021-05-17 11:20:12 +02:00 committed by GitHub
parent 7b18860dcd
commit ff856a9bba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 34 deletions

View file

@ -283,14 +283,9 @@ class ModbusRegisterSensor(RestoreEntity, SensorEntity):
"""Update the state of the sensor."""
# remark "now" is a dummy parameter to avoid problems with
# async_track_time_interval
if self._register_type == CALL_TYPE_REGISTER_INPUT:
result = await self._hub.async_read_input_registers(
self._slave, self._register, self._count
)
else:
result = await self._hub.async_read_holding_registers(
self._slave, self._register, self._count
)
result = await self._hub.async_pymodbus_call(
self._slave, self._register, self._count, self._register_type
)
if result is None:
self._available = False
self.async_write_ha_state()