Skip to content

Commit

Permalink
Refactor pack_value function and enhance bitmask handling
Browse files Browse the repository at this point in the history
- Updated `pack_value` to return `RegType` instead of a tuple, improving type clarity.
- Removed deprecated `patch_bitmask` function and reintroduced it with improved implementation.
- Simplified `NumberRWSensor` to utilize `pack_value` for dynamic register packing based on address length.
- Enhanced code readability and maintainability by consolidating bitmask operations.
  • Loading branch information
maslyankov committed Jan 20, 2025
1 parent f46e4cb commit 45daf5c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/sunsynk/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NumType = float | int


def pack_value(value: int, bits: int = 16, signed: bool = True) -> int | tuple[int, int]:
def pack_value(value: int, bits: int = 16, signed: bool = True) -> RegType:
"""Pack a value into register format.
Args:
Expand All @@ -27,7 +27,7 @@ def pack_value(value: int, bits: int = 16, signed: bool = True) -> int | tuple[i
"""
if bits == 16:
fmt = 'h' if signed else 'H'
return struct.unpack('H', struct.pack(fmt, value))[0]
return struct.unpack('H', struct.pack(fmt, value))
if bits == 32:
fmt = 'i' if signed else 'I'
return struct.unpack('2H', struct.pack(fmt, value))
Expand Down Expand Up @@ -102,6 +102,11 @@ def hex_str(regs: RegType, address: RegType | None = None) -> str:
return f"{{{' '.join(res)}}}"


def patch_bitmask(value: int, patch: int, bitmask: int) -> int:
"""Combine bitmask values."""
return (patch & bitmask) + (value & (0xFFFF - bitmask))


class SSTime:
"""Deals with inverter time format conversion complexities."""

Expand Down Expand Up @@ -151,8 +156,3 @@ def str_value(self, value: str) -> None:
self.minutes = int(hours) * 60 + int(minutes)
except ValueError:
_LOGGER.warning("Invalid time string: %s (expected hh:mm)", value)


def patch_bitmask(value: int, patch: int, bitmask: int) -> int:
"""Combine bitmask values."""
return (patch & bitmask) + (value & (0xFFFF - bitmask))
7 changes: 1 addition & 6 deletions src/sunsynk/rwsensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ def value_to_reg(self, value: ValType, resolve: ResolveType) -> RegType:
maxv = resolve_num(resolve, self.max, 100)
val = int(max(minv, min(maxv, fval)) / abs(self.factor))

if len(self.address) == 1:
return self.reg(pack_value(val, bits=16, signed=self.factor < 0))
if len(self.address) == 2:
low, high = pack_value(val, bits=32, signed=self.factor < 0) # type: ignore
return self.reg(low, high)
raise NotImplementedError(f"Address length not supported: {self.address}")
return self.reg(*pack_value(val, bits=len(self.addrees)*16, signed=self.factor < 0))


@attrs.define(slots=True, eq=False)
Expand Down

0 comments on commit 45daf5c

Please sign in to comment.