Skip to content

Fix check on non numeric custom sensor device classes#89052

Merged
balloob merged 4 commits into
home-assistant:devfrom
jbouwh:fix-sensor-numeric-check
Mar 2, 2023
Merged

Fix check on non numeric custom sensor device classes#89052
balloob merged 4 commits into
home-assistant:devfrom
jbouwh:fix-sensor-numeric-check

Conversation

@jbouwh
Copy link
Copy Markdown
Contributor

@jbouwh jbouwh commented Mar 2, 2023

Proposed change

Update SensorEntity._numeric_state_expected to match SensorEntity.state

This fixes an issue a where an invalid native_unit_of_measurement setting, state_class or suggested_display_precision has priority over a valid device class which is not numeric.
If for example a sensor has a timestamp device class and a custom UOM (or "") the sensor should be treated as not numeric.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

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
  • 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
Copy Markdown
Contributor

home-assistant Bot commented Mar 2, 2023

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sensor 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 sensor Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@jbouwh jbouwh added this to the 2023.3.1 milestone Mar 2, 2023
Comment thread homeassistant/components/sensor/__init__.py Outdated
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
@jbouwh jbouwh changed the title Check custom device classes first if they are not numeric Fix check on non numeric custom sensor device classes Mar 2, 2023
@epenet
Copy link
Copy Markdown
Contributor

epenet commented Mar 2, 2023

I am not sure I agree with this.

This fixes an issue a where an invalid native_unit_of_measurement setting, state_class or suggested_display_precision has priority over a valid device class which is not numeric.
If for example a sensor has a timestamp device class and a custom UOM (or "") the sensor should be treated as not numeric.

I think the current check is correct - you should NOT be allowed to set a custom UOM or suggested_display_precision on a timestamp sensor.

I am not sure how best to fix it - but IMO this is not the correct way.

@epenet
Copy link
Copy Markdown
Contributor

epenet commented Mar 2, 2023

Can you maybe add some tests to make it clearer?
If the issue is only on the "empty string" UOM, then maybe it is sufficient to say or self.native_unit_of_measurement not in {None, ""}

@jbouwh
Copy link
Copy Markdown
Contributor Author

jbouwh commented Mar 2, 2023

Can you maybe add some tests to make it clearer? If the issue is only on the "empty string" UOM, then maybe it is sufficient to say or self.native_unit_of_measurement not in {None, ""}

It is about the empty uom , but I think the same applies to state_class

@emontnemery
Copy link
Copy Markdown
Contributor

emontnemery commented Mar 2, 2023

@epenet the problem being fixed is not that the unit of measurement is the empty string. The problem being fixed is that the order of checks in SensorEntity._numeric_state_expected did not match the order of checks in SensorEntity.state which broke sensors which previously worked.

If we think it adds value to again change the order of the checks in SensorEntity.state we should do that together with introducing warnings with a clear date when they will stop working.

Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good, but we should consider adding a test.

@epenet
Copy link
Copy Markdown
Contributor

epenet commented Mar 2, 2023

The problem being fixed is that the order of checks in SensorEntity._numeric_state_expected did not match the order of checks in SensorEntity.state

I think we should add a comment in the code to make the link explicit then.

Comment thread homeassistant/components/sensor/__init__.py
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
@balloob balloob merged commit 8968ed1 into home-assistant:dev Mar 2, 2023
balloob added a commit that referenced this pull request Mar 2, 2023
* Custom device classes are not numeric

* Update homeassistant/components/sensor/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Add test

* Update homeassistant/components/sensor/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
@balloob balloob mentioned this pull request Mar 2, 2023
@jbouwh jbouwh deleted the fix-sensor-numeric-check branch March 2, 2023 19:32
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 3, 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.

MQTT discovered sensors with problem causes MQTT integration to crash at boot

4 participants