Remove dead code in modbus sensor and 100% test coverage (#49634)

This commit is contained in:
jan iversen 2021-04-25 15:25:02 +02:00 committed by GitHub
parent 0862212942
commit 914451d99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -306,22 +306,16 @@ class ModbusRegisterSensor(RestoreEntity, SensorEntity):
v_result.append(f"{float(v_temp):.{self._precision}f}")
self._value = ",".join(map(str, v_result))
else:
val = val[0]
# Apply scale and precision to floats and ints
if isinstance(val, (float, int)):
val = self._scale * val + self._offset
val = self._scale * val[0] + self._offset
# We could convert int to float, and the code would still work; however
# we lose some precision, and unit tests will fail. Therefore, we do
# the conversion only when it's absolutely necessary.
if isinstance(val, int) and self._precision == 0:
self._value = str(val)
else:
self._value = f"{float(val):.{self._precision}f}"
else:
# Don't process remaining datatypes (bytes and booleans)
# We could convert int to float, and the code would still work; however
# we lose some precision, and unit tests will fail. Therefore, we do
# the conversion only when it's absolutely necessary.
if isinstance(val, int) and self._precision == 0:
self._value = str(val)
else:
self._value = f"{float(val):.{self._precision}f}"
self._available = True
self.schedule_update_ha_state()