Return None instead of raising an exception on missing, valid emeter keys (#146)

Fixes #142

Also, update the pre-commit hooks to their newest versions
This commit is contained in:
Teemu R
2021-03-18 19:22:10 +01:00
committed by GitHub
parent 2fe1b209d0
commit 1ee4757fdb
6 changed files with 28 additions and 9 deletions

View File

@@ -12,6 +12,7 @@ Module-specific errors are raised as `SmartDeviceException` and are expected
to be handled by the user of the library.
"""
from importlib_metadata import version # type: ignore
from kasa.discover import Discover
from kasa.exceptions import SmartDeviceException
from kasa.protocol import TPLinkSmartHomeProtocol

View File

@@ -87,7 +87,8 @@ class EmeterStatus(dict):
if i.startswith(item):
return self.__getitem__(i) / 1000
raise SmartDeviceException("Unable to find a value for '%s'" % item)
_LOGGER.debug(f"Unable to find value for '{item}'")
return None
def requires_update(f):

View File

@@ -115,3 +115,19 @@ async def test_current_consumption(dev):
assert x >= 0.0
else:
assert await dev.current_consumption() is None
async def test_emeterstatus_missing_current():
"""KL125 does not report 'current' for emeter."""
from kasa import EmeterStatus
regular = EmeterStatus(
{"err_code": 0, "power_mw": 0, "total_wh": 13, "current_ma": 123}
)
assert regular["current"] == 0.123
with pytest.raises(KeyError):
regular["invalid_key"]
missing_current = EmeterStatus({"err_code": 0, "power_mw": 0, "total_wh": 13})
assert missing_current["current"] is None

View File

@@ -1,8 +1,8 @@
import sys
import pytest
import xdoctest
from kasa.tests.conftest import get_device_for_file