mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 18:54:08 +00:00
Avoid linear search for emeter realtime and emeter_today (#622)
* Avoid linear search for emeter realtime and emeter_today Most of the time the data we want is at the end of the list so we now search backwards to avoid having to scale all the data and throw most of it away * more tweaks * coverage * coverage * preen * coverage * branch cover
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import datetime
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from kasa import EmeterStatus, SmartDeviceException
|
||||
from kasa.modules.emeter import Emeter
|
||||
|
||||
from .conftest import has_emeter, has_emeter_iot, no_emeter
|
||||
from .newfakes import CURRENT_CONSUMPTION_SCHEMA
|
||||
@@ -116,3 +120,29 @@ async def test_emeterstatus_missing_current():
|
||||
|
||||
missing_current = EmeterStatus({"err_code": 0, "power_mw": 0, "total_wh": 13})
|
||||
assert missing_current["current"] is None
|
||||
|
||||
|
||||
async def test_emeter_daily():
|
||||
"""Test fetching the emeter for today.
|
||||
|
||||
This test uses inline data since the fixtures
|
||||
will not have data for the current day.
|
||||
"""
|
||||
emeter_data = {
|
||||
"get_daystat": {
|
||||
"day_list": [{"day": 1, "energy_wh": 8, "month": 1, "year": 2023}],
|
||||
"err_code": 0,
|
||||
}
|
||||
}
|
||||
|
||||
class MockEmeter(Emeter):
|
||||
@property
|
||||
def data(self):
|
||||
return emeter_data
|
||||
|
||||
emeter = MockEmeter(Mock(), "emeter")
|
||||
now = datetime.datetime.now()
|
||||
emeter_data["get_daystat"]["day_list"].append(
|
||||
{"day": now.day, "energy_wh": 500, "month": now.month, "year": now.year}
|
||||
)
|
||||
assert emeter.emeter_today == 0.500
|
||||
|
@@ -1,3 +1,6 @@
|
||||
import datetime
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from kasa.modules import Usage
|
||||
@@ -20,3 +23,65 @@ def test_usage_convert_stat_data():
|
||||
assert isinstance(k, int)
|
||||
assert isinstance(v, int)
|
||||
assert k == 4 and v == 30
|
||||
|
||||
|
||||
def test_usage_today():
|
||||
"""Test fetching the usage for today.
|
||||
|
||||
This test uses inline data since the fixtures
|
||||
will not have data for the current day.
|
||||
"""
|
||||
emeter_data = {
|
||||
"get_daystat": {
|
||||
"day_list": [],
|
||||
"err_code": 0,
|
||||
}
|
||||
}
|
||||
|
||||
class MockUsage(Usage):
|
||||
@property
|
||||
def data(self):
|
||||
return emeter_data
|
||||
|
||||
usage = MockUsage(Mock(), "usage")
|
||||
assert usage.usage_today is None
|
||||
now = datetime.datetime.now()
|
||||
emeter_data["get_daystat"]["day_list"].extend(
|
||||
[
|
||||
{"day": now.day - 1, "time": 200, "month": now.month - 1, "year": now.year},
|
||||
{"day": now.day, "time": 500, "month": now.month, "year": now.year},
|
||||
{"day": now.day + 1, "time": 100, "month": now.month + 1, "year": now.year},
|
||||
]
|
||||
)
|
||||
assert usage.usage_today == 500
|
||||
|
||||
|
||||
def test_usage_this_month():
|
||||
"""Test fetching the usage for this month.
|
||||
|
||||
This test uses inline data since the fixtures
|
||||
will not have data for the current month.
|
||||
"""
|
||||
emeter_data = {
|
||||
"get_monthstat": {
|
||||
"month_list": [],
|
||||
"err_code": 0,
|
||||
}
|
||||
}
|
||||
|
||||
class MockUsage(Usage):
|
||||
@property
|
||||
def data(self):
|
||||
return emeter_data
|
||||
|
||||
usage = MockUsage(Mock(), "usage")
|
||||
assert usage.usage_this_month is None
|
||||
now = datetime.datetime.now()
|
||||
emeter_data["get_monthstat"]["month_list"].extend(
|
||||
[
|
||||
{"time": 200, "month": now.month - 1, "year": now.year},
|
||||
{"time": 500, "month": now.month, "year": now.year},
|
||||
{"time": 100, "month": now.month + 1, "year": now.year},
|
||||
]
|
||||
)
|
||||
assert usage.usage_this_month == 500
|
||||
|
Reference in New Issue
Block a user