Skip to content

Commit 43483d8

Browse files
committed
Fix duplication, add docstring
1 parent 7710551 commit 43483d8

File tree

3 files changed

+50
-58
lines changed

3 files changed

+50
-58
lines changed

plugwise/helper.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
ADAM,
1616
ANNA,
1717
ATTR_NAME,
18-
ATTR_UNIT_OF_MEASUREMENT,
19-
BINARY_SENSORS,
2018
DATA,
2119
DEVICE_MEASUREMENTS,
2220
DHW_SETPOINT,
@@ -29,22 +27,16 @@
2927
NONE,
3028
OFF,
3129
P1_MEASUREMENTS,
32-
SENSORS,
33-
SPECIALS,
34-
SWITCHES,
3530
TEMP_CELSIUS,
3631
THERMOSTAT_CLASSES,
3732
TOGGLES,
3833
UOM,
3934
ActuatorData,
4035
ActuatorDataType,
4136
ActuatorType,
42-
BinarySensorType,
4337
DeviceData,
4438
GatewayData,
4539
SensorType,
46-
SpecialType,
47-
SwitchType,
4840
ThermoLoc,
4941
ToggleNameType,
5042
)
@@ -58,6 +50,7 @@
5850
check_model,
5951
escape_illegal_xml_characters,
6052
format_measure,
53+
match_on_true_cases,
6154
skip_obsolete_measurements,
6255
)
6356

@@ -585,29 +578,12 @@ def _appliance_measurements(
585578
measurement = new_name
586579

587580
match measurement:
588-
# measurements with states "on" or "off" that need to be passed directly
589-
case "select_dhw_mode":
590-
data["select_dhw_mode"] = appl_p_loc.text
591-
case _ as measurement if measurement in BINARY_SENSORS:
592-
bs_key = cast(BinarySensorType, measurement)
593-
bs_value = appl_p_loc.text in ("on", "true")
594-
data["binary_sensors"][bs_key] = bs_value
595-
case _ as measurement if measurement in SENSORS:
596-
s_key = cast(SensorType, measurement)
597-
s_value = format_measure(
598-
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
599-
)
600-
data["sensors"][s_key] = s_value
601-
case _ as measurement if measurement in SWITCHES:
602-
sw_key = cast(SwitchType, measurement)
603-
sw_value = appl_p_loc.text in ("on", "true")
604-
data["switches"][sw_key] = sw_value
605-
case _ as measurement if measurement in SPECIALS:
606-
sp_key = cast(SpecialType, measurement)
607-
sp_value = appl_p_loc.text in ("on", "true")
608-
data[sp_key] = sp_value
609581
case "elga_status_code":
610582
data["elga_status_code"] = int(appl_p_loc.text)
583+
case "select_dhw_mode":
584+
data["select_dhw_mode"] = appl_p_loc.text
585+
586+
match_on_true_cases(measurement, attrs, appl_p_loc, data)
611587

612588
i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement'
613589
if (appl_i_loc := appliance.find(i_locator)) is not None:

plugwise/legacy/helper.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
ACTUATOR_CLASSES,
1313
APPLIANCES,
1414
ATTR_NAME,
15-
ATTR_UNIT_OF_MEASUREMENT,
16-
BINARY_SENSORS,
1715
DATA,
1816
DEVICE_MEASUREMENTS,
1917
ENERGY_WATT_HOUR,
@@ -24,25 +22,24 @@
2422
NONE,
2523
OFF,
2624
P1_LEGACY_MEASUREMENTS,
27-
SENSORS,
28-
SPECIALS,
29-
SWITCHES,
3025
TEMP_CELSIUS,
3126
THERMOSTAT_CLASSES,
3227
UOM,
3328
ActuatorData,
3429
ActuatorDataType,
3530
ActuatorType,
3631
ApplianceType,
37-
BinarySensorType,
3832
DeviceData,
3933
GatewayData,
4034
SensorType,
41-
SpecialType,
42-
SwitchType,
4335
ThermoLoc,
4436
)
45-
from plugwise.util import format_measure, skip_obsolete_measurements, version_to_model
37+
from plugwise.util import (
38+
format_measure,
39+
match_on_true_cases,
40+
skip_obsolete_measurements,
41+
version_to_model,
42+
)
4643

4744
# This way of importing aiohttp is because of patch/mocking in testing (aiohttp timeouts)
4845
from defusedxml import ElementTree as etree
@@ -341,25 +338,7 @@ def _appliance_measurements(
341338
if new_name := getattr(attrs, ATTR_NAME, None):
342339
measurement = new_name
343340

344-
match measurement:
345-
case _ as measurement if measurement in BINARY_SENSORS:
346-
bs_key = cast(BinarySensorType, measurement)
347-
bs_value = appl_p_loc.text in ("on", "true")
348-
data["binary_sensors"][bs_key] = bs_value
349-
case _ as measurement if measurement in SENSORS:
350-
s_key = cast(SensorType, measurement)
351-
s_value = format_measure(
352-
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
353-
)
354-
data["sensors"][s_key] = s_value
355-
case _ as measurement if measurement in SWITCHES:
356-
sw_key = cast(SwitchType, measurement)
357-
sw_value = appl_p_loc.text in ("on", "true")
358-
data["switches"][sw_key] = sw_value
359-
case _ as measurement if measurement in SPECIALS:
360-
sp_key = cast(SpecialType, measurement)
361-
sp_value = appl_p_loc.text in ("on", "true")
362-
data[sp_key] = sp_value
341+
match_on_true_cases(measurement, attrs, appl_p_loc, data)
363342

364343
i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement'
365344
if (appl_i_loc := appliance.find(i_locator)) is not None:

plugwise/util.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,38 @@
33

44
import datetime as dt
55
import re
6+
from typing import cast
67

78
from plugwise.constants import (
89
ATTR_UNIT_OF_MEASUREMENT,
10+
BINARY_SENSORS,
11+
DATA,
912
ELECTRIC_POTENTIAL_VOLT,
1013
ENERGY_KILO_WATT_HOUR,
1114
HW_MODELS,
1215
OBSOLETE_MEASUREMENTS,
1316
PERCENTAGE,
1417
POWER_WATT,
18+
SENSORS,
1519
SPECIAL_FORMAT,
20+
SPECIALS,
21+
SWITCHES,
1622
TEMP_CELSIUS,
23+
UOM,
24+
BinarySensorType,
1725
DeviceData,
1826
ModelData,
27+
SensorType,
28+
SpecialType,
29+
SwitchType,
1930
)
2031

2132
from defusedxml import ElementTree as etree
2233
from munch import Munch
2334

2435

2536
def check_alternative_location(loc: Munch, legacy: bool) -> Munch:
26-
"""Try."""
37+
"""Helper-function for _power_data_peak_value()."""
2738
if in_alternative_location(loc, legacy):
2839
# Avoid double processing by skipping one peak-list option
2940
if loc.peak_select == "nl_offpeak":
@@ -146,6 +157,32 @@ def get_vendor_name(module: etree, model_data: ModelData) -> ModelData:
146157
return model_data
147158

148159

160+
def match_on_true_cases(
161+
measurement: str,
162+
attrs: DATA | UOM,
163+
location: etree,
164+
data: DeviceData,
165+
) -> None:
166+
"""Helper-function for common match-case execution."""
167+
value = location.text in ("on", "true")
168+
match measurement:
169+
case _ as measurement if measurement in BINARY_SENSORS:
170+
bs_key = cast(BinarySensorType, measurement)
171+
data["binary_sensors"][bs_key] = value
172+
case _ as measurement if measurement in SENSORS:
173+
s_key = cast(SensorType, measurement)
174+
s_value = format_measure(
175+
location.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
176+
)
177+
data["sensors"][s_key] = s_value
178+
case _ as measurement if measurement in SWITCHES:
179+
sw_key = cast(SwitchType, measurement)
180+
data["switches"][sw_key] = value
181+
case _ as measurement if measurement in SPECIALS:
182+
sp_key = cast(SpecialType, measurement)
183+
data[sp_key] = value
184+
185+
149186
def power_data_local_format(
150187
attrs: dict[str, str], key_string: str, val: str
151188
) -> float | int:

0 commit comments

Comments
 (0)