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

MQTT sensors handling of publishing NaN values #7768

Merged
merged 15 commits into from
Dec 2, 2024
Merged

Conversation

kbullet
Copy link
Contributor

@kbullet kbullet commented Nov 14, 2024

What does this implement/fix?

Publishing "None" instead of "NaN" values for sensors in MQTT. This is to align with HASS's MQTT changes as it no longer seem to like them NaN values in sensor states and it's spamming the logs each time nan is published e.g.

ValueError: Sensor sensor.ld2410_sensor_g8_move_energy has device class 'None', state class 'None' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-finite value: 'nan'
2024-11-08 15:43:41.169 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/mqtt/entity.py", line 856, in _async_process_discovery_update
await discovery_update(payload)
File "/usr/src/homeassistant/homeassistant/components/mqtt/entity.py", line 1137, in discovery_update
self.async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
self._async_write_ha_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
self.__async_calculate_state()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1067, in __async_calculate_state
state = self._stringify_state(available)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1011, in _stringify_state
if (state := self.state) is None:
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/sensor/init.py", line 675, in state
raise ValueError(
ValueError: Sensor sensor.ld2410_sensor_g8_still_energy has device class 'None', state class 'None' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-finite value: 'nan'

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

N/A

Pull request in esphome-docs with documentation (if applicable):

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

mqtt:
  broker: $mqtt_broker
  username: $mqtt_user
  password: $mqtt_password
  publish_nan_as_none: true

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

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

@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.80%. Comparing base (4d8b5ed) to head (900b3dc).
Report is 1734 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #7768      +/-   ##
==========================================
+ Coverage   53.70%   53.80%   +0.09%     
==========================================
  Files          50       50              
  Lines        9408     9813     +405     
  Branches     1654     1353     -301     
==========================================
+ Hits         5053     5280     +227     
- Misses       4056     4207     +151     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kbullet
Copy link
Contributor Author

kbullet commented Nov 16, 2024

@jesserockz, @clydebarrow, wdyt? Similarly the way the API handles NaN to None state

@jesserockz jesserockz added this to the 2024.11.0b3 milestone Nov 17, 2024
@jesserockz jesserockz removed this from the 2024.11.0b3 milestone Nov 17, 2024
Copy link
Member

@jesserockz jesserockz left a comment

Choose a reason for hiding this comment

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

I originally put this on the beta milestone, but just realised this could be a breaking change for people not using HA.

I think this needs a configuration option (something like publish_nan_as_none) for either the global mqtt config, or on the mqtt sensor schema that defaults to false to keep existing functionality.

@esphome
Copy link

esphome bot commented Nov 17, 2024

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.

@esphome esphome bot marked this pull request as draft November 17, 2024 20:16
@kbullet
Copy link
Contributor Author

kbullet commented Nov 19, 2024

Thanks @jesserockz for your review and guidance. After further consideration and as per your suggestion I've updated the PR to have a global config for MQTT to avoid breaking things for non HA users.

Also updated docs esphome/esphome-docs#4452

Cheers!

@kbullet kbullet requested a review from jesserockz November 19, 2024 09:05
esphome/components/mqtt/__init__.py Outdated Show resolved Hide resolved
esphome/components/mqtt/__init__.py Outdated Show resolved Hide resolved
kbullet and others added 2 commits November 21, 2024 09:38
Updated

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
@kbullet kbullet marked this pull request as ready for review November 21, 2024 03:06
@kbullet kbullet requested a review from a team as a code owner November 21, 2024 03:06
@esphome esphome bot requested a review from jesserockz November 21, 2024 03:06
@kbullet
Copy link
Contributor Author

kbullet commented Nov 28, 2024

Hi, @jesserockz thanks for the suggestions is there anything else you need from my side for this PR?

@kbx81 kbx81 enabled auto-merge (squash) December 2, 2024 23:29
@kbx81 kbx81 merged commit 584dbf2 into esphome:dev Dec 2, 2024
27 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2024
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.

4 participants