Skip to content

Commit

Permalink
refactor: parser.py constants moved to const.py
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Jul 14, 2024
1 parent c5d330d commit 2fafedf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 7 additions & 1 deletion custom_components/solarman/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
TIMINGS_QUERY_INTERVAL_DEFAULT = 60
TIMINGS_QUERY_EXCEPT_SLEEP = 4

DIGITS_DEFAULT = 6
REGISTERS_MIN_SPAN_DEFAULT = 25
REGISTERS_DIGITS_DEFAULT = 6

REQUEST_CODE = "code"
REQUEST_CODE_ALT = "mb_functioncode"
REQUEST_START = "start"
REQUEST_END = "end"

SERVICES_PARAM_REGISTER = "register"
SERVICES_PARAM_VALUE = "value"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _accounting(self):
if self.last_update_success:
self.counter += 1

# Temporary fix to retrieve all data after disconnect
# Temporary fix to retrieve all data after reconnect
if self.inverter.status != 1:
self.counter = 0

Expand Down
13 changes: 8 additions & 5 deletions custom_components/solarman/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ class ParameterParser:
def __init__(self, parameter_definition):
self._lookups = parameter_definition
self._update_interval = TIMINGS_QUERY_INTERVAL_DEFAULT
self._digits = DIGITS_DEFAULT
self._min_span = REGISTERS_MIN_SPAN_DEFAULT
self._digits = REGISTERS_DIGITS_DEFAULT
self._result = {}

if "default" in parameter_definition:
default = parameter_definition["default"]
if "update_interval" in default:
self._update_interval = default["update_interval"]
if "min_span" in default:
self._min_span = default["min_span"]
if "digits" in default:
self._digits = default["digits"]

_LOGGER.debug(f"Default update_interval: {self._update_interval}, digits: {self._digits}")
_LOGGER.debug(f"Default update_interval: {self._update_interval}, min_span: {self._min_span}, digits: {self._digits}")

def lookup(self):
return self._lookups["parameters"]

def is_valid(self, parameters):
return "name" in parameters and "rule" in parameters
return "name" in parameters and "rule" in parameters # and "registers" in parameters

def is_enabled(self, parameters):
return not "disabled" in parameters
Expand Down Expand Up @@ -80,9 +83,9 @@ def get_requests(self, runtime = 0):

registers.sort()

groups = group_when(registers, lambda x, y: y - x > 25)
groups = group_when(registers, lambda x, y: y - x > self._min_span)

return [{ "start": r[0], "end": r[-1], "mb_functioncode": 0x03 } for r in groups]
return [{ REQUEST_START: r[0], REQUEST_END: r[-1], REQUEST_CODE: 0x03 } for r in groups]

def parse(self, rawData, start, length):
for i in self.lookup():
Expand Down

0 comments on commit 2fafedf

Please sign in to comment.