|
1 | 1 | """Plugwise Home Assistant module.""" |
2 | | - |
3 | 2 | import asyncio |
4 | 3 | import datetime as dt |
5 | 4 | import logging |
|
15 | 14 | # Version detection |
16 | 15 | import semver |
17 | 16 |
|
18 | | -from plugwise.constants import ( |
| 17 | +from .constants import ( |
19 | 18 | APPLIANCES, |
20 | 19 | ATTR_NAME, |
21 | 20 | ATTR_TYPE, |
|
31 | 30 | LOCATIONS, |
32 | 31 | MODULES, |
33 | 32 | NOTIFICATIONS, |
| 33 | + POWER_WATT, |
34 | 34 | RULES, |
35 | 35 | SMILES, |
36 | 36 | STATUS, |
37 | 37 | SWITCH_GROUP_TYPES, |
38 | 38 | SYSTEM, |
39 | 39 | ) |
40 | | -from plugwise.exceptions import ( |
| 40 | +from .exceptions import ( |
41 | 41 | ConnectionFailedError, |
42 | 42 | DeviceSetupError, |
43 | 43 | DeviceTimeoutError, |
|
47 | 47 | UnsupportedDeviceError, |
48 | 48 | XMLDataMissingError, |
49 | 49 | ) |
50 | | -from plugwise.util import ( |
| 50 | +from .util import ( |
51 | 51 | determine_selected, |
52 | 52 | escape_illegal_xml_characters, |
53 | 53 | format_measure, |
@@ -87,8 +87,6 @@ async def _create_session() -> aiohttp.ClientSession: |
87 | 87 | self.websession = websession |
88 | 88 |
|
89 | 89 | 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"} |
92 | 90 |
|
93 | 91 | self._timeout = timeout |
94 | 92 | self._endpoint = f"http://{host}:{str(port)}" |
@@ -232,10 +230,7 @@ async def request( |
232 | 230 | try: |
233 | 231 | with async_timeout.timeout(self._timeout): |
234 | 232 | 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) |
239 | 234 | if method == "put": |
240 | 235 | resp = await self.websession.put( |
241 | 236 | url, data=data, headers=headers, auth=self._auth |
@@ -905,25 +900,28 @@ def get_power_data_from_location(self, loc_id): |
905 | 900 | key_string = f"{measurement}_{peak}_{log_found}" |
906 | 901 | net_string = f"net_electricity_{log_found}" |
907 | 902 | 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))) |
908 | 907 | if all( |
909 | 908 | item in key_string for item in ["electricity", "cumulative"] |
910 | 909 | ): |
911 | 910 | 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 | | - |
918 | 911 | # Energy differential |
919 | 912 | if "electricity" in measurement: |
920 | | - f_val = float(f"{round(float(val), 1):.1f}") |
921 | 913 | diff = 1 |
922 | 914 | if "produced" in measurement: |
923 | 915 | diff = -1 |
924 | 916 | 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}" |
927 | 925 |
|
928 | 926 | direct_data[key_string] = f_val |
929 | 927 |
|
|
0 commit comments