Skip to content

Commit

Permalink
Improve common function-name
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwew committed May 18, 2024
1 parent 43483d8 commit b31d57b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions plugwise/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
)
from plugwise.util import (
check_model,
common_match_cases,
escape_illegal_xml_characters,
format_measure,
match_on_true_cases,
skip_obsolete_measurements,
)

Expand Down Expand Up @@ -583,7 +583,7 @@ def _appliance_measurements(
case "select_dhw_mode":
data["select_dhw_mode"] = appl_p_loc.text

match_on_true_cases(measurement, attrs, appl_p_loc, data)
common_match_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
4 changes: 2 additions & 2 deletions plugwise/legacy/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
ThermoLoc,
)
from plugwise.util import (
common_match_cases,
format_measure,
match_on_true_cases,
skip_obsolete_measurements,
version_to_model,
)
Expand Down Expand Up @@ -338,7 +338,7 @@ def _appliance_measurements(
if new_name := getattr(attrs, ATTR_NAME, None):
measurement = new_name

match_on_true_cases(measurement, attrs, appl_p_loc, data)
common_match_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
52 changes: 26 additions & 26 deletions plugwise/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ def check_model(name: str | None, vendor_name: str | None) -> str | None:
return name


def common_match_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 escape_illegal_xml_characters(xmldata: str) -> str:
"""Replace illegal &-characters."""
return re.sub(r"&([^a-zA-Z#])", r"&\1", xmldata)
Expand Down Expand Up @@ -157,32 +183,6 @@ 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 b31d57b

Please sign in to comment.