Skip to content

Commit 88c21d1

Browse files
committed
Copy updates from fixtures_local branch
1 parent 6e340ce commit 88c21d1

File tree

2 files changed

+41
-61
lines changed

2 files changed

+41
-61
lines changed

plugwise/smile.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
"""Plugwise Home Assistant module."""
2-
32
import asyncio
43
import datetime as dt
54
import logging
65

76
import aiohttp
87
import async_timeout
9-
from dateutil.parser import parse
10-
from defusedxml import ElementTree as etree
11-
12-
# Time related
13-
import pytz
14-
15-
# Version detection
16-
import semver
17-
18-
from plugwise.constants import (
8+
from .constants import (
199
APPLIANCES,
2010
ATTR_NAME,
2111
ATTR_TYPE,
@@ -31,13 +21,16 @@
3121
LOCATIONS,
3222
MODULES,
3323
NOTIFICATIONS,
24+
POWER_WATT,
3425
RULES,
3526
SMILES,
3627
STATUS,
3728
SWITCH_GROUP_TYPES,
3829
SYSTEM,
3930
)
40-
from plugwise.exceptions import (
31+
from dateutil.parser import parse
32+
from defusedxml import ElementTree as etree
33+
from .exceptions import (
4134
ConnectionFailedError,
4235
DeviceSetupError,
4336
DeviceTimeoutError,
@@ -47,7 +40,13 @@
4740
UnsupportedDeviceError,
4841
XMLDataMissingError,
4942
)
50-
from plugwise.util import (
43+
44+
# Time related
45+
import pytz
46+
47+
# Version detection
48+
import semver
49+
from .util import (
5150
determine_selected,
5251
escape_illegal_xml_characters,
5352
format_measure,
@@ -87,8 +86,6 @@ async def _create_session() -> aiohttp.ClientSession:
8786
self.websession = websession
8887

8988
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"}
9289

9390
self._timeout = timeout
9491
self._endpoint = f"http://{host}:{str(port)}"
@@ -232,10 +229,7 @@ async def request(
232229
try:
233230
with async_timeout.timeout(self._timeout):
234231
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-
)
232+
resp = await self.websession.get(url, auth=self._auth)
239233
if method == "put":
240234
resp = await self.websession.put(
241235
url, data=data, headers=headers, auth=self._auth
@@ -905,25 +899,31 @@ def get_power_data_from_location(self, loc_id):
905899
key_string = f"{measurement}_{peak}_{log_found}"
906900
net_string = f"net_electricity_{log_found}"
907901
val = loc_logs.find(locator).text
902+
if attrs[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT:
903+
f_val = int(round(float(val)))
904+
else:
905+
f_val = format_measure(val, attrs[ATTR_UNIT_OF_MEASUREMENT])
908906
if all(
909907
item in key_string for item in ["electricity", "cumulative"]
910908
):
911909
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-
918910
# Energy differential
919911
if "electricity" in measurement:
920-
f_val = float(f"{round(float(val), 1):.1f}")
921912
diff = 1
922913
if "produced" in measurement:
923914
diff = -1
924915
if net_string not in direct_data:
925-
direct_data[net_string] = float()
926-
direct_data[net_string] += float(f_val * diff)
916+
direct_data[net_string] = 0
917+
if f_val != 0:
918+
if isinstance(f_val, int):
919+
direct_data[net_string] += int(
920+
round(float(f_val * diff))
921+
)
922+
else:
923+
direct_data[net_string] += float(f_val * diff)
924+
925+
if "gas" in measurement:
926+
key_string = f"{measurement}_{log_found}"
927927

928928
direct_data[key_string] = f_val
929929

tests/test_smile.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -330,27 +330,7 @@ async def device_test(self, smile=Smile, testdata=None):
330330
measure_key, measure_assert
331331
),
332332
)
333-
if isinstance(data[measure_key], float):
334-
if all(
335-
item in measure_key
336-
for item in ["electricity", "cumulative"]
337-
):
338-
measure = float(
339-
"{:.1f}".format(
340-
round(float(data[measure_key]) / 1000, 1)
341-
)
342-
)
343-
elif float(data[measure_key]) < 10:
344-
measure = float(
345-
"{:.2f}".format(round(float(data[measure_key]), 2))
346-
)
347-
else:
348-
measure = float(
349-
"{:.1f}".format(round(float(data[measure_key]), 1))
350-
)
351-
assert measure == measure_assert
352-
else:
353-
assert data[measure_key] == measure_assert
333+
assert data[measure_key] == measure_assert
354334

355335
@pytest.mark.asyncio
356336
async def tinker_relay(self, smile, dev_ids=None, members=None, unhappy=False):
@@ -571,9 +551,9 @@ async def test_connect_smile_p1_v2(self):
571551
"938696c4bcdb4b8a9a595cb38ed43913": {
572552
"electricity_consumed_peak_point": 458.0,
573553
"net_electricity_point": 458.0,
574-
"gas_consumed_cumulative": 584.4,
575-
"electricity_produced_peak_cumulative": 1296.1,
576-
"electricity_produced_off_peak_cumulative": 482.6,
554+
"gas_consumed_cumulative": 584.433,
555+
"electricity_produced_peak_cumulative": 1296.136,
556+
"electricity_produced_off_peak_cumulative": 482.598,
577557
}
578558
}
579559

@@ -608,7 +588,7 @@ async def test_connect_smile_p1_v2_2(self):
608588
"199aa40f126840f392983d171374ab0b": {
609589
"electricity_consumed_peak_point": 368.0,
610590
"net_electricity_point": 368.0,
611-
"gas_consumed_cumulative": 2638.0,
591+
"gas_consumed_cumulative": 2637.993,
612592
"electricity_produced_peak_cumulative": 0.0,
613593
}
614594
}
@@ -1146,7 +1126,7 @@ async def test_connect_p1v3(self):
11461126
"ba4de7613517478da82dd9b6abea36af": {
11471127
"electricity_consumed_peak_point": 650.0,
11481128
"electricity_produced_peak_cumulative": 0.0,
1149-
"electricity_consumed_off_peak_cumulative": 10263.2,
1129+
"electricity_consumed_off_peak_cumulative": 10263.159,
11501130
}
11511131
}
11521132

@@ -1178,7 +1158,7 @@ async def test_connect_p1v3solarfake(self):
11781158
"ba4de7613517478da82dd9b6abea36af": {
11791159
"electricity_consumed_peak_point": 644.0,
11801160
"electricity_produced_peak_cumulative": 20.0,
1181-
"electricity_consumed_off_peak_cumulative": 10263.2,
1161+
"electricity_consumed_off_peak_cumulative": 10263.159,
11821162
"net_electricity_point": 244,
11831163
}
11841164
}
@@ -1211,11 +1191,11 @@ async def test_connect_p1v3_full_option(self):
12111191
# Gateway / P1 itself
12121192
"e950c7d5e1ee407a858e2a8b5016c8b3": {
12131193
"electricity_consumed_peak_point": 0.0,
1214-
"electricity_produced_peak_cumulative": 396.6,
1215-
"electricity_consumed_off_peak_cumulative": 551.1,
1216-
"electricity_produced_peak_point": 2761.0,
1217-
"net_electricity_point": -2761.0,
1218-
"gas_consumed_cumulative": 584.9,
1194+
"electricity_produced_peak_cumulative": 396.559,
1195+
"electricity_consumed_off_peak_cumulative": 551.09,
1196+
"electricity_produced_peak_point": 2761,
1197+
"net_electricity_point": -2761,
1198+
"gas_consumed_cumulative": 584.85,
12191199
}
12201200
}
12211201

@@ -1432,4 +1412,4 @@ class ConnectError(PlugwiseTestError):
14321412
"""Raised when connectivity test fails."""
14331413

14341414
class UnexpectedError(PlugwiseTestError):
1435-
"""Raised when something went against logic."""
1415+
"""Raised when something went against logic."""

0 commit comments

Comments
 (0)