forked from dgomes/home-assistant-custom-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_helper.py
67 lines (55 loc) · 2.15 KB
/
filter_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Filter helpers for Home Assistant."""
import logging
import inspect
from homeassistant.components.filter.sensor import (
OutlierFilter, LowPassFilter, ThrottleFilter,
TimeSMAFilter, FilterState)
import homeassistant.util.dt as dt_util
FILTER_LOWPASS = 'lowpass'
FILTER_OUTLIER = 'outlier'
FILTER_TIME_SMA = 'time_sma'
FILTER_THROTTLE = 'throttle'
FILTERS = {
FILTER_LOWPASS: LowPassFilter,
FILTER_OUTLIER: OutlierFilter,
FILTER_TIME_SMA: TimeSMAFilter,
FILTER_THROTTLE: ThrottleFilter
}
class FakeState(object):
"""Fake HA state."""
def __init__(self, value):
"""Keep value and timestamp."""
self.last_updated = dt_util.utcnow()
self.state = value
class Filter(object):
"""Filter decorator."""
def __init__(self, filter_algorithm, **kwargs):
"""Decorator constructor, selects algorithm and configures window.
Args:
filter_algorithm (string): must be one of the defined filters
kwargs (dict): arguments to be passed to the specific filter
"""
try:
module_name = inspect.stack()[1][0].f_globals['__name__']
Filter.logger = logging.getLogger(module_name)
Filter.logger.debug("Filter %s(%s) on %s", filter_algorithm, kwargs,
module_name)
except:
Filter.logger = logging.getLogger("custom_components")
if filter_algorithm in FILTERS:
self.filter = FILTERS[filter_algorithm](**kwargs)
else:
self.logger.error("Unknown filter <%s>", filter_algorithm)
def __call__(self, func):
"""Decorate function as filter."""
def func_wrapper(sensor_object):
"""Wrap for the original state() function."""
new_state = FakeState(func(sensor_object))
try:
filtered_state = self.filter.filter_state(new_state)
except TypeError:
return None
Filter.logger.debug("%s(%s) -> %s", sensor_object.entity_id,
new_state.state, filtered_state.state)
return filtered_state.state
return func_wrapper