Skip to content

Commit bf15635

Browse files
committed
Remove not used selfs, from functions, move above Class
1 parent 2cbd488 commit bf15635

File tree

3 files changed

+84
-84
lines changed

3 files changed

+84
-84
lines changed

plugwise/common.py

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,86 @@
3030
from munch import Munch
3131

3232

33+
def collect_power_values(
34+
data: GwEntityData, loc: Munch, tariff: str, legacy: bool = False
35+
) -> None:
36+
"""Something."""
37+
for loc.peak_select in ("nl_peak", "nl_offpeak"):
38+
loc.locator = (
39+
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
40+
f'measurement[@{tariff}="{loc.peak_select}"]'
41+
)
42+
if legacy:
43+
loc.locator = (
44+
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
45+
f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
46+
)
47+
48+
loc = power_data_peak_value(loc, legacy)
49+
if not loc.found:
50+
continue
51+
52+
data = power_data_energy_diff(loc.measurement, loc.net_string, loc.f_val, data)
53+
key = cast(SensorType, loc.key_string)
54+
data["sensors"][key] = loc.f_val
55+
56+
57+
def power_data_peak_value(loc: Munch, legacy: bool) -> Munch:
58+
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
59+
loc.found = True
60+
if loc.logs.find(loc.locator) is None:
61+
loc = check_alternative_location(loc, legacy)
62+
if not loc.found:
63+
return loc
64+
65+
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
66+
peak = "off_peak"
67+
log_found = loc.log_type.split("_")[0]
68+
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
69+
if "gas" in loc.measurement or loc.log_type == "point_meter":
70+
loc.key_string = f"{loc.measurement}_{log_found}"
71+
# Only for P1 Actual -------------------#
72+
if "phase" in loc.measurement:
73+
loc.key_string = f"{loc.measurement}"
74+
# --------------------------------------#
75+
loc.net_string = f"net_electricity_{log_found}"
76+
val = loc.logs.find(loc.locator).text
77+
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
78+
79+
return loc
80+
81+
82+
def power_data_energy_diff(
83+
measurement: str,
84+
net_string: SensorType,
85+
f_val: float | int,
86+
data: GwEntityData,
87+
) -> GwEntityData:
88+
"""Calculate differential energy."""
89+
if (
90+
"electricity" in measurement
91+
and "phase" not in measurement
92+
and "interval" not in net_string
93+
):
94+
diff = 1
95+
if "produced" in measurement:
96+
diff = -1
97+
if net_string not in data["sensors"]:
98+
tmp_val: float | int = 0
99+
else:
100+
tmp_val = data["sensors"][net_string]
101+
102+
if isinstance(f_val, int):
103+
tmp_val += f_val * diff
104+
else:
105+
tmp_val += float(f_val * diff)
106+
tmp_val = float(f"{round(tmp_val, 3):.3f}")
107+
108+
data["sensors"][net_string] = tmp_val
109+
110+
return data
111+
112+
33113
class SmileCommon:
34114
"""The SmileCommon class."""
35115

@@ -110,86 +190,6 @@ def _appl_thermostat_info(
110190

111191
return appl
112192

113-
def _collect_power_values(
114-
self, data: GwEntityData, loc: Munch, tariff: str, legacy: bool = False
115-
) -> None:
116-
"""Something."""
117-
for loc.peak_select in ("nl_peak", "nl_offpeak"):
118-
loc.locator = (
119-
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
120-
f'measurement[@{tariff}="{loc.peak_select}"]'
121-
)
122-
if legacy:
123-
loc.locator = (
124-
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
125-
f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
126-
)
127-
128-
loc = self._power_data_peak_value(loc, legacy)
129-
if not loc.found:
130-
continue
131-
132-
data = self._power_data_energy_diff(
133-
loc.measurement, loc.net_string, loc.f_val, data
134-
)
135-
key = cast(SensorType, loc.key_string)
136-
data["sensors"][key] = loc.f_val
137-
138-
def _power_data_peak_value(self, loc: Munch, legacy: bool) -> Munch:
139-
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
140-
loc.found = True
141-
if loc.logs.find(loc.locator) is None:
142-
loc = check_alternative_location(loc, legacy)
143-
if not loc.found:
144-
return loc
145-
146-
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
147-
peak = "off_peak"
148-
log_found = loc.log_type.split("_")[0]
149-
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
150-
if "gas" in loc.measurement or loc.log_type == "point_meter":
151-
loc.key_string = f"{loc.measurement}_{log_found}"
152-
# Only for P1 Actual -------------------#
153-
if "phase" in loc.measurement:
154-
loc.key_string = f"{loc.measurement}"
155-
# --------------------------------------#
156-
loc.net_string = f"net_electricity_{log_found}"
157-
val = loc.logs.find(loc.locator).text
158-
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
159-
160-
return loc
161-
162-
def _power_data_energy_diff(
163-
self,
164-
measurement: str,
165-
net_string: SensorType,
166-
f_val: float | int,
167-
data: GwEntityData,
168-
) -> GwEntityData:
169-
"""Calculate differential energy."""
170-
if (
171-
"electricity" in measurement
172-
and "phase" not in measurement
173-
and "interval" not in net_string
174-
):
175-
diff = 1
176-
if "produced" in measurement:
177-
diff = -1
178-
if net_string not in data["sensors"]:
179-
tmp_val: float | int = 0
180-
else:
181-
tmp_val = data["sensors"][net_string]
182-
183-
if isinstance(f_val, int):
184-
tmp_val += f_val * diff
185-
else:
186-
tmp_val += float(f_val * diff)
187-
tmp_val = float(f"{round(tmp_val, 3):.3f}")
188-
189-
data["sensors"][net_string] = tmp_val
190-
191-
return data
192-
193193
def _create_gw_entities(self, appl: Munch) -> None:
194194
"""Helper-function for creating/updating gw_entities."""
195195
self.gw_entities[appl.entity_id] = {"dev_class": appl.pwclass}

plugwise/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import datetime as dt
99
from typing import cast
1010

11-
from plugwise.common import SmileCommon
11+
from plugwise.common import SmileCommon, collect_power_values
1212
from plugwise.constants import (
1313
ACTIVE_ACTUATORS,
1414
ACTUATOR_CLASSES,
@@ -389,7 +389,7 @@ def _power_data_from_location(self) -> GwEntityData:
389389
loc.logs = self._home_location.find("./logs")
390390
for loc.measurement, loc.attrs in P1_MEASUREMENTS.items():
391391
for loc.log_type in log_list:
392-
self._collect_power_values(data, loc, t_string)
392+
collect_power_values(data, loc, t_string)
393393

394394
self._count += len(data["sensors"])
395395
return data

plugwise/legacy/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import cast
99

10-
from plugwise.common import SmileCommon
10+
from plugwise.common import SmileCommon, collect_power_values
1111
from plugwise.constants import (
1212
ACTIVE_ACTUATORS,
1313
ACTUATOR_CLASSES,
@@ -308,7 +308,7 @@ def _power_data_from_modules(self) -> GwEntityData:
308308
loc.meas_list = loc.measurement.split("_")
309309
for loc.logs in mod_logs:
310310
for loc.log_type in mod_list:
311-
self._collect_power_values(data, loc, t_string, legacy=True)
311+
collect_power_values(data, loc, t_string, legacy=True)
312312

313313
self._count += len(data["sensors"])
314314
return data

0 commit comments

Comments
 (0)