Skip to content

Commit 2056b20

Browse files
CoMPaTechbouwew
authored andcommitted
Refactor format_measure
1 parent 1c16bb3 commit 2056b20

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

plugwise/util.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,34 +99,39 @@ def escape_illegal_xml_characters(xmldata: str) -> str:
9999

100100
def format_measure(measure: str, unit: str) -> float | int | bool:
101101
"""Format measure to correct type."""
102+
# TODO: handle with appropriate care 20220405
103+
# continuously reassigning the same value with different type isn't
104+
# to typings liking
105+
result: int | float | bool = False
102106
try:
107+
result = int(measure)
103108
if unit == TEMP_CELSIUS:
104-
return float(measure)
105-
return int(measure)
109+
result = float(measure)
106110
except ValueError:
107-
if unit == PERCENTAGE:
108-
if 0 < float(measure) <= 1:
109-
return int(float(measure) * 100)
110-
111-
f_measure = float(measure)
112-
if unit == ENERGY_KILO_WATT_HOUR:
113-
f_measure = f_measure / 1000
114111
try:
112+
float_measure = float(measure)
113+
if unit == PERCENTAGE:
114+
if 0 < float_measure <= 1:
115+
return int(float_measure * 100)
116+
117+
if unit == ENERGY_KILO_WATT_HOUR:
118+
float_measure = float_measure / 1000
119+
115120
if unit in SPECIAL_FORMAT:
116-
f_measure = float(f"{round(f_measure, 3):.3f}")
121+
result = float(f"{round(float_measure, 3):.3f}")
117122
else:
118-
if abs(f_measure) < 10:
119-
f_measure = float(f"{round(f_measure, 2):.2f}")
120-
elif abs(f_measure) >= 10 and abs(f_measure) < 100:
121-
f_measure = float(f"{round(f_measure, 1):.1f}")
122-
elif abs(f_measure) >= 100:
123-
f_measure = int(round(f_measure))
123+
if abs(float_measure) < 10:
124+
result = float(f"{round(float_measure, 2):.2f}")
125+
elif abs(float_measure) >= 10 and abs(float_measure) < 100:
126+
result = float(f"{round(float_measure, 1):.1f}")
127+
elif abs(float_measure) >= 100:
128+
result = int(round(float_measure))
124129
except ValueError:
125130
if measure in ["on", "true"]:
126-
return True
131+
result = True
127132
if measure in ["off", "false"]:
128-
return False
129-
return f_measure
133+
result = False
134+
return result
130135

131136

132137
def in_between(now: datetime.time, start: datetime.time, end: datetime.time) -> bool:

0 commit comments

Comments
 (0)