@@ -17,6 +17,7 @@ def unwrap_bool(json_str: str) -> bool | None:
1717 except (json .JSONDecodeError , KeyError , ValueError , TypeError ):
1818 return None
1919
20+
2021def unwrap_int (json_str : str ) -> int | None :
2122 """Unwrap an integer value from a JSON string."""
2223 try :
@@ -27,6 +28,7 @@ def unwrap_int(json_str: str) -> int | None:
2728 except (json .JSONDecodeError , KeyError , ValueError , TypeError ):
2829 return None
2930
31+
3032def unwrap_int_default_0 (json_str : str ) -> int :
3133 """Unwrap an integer value from a JSON string, defaulting to 0."""
3234 try :
@@ -37,6 +39,7 @@ def unwrap_int_default_0(json_str: str) -> int:
3739 except (json .JSONDecodeError , KeyError , ValueError , TypeError ):
3840 return 0
3941
42+
4043def unwrap_int_seconds_to_hours (json_str : str , precision : int | None ) -> float | None :
4144 """Convert seconds to hours."""
4245 seconds = unwrap_int (json_str )
@@ -45,6 +48,7 @@ def unwrap_int_seconds_to_hours(json_str: str, precision: int | None) -> float |
4548 hours = seconds / 3600
4649 return hours if precision is None else round (hours , precision )
4750
51+
4852def unwrap_int_seconds_to_minutes (json_str : str , precision : int | None ) -> float | None :
4953 """Convert seconds to minutes."""
5054 seconds = unwrap_int (json_str )
@@ -53,6 +57,7 @@ def unwrap_int_seconds_to_minutes(json_str: str, precision: int | None) -> float
5357 minutes = seconds / 60
5458 return minutes if precision is None else round (minutes , precision )
5559
60+
5661def unwrap_float (json_str : str , precision : int | None , json_value : str = "value" ) -> float | None :
5762 """Unwrap a float value from a JSON string."""
5863 try :
@@ -64,6 +69,7 @@ def unwrap_float(json_str: str, precision: int | None, json_value: str = "value"
6469 except (json .JSONDecodeError , KeyError , ValueError , TypeError ):
6570 return None
6671
72+
6773def unwrap_float_m3_to_liters (json_str : str , precision : int | None ) -> float | None :
6874 """Convert cubic meters to liters."""
6975 value = unwrap_float (json_str , None )
@@ -72,6 +78,7 @@ def unwrap_float_m3_to_liters(json_str: str, precision: int | None) -> float | N
7278 liters = value * 1000
7379 return liters if precision is None else round (liters , precision )
7480
81+
7582def unwrap_string (json_str : str ) -> str | None :
7683 """Unwrap a string value from a JSON string."""
7784 try :
@@ -92,6 +99,7 @@ def unwrap_enum(json_str: str, enum: type[VictronEnum]) -> VictronEnum | None:
9299 val = data ["value" ]
93100 return enum .from_code (val ) if val is not None else None
94101
102+
95103def unwrap_bitmask (json_str : str , enum : type [VictronEnum ]) -> str | None :
96104 """Unwrap a bitmask value from a JSON string."""
97105 try :
@@ -101,10 +109,11 @@ def unwrap_bitmask(json_str: str, enum: type[VictronEnum]) -> str | None:
101109 val = data ["value" ]
102110 if val is None :
103111 return None
104- vals = [2 ** idx for idx ,bit in enumerate (bin (val )[:1 :- 1 ]) if int (bit )] if int (val )> 0 else [0 ]
112+ vals = [2 ** idx for idx , bit in enumerate (bin (val )[:1 :- 1 ]) if int (bit )] if int (val ) > 0 else [0 ]
105113 enums = [enum .from_code (v ) for v in vals ]
106114 return str .join (BITMASK_SEPARATOR , [e .string for e in enums if e is not None ])
107115
116+
108117def unwrap_epoch (json_str : str ) -> datetime | None :
109118 """Unwrap a timestamp value from a JSON string."""
110119 try :
@@ -116,13 +125,17 @@ def unwrap_epoch(json_str: str) -> datetime | None:
116125 except (json .JSONDecodeError , KeyError , ValueError , TypeError ):
117126 return None
118127
128+
119129def wrap_enum (enum_val : VictronEnum | str , enum_expected : type [VictronEnum ]) -> str :
120130 """Wrap an Enum value into a JSON string with a 'value' key."""
121131 if isinstance (enum_val , VictronEnum ):
122132 return json .dumps ({"value" : enum_val .code })
123133 return json .dumps ({"value" : enum_expected .from_id_or_string (enum_val ).code })
124134
125- def wrap_bitmask (bitmask_val : VictronEnum | str | Iterable [VictronEnum ] | Iterable [str ], enum_expected : type [VictronEnum ]) -> str :
135+
136+ def wrap_bitmask (
137+ bitmask_val : VictronEnum | str | Iterable [VictronEnum ] | Iterable [str ], enum_expected : type [VictronEnum ]
138+ ) -> str :
126139 """Wrap an bitmask value into a JSON string with a 'value' key."""
127140 if isinstance (bitmask_val , VictronEnum ):
128141 bitmask_val = [bitmask_val ]
@@ -137,18 +150,22 @@ def wrap_bitmask(bitmask_val: VictronEnum | str | Iterable[VictronEnum] | Iterab
137150 val += int (enum_expected .from_id_or_string (v ).code )
138151 return json .dumps ({"value" : val })
139152
153+
140154def wrap_int (value : int | None ) -> str :
141155 """Wrap an integer value into a JSON string with a 'value' key."""
142156 return json .dumps ({"value" : value })
143157
158+
144159def wrap_int_hours_to_seconds (value : int | None ) -> str :
145160 """Wrap an integer value into a JSON string with a 'value' key."""
146161 return json .dumps ({"value" : value * 3600 if value is not None else None })
147162
163+
148164def wrap_int_minutes_to_seconds (value : int | None ) -> str :
149165 """Wrap an integer value into a JSON string with a 'value' key."""
150166 return json .dumps ({"value" : value * 60 if value is not None else None })
151167
168+
152169def wrap_int_default_0 (value : int | None ) -> str :
153170 """Wrap an integer value into a JSON string with a 'value' key, defaulting to 0 if None."""
154171 return json .dumps ({"value" : value if value is not None else 0 })
@@ -163,11 +180,12 @@ def wrap_string(value: str | None) -> str:
163180 """Wrap a string value into a JSON string with a 'value' key."""
164181 return json .dumps ({"value" : value })
165182
183+
166184def wrap_epoch (value : datetime | None ) -> str :
167185 """Wrap a datetime value into a JSON string with a 'value' key in the format of an epoch timestamp."""
168186 if value is None :
169187 return json .dumps ({"value" : None })
170- return json .dumps ({"value" : datetime .timestamp (value ) })
188+ return json .dumps ({"value" : datetime .timestamp (value )})
171189
172190
173191VALUE_TYPE_UNWRAPPER = {
@@ -180,7 +198,7 @@ def wrap_epoch(value: datetime | None) -> str:
180198 ValueType .EPOCH : unwrap_epoch ,
181199 ValueType .INT_SECONDS_TO_HOURS : unwrap_int_seconds_to_hours ,
182200 ValueType .INT_SECONDS_TO_MINUTES : unwrap_int_seconds_to_minutes ,
183- ValueType .FLOAT_M3_TO_LITERS : unwrap_float_m3_to_liters
201+ ValueType .FLOAT_M3_TO_LITERS : unwrap_float_m3_to_liters ,
184202}
185203
186204VALUE_TYPE_WRAPPER = {
@@ -192,5 +210,5 @@ def wrap_epoch(value: datetime | None) -> str:
192210 ValueType .BITMASK : wrap_bitmask ,
193211 ValueType .EPOCH : wrap_epoch ,
194212 ValueType .INT_SECONDS_TO_HOURS : wrap_int_hours_to_seconds ,
195- ValueType .INT_SECONDS_TO_MINUTES : wrap_int_minutes_to_seconds
213+ ValueType .INT_SECONDS_TO_MINUTES : wrap_int_minutes_to_seconds ,
196214}
0 commit comments