-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Statistics component fix device_class for incremental source sensors #88096
Statistics component fix device_class for incremental source sensors #88096
Conversation
Hey there @fabaff, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
@emontnemery thanks for your review! |
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.
Looks good, just a minor comment + question
self._update_listener() # pragma: no cover | ||
self._update_listener = None # pragma: no cover |
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.
When does this happen?
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.
Can't exactly tell you. I did not develop this part of the code. Pytest always showed missing coverage for these two lines, hence the comment. Maybe the code can be removed or changed, but I didn't feel the need.
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.
There shouldn't be any pragma: no cover
in homeassistant code.
It is better to have reduced coverage than invalid coverage.
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.
Opened #88173
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 do not agree and don't see how it would make sense in this particular case. Anyhow, understand the incentive generally and won't fight with you.
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.
If the code is there, there must be a reason...
- If it is dead code that can't be reached, then it should be removed
- If it is not dead code, then we need to be reminded that coverage should be improved
The only time when no cover
is in use is when we raise an exception for unreachable code.
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.
There is not reason to introduce fake coverage. Epenet is right here, it should not have had these flags. I've therefore merged the PR to remove them.
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.
On second thought @epenet I agree with you. I did not develop this code nor did I ever touch it. I believe it's "dead code" as you called it and will do some tests and provide a fix in a future PR.
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.
Thanks, @ThomDietrich 👍
if sensor_device_class is None: | ||
return None |
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.
Can we add coverage for this one?
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.
Will do! This wasn't raised by pytest before the changes regarding DEVICE_CLASS_STATE_CLASSES
sensor_device_class = try_parse_enum(SensorDeviceClass, source_device_class) | ||
if sensor_device_class is None: |
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.
Wallrus could have been used.
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.
Could certainly, but then black will line-break this logic into oblivion!? I already had arguably better code locally.
You wanna suggest something? Maybe you have a better idea than me
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.
My suggestion was already above, just wallrus it. We don't worry about formatting, that is what black is for.
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.
Doesn't that contradict the wish for easily readable and maintainable code?
I'll have a look
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.
Sure, that is what black is for. You dont' have to agree with its style, but it is the style we are using consistently. The formatting is maintained by black and thus not a concern.
sensor_state_classes = DEVICE_CLASS_STATE_CLASSES.get( | ||
sensor_device_class, set() | ||
) |
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 creation of an empty set()
here is not needed.
sensor_state_classes = DEVICE_CLASS_STATE_CLASSES.get( | ||
sensor_device_class, set() | ||
) | ||
if SensorStateClass.MEASUREMENT not in sensor_state_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.
Wallrus could have been used.
if self.is_binary | ||
else f"_stat_{characteristic}", |
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.
We don't allow the use of multi-line inline if-statements; as they become hard to read. Please adjust this in a followup PR to have a regular if statement.
source_device_class = source_state.attributes.get(ATTR_DEVICE_CLASS) | ||
if source_device_class is None: | ||
return None | ||
sensor_device_class = try_parse_enum(SensorDeviceClass, source_device_class) |
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.
We do not need to parse the enum to try to look it up in DEVICE_CLASS_STATE_CLASSES
.
@frenck thanks for the detailed feedback. I will provide a follow-up PR and reference you in a few days time. Cheers |
…88096) * Return None device_class for incremental source sensors * Ignore linting error * Fix ignore linting error * Fix ignore linting error * Fix ignore linting error * Catch potential parsing error with enum
I was assuming this PR was supposed to fix the following type of log entry - this is from 2023.2.5 - and so it hasn't. Or was I missing the point ? And despite the log entry all these sensors appear to work just fine for me. (I have no idea how else to paste this log entry in - this is the way it copies it to clipboard direct from the HA interface) Logger: homeassistant.components.sensor Entity sensor.smart_meter_gas_import_this_week (<class 'homeassistant.components.mqtt.sensor.MqttSensor'>) is using state class 'measurement' which is impossible considering device class ('energy') it is using; expected None or one of 'total', 'total_increasing'; 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 |
@ianfretwell This is a merged PR. If you experience issues, please raise an issue in the issue tracker. Merged PR aren't trackable as an issue. Thanks 👍 |
Proposed change
Fixes errors shown since 2023.02: #87376
This PR implements the proposal explained here: #87376 (comment)
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: