Skip to content

Commit 0efc187

Browse files
committed
Copy update.py from fixtures_local
1 parent 4bc6c84 commit 0efc187

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

plugwise/util.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
import crcmod
1212

13-
from plugwise.constants import (
13+
from .constants import (
1414
ENERGY_KILO_WATT_HOUR,
1515
HW_MODELS,
1616
LOGADDR_OFFSET,
1717
PERCENTAGE,
1818
PLUGWISE_EPOCH,
19+
POWER_WATT,
1920
UTF8_DECODE,
21+
VOLUME_CUBIC_METERS,
2022
)
2123

2224
crc_fun = crcmod.mkCrcFun(0x11021, rev=False, initCrc=0x0000, xorOut=0x0000)
@@ -86,20 +88,24 @@ def escape_illegal_xml_characters(xmldata):
8688

8789
def format_measure(measure, unit):
8890
"""Format measure to correct type."""
89-
if unit == PERCENTAGE and float(measure) > 0:
90-
measure = int(float(measure) * 100)
91-
if unit == ENERGY_KILO_WATT_HOUR:
92-
measure = float(measure) / 1000
91+
SPECIAL_FORMAT = [ENERGY_KILO_WATT_HOUR, VOLUME_CUBIC_METERS]
9392
try:
9493
measure = int(measure)
9594
except ValueError:
95+
if unit == PERCENTAGE and float(measure) > 0:
96+
measure = int(float(measure) * 100)
97+
if unit == ENERGY_KILO_WATT_HOUR:
98+
measure = float(measure) / 1000
9699
try:
97-
if float(measure) < 10:
98-
measure = float(f"{round(float(measure), 2):.2f}")
99-
elif float(measure) >= 10 and float(measure) < 100:
100-
measure = float(f"{round(float(measure), 1):.1f}")
101-
elif float(measure) >= 100:
102-
measure = int(round(float(measure)))
100+
if unit in SPECIAL_FORMAT:
101+
measure = float(f"{round(float(measure), 3):.3f}")
102+
else:
103+
if abs(float(measure)) < 10:
104+
measure = float(f"{round(float(measure), 2):.2f}")
105+
elif abs(float(measure)) >= 10 and abs(float(measure)) < 100:
106+
measure = float(f"{round(float(measure), 1):.1f}")
107+
elif abs(float(measure)) >= 100:
108+
measure = int(round(float(measure)))
103109
except ValueError:
104110
if measure == "on":
105111
measure = True
@@ -329,4 +335,4 @@ def serialize(self):
329335

330336
def deserialize(self, val):
331337
Int.deserialize(self, val)
332-
self.value = (self.value - LOGADDR_OFFSET) // 32
338+
self.value = (self.value - LOGADDR_OFFSET) // 32

0 commit comments

Comments
 (0)