Skip to content

Commit 62e8d92

Browse files
authored
Merge pull request #52 from plugwise/formatting-issues
Fixing Core PR #44349
2 parents 6e340ce + a336e85 commit 62e8d92

File tree

11 files changed

+81
-3954
lines changed

11 files changed

+81
-3954
lines changed

.github/workflows/verify.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name: Latest commit
55

66
env:
7-
CACHE_VERSION: 1
7+
CACHE_VERSION: 2
88
DEFAULT_PYTHON: 3.9
99
PRE_COMMIT_HOME: ~/.cache/pre-commit
1010

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.8.5 - Fix sensor scaling
4+
- Fix for via HA Core issue #44349
5+
- Remove aiohttp-workaround - issue solved in aiohttp 3.7.1
6+
7+
(## 0.8.4 - Not released: Fix "Gas Consumed Interval stays 0" )
8+
39
## 0.8.2/0.8.3 - Code quality improvements
410
- Switch Smile to defusedxml from lxml (improving security)
511
- Lint and flake recommendations fixed

plugwise/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Plugwise module."""
22

3-
__version__ = "0.8.3"
3+
__version__ = "0.8.5"
44

55
from plugwise.smile import Smile
66
from plugwise.stick import stick

plugwise/smile.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Plugwise Home Assistant module."""
2-
32
import asyncio
43
import datetime as dt
54
import logging
@@ -15,7 +14,7 @@
1514
# Version detection
1615
import semver
1716

18-
from plugwise.constants import (
17+
from .constants import (
1918
APPLIANCES,
2019
ATTR_NAME,
2120
ATTR_TYPE,
@@ -31,13 +30,14 @@
3130
LOCATIONS,
3231
MODULES,
3332
NOTIFICATIONS,
33+
POWER_WATT,
3434
RULES,
3535
SMILES,
3636
STATUS,
3737
SWITCH_GROUP_TYPES,
3838
SYSTEM,
3939
)
40-
from plugwise.exceptions import (
40+
from .exceptions import (
4141
ConnectionFailedError,
4242
DeviceSetupError,
4343
DeviceTimeoutError,
@@ -47,7 +47,7 @@
4747
UnsupportedDeviceError,
4848
XMLDataMissingError,
4949
)
50-
from plugwise.util import (
50+
from .util import (
5151
determine_selected,
5252
escape_illegal_xml_characters,
5353
format_measure,
@@ -87,8 +87,6 @@ async def _create_session() -> aiohttp.ClientSession:
8787
self.websession = websession
8888

8989
self._auth = aiohttp.BasicAuth(username, password=password)
90-
# Work-around for Stretchv2-aiohttp-deflate-error, can be removed for aiohttp v3.7
91-
self._headers = {"Accept-Encoding": "gzip"}
9290

9391
self._timeout = timeout
9492
self._endpoint = f"http://{host}:{str(port)}"
@@ -232,10 +230,7 @@ async def request(
232230
try:
233231
with async_timeout.timeout(self._timeout):
234232
if method == "get":
235-
# Work-around, see above, can be removed for aiohttp v3.7:
236-
resp = await self.websession.get(
237-
url, auth=self._auth, headers=self._headers
238-
)
233+
resp = await self.websession.get(url, auth=self._auth)
239234
if method == "put":
240235
resp = await self.websession.put(
241236
url, data=data, headers=headers, auth=self._auth
@@ -905,25 +900,28 @@ def get_power_data_from_location(self, loc_id):
905900
key_string = f"{measurement}_{peak}_{log_found}"
906901
net_string = f"net_electricity_{log_found}"
907902
val = loc_logs.find(locator).text
903+
f_val = format_measure(val, attrs[ATTR_UNIT_OF_MEASUREMENT])
904+
# Format only HOME_MEASUREMENT POWER_WATT values, do not move to util-format_meaure function!
905+
if attrs[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT:
906+
f_val = int(round(float(val)))
908907
if all(
909908
item in key_string for item in ["electricity", "cumulative"]
910909
):
911910
f_val = format_measure(val, ENERGY_KILO_WATT_HOUR)
912-
else:
913-
f_val = format_measure(val, attrs[ATTR_UNIT_OF_MEASUREMENT])
914-
if "gas" in measurement:
915-
key_string = f"{measurement}_{log_found}"
916-
f_val = float(f"{round(float(val), 3):.3f}")
917-
918911
# Energy differential
919912
if "electricity" in measurement:
920-
f_val = float(f"{round(float(val), 1):.1f}")
921913
diff = 1
922914
if "produced" in measurement:
923915
diff = -1
924916
if net_string not in direct_data:
925-
direct_data[net_string] = float()
926-
direct_data[net_string] += float(f_val * diff)
917+
direct_data[net_string] = 0
918+
if isinstance(f_val, int):
919+
direct_data[net_string] += f_val * diff
920+
else:
921+
direct_data[net_string] += float(f_val * diff)
922+
923+
if "gas" in measurement:
924+
key_string = f"{measurement}_{log_found}"
927925

928926
direct_data[key_string] = f_val
929927

plugwise/util.py

Lines changed: 18 additions & 11 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,25 @@ 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+
return int(float(measure) * 100)
97+
98+
if unit == ENERGY_KILO_WATT_HOUR:
99+
measure = float(measure) / 1000
96100
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)))
101+
if unit in SPECIAL_FORMAT:
102+
measure = float(f"{round(float(measure), 3):.3f}")
103+
else:
104+
if abs(float(measure)) < 10:
105+
measure = float(f"{round(float(measure), 2):.2f}")
106+
elif abs(float(measure)) >= 10 and abs(float(measure)) < 100:
107+
measure = float(f"{round(float(measure), 1):.1f}")
108+
elif abs(float(measure)) >= 100:
109+
measure = int(round(float(measure)))
103110
except ValueError:
104111
if measure == "on":
105112
measure = True

0 commit comments

Comments
 (0)