Skip to content

Commit 0134aa0

Browse files
committed
Remove boolean-output from format_measure()
1 parent e1513ad commit 0134aa0

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

plugwise/helper.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,9 @@ def _appliance_measurements(
861861
data["select_dhw_mode"] = appl_p_loc.text
862862
case _ as meas_rn if meas_rn in BINARY_SENSORS:
863863
bs_key = cast(BinarySensorType, meas_rn)
864-
bs_value = format_measure(
865-
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
864+
bs_value = appl_p_loc.text in ["on", "true"]
866865
)
867-
assert isinstance(bs_value, bool)
866+
# assert isinstance(bs_value, bool)
868867
data["binary_sensors"][bs_key] = bs_value
869868
case _ as meas_rn if meas_rn in SENSORS:
870869
s_key = cast(SensorType, meas_rn)
@@ -888,16 +887,11 @@ def _appliance_measurements(
888887
]
889888
case _ as meas_rn if meas_rn in SWITCHES:
890889
sw_key = cast(SwitchType, meas_rn)
891-
value = format_measure(
892-
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
893-
)
894-
sw_value = cast(bool, value)
890+
sw_value = appl_p_loc.text in ["on", "true"]
895891
data["switches"][sw_key] = sw_value
896892
case "c_heating_state":
897-
value = format_measure(
898-
appl_p_loc.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
899-
)
900-
assert isinstance(value, bool)
893+
value = appl_p_loc.text in ["on", "true"]
894+
# assert isinstance(value, bool)
901895
data["c_heating_state"] = value
902896
case "elga_status_code":
903897
data["elga_status_code"] = int(appl_p_loc.text)

plugwise/util.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,33 @@ def escape_illegal_xml_characters(xmldata: str) -> str:
2020
return re.sub(r"&([^a-zA-Z#])", r"&\1", xmldata)
2121

2222

23-
def format_measure(measure: str, unit: str) -> bool | float | int:
23+
def format_measure(measure: str, unit: str) -> float | int:
2424
"""Format measure to correct type."""
25-
result: bool | float | int = False
25+
result: float | int = 0
2626
try:
2727
result = int(measure)
2828
if unit == TEMP_CELSIUS:
2929
result = float(measure)
3030
except ValueError:
31-
try:
32-
float_measure = float(measure)
33-
if unit == PERCENTAGE:
34-
if 0 < float_measure <= 1:
35-
return int(float_measure * 100)
36-
37-
if unit == ENERGY_KILO_WATT_HOUR:
38-
float_measure = float_measure / 1000
39-
40-
if unit in SPECIAL_FORMAT:
41-
result = float(f"{round(float_measure, 3):.3f}")
42-
elif unit == ELECTRIC_POTENTIAL_VOLT:
31+
float_measure = float(measure)
32+
if unit == PERCENTAGE:
33+
if 0 < float_measure <= 1:
34+
return int(float_measure * 100)
35+
36+
if unit == ENERGY_KILO_WATT_HOUR:
37+
float_measure = float_measure / 1000
38+
39+
if unit in SPECIAL_FORMAT:
40+
result = float(f"{round(float_measure, 3):.3f}")
41+
elif unit == ELECTRIC_POTENTIAL_VOLT:
42+
result = float(f"{round(float_measure, 1):.1f}")
43+
else:
44+
if abs(float_measure) < 10:
45+
result = float(f"{round(float_measure, 2):.2f}")
46+
elif abs(float_measure) >= 10 and abs(float_measure) < 100:
4347
result = float(f"{round(float_measure, 1):.1f}")
44-
else:
45-
if abs(float_measure) < 10:
46-
result = float(f"{round(float_measure, 2):.2f}")
47-
elif abs(float_measure) >= 10 and abs(float_measure) < 100:
48-
result = float(f"{round(float_measure, 1):.1f}")
49-
elif abs(float_measure) >= 100:
50-
result = int(round(float_measure))
51-
except ValueError:
52-
result = measure in ["on", "true"]
48+
elif abs(float_measure) >= 100:
49+
result = int(round(float_measure))
5350

5451
return result
5552

0 commit comments

Comments
 (0)