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

Handle explicit Modbus NaN values #90800

Merged
merged 3 commits into from Aug 6, 2023

Conversation

joanwa
Copy link
Contributor

@joanwa joanwa commented Apr 4, 2023

Proposed change

Add optional modbus config for explicit NaN values specified by the device vendor. Without this option, template sensore are required to remove the NaN values. See related issue #69656

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • [ x] 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)
  • [x ] 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

home-assistant bot commented Apr 4, 2023

Hi @joanwa

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

home-assistant bot commented Apr 4, 2023

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.

@home-assistant
Copy link

home-assistant bot commented Apr 4, 2023

Hi @joanwa

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@joanwa
Copy link
Contributor Author

joanwa commented Apr 4, 2023

So what I'm unsure about is what the return value should be in case of NaN. Should it be 0 or 'unavailable' or none? In the PR I opted for 0, just because it is of the same return type.
For my current workaround with template sensors I use 'unavailable'. Might be the more logic choice.

There's also some discussions here:
https://community.home-assistant.io/t/difference-between-unknown-unavailable-none-and-null/430966/3

@home-assistant home-assistant bot marked this pull request as draft April 4, 2023 21:12
@home-assistant
Copy link

home-assistant bot commented Apr 4, 2023

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.

@MartinHjelmare MartinHjelmare changed the title handle explicit Modbus NaN values Handle explicit Modbus NaN values Apr 4, 2023
@janiversen
Copy link
Member

You have a CI problem, please solve that.

@janiversen
Copy link
Member

Furthermore your PR is marked as draft, please mark it as ready for review when you want a formal review.

@joanwa
Copy link
Contributor Author

joanwa commented Apr 7, 2023

You have a CI problem, please solve that.

yes will do

@janiversen
Copy link
Member

Let’s see if your CI problem is solved. This PR is still marked as draft, so we cannot review/approve it.

@joanwa joanwa force-pushed the modbus-nan-values branch 3 times, most recently from dbd35e3 to efce9bd Compare April 17, 2023 06:58
@joanwa
Copy link
Contributor Author

joanwa commented Apr 27, 2023

polite question, is something missing from my side for a revised review?

@janiversen
Copy link
Member

janiversen commented Apr 27, 2023

A polite answer you have not answered my last review comments…and if I remember right your doc. PR also have issues (I might remember wrong ), it is marked as draft, so not ready for review.

@joanwa
Copy link
Contributor Author

joanwa commented Apr 27, 2023

A polite answer you have not answered my last review comments…and if I remember right your doc. PR also have issues (I might remember wrong ), it is marked as draft, so not ready for review.

actually answered to all of them, incorporated some changes in the code. After a few days I clicked on 'resolve comment' since I thought this was part of the review process, but I think this just made your comments and my answers vanish :/

@janiversen
Copy link
Member

The current status is the PR is "changes requested" so it is hard to know that they are solved.

@janiversen
Copy link
Member

Just controlled there are 6 review comments without response. You might have solved all 6 in the commits you made, but how do you expect me to know that

@joanwa
Copy link
Contributor Author

joanwa commented Apr 28, 2023

Just controlled there are 6 review comments without response. You might have solved all 6 in the commits you made, but how do you expect me to know that

I really don't know what to do now. I only see 2 requests for changes from you above, and I changed the code and responded to the latter. I definitely shouldn't have squashed the commits, therefore the original lines of code on which you commented is now gone. I'm eager to do whatever else needs to be done for this PR. It's my first one for HA in general.

@janiversen
Copy link
Member

Plase look more carefully:

image

image

image

6 open review comments.

@frenck
Copy link
Member

frenck commented Apr 29, 2023

@janiversen Those comments in your review screenshots, have the pending badge:

image

Meaning you still need to submit them as a review. You are the only one that sees those.

../Frenck

@joanwa
Copy link
Contributor Author

joanwa commented Apr 29, 2023

@janiversen Those comments in your review screenshots, have the pending badge:

image

Meaning you still need to submit them as a review. You are the only one that sees those.

../Frenck

thanks @frenck for spotting, @janiversen I just wanted to say I don't see any of them and was waiting for something to happen.

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.

sorry my fault.

@@ -105,6 +107,7 @@ def get_optional_numeric_config(config_name: str) -> int | float | None:

self._min_value = get_optional_numeric_config(CONF_MIN_VALUE)
self._max_value = get_optional_numeric_config(CONF_MAX_VALUE)
self._nan_value = entry.get(CONF_NAN_VALUE)
Copy link
Member

Choose a reason for hiding this comment

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

What happens if nan value is not present in the config (this line fails)

Copy link
Contributor Author

@joanwa joanwa May 1, 2023

Choose a reason for hiding this comment

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

should work with a default None value from the input Schema validation in __init__.py. Forgot the default value there and will add with a commit. I could also do a proper hex string enforcement using voluptuous in the init. What do you think?

