Skip to content

Commit

Permalink
Improve logs when registers cannot be converted
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk committed Feb 4, 2024
1 parent 1d0b3f3 commit 23922c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ def __init__(self, message: str, frame: bytes) -> None:

class CommunicationError(ExceptionBase):
"""Exception to indicate a communication error."""


class ConversionError(ExceptionBase):
"""Exception to indicate an error converting register values."""
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from typing import Any, Callable, Optional, Union

from pydantic.utils import GetterDict
from custom_components.givenergy_local.givenergy_modbus.exceptions import (
ConversionError,
)

from custom_components.givenergy_local.givenergy_modbus.model import TimeSlot

Expand Down Expand Up @@ -160,23 +163,28 @@ def get(self, key: str, default: Any = None) -> Any:
if None in regs:
return None

if r.pre_conv:
if isinstance(r.pre_conv, tuple):
args = regs + list(r.pre_conv[1:])
val = r.pre_conv[0](*args)
try:
if r.pre_conv:
if isinstance(r.pre_conv, tuple):
args = regs + list(r.pre_conv[1:])
val = r.pre_conv[0](*args)
else:
val = r.pre_conv(*regs)
else:
val = r.pre_conv(*regs)
else:
val = regs
val = regs

if r.post_conv:
if isinstance(r.post_conv, tuple):
return r.post_conv[0](val, *r.post_conv[1:])
else:
if not isinstance(r.post_conv, Callable):
pass
return r.post_conv(val)
return val
if r.post_conv:
if isinstance(r.post_conv, tuple):
return r.post_conv[0](val, *r.post_conv[1:])
else:
if not isinstance(r.post_conv, Callable):
pass
return r.post_conv(val)
return val
except ValueError as err:
raise ConversionError(
f"Failed to convert {key} from {regs}: {err}"
) from err

@classmethod
def to_fields(cls) -> dict[str, tuple[Any, None]]:
Expand Down

0 comments on commit 23922c3

Please sign in to comment.