Skip to content

Commit

Permalink
Enhance Sensor class: introduce TypeVar for dependencies and improve …
Browse files Browse the repository at this point in the history
…update logic for load limit handling
  • Loading branch information
maslyankov committed Jan 3, 2025
1 parent d921737 commit 5c8cabd
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/sunsynk/sensors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sensor classes represent modbus registers for an inverter."""

from __future__ import annotations
from typing import TypeVar

import logging

Expand All @@ -18,6 +19,8 @@

_LOGGER = logging.getLogger(__name__)

SensorType = TypeVar("SensorType", bound="Sensor")


@attrs.define(slots=True, eq=False)
class Sensor:
Expand Down Expand Up @@ -64,16 +67,18 @@ def masked(self, regs: RegType) -> RegType:
return regs

@property
def dependencies(self) -> list[str]:
"""Return list of dependency sensor IDs."""
if self.zero_export_absolute:
return ["load_limit"]
def dependencies(self) -> list[SensorType]:
"""Return list of dependency sensors."""
return []

def update_dependencies(self, sensors: dict[str, ValType]) -> None:
"""Update dependency values."""
if self.zero_export_absolute:
self._load_limit = sensors.get("load_limit")
load_limit = sensors.get("load_limit")
if isinstance(load_limit, (int, float)):
self._load_limit = int(load_limit)
else:
self._load_limit = None

def __hash__(self) -> int:
"""Hash the sensor id."""
Expand Down

0 comments on commit 5c8cabd

Please # to comment.