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

Replace Float 'nan' with None for modbus floats #93832

Merged
merged 3 commits into from Aug 6, 2023

Conversation

String-656
Copy link
Contributor

@String-656 String-656 commented May 30, 2023

Proposed change

Detect NaN from modbus registers. Float 16/32/64 can return NaN 0x7FC0 which currently gets converted to a string, resulting in the core throwing an error as it's expecting a number. The PR detects 'nan' in unpack_structure_result, replacing with None

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @adamchengtkc, @janiversen, @vzahradnik, mind taking a look at this pull request as it has been labeled with an integration (modbus) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of modbus can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign modbus Removes the current integration label and assignees on the pull request, add the integration domain after the command.

Copy link
Member

@janiversen janiversen left a comment

Choose a reason for hiding this comment

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

Test cases are missing, to verify your changes work.

v_result.append(f"{float(v_temp):.{self._precision}f}")
# NaN float detection replace with None
# Issue: https://github.com/home-assistant/core/issues/93297
if v_temp != v_temp:
Copy link
Member

Choose a reason for hiding this comment

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

This looks wrong. this can never be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

floats that equal NaN are never equal to itself. The array temp variable, "v_temp" and further down the "val_result" are floats. I will provide testing evidence showing the case when NaN is received.

Copy link
Member

Choose a reason for hiding this comment

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

you test if the variable if different from the same variable.

If you look at the failing tests, pylint also mark this as an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, that's one way of detecting NaN. See if it is equal to its self.
https://stackoverflow.com/questions/10034149/why-is-nan-not-equal-to-nan

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft May 31, 2023 07:04
@String-656
Copy link
Contributor Author

String-656 commented May 31, 2023

I had a read of https://developers.home-assistant.io/docs/development_testing .. and I'm out of my depth.. I don't know where to start with creating sufficient test records.

Below is a test that shows that val_result != val_result is true when the registers for a float32 return 0x7FC0. I added a debug output to the code, as seen highlighted on the right.

New code when register is NaN...
image

New code returning val_result when float32 register is a normal number..
image

Additionally I see the pylint and mypy didn't pass above in the checks. The comparison to it's self is one way to detect NaN, but the the following line I don't know how to solve as the function is passing back a string list, where one of those float values COULD be a NaN... e.g. If unpack() returns a tuple greater than 1, and one of those registers is (0x7FC0)

base_platform.py:224: error: Argument 1 to "append" of "list" has incompatible type "None"; expected "str" [arg-type]

If you can't send 'None' in a str List, then I don't know what value you would put in there so that the core doesn't throw an error... Note I couldn't simulate a scenario where unpack() returns a tuple greater than 1. I would expect you might see that if you used Modbus Function Code 3, read multiple holding registers...

@janiversen
Copy link
Member

look in the test directory and e.g. add it to the existing sensor tests. The modbus integration is proud of having 100% test coverage (all code lines are passed at least once in a test run) and we want to keep it that way.

@janiversen janiversen mentioned this pull request Aug 6, 2023
17 tasks
Copy link
Member

@janiversen janiversen left a comment

Choose a reason for hiding this comment

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

LGTM (with the extras)

@janiversen janiversen merged commit 56dd88d into home-assistant:dev Aug 6, 2023
21 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modbus - Exception since 2023.5.0: state class None: numeric value, however, it has the non-numeric value.
2 participants