-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Add numeric_state_expected property to Sensor class #87013
Conversation
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
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.
In general, I think this could help. However, I don't think this is a settable property, but more of a helper providing information based on existing information of the 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.
I think that on line 504, you should replace the set with the new _NON_NUMERIC_DEVICE_CLASSES
constant.
And on line 599 you should be able to adjust so that the "is_numeric" check doesn't need to be synchronised.
if not self.is_numeric:
return value
Please extend the PR description and explain how this will be used and where. |
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.
I'm not sure if you saw this comment:
I think that on line 504, you should replace the set with the new _NON_NUMERIC_DEVICE_CLASSES constant.
Also, I'm not convinced about the is_numeric
naming of the property.
Not sure about a better name, but maybe numeric_state_expected
is better?
I'm not sure we should make a general helper for this. Normally integrations are in control of the sensor properties already and don't need to check. |
Have renamed the property. and... used the new const at line 504... |
I am not sure how many other integrations need to know if a sensor is expected to be numeric... We already have a check inside the sensor platform, so this is just turning the check into a property that can be used both internally and externally from other integration. |
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.
I think it looks good, but I think @MartinHjelmare or @frenck need to confirm.
@@ -246,6 +247,22 @@ def device_class(self) -> SensorDeviceClass | None: | |||
return self.entity_description.device_class | |||
return None | |||
|
|||
@final | |||
@property | |||
def numeric_state_expected(self) -> bool: |
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.
This method is partially flawed.
For example, a sensor without state class, without a unit of measurement and without precision and without a device class will return false.
The fact is: We don't know. Could be numeric, could be textual state 🤷
I guess that is its goal, but we should make that more clear in the doc comments.
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.
That happens for sensors that just return a text state.
This would work too:
return not (device_class is None or device_class in NON_NUMERIC_DEVICE_CLASSES)
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.
That results in the same unknown issue.
We have sensors that don't provide enough information to know if it is either number or not (regardless of any logic we can apply). That was my point with my comment above, we should document that.
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.
An sensor can have a ''
as a state, but a numeric sensor can not. But I agree it is hard to understand the logic.
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.
That's why I wasn't sure about naming. I didn't like is_numeric
because it implied the value had been checked
I proposed numeric_state_expected
because if that condition is True, then it HAS to be a number.
If it returns False, then it CAN be a number, but it doesn't HAVE to be.
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.
Adjusted the docstr
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.
Ah the state can be None
too, may be better to just set it to Return true if the sensor must be numeric.
?
update: adjusted it
Co-authored-by: epenet <[email protected]>
Co-authored-by: epenet <[email protected]>
Co-authored-by: epenet <[email protected]>
Co-authored-by: Franck Nijhof <[email protected]>
3d9446f
to
20f3ca5
Compare
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.
LGTM, thanks @jbouwh 👍
…7013) * Add is_numeric property to Sensor class * Follw up comment * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * Tests and corrections * Simplify converion check * Correct custom device class handling * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * rename to numeric_state_expected * Replace with new const * Adjust docstr * Update homeassistant/components/sensor/__init__.py Co-authored-by: Franck Nijhof <[email protected]> * Move to const * Correct logic * Do not use bool * Adjust docstr must be numeric * remote state from docstr * protect numeric_state_expected * Use try_parse_enum for custom class check * Remove redundant type hints --------- Co-authored-by: epenet <[email protected]> Co-authored-by: Franck Nijhof <[email protected]>
…7013) * Add is_numeric property to Sensor class * Follw up comment * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * Tests and corrections * Simplify converion check * Correct custom device class handling * Update homeassistant/components/sensor/__init__.py Co-authored-by: epenet <[email protected]> * rename to numeric_state_expected * Replace with new const * Adjust docstr * Update homeassistant/components/sensor/__init__.py Co-authored-by: Franck Nijhof <[email protected]> * Move to const * Correct logic * Do not use bool * Adjust docstr must be numeric * remote state from docstr * protect numeric_state_expected * Use try_parse_enum for custom class check * Remove redundant type hints --------- Co-authored-by: epenet <[email protected]> Co-authored-by: Franck Nijhof <[email protected]>
Proposed change
Add
numeric_state_expected
helper property to sensor class. The helper will support integrations to validate if numeric value types are supported. An example is the validation of incoming values for a MQTT sensor. For numeric sensors non numeric values are not accepted as state. See also: #87004Type 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: