Skip to content

Commit

Permalink
Fix duplication, add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwew committed May 18, 2024
1 parent 7710551 commit 43483d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 58 deletions.
34 changes: 5 additions & 29 deletions plugwise/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
ADAM,
ANNA,
ATTR_NAME,
ATTR_UNIT_OF_MEASUREMENT,
BINARY_SENSORS,
DATA,
DEVICE_MEASUREMENTS,
DHW_SETPOINT,
Expand All @@ -29,22 +27,16 @@
NONE,
OFF,
P1_MEASUREMENTS,
SENSORS,
SPECIALS,
SWITCHES,
TEMP_CELSIUS,
THERMOSTAT_CLASSES,
TOGGLES,
UOM,
ActuatorData,
ActuatorDataType,
ActuatorType,
BinarySensorType,
DeviceData,
GatewayData,
SensorType,
SpecialType,
SwitchType,
ThermoLoc,
ToggleNameType,
)
Expand All @@ -58,6 +50,7 @@
check_model,
escape_illegal_xml_characters,
format_measure,
match_on_true_cases,
skip_obsolete_measurements,
)

Expand Down Expand Up @@ -585,29 +578,12 @@ def _appliance_measurements(
measurement = new_name

match measurement:
# measurements with states "on" or "off" that need to be passed directly
case "select_dhw_mode":
data["select_dhw_mode"] = appl_p_loc.text
case _ as measurement if measurement in BINARY_SENSORS:
bs_key = cast(BinarySensorType, measurement)
bs_value = appl_p_loc.text in ("on", "true")
data["binary_sensors"][bs_key] = bs_value
case _ as measurement if measurement in SENSORS:
s_key = cast(SensorType, measurement)
s_value = format_measure(
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
)
data["sensors"][s_key] = s_value
case _ as measurement if measurement in SWITCHES:
sw_key = cast(SwitchType, measurement)
sw_value = appl_p_loc.text in ("on", "true")
data["switches"][sw_key] = sw_value
case _ as measurement if measurement in SPECIALS:
sp_key = cast(SpecialType, measurement)
sp_value = appl_p_loc.text in ("on", "true")
data[sp_key] = sp_value
case "elga_status_code":
data["elga_status_code"] = int(appl_p_loc.text)
case "select_dhw_mode":
data["select_dhw_mode"] = appl_p_loc.text

match_on_true_cases(measurement, attrs, appl_p_loc, data)

i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement'
if (appl_i_loc := appliance.find(i_locator)) is not None:
Expand Down
35 changes: 7 additions & 28 deletions plugwise/legacy/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
ACTUATOR_CLASSES,
APPLIANCES,
ATTR_NAME,
ATTR_UNIT_OF_MEASUREMENT,
BINARY_SENSORS,
DATA,
DEVICE_MEASUREMENTS,
ENERGY_WATT_HOUR,
Expand All @@ -24,25 +22,24 @@
NONE,
OFF,
P1_LEGACY_MEASUREMENTS,
SENSORS,
SPECIALS,
SWITCHES,
TEMP_CELSIUS,
THERMOSTAT_CLASSES,
UOM,
ActuatorData,
ActuatorDataType,
ActuatorType,
ApplianceType,
BinarySensorType,
DeviceData,
GatewayData,
SensorType,
SpecialType,
SwitchType,
ThermoLoc,
)
from plugwise.util import format_measure, skip_obsolete_measurements, version_to_model
from plugwise.util import (
format_measure,
match_on_true_cases,
skip_obsolete_measurements,
version_to_model,
)

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

match measurement:
case _ as measurement if measurement in BINARY_SENSORS:
bs_key = cast(BinarySensorType, measurement)
bs_value = appl_p_loc.text in ("on", "true")
data["binary_sensors"][bs_key] = bs_value
case _ as measurement if measurement in SENSORS:
s_key = cast(SensorType, measurement)
s_value = format_measure(
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
)
data["sensors"][s_key] = s_value
case _ as measurement if measurement in SWITCHES:
sw_key = cast(SwitchType, measurement)
sw_value = appl_p_loc.text in ("on", "true")
data["switches"][sw_key] = sw_value
case _ as measurement if measurement in SPECIALS:
sp_key = cast(SpecialType, measurement)
sp_value = appl_p_loc.text in ("on", "true")
data[sp_key] = sp_value
match_on_true_cases(measurement, attrs, appl_p_loc, data)

i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement'
if (appl_i_loc := appliance.find(i_locator)) is not None:
Expand Down
39 changes: 38 additions & 1 deletion plugwise/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,38 @@

import datetime as dt
import re
from typing import cast

from plugwise.constants import (
ATTR_UNIT_OF_MEASUREMENT,
BINARY_SENSORS,
DATA,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
HW_MODELS,
OBSOLETE_MEASUREMENTS,
PERCENTAGE,
POWER_WATT,
SENSORS,
SPECIAL_FORMAT,
SPECIALS,
SWITCHES,
TEMP_CELSIUS,
UOM,
BinarySensorType,
DeviceData,
ModelData,
SensorType,
SpecialType,
SwitchType,
)

from defusedxml import ElementTree as etree
from munch import Munch


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


def match_on_true_cases(
measurement: str,
attrs: DATA | UOM,
location: etree,
data: DeviceData,
) -> None:
"""Helper-function for common match-case execution."""
value = location.text in ("on", "true")
match measurement:
case _ as measurement if measurement in BINARY_SENSORS:
bs_key = cast(BinarySensorType, measurement)
data["binary_sensors"][bs_key] = value
case _ as measurement if measurement in SENSORS:
s_key = cast(SensorType, measurement)
s_value = format_measure(
location.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
)
data["sensors"][s_key] = s_value
case _ as measurement if measurement in SWITCHES:
sw_key = cast(SwitchType, measurement)
data["switches"][sw_key] = value
case _ as measurement if measurement in SPECIALS:
sp_key = cast(SpecialType, measurement)
data[sp_key] = value


def power_data_local_format(
attrs: dict[str, str], key_string: str, val: str
) -> float | int:
Expand Down

0 comments on commit 43483d8

Please sign in to comment.