Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modbus, allow received int to be a float. #110648

Merged
merged 2 commits into from Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/modbus/base_platform.py
Expand Up @@ -199,6 +199,8 @@ def __init__(self, hass: HomeAssistant, hub: ModbusHub, config: dict) -> None:
self._precision = config.get(CONF_PRECISION, 2)
else:
self._precision = config.get(CONF_PRECISION, 0)
if self._precision > 0 or self._scale != int(self._scale):
self._value_is_int = False
Comment on lines +202 to +203
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the default precision not be 2 if the returned value is not an int?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no, that is being set in line 199. The default of 2 is if the user configures e.g. "datatype: float", not what's being returned.

The user can configure "precision: 3" and "datatype: int32" then the returned value will e.g. be "1.000"
The user can configure "scale: 0.001" and "datatype:int32" then value 123 would be returned as 1.23

This is however not what is happening here, in this case user have configured e.g. "datatype: int16" and "precision: 2" or "scale: 0.001" that means that the returned value is no longer and int but a float, and hence "self._value_is_int = False" is correct.


def _swap_registers(self, registers: list[int], slave_count: int) -> list[int]:
"""Do swap as needed."""
Expand Down