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
Filtered values are no longer rounded if values are not changed/calculated #76164
Conversation
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 the correct approach is just to change the schema.
@@ -488,7 +488,7 @@ class RangeFilter(Filter, SensorEntity): | |||
def __init__( | |||
self, | |||
entity, | |||
precision: int | None = DEFAULT_PRECISION, | |||
precision, |
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.
Any reason not to keep int | None
type definition? Or is it standard to not include it if you're not including a default value?
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 I include the type definition mypy complains :(
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 mypy probably complains for a reason...
I started to look into it and I found it very awkward that functions only had partial type hints, and some even had invalid type hints.
I suggest that we fix the type hints first: #86141
Failing checks seem unrelated to this change... any way to just re-run them? |
@@ -79,7 +79,7 @@ | |||
ICON = "mdi:chart-line-variant" | |||
|
|||
FILTER_SCHEMA = vol.Schema( | |||
{vol.Optional(CONF_FILTER_PRECISION, default=DEFAULT_PRECISION): vol.Coerce(int)} | |||
{vol.Optional(CONF_FILTER_PRECISION, default=None): vol.Any(None, vol.Coerce(int))} |
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.
Definition of DEFAULT_PRECISION
can be removed.
I don't understand how removing default precision only applies to 3 filters, while all filter schemas are extending from FILTER_SCHEMA
? If we want a different default value, we could keep track of a dictionary mapping filter name => default value
, only used when we detect that precision 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.
I'm trying to make this a less of a breaking change as possible, so I don't want to remove DEFAULT_PRECISION (that was my mistake on the refactor after the 1st review)
Also preferred to have the default value next to the class definition instead of a global dict
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
still valid :( |
I have finished adding typing to |
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
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 type hint for precision
is still wrong on every __init__
method.
I suggest that you change the schema to:
{vol.Optional(CONF_FILTER_PRECISION): vol.Coerce(int)}
And set the default value on each init method.
def __init__(self, window_size: timedelta, entity: str, precision: int | None = None) -> None:
or
def __init__(self, window_size: timedelta, entity: str, precision: int | None = DEFAULT_PRECISION) -> None:
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Can't set the default value on each init method because there are arguments without default values after precision |
You can change the order of the arguments, as it is always built using keyword arguments with filters = [
FILTERS[_filter.pop(CONF_FILTER_NAME)](entity=entity_id, **_filter)
for _filter in filter_configs
] |
if self.filter_precision is not None: | ||
filtered.set_precision(self.filter_precision) |
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.
Is this change necessary?
Does it not end up being a duplicate of the change on line 384/386?
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 believe so, filter_precision starts as 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.
What I mean is that the first line in set_precision is check that the passed value is not None.
if precision is not None and isinstance(self.state, Number):
So you are first checking that it's not None outside the function, and then checking a second time for the same thing inside the function.
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.
Sorry misunderstood initial comment (fixed)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
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 👍
@balloob are you happy?
FILTER_SCHEMA = vol.Schema( | ||
{vol.Optional(CONF_FILTER_PRECISION, default=DEFAULT_PRECISION): vol.Coerce(int)} | ||
) | ||
FILTER_SCHEMA = vol.Schema({vol.Optional(CONF_FILTER_PRECISION): vol.Coerce(int)}) |
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.
Just a note for a follow-up PR, since precision is now a user-facing sensor option, this can be completely dropped in a future PR.
Breaking change
Values are no longer rounded to the default 2 decimals points for the "Range", "Throttle" and "TimeThrottle" filters, and will keep the source precision
Proposed change
Filtered values are no longer rounded if values are not changed/calculated such as in the case of "Range", "Throttle" and "TimeThrottle" filters
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
.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: