core
core copied to clipboard
Fix handling `None` or empty value for numeric MQTT sensor
Breaking change
The behavior for receiving value on sensors that expect numeric* values has changed.
- From now on a
None
value will set such a sensor sensor to anunknown
state. - Empty values on such sensor
''
are ignored an will not effect the state of the sensor. Integrations need to be corrected to send the correct values if an update is published and no valid update value is available for the sensor.
* Sensors expect numeric values if on of the following applies:
-
device_class
is set but is not aDATE
,TIMESTAMP
,ENUM
or any custom device class. - a
state_class
is set. -
unit_of_measurement
is set.
Proposed change
MQTT sensors can accept string or numeric values. When device_class
is set a numeric value is expected. Many configurations use value_templates
when getting a sensor value from a published JSON.
When a sensor has no value, and the device_class
is set, this would give warnings in the log indicating an invalid non-numeric value was received.
This PR will change the behavior for MQTT sensors that have a device_class
set and receive a value in two ways:
- ignore when empty string value
''
is returned from thevalue_template
for a numeric sensor. - set a sensor to an
unknown state
whenNone
as value us returned from thevalue template
.
This is needed to allow maintainers get rid of warnings about invalid non-numeric in values in the logs asking people to open an issue.
example log for value rendering to an empty state:
Sensor sensor.power_dc has device class power, state class measurement and unit W thus indicating it has a numeric value; however, it has the non-numeric value: (<class 'str'>); Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mqtt%22
Type of change
- [ ] Dependency upgrade
- [x] 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)
- [x] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: fixes #87194
- This PR is related to issue:
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/26026
Checklist
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] 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:
- [x] Documentation added/updated for www.home-assistant.io
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 runningpython3 -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:
- [ ] I have reviewed two other open pull requests in this repository.
Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration (mqtt
) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of mqtt
can trigger bot actions by commenting:
-
@home-assistant close
Closes the issue. -
@home-assistant rename Awesome new title
Change the title of the issue. -
@home-assistant reopen
Reopen the issue. -
@home-assistant unassign mqtt
Removes the current integration label and assignees on the issue, add the integration domain after the command.
Note: it is a breaking change, but should it be considered for 2023.2 or are we too close to release?
~~I think we should add this to 2023.2 since the alternative is a lot of warnings asking users to open issues.~~
@epenet We will not add this to 2023.2, that allows us up update the sensor base class first as you suggested. Will set this PR to draft for now.
~Would like to merge #87129 before this PR, and then update the docs PR to say that a numeric value is also expected if suggested_display_precision
is set.~