Skip to content

Commit

Permalink
Merge pull request #54 from boneIO-eu/socomec
Browse files Browse the repository at this point in the history
fix filter run twice for energy meters
  • Loading branch information
pszafer authored Nov 14, 2024
2 parents c86c09a + 48a5858 commit 3289881
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 6 additions & 3 deletions boneio/helper/filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Filter class to adjust sensor values."""

from __future__ import annotations
from typing import Optional
import logging

_LOGGER = logging.getLogger(__name__)
Expand All @@ -9,7 +10,7 @@
FILTERS = {
"offset": lambda x, y: x + y,
"round": lambda x, y: round(x, y),
"multiply": lambda x, y: x * y,
"multiply": lambda x, y: x * y if x else x,
"filter_out": lambda x, y: None if x == y else x,
"filter_out_greater": lambda x, y: None if x > y else x,
"filter_out_lower": lambda x, y: None if x < y else x,
Expand All @@ -19,8 +20,10 @@
class Filter:
_filters = []

def _apply_filters(self, value: float, filters: list = []) -> float | None:
filters = filters if filters else self._filters
def _apply_filters(
self, value: float | None, filters: Optional[list] = None
) -> float | None:
filters = filters if filters is not None else self._filters
for filter in filters:
for k, v in filter.items():
if k not in FILTERS:
Expand Down
6 changes: 1 addition & 5 deletions boneio/modbus/single_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ def set_user_filters(self, user_filters: list) -> None:
self._user_filters = user_filters

def set_value(self, value):
for filter in self._filters:
for key, _v in filter.items():
if key in allowed_operations:
lamda_function = allowed_operations[key]
value = lamda_function(value, _v)
value = self._apply_filters(value=value)
value = self._apply_filters(
value=value,
filters=self._user_filters,
Expand Down

0 comments on commit 3289881

Please sign in to comment.