Skip to content

Commit

Permalink
sensor: add history field options
Browse files Browse the repository at this point in the history
Signed-off-by:  Eric Callahan <[email protected]>
  • Loading branch information
Arksine committed May 4, 2024
1 parent b60e6dc commit 7411c59
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions moonraker/components/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
from collections import defaultdict, deque
from functools import partial
from ..common import RequestType
from ..common import RequestType, HistoryFieldData

# Annotation imports
from typing import (
Expand All @@ -24,12 +24,14 @@
Type,
TYPE_CHECKING,
Union,
Callable
)

if TYPE_CHECKING:
from ..confighelper import ConfigHelper
from ..common import WebRequest
from .mqtt import MQTTClient
from .history import History

SENSOR_UPDATE_TIME = 1.0
SENSOR_EVENT_NAME = "sensors:sensor_update"
Expand All @@ -56,6 +58,53 @@ def __init__(self, config: ConfigHelper) -> None:
self.values: DefaultDict[str, Deque[Union[int, float]]] = defaultdict(
lambda: deque(maxlen=store_size)
)
history: History = self.server.lookup_component("history")
self.field_info: Dict[str, List[HistoryFieldData]] = {}
all_opts = list(config.get_options().keys())
cfg_name = config.get_name()
hist_field_prefix = "history_field_"
for opt in all_opts:
if not opt.startswith(hist_field_prefix):
continue
name = opt[len(hist_field_prefix):]
field_cfg: Dict[str, str] = config.getdict(opt)
ident: Optional[str] = field_cfg.get("parameter")
if ident is None:
raise config.error(
f"[{cfg_name}]: option '{opt}', key 'parameter' must be"
f"specified"
)
do_init: str = field_cfg.get("init_tracker", "false").lower()
reset_cb = self._gen_reset_callback(ident) if do_init == "true" else None
excl_paused: str = field_cfg.get("exclude_paused", "false").lower()
report_total: str = field_cfg.get("report_total", "false").lower()
report_max: str = field_cfg.get("report_maximum", "false").lower()
precision: Optional[str] = field_cfg.get("precision")
try:
fdata = HistoryFieldData(
name,
cfg_name,
field_cfg.get("desc", f"{ident} tracker"),
field_cfg.get("strategy", "basic"),
units=field_cfg.get("units"),
reset_callback=reset_cb,
exclude_paused=excl_paused == "true",
report_total=report_total == "true",
report_maximum=report_max == "true",
precision=int(precision) if precision is not None else None,
)
except Exception as e:
raise config.error(
f"[{cfg_name}]: option '{opt}', error encountered during "
f"sensor field configuration: {e}"
) from e
self.field_info.setdefault(ident, []).append(fdata)
history.register_auxiliary_field(fdata)

def _gen_reset_callback(self, param_name: str) -> Callable[[], float]:
def on_reset() -> float:
return self.last_measurements.get(param_name, 0)
return on_reset

def _update_sensor_value(self, eventtime: float) -> None:
"""
Expand Down Expand Up @@ -108,6 +157,7 @@ def _on_state_update(self, payload: bytes) -> None:
context = {
"payload": payload.decode(),
"set_result": partial(_set_result, store=measurements),
"log_debug": logging.debug
}

try:
Expand All @@ -118,11 +168,12 @@ def _on_state_update(self, payload: bytes) -> None:
else:
self.error_state = None
self.last_measurements = measurements
logging.debug(
"Received updated sensor value for %s: %s",
self.name,
self.last_measurements,
)
for name, value in measurements.items():
fdata_list = self.field_info.get(name)
if fdata_list is None:
continue
for fdata in fdata_list:
fdata.tracker.update(value)

async def _on_mqtt_disconnected(self):
self.error_state = "MQTT Disconnected"
Expand Down

0 comments on commit 7411c59

Please sign in to comment.