mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-19 19:20:22 +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:
@@ -10,8 +10,9 @@ class Usage(Module):
|
||||
|
||||
def query(self):
|
||||
"""Return the base query."""
|
||||
year = datetime.now().year
|
||||
month = datetime.now().month
|
||||
now = datetime.now()
|
||||
year = now.year
|
||||
month = now.month
|
||||
|
||||
req = self.query_for_command("get_realtime")
|
||||
req = merge(
|
||||
@@ -40,21 +41,21 @@ class Usage(Module):
|
||||
def usage_today(self):
|
||||
"""Return today's usage in minutes."""
|
||||
today = datetime.now().day
|
||||
converted = [x["time"] for x in self.daily_data if x["day"] == today]
|
||||
if not converted:
|
||||
return None
|
||||
|
||||
return converted.pop()
|
||||
# Traverse the list in reverse order to find the latest entry.
|
||||
for entry in reversed(self.daily_data):
|
||||
if entry["day"] == today:
|
||||
return entry["time"]
|
||||
return None
|
||||
|
||||
@property
|
||||
def usage_this_month(self):
|
||||
"""Return usage in this month in minutes."""
|
||||
this_month = datetime.now().month
|
||||
converted = [x["time"] for x in self.monthly_data if x["month"] == this_month]
|
||||
if not converted:
|
||||
return None
|
||||
|
||||
return converted.pop()
|
||||
# Traverse the list in reverse order to find the latest entry.
|
||||
for entry in reversed(self.monthly_data):
|
||||
if entry["month"] == this_month:
|
||||
return entry["time"]
|
||||
return None
|
||||
|
||||
async def get_raw_daystat(self, *, year=None, month=None) -> Dict:
|
||||
"""Return raw daily stats for the given year & month."""
|
||||
|
||||
Reference in New Issue
Block a user