Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name: Latest commit

env:
CACHE_VERSION: 1
CACHE_VERSION: 2
DEFAULT_PYTHON: 3.9
PRE_COMMIT_HOME: ~/.cache/pre-commit

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.8.5 - Fix sensor scaling
- Fix for via HA Core issue #44349
- Remove aiohttp-workaround - issue solved in aiohttp 3.7.1

(## 0.8.4 - Not released: Fix "Gas Consumed Interval stays 0" )

## 0.8.2/0.8.3 - Code quality improvements
- Switch Smile to defusedxml from lxml (improving security)
- Lint and flake recommendations fixed
Expand Down
2 changes: 1 addition & 1 deletion plugwise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Plugwise module."""

__version__ = "0.8.3"
__version__ = "0.8.5"

from plugwise.smile import Smile
from plugwise.stick import stick
36 changes: 17 additions & 19 deletions plugwise/smile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Plugwise Home Assistant module."""

import asyncio
import datetime as dt
import logging
Expand All @@ -15,7 +14,7 @@
# Version detection
import semver

from plugwise.constants import (
from .constants import (
APPLIANCES,
ATTR_NAME,
ATTR_TYPE,
Expand All @@ -31,13 +30,14 @@
LOCATIONS,
MODULES,
NOTIFICATIONS,
POWER_WATT,
RULES,
SMILES,
STATUS,
SWITCH_GROUP_TYPES,
SYSTEM,
)
from plugwise.exceptions import (
from .exceptions import (
ConnectionFailedError,
DeviceSetupError,
DeviceTimeoutError,
Expand All @@ -47,7 +47,7 @@
UnsupportedDeviceError,
XMLDataMissingError,
)
from plugwise.util import (
from .util import (
determine_selected,
escape_illegal_xml_characters,
format_measure,
Expand Down Expand Up @@ -87,8 +87,6 @@ async def _create_session() -> aiohttp.ClientSession:
self.websession = websession

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

self._timeout = timeout
self._endpoint = f"http://{host}:{str(port)}"
Expand Down Expand Up @@ -232,10 +230,7 @@ async def request(
try:
with async_timeout.timeout(self._timeout):
if method == "get":
# Work-around, see above, can be removed for aiohttp v3.7:
resp = await self.websession.get(
url, auth=self._auth, headers=self._headers
)
resp = await self.websession.get(url, auth=self._auth)
if method == "put":
resp = await self.websession.put(
url, data=data, headers=headers, auth=self._auth
Expand Down Expand Up @@ -905,25 +900,28 @@ def get_power_data_from_location(self, loc_id):
key_string = f"{measurement}_{peak}_{log_found}"
net_string = f"net_electricity_{log_found}"
val = loc_logs.find(locator).text
f_val = format_measure(val, attrs[ATTR_UNIT_OF_MEASUREMENT])
# Format only HOME_MEASUREMENT POWER_WATT values, do not move to util-format_meaure function!
if attrs[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT:
f_val = int(round(float(val)))
if all(
item in key_string for item in ["electricity", "cumulative"]
):
f_val = format_measure(val, ENERGY_KILO_WATT_HOUR)
else:
f_val = format_measure(val, attrs[ATTR_UNIT_OF_MEASUREMENT])
if "gas" in measurement:
key_string = f"{measurement}_{log_found}"
f_val = float(f"{round(float(val), 3):.3f}")

# Energy differential
if "electricity" in measurement:
f_val = float(f"{round(float(val), 1):.1f}")
diff = 1
if "produced" in measurement:
diff = -1
if net_string not in direct_data:
direct_data[net_string] = float()
direct_data[net_string] += float(f_val * diff)
direct_data[net_string] = 0
if isinstance(f_val, int):
direct_data[net_string] += f_val * diff
else:
direct_data[net_string] += float(f_val * diff)

if "gas" in measurement:
key_string = f"{measurement}_{log_found}"

direct_data[key_string] = f_val

Expand Down
29 changes: 18 additions & 11 deletions plugwise/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

import crcmod

from plugwise.constants import (
from .constants import (
ENERGY_KILO_WATT_HOUR,
HW_MODELS,
LOGADDR_OFFSET,
PERCENTAGE,
PLUGWISE_EPOCH,
POWER_WATT,
UTF8_DECODE,
VOLUME_CUBIC_METERS,
)

crc_fun = crcmod.mkCrcFun(0x11021, rev=False, initCrc=0x0000, xorOut=0x0000)
Expand Down Expand Up @@ -86,20 +88,25 @@ def escape_illegal_xml_characters(xmldata):

def format_measure(measure, unit):
"""Format measure to correct type."""
if unit == PERCENTAGE and float(measure) > 0:
measure = int(float(measure) * 100)
if unit == ENERGY_KILO_WATT_HOUR:
measure = float(measure) / 1000
SPECIAL_FORMAT = [ENERGY_KILO_WATT_HOUR, VOLUME_CUBIC_METERS]
try:
measure = int(measure)
except ValueError:
if unit == PERCENTAGE and float(measure) > 0:
return int(float(measure) * 100)

if unit == ENERGY_KILO_WATT_HOUR:
measure = float(measure) / 1000
try:
if float(measure) < 10:
measure = float(f"{round(float(measure), 2):.2f}")
elif float(measure) >= 10 and float(measure) < 100:
measure = float(f"{round(float(measure), 1):.1f}")
elif float(measure) >= 100:
measure = int(round(float(measure)))
if unit in SPECIAL_FORMAT:
measure = float(f"{round(float(measure), 3):.3f}")
else:
if abs(float(measure)) < 10:
measure = float(f"{round(float(measure), 2):.2f}")
elif abs(float(measure)) >= 10 and abs(float(measure)) < 100:
measure = float(f"{round(float(measure), 1):.1f}")
elif abs(float(measure)) >= 100:
measure = int(round(float(measure)))
except ValueError:
if measure == "on":
measure = True
Expand Down
Loading