Skip to content

Adding range filter#13390

Merged
dgomes merged 15 commits into
home-assistant:devfrom
nielstron:filter-band-pass
Jul 3, 2018
Merged

Adding range filter#13390
dgomes merged 15 commits into
home-assistant:devfrom
nielstron:filter-band-pass

Conversation

@nielstron
Copy link
Copy Markdown
Contributor

@nielstron nielstron commented Mar 22, 2018

Description:

As there were issues with the outlier filter this adds a range filter on its own. It only allows values in a given range to pass.

Related issue (if applicable): additional way to solve #13363

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4985

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: filter
    name: "filtered realistic humidity"
    entity_id: sensor.realistic_humidity
    filters:
      - filter: range
        lower_bound: -30.0
        upper_bound: 50

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

lower_bound (float): band lower bound
"""

def __init__(self, window_size=1, precision=None, entity,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SyntaxError: non-default argument follows default argument

Comment thread tests/components/sensor/test_filter.py Outdated
upper = 20
filt = BandPassFilter(entity=None,
lower_bound=lower,
upper_bound=upper)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent

Comment thread tests/components/sensor/test_filter.py Outdated
lower = 10
upper = 20
filt = BandPassFilter(entity=None,
lower_bound=lower,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent

Comment thread tests/components/sensor/test_filter.py Outdated

from homeassistant.components.sensor.filter import (
LowPassFilter, OutlierFilter, ThrottleFilter, TimeSMAFilter)
LowPassFilter, OutlierFilter, ThrottleFilter, TimeSMAFilter, BandPassFilter)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

"""Initialize common attributes."""
super().__init__(FILTER_NAME_OUTLIER, precision, entity)
self.states = deque(maxlen=window_size)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

self.states.append(filtered)
return filtered

class HistoricalFilter(Filter):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

@nielstron nielstron changed the title Adding bandpass filter Adding bandpass filter and overhauling filters Mar 22, 2018
@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Mar 22, 2018

I like the HistoricalFilter, but please keep the PR to one issue only.

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Mar 22, 2018

Also take into consideration that #13075 has rewritten the filters code to some extend

@nielstron nielstron changed the title Adding bandpass filter and overhauling filters Adding bandpass filter Mar 22, 2018
self._stats_internal = Counter()

def _filter_state(self, new_state):
"""Implement the outlier filter."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Update the description

