mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-19 19:20:22 +00:00
Improve usage module, consolidate API with emeter (#249)
* Consolidate API for both emeter&usage modules * Add new cli command 'usage' to query usage
This commit is contained in:
@@ -17,22 +17,53 @@ class Usage(Module):
|
||||
req, self.query_for_command("get_daystat", {"year": year, "month": month})
|
||||
)
|
||||
req = merge(req, self.query_for_command("get_monthstat", {"year": year}))
|
||||
req = merge(req, self.query_for_command("get_next_action"))
|
||||
|
||||
return req
|
||||
|
||||
async def get_daystat(self, year, month):
|
||||
"""Return stats for the current day."""
|
||||
@property
|
||||
def daily_data(self):
|
||||
"""Return statistics on daily basis."""
|
||||
return self.data["get_daystat"]["day_list"]
|
||||
|
||||
@property
|
||||
def monthly_data(self):
|
||||
"""Return statistics on monthly basis."""
|
||||
return self.data["get_monthstat"]["month_list"]
|
||||
|
||||
@property
|
||||
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()
|
||||
|
||||
@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()
|
||||
|
||||
async def get_daystat(self, *, year=None, month=None):
|
||||
"""Return daily stats for the given year & month."""
|
||||
if year is None:
|
||||
year = datetime.now().year
|
||||
if month is None:
|
||||
month = datetime.now().month
|
||||
|
||||
return await self.call("get_daystat", {"year": year, "month": month})
|
||||
|
||||
async def get_monthstat(self, year):
|
||||
"""Return stats for the current month."""
|
||||
async def get_monthstat(self, *, year=None):
|
||||
"""Return monthly stats for the given year."""
|
||||
if year is None:
|
||||
year = datetime.now().year
|
||||
|
||||
return await self.call("get_monthstat", {"year": year})
|
||||
|
||||
async def erase_stats(self):
|
||||
|
||||
Reference in New Issue
Block a user