@@ -116,6 +116,30 @@ def check_model(name: str | None, vendor_name: str | None) -> str | None:
116116 return None
117117
118118
119+ def collect_power_values (
120+ data : GwEntityData , loc : Munch , tariff : str , legacy : bool = False
121+ ) -> None :
122+ """Something."""
123+ for loc .peak_select in ("nl_peak" , "nl_offpeak" ):
124+ loc .locator = (
125+ f'./{ loc .log_type } [type="{ loc .measurement } "]/period/'
126+ f'measurement[@{ tariff } ="{ loc .peak_select } "]'
127+ )
128+ if legacy :
129+ loc .locator = (
130+ f"./{ loc .meas_list [0 ]} _{ loc .log_type } /measurement"
131+ f'[@directionality="{ loc .meas_list [1 ]} "][@{ tariff } ="{ loc .peak_select } "]'
132+ )
133+
134+ loc = power_data_peak_value (loc , legacy )
135+ if not loc .found :
136+ continue
137+
138+ data = power_data_energy_diff (loc .measurement , loc .net_string , loc .f_val , data )
139+ key = cast (SensorType , loc .key_string )
140+ data ["sensors" ][key ] = loc .f_val
141+
142+
119143def common_match_cases (
120144 measurement : str ,
121145 attrs : DATA | UOM ,
@@ -199,6 +223,37 @@ def get_vendor_name(module: etree, model_data: ModuleData) -> ModuleData:
199223 return model_data
200224
201225
226+ def power_data_energy_diff (
227+ measurement : str ,
228+ net_string : SensorType ,
229+ f_val : float | int ,
230+ data : GwEntityData ,
231+ ) -> GwEntityData :
232+ """Calculate differential energy."""
233+ if (
234+ "electricity" in measurement
235+ and "phase" not in measurement
236+ and "interval" not in net_string
237+ ):
238+ diff = 1
239+ if "produced" in measurement :
240+ diff = - 1
241+ if net_string not in data ["sensors" ]:
242+ tmp_val : float | int = 0
243+ else :
244+ tmp_val = data ["sensors" ][net_string ]
245+
246+ if isinstance (f_val , int ):
247+ tmp_val += f_val * diff
248+ else :
249+ tmp_val += float (f_val * diff )
250+ tmp_val = float (f"{ round (tmp_val , 3 ):.3f} " )
251+
252+ data ["sensors" ][net_string ] = tmp_val
253+
254+ return data
255+
256+
202257def power_data_local_format (
203258 attrs : dict [str , str ], key_string : str , val : str
204259) -> float | int :
@@ -212,6 +267,31 @@ def power_data_local_format(
212267 return format_measure (val , attrs_uom )
213268
214269
270+ def power_data_peak_value (loc : Munch , legacy : bool ) -> Munch :
271+ """Helper-function for _power_data_from_location() and _power_data_from_modules()."""
272+ loc .found = True
273+ if loc .logs .find (loc .locator ) is None :
274+ loc = check_alternative_location (loc , legacy )
275+ if not loc .found :
276+ return loc
277+
278+ if (peak := loc .peak_select .split ("_" )[1 ]) == "offpeak" :
279+ peak = "off_peak"
280+ log_found = loc .log_type .split ("_" )[0 ]
281+ loc .key_string = f"{ loc .measurement } _{ peak } _{ log_found } "
282+ if "gas" in loc .measurement or loc .log_type == "point_meter" :
283+ loc .key_string = f"{ loc .measurement } _{ log_found } "
284+ # Only for P1 Actual -------------------#
285+ if "phase" in loc .measurement :
286+ loc .key_string = f"{ loc .measurement } "
287+ # --------------------------------------#
288+ loc .net_string = f"net_electricity_{ log_found } "
289+ val = loc .logs .find (loc .locator ).text
290+ loc .f_val = power_data_local_format (loc .attrs , loc .key_string , val )
291+
292+ return loc
293+
294+
215295def remove_empty_platform_dicts (data : GwEntityData ) -> None :
216296 """Helper-function for removing any empty platform dicts."""
217297 if not data ["binary_sensors" ]:
0 commit comments