"""

def __init__(self, window_size=1, precision=None, entity,
lower_bound=math.inf, upper_bound=-math.inf):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't need to set defaults. This is already handled by voluptuous

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't need to set defaults, voluptuous handles that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would avoid using math.inf (saving the math import) by just defaulting to None

Copy link
Copy Markdown
Contributor Author

@nielstron nielstron Mar 22, 2018

Choose a reason for hiding this comment

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

I would like to keep this (or switch to float('inf') - saving a math import - or sth similar) because this is mathematically more elegant (the lower bound is negative infinite and thus not existant). With None more ugly comparisons are included in the source code.

Also since the newest update default values have to pass the voloptuous check too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if lower_bound and value < lower_bound:
    return value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lets keep python 3.5 math.inf

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I disagree. If there is no bound, we shouldn't check it.

if lower_bound is not None and value < lower_bound
    return value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does None pass the validity check for float values in voluptuous?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, Just mark the key as vol.Optional and not give any default.

@fronzbot
Copy link
Copy Markdown
Contributor

A bit nit-picky, but "bandpass" filter is not a good name for this. It's really more like a thresholding filter. Bandpass implies it's getting rid of both high frequency content (noisy info or something) as well as low frequency stuff (ie. maybe some constant value).

Just figured I'd point that out seeing as we have a real lowpass filter already implemented, so sticking with the right nomenclature seems important (especially if a real bandpass filter is eventually implemented)

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Mar 26, 2018

I agree with @fronzbot, we should be rigorous with the naming to avoid misinterpretations by users.

@nielstron
Copy link
Copy Markdown
Contributor Author

nielstron commented Mar 27, 2018 via email

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Mar 27, 2018

  • See no need to split, just rename it to threshold filter or limit filter
  • returning None is not a good option, it will interfere with next in chain filters.

@nielstron
Copy link
Copy Markdown
Contributor Author

nielstron commented Mar 28, 2018 via email

@robmarkcole
Copy link
Copy Markdown
Contributor

robmarkcole commented Mar 29, 2018

Perhaps Range Filter or Bounded window

Comment thread tests/components/sensor/test_filter.py Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

Comment thread tests/components/sensor/test_filter.py Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

Comment thread tests/components/sensor/test_filter.py Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

@nielstron nielstron changed the title Adding bandpass filter Adding range filter Mar 30, 2018
@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Apr 7, 2018

@nielstron history just got merged, you will need to fix the conflicts

@nielstron
Copy link
Copy Markdown
Contributor Author

@dgomes What do you mean "lets keep python 3.5 math.inf"? Is there still a change requested?

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Jun 7, 2018

I was accepting the use of math.inf

but then balloob commented: #13390 (comment)

I think you should follow the comment

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 8, 2018

🎈

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 15, 2018

Any update on this?

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Jul 1, 2018

@nielstron can you make the change and we merge this :) ?

@homeassistant
Copy link
Copy Markdown
Contributor

Hello @nielstron,

When attempting to inspect the commits of your pull request for CLA signature status among all authors we encountered commit(s) which were not linked to a GitHub account, thus not allowing us to determine their status(es).

The commits that are missing a linked GitHub account are the following:

Unfortunately, we are unable to accept this pull request until this situation is corrected.

Here are your options:

  1. If you had an email address set for the commit that simply wasn't linked to your GitHub account you can link that email now and it will retroactively apply to your commits. The simplest way to do this is to click the link to one of the above commits and look for a blue question mark in a blue circle in the top left. Hovering over that bubble will show you what email address you used. Clicking on that button will take you to your email address settings on GitHub. Just add the email address on that page and you're all set. GitHub has more information about this option in their help center.

  2. If you didn't use an email address at all, it was an invalid email, or it's one you can't link to your GitHub, you will need to change the authorship information of the commit and your global Git settings so this doesn't happen again going forward. GitHub provides some great instructions on how to change your authorship information in their help center.

    • If you only made a single commit you should be able to run
      git commit --amend --author="Author Name <email@address.com>"
      
      (substituting Author Name and email@address.com for your actual information) to set the authorship information.
    • If you made more than one commit and the commit with the missing authorship information is not the most recent one you have two options:
      1. You can re-create all commits missing authorship information. This is going to be the easiest solution for developers that aren't extremely confident in their Git and command line skills.
      2. You can use this script that GitHub provides to rewrite history. Please note: this should be used only if you are very confident in your abilities and understand its impacts.
    • Whichever method you choose, I will come by to re-check the pull request once you push the fixes to this branch.

We apologize for this inconvenience, especially since it usually bites new contributors to Home Assistant. We hope you understand the need for us to protect ourselves and the great community we all have built legally. The best thing to come out of this is that you only need to fix this once and it benefits the entire Home Assistant and GitHub community.

Thanks, I look forward to checking this PR again soon! ❤️


def __init__(self, entity,
lower_bound, upper_bound):
lower_bound=None, upper_bound=None):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is no need to set *bound=None

@homeassistant
Copy link
Copy Markdown
Contributor

Hello @nielstron,

When attempting to inspect the commits of your pull request for CLA signature status among all authors we encountered commit(s) which were not linked to a GitHub account, thus not allowing us to determine their status(es).

The commits that are missing a linked GitHub account are the following:

Unfortunately, we are unable to accept this pull request until this situation is corrected.

Here are your options:

  1. If you had an email address set for the commit that simply wasn't linked to your GitHub account you can link that email now and it will retroactively apply to your commits. The simplest way to do this is to click the link to one of the above commits and look for a blue question mark in a blue circle in the top left. Hovering over that bubble will show you what email address you used. Clicking on that button will take you to your email address settings on GitHub. Just add the email address on that page and you're all set. GitHub has more information about this option in their help center.

  2. If you didn't use an email address at all, it was an invalid email, or it's one you can't link to your GitHub, you will need to change the authorship information of the commit and your global Git settings so this doesn't happen again going forward. GitHub provides some great instructions on how to change your authorship information in their help center.

    • If you only made a single commit you should be able to run
      git commit --amend --author="Author Name <email@address.com>"
      
      (substituting Author Name and email@address.com for your actual information) to set the authorship information.
    • If you made more than one commit and the commit with the missing authorship information is not the most recent one you have two options:
      1. You can re-create all commits missing authorship information. This is going to be the easiest solution for developers that aren't extremely confident in their Git and command line skills.
      2. You can use this script that GitHub provides to rewrite history. Please note: this should be used only if you are very confident in your abilities and understand its impacts.
    • Whichever method you choose, I will come by to re-check the pull request once you push the fixes to this branch.

We apologize for this inconvenience, especially since it usually bites new contributors to Home Assistant. We hope you understand the need for us to protect ourselves and the great community we all have built legally. The best thing to come out of this is that you only need to fix this once and it benefits the entire Home Assistant and GitHub community.

Thanks, I look forward to checking this PR again soon! ❤️

@nielstron
Copy link
Copy Markdown
Contributor Author

There you go, sorry for the delay

@dgomes
Copy link
Copy Markdown
Contributor

dgomes commented Jul 3, 2018

🥇

@dgomes dgomes merged commit 66e33c7 into home-assistant:dev Jul 3, 2018
@ghost ghost removed the almost-done label Jul 3, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
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.

7 participants