-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Remove modbus pragma no cover and solve nan #99221
Conversation
Hey there @adamchengtkc, @vzahradnik, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
3007261
to
926ed1e
Compare
@MartinHjelmare You reviewed the last PR with nan, this is a followup. |
@@ -230,14 +234,20 @@ def unpack_structure_result(self, registers: list[int]) -> str | None: | |||
if isinstance(v_temp, int) and self._precision == 0: | |||
v_result.append(str(v_temp)) | |||
elif v_temp is None: | |||
v_result.append("") # pragma: no cover | |||
v_result.append("nan") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the nan
result handled in the sensor entity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the short answer is "the same way as line 240 is handled, which is not changed in this PR" 😬
The long answer is "it does not"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I correct myself. This is not the normal sensor code, this is the part, where the user have requested a comma separated list of multiple values...this is returned as a string in the sensor code.
using "nan" in the string (as already done in line 240) secures that the string returned will look like:
"123,nan,17" and not "123,,17"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And how will the sensor parse that string when converting to a sensor state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will not, it is returned as a string and the user must have some automation that splits it.
Remark, this code is only active if the user configured "structure:" indicating he wants a comma separated string.
For normal sensors this code is bypassed (see "if len" a couple of lines earlier).
The comma separated list is actually some very old code I never touched...modern configurations use "slave_count:" which generates n normal sensors, so they can be used normally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we'll raise a ValueError
here if we either try to make an integer from "nan"
or if we try to make a float we'll raise in the base sensor class since we don't allow infinite numbers:
core/homeassistant/components/modbus/sensor.py
Lines 124 to 130 in aff49cb
result = self.unpack_structure_result(raw_result.registers) | |
if self._coordinator: | |
if result: | |
result_array = list( | |
map(float if self._precision else int, result.split(",")) | |
) | |
self._attr_native_value = result_array[0] |
core/homeassistant/components/sensor/__init__.py
Lines 603 to 610 in aff49cb
if not isfinite(numerical_value): | |
raise ValueError( | |
f"Sensor {self.entity_id} has device class '{device_class}', " | |
f"state class '{state_class}' unit '{unit_of_measurement}' and " | |
f"suggested precision '{suggested_precision}' thus indicating it " | |
f"has a numeric value; however, it has the non-finite value: " | |
f"'{numerical_value}'" | |
) |
Is there some guard for that happening that I'm not seeing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm you got a point, let me change it to a zero.
Actually a very good catch, thanks for that, I wonder why none of the people who use this have reported a problem....probably they have not had a "nan".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed !!
What I still do not understand, is why the test I made worked
Thanks again for catching this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MartinHjelmare a polite ping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the tests, it's the slave sensors that have the coordinator which will use the problematic code path.
9081bc6
to
90cfbf6
Compare
elif v_temp != v_temp: # noqa: PLR0124 | ||
# NaN float detection replace with None | ||
v_result.append("nan") # pragma: no cover | ||
v_result.append("nan") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have the same problem here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, I did change that too, but seems I forgot to press "save".
Done.
cd36e39
to
ab8491f
Compare
Do you want to update the slave tests to cover the cases? |
It does not affect the slave sensors, but I missed one general sensor test ! corrected. |
Isn't this code path relevant for the slave sensors? core/homeassistant/components/modbus/sensor.py Lines 125 to 134 in 71207e1
|
Yes that is slave_sensors, but the nan was in the receiving part, which is tested in this is the distribution, so no need to repeat the nan test here. But lets see if cov claims its missing. |
I don't believe we're covering the sensor coordinator code path in the |
no test_struct_sensor covers receiving data via struct....that is the nan changes.
|
Right, so my suggestion is that we add a test that covers a coordinator sensor needing to handle the changed NaN cases in |
Got you, done. More test is always good, I am proud of having coverage report 100% coverage so most changes without a proper test is caught. |
Thanks! |
* Remove pragma no cover. * Ruff ! * Review comments. * update test. * Review. * review. * Add slave test.
Breaking change
Proposed change
Added tests to be able to remove the last "pragma: no cover"
While removing the tests found a bug in the "nan" implementation, which is also corrected, since it in the same lines.
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: