Skip to content

Commit

Permalink
added tls
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Mar 27, 2024
1 parent 438f7c8 commit 4db64a3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
17 changes: 17 additions & 0 deletions docs/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ Example
refresh action: 01:30:00
Throttle Action
--------------------------------------

.. autopydantic_model:: ThrottleAction
:inherited-members: BaseModel


Example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
..
YamlModel: ThrottleAction
.. code-block:: yaml
throttle action: 30
Math
======================================

Expand Down
9 changes: 8 additions & 1 deletion src/sml2mqtt/config/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class RefreshAction(BaseModel):
every: DurationType = Field(alias='refresh action', description='Refresh interval')


class ThrottleAction(BaseModel):
"""Action which only lets one value pass in the defined period. If the last passed value is not at least
``period`` old any new value will not be forwarded.
"""
period: DurationType = Field(alias='throttle action', description='Throttle period')


# -------------------------------------------------------------------------------------------------
# Math
# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -253,7 +260,7 @@ class MeanOfInterval(HasIntervalFields):

OperationsModels = (
OnChangeFilter, DeltaFilter, HeartbeatFilter, RangeFilter,
RefreshAction,
RefreshAction, ThrottleAction,
Factor, Offset, Round,
NegativeOnEnergyMeterWorkaround,
Or, Sequence,
Expand Down
2 changes: 1 addition & 1 deletion src/sml2mqtt/sml_value/operations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .actions import RefreshActionOperation
from .actions import RefreshActionOperation, ThrottleActionOperation
from .date_time import DateTimeFinder, MaxValueOperation, MinValueOperation, VirtualMeterOperation
from .filter import (
DeltaFilterOperation,
Expand Down
25 changes: 25 additions & 0 deletions src/sml2mqtt/sml_value/operations/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,28 @@ def __repr__(self):
@override
def describe(self, indent: str = '') -> Generator[str, None, None]:
yield f'{indent:s}- Refresh Action: {format_period(self.every)}'


class ThrottleActionOperation(ValueOperationBase):
def __init__(self, period: DurationType):
self.period: Final = get_duration(period)
self.last_time: float = -1_000_000_000

@override
def process_value(self, value: float | None, info: SmlValueInfo) -> float | None:
if value is None:
return None

now = monotonic()
if self.last_time + self.period > now:
return None

self.last_time = now
return value

def __repr__(self):
return f'<ThrottleAction: {self.period}s at 0x{id(self):x}>'

@override
def describe(self, indent: str = '') -> Generator[str, None, None]:
yield f'{indent:s}- Throttle Action: {format_period(self.period)}'
3 changes: 3 additions & 0 deletions src/sml2mqtt/sml_value/setup_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
RefreshAction,
Round,
Sequence,
ThrottleAction,
VirtualMeter,
)
from sml2mqtt.sml_value.base import OperationContainerBase, ValueOperationBase
Expand All @@ -41,6 +42,7 @@
RefreshActionOperation,
RoundOperation,
SequenceOperation,
ThrottleActionOperation,
VirtualMeterOperation,
)

Expand All @@ -67,6 +69,7 @@ def create_sequence(operations: list[OperationsType]):
DeltaFilter: DeltaFilterOperation,

RefreshAction: RefreshActionOperation,
ThrottleAction: ThrottleActionOperation,

Factor: FactorOperation,
Offset: OffsetOperation,
Expand Down
25 changes: 20 additions & 5 deletions tests/sml_values/test_operations/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tests.sml_values.test_operations.helper import check_description, check_operation_repr

from sml2mqtt.sml_value.operations import RefreshActionOperation
from sml2mqtt.sml_value.operations import RefreshActionOperation, ThrottleActionOperation
from sml2mqtt.sml_value.operations._helper import format_period


Expand All @@ -17,6 +17,7 @@ def test_format_period():
def test_refresh_action(monotonic):
f = RefreshActionOperation(30)
check_operation_repr(f, '30s')
check_description(f, '- Refresh Action: 30 seconds')

assert f.process_value(1, None) == 1
assert f.process_value(None, None) is None
Expand All @@ -31,7 +32,21 @@ def test_refresh_action(monotonic):
monotonic.add(0.02)
assert f.process_value(None, None) == 2

check_description(
RefreshActionOperation(30),
'- Refresh Action: 30 seconds'
)

def test_throttle_action(monotonic):
f = ThrottleActionOperation(30)
check_operation_repr(f, '30s')
check_description(f, '- Throttle Action: 30 seconds')

assert f.process_value(None, None) is None
assert f.process_value(1, None) == 1

monotonic.set(29.99)
assert f.process_value(1, None) is None
monotonic.set(30)
assert f.process_value(1, None) == 1

monotonic.set(59.99)
assert f.process_value(1, None) is None
monotonic.set(60)
assert f.process_value(1, None) == 1

0 comments on commit 4db64a3

Please sign in to comment.