Enable ruff check for ANN (#1139)

This commit is contained in:
Teemu R.
2024-11-10 19:55:13 +01:00
committed by GitHub
parent 6b44fe6242
commit 66eb17057e
89 changed files with 596 additions and 452 deletions

View File

@@ -2,6 +2,8 @@
from __future__ import annotations
from typing import NoReturn
from ...emeterstatus import EmeterStatus
from ...exceptions import KasaException
from ...interfaces.energy import Energy as EnergyInterface
@@ -31,34 +33,34 @@ class Energy(SmartModule, EnergyInterface):
# Fallback if get_energy_usage does not provide current_power,
# which can happen on some newer devices (e.g. P304M).
elif (
power := self.data.get("get_current_power").get("current_power")
power := self.data.get("get_current_power", {}).get("current_power")
) is not None:
return power
return None
@property
@raise_if_update_error
def energy(self):
def energy(self) -> dict:
"""Return get_energy_usage results."""
if en := self.data.get("get_energy_usage"):
return en
return self.data
def _get_status_from_energy(self, energy) -> EmeterStatus:
def _get_status_from_energy(self, energy: dict) -> EmeterStatus:
return EmeterStatus(
{
"power_mw": energy.get("current_power"),
"total": energy.get("today_energy") / 1_000,
"power_mw": energy.get("current_power", 0),
"total": energy.get("today_energy", 0) / 1_000,
}
)
@property
@raise_if_update_error
def status(self):
def status(self) -> EmeterStatus:
"""Get the emeter status."""
return self._get_status_from_energy(self.energy)
async def get_status(self):
async def get_status(self) -> EmeterStatus:
"""Return real-time statistics."""
res = await self.call("get_energy_usage")
return self._get_status_from_energy(res["get_energy_usage"])
@@ -67,13 +69,13 @@ class Energy(SmartModule, EnergyInterface):
@raise_if_update_error
def consumption_this_month(self) -> float | None:
"""Get the emeter value for this month in kWh."""
return self.energy.get("month_energy") / 1_000
return self.energy.get("month_energy", 0) / 1_000
@property
@raise_if_update_error
def consumption_today(self) -> float | None:
"""Get the emeter value for today in kWh."""
return self.energy.get("today_energy") / 1_000
return self.energy.get("today_energy", 0) / 1_000
@property
@raise_if_update_error
@@ -97,22 +99,26 @@ class Energy(SmartModule, EnergyInterface):
"""Retrieve current energy readings."""
return self.status
async def erase_stats(self):
async def erase_stats(self) -> NoReturn:
"""Erase all stats."""
raise KasaException("Device does not support periodic statistics")
async def get_daily_stats(self, *, year=None, month=None, kwh=True) -> dict:
async def get_daily_stats(
self, *, year: int | None = None, month: int | None = None, kwh: bool = True
) -> dict:
"""Return daily stats for the given year & month.
The return value is a dictionary of {day: energy, ...}.
"""
raise KasaException("Device does not support periodic statistics")
async def get_monthly_stats(self, *, year=None, kwh=True) -> dict:
async def get_monthly_stats(
self, *, year: int | None = None, kwh: bool = True
) -> dict:
"""Return monthly stats for the given year."""
raise KasaException("Device does not support periodic statistics")
async def _check_supported(self):
async def _check_supported(self) -> bool:
"""Additional check to see if the module is supported by the device."""
# Energy module is not supported on P304M parent device
return "device_on" in self._device.sys_info