@@ -161,6 +164,10 @@ def __init__(self, hub: ModbusHub, config: dict) -> None:
self._scale = config[CONF_SCALE]
self._offset = config[CONF_OFFSET]
self._count = config[CONF_COUNT]
if self._nan_value is not None:
self._nan_value = struct.unpack(
self._structure, bytes.fromhex(self._nan_value[2:])
Copy link
Member

Choose a reason for hiding this comment

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

According to the doc it is a hex string (without specific definition) but it seems the code assumes something special for the first 2 chars.

Copy link
Member

Choose a reason for hiding this comment

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

also you cannot use _structure, that will give wrong results or break when configuring a custom format.

why no simply do something like:
‘’’
x = int("deadbeef", 16)
‘’’
that works with 0x as well

Copy link
Contributor Author

@joanwa joanwa May 1, 2023

Choose a reason for hiding this comment

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

1st comment
yes, the special 0x. Could do prettier things like a split after 0x, which is less performant. But if format is enforced with the preceding 0x, will this be ok?

2nd comment
this doesn't work, as x should have different values when using e.g. uint16 or int16, which again is set with the struct from _structure. int("deadbeef", 16) will always convert it to the same int value in base 16.

@@ -161,6 +164,10 @@ def __init__(self, hub: ModbusHub, config: dict) -> None:
self._scale = config[CONF_SCALE]
self._offset = config[CONF_OFFSET]
self._count = config[CONF_COUNT]
if self._nan_value is not None:
self._nan_value = struct.unpack(
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the user configures "jan was here" which are allowed since it is defined as a string.

Copy link
Contributor Author

@joanwa joanwa May 1, 2023

Choose a reason for hiding this comment

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

See above comment, could do hex string enforcement using voluptuous in the __init__.py or also just preceding here with a regex check with proper error message is hex format is incorrect?

@@ -161,6 +164,10 @@ def __init__(self, hub: ModbusHub, config: dict) -> None:
self._scale = config[CONF_SCALE]
self._offset = config[CONF_OFFSET]
self._count = config[CONF_COUNT]
if self._nan_value is not None:
self._nan_value = struct.unpack(
self._structure, bytes.fromhex(self._nan_value[2:])
Copy link
Member

Choose a reason for hiding this comment

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

_structure is optional, why use that to unpack your hex string?

Copy link
Contributor Author

@joanwa joanwa Apr 29, 2023

Choose a reason for hiding this comment

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

yes, otherwise pythons int("hex_string") yields the integer representation of "hex_string"regardless of the modbus datatype, which can be uint16, int16, uint32, int32, float32,... and all as either little endian or big endian.
The "hex_string" NaN value has to be decoded in the same way as the actual incoming modbus sensor value is decoded.

Copy link
Member

Choose a reason for hiding this comment

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

see above this breaks with custom formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't find a good solution yet

def __process_raw_value(self, entry: float | int | str) -> float | int | str:
"""Process value from sensor with NaN handling, scaling, offset, min/max etc."""
if self._nan_value and entry == self._nan_value:
return STATE_UNAVAILABLE
Copy link
Member

Choose a reason for hiding this comment

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

this should return a value not a state, this might have a number of side effects.

Copy link
Contributor Author

@joanwa joanwa Apr 29, 2023

Choose a reason for hiding this comment

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

my initial version had return value 0, and was wondering if "unavailable" was better. For this, you suggested it should be "unavailable".
Or is your issue STATE_UNAVAILABLE vs. "unavailable"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you have any guidance here?

(
{
CONF_DATA_TYPE: DataType.INT32,
CONF_NAN_VALUE: "0x80000000",
Copy link
Member

Choose a reason for hiding this comment

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

You miss tests:

  • nan_value configured as "jan was here"
  • nan_value configured as a hex string (no 0x) allowed according to the doc
  • nan_value, configured but no match
  • nan_value match (check that raw_value is not broken).

In general you need to look at the coverage output and secure the lines you have changed are covered by tests.

@home-assistant home-assistant bot marked this pull request as draft April 29, 2023 10:29
@janiversen
Copy link
Member

@frenck good catch, thanks.

@janiversen
Copy link
Member

Any progress on this, there are a number of open review comments and it is out of date with the dev branch.

In case of no progress it will be closed at a later date.

@joanwa
Copy link
Contributor Author

joanwa commented May 15, 2023

Any progress on this, there are a number of open review comments and it is out of date with the dev branch.

In case of no progress it will be closed at a later date.

will commit updated unittests, and the things where I know what to do. Still some open questions, see above

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.

Added extra to make it work.

@janiversen janiversen marked this pull request as ready for review August 6, 2023 11:35
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 c4a5373 into home-assistant:dev Aug 6, 2023
21 checks passed
Copy link
Contributor Author

@joanwa joanwa Aug 7, 2023

Choose a reason for hiding this comment

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

thanks for picking this up, but I think this has issues when the modbus configuration variable swap is used with swapped order of bytes/words. This was my motivation to use the above struct.unpack() with the same self._structure in homeassistant/components/modbus/base_platform.py used to encode the actual incoming modbus values. Since you mentioned this is not a good idea, I didn't how to proceed.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2023
@@ -225,6 +230,10 @@ def unpack_structure_result(self, registers: list[int]) -> str | None:
# the conversion only when it's absolutely necessary.
if isinstance(val_result, int) and self._precision == 0:
return str(val_result)
if isinstance(val_result, str):
if val_result == "nan":
val_result = STATE_UNAVAILABLE # pragma: no cover
Copy link
Member

Choose a reason for hiding this comment

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

STATE_UNAVAILABLE should never be set directly as state. We use the entity property available set to False to set an entity state as unavailable.

Copy link
Member

Choose a reason for hiding this comment

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

@MartinHjelmare
Something is not right, when doing it I get:

  File "/Users/jan/repos/core/homeassistant/components/modbus/base_platform.py", line 193, in __process_raw_value
    self.available = False
    ^^^^^^^^^^^^^^
AttributeError: property 'available' of 'ModbusRegisterSensor' object has no setter

Copy link
Member

Choose a reason for hiding this comment

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

It's a property, so it can't be set as an attribute. It needs to return a value. You can use self._attr_available if you want to set the value directly.

https://developers.home-assistant.io/docs/core/entity#property-implementation

@home-assistant home-assistant unlocked this conversation Aug 21, 2023
@janiversen
Copy link
Member

Ok will change it in a followup PR.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 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.

specify Modbus NaN values per sensor
4 participants