55import re
66import struct
77from dataclasses import dataclass , field
8- from typing import TYPE_CHECKING , Annotated , Dict , List , Literal , Optional
8+ from typing import TYPE_CHECKING , Annotated , Dict , List , Literal
99
1010import numpy as np
1111
@@ -397,7 +397,7 @@ def get_hardware_id(self, module:int=0) -> str:
397397 logger .error ("Unexpected error during process: %s" , e )
398398 raise # Re-raise the exception for the caller to handle
399399
400- def read_config (self , module :int = 0 ) -> Optional [ LifuUserConfig ] :
400+ def read_config (self , module :int = 0 ) -> LifuUserConfig | None :
401401 """
402402 Read the user configuration from device flash.
403403
@@ -450,7 +450,7 @@ def read_config(self, module:int=0) -> Optional[LifuUserConfig]:
450450 logger .exception ("Unexpected error reading config" )
451451 raise
452452
453- def write_config (self , config : LifuUserConfig , module :int = 0 ) -> Optional [ LifuUserConfig ] :
453+ def write_config (self , config : LifuUserConfig , module :int = 0 ) -> LifuUserConfig | None :
454454 """
455455 Write user configuration to device flash.
456456
@@ -517,7 +517,7 @@ def write_config(self, config: LifuUserConfig, module:int=0) -> Optional[LifuUse
517517 logger .exception ("Unexpected error writing config" )
518518 raise
519519
520- def write_config_json (self , json_str : str , module :int = 0 ) -> Optional [ LifuUserConfig ] :
520+ def write_config_json (self , json_str : str , module :int = 0 ) -> LifuUserConfig | None :
521521 """
522522 Write user configuration from a JSON string.
523523
@@ -540,6 +540,13 @@ def write_config_json(self, json_str: str, module:int=0) -> Optional[LifuUserCon
540540 return self .write_config (module = module , config = config )
541541 except json .JSONDecodeError as e :
542542 logger .error (f"Invalid JSON: { e } " )
543+ raise ValueError (f"Invalid JSON: { e } " ) from e
544+ except ValueError as v :
545+ logger .error ("ValueError: %s" , v )
546+ raise
547+ except (OSError , RuntimeError , AttributeError ) as e :
548+ logger .exception ("Unexpected error writing config from JSON" )
549+ raise
543550
544551 def get_temperature (self , module :int = 1 ) -> float :
545552 """
@@ -982,7 +989,7 @@ def async_mode(self, enable: bool | None = None) -> bool:
982989 else :
983990 payload = None
984991
985- r = self .uart .send_packet (id = None , packetType = OW_CONTROLLER , command = OW_CMD_ASYNC , addr = 0 , data = payload )
992+ r = self .uart .send_packet (id = None , packetType = OW_CMD , command = OW_CMD_ASYNC , addr = 0 , data = payload )
986993 self .uart .clear_buffer ()
987994 # r.print_packet()
988995 if r .packet_type == OW_ERROR :
@@ -1332,7 +1339,7 @@ def write_block(self, identifier: int, start_address: int, reg_values: List[int]
13321339 logger .error ("Unexpected error in write_block: %s" , e )
13331340 raise # Re-raise the exception for the caller to handle
13341341
1335- def read_block (self , identifier : int , start_address : int , count : int ) -> Optional [ List [int ]] :
1342+ def read_block (self , identifier : int , start_address : int , count : int ) -> List [int ] | None :
13361343 """
13371344 Read a block of consecutive register values from the TX device.
13381345
0 commit comments