mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Enable ruff check for ANN (#1139)
This commit is contained in:
@@ -69,7 +69,7 @@ class SmartDevice(Device):
|
||||
self._on_since: datetime | None = None
|
||||
self._info: dict[str, Any] = {}
|
||||
|
||||
async def _initialize_children(self):
|
||||
async def _initialize_children(self) -> None:
|
||||
"""Initialize children for power strips."""
|
||||
child_info_query = {
|
||||
"get_child_device_component_list": None,
|
||||
@@ -108,7 +108,9 @@ class SmartDevice(Device):
|
||||
"""Return the device modules."""
|
||||
return cast(ModuleMapping[SmartModule], self._modules)
|
||||
|
||||
def _try_get_response(self, responses: dict, request: str, default=None) -> dict:
|
||||
def _try_get_response(
|
||||
self, responses: dict, request: str, default: Any | None = None
|
||||
) -> dict:
|
||||
response = responses.get(request)
|
||||
if isinstance(response, SmartErrorCode):
|
||||
_LOGGER.debug(
|
||||
@@ -126,7 +128,7 @@ class SmartDevice(Device):
|
||||
f"{request} not found in {responses} for device {self.host}"
|
||||
)
|
||||
|
||||
async def _negotiate(self):
|
||||
async def _negotiate(self) -> None:
|
||||
"""Perform initialization.
|
||||
|
||||
We fetch the device info and the available components as early as possible.
|
||||
@@ -146,7 +148,8 @@ class SmartDevice(Device):
|
||||
self._info = self._try_get_response(resp, "get_device_info")
|
||||
|
||||
# Create our internal presentation of available components
|
||||
self._components_raw = resp["component_nego"]
|
||||
self._components_raw = cast(dict, resp["component_nego"])
|
||||
|
||||
self._components = {
|
||||
comp["id"]: int(comp["ver_code"])
|
||||
for comp in self._components_raw["component_list"]
|
||||
@@ -167,7 +170,7 @@ class SmartDevice(Device):
|
||||
"""Update the internal device info."""
|
||||
self._info = self._try_get_response(info_resp, "get_device_info")
|
||||
|
||||
async def update(self, update_children: bool = False):
|
||||
async def update(self, update_children: bool = False) -> None:
|
||||
"""Update the device."""
|
||||
if self.credentials is None and self.credentials_hash is None:
|
||||
raise AuthenticationError("Tapo plug requires authentication.")
|
||||
@@ -206,7 +209,7 @@ class SmartDevice(Device):
|
||||
|
||||
async def _handle_module_post_update(
|
||||
self, module: SmartModule, update_time: float, had_query: bool
|
||||
):
|
||||
) -> None:
|
||||
if module.disabled:
|
||||
return # pragma: no cover
|
||||
if had_query:
|
||||
@@ -312,7 +315,7 @@ class SmartDevice(Device):
|
||||
responses[meth] = SmartErrorCode.INTERNAL_QUERY_ERROR
|
||||
return responses
|
||||
|
||||
async def _initialize_modules(self):
|
||||
async def _initialize_modules(self) -> None:
|
||||
"""Initialize modules based on component negotiation response."""
|
||||
from .smartmodule import SmartModule
|
||||
|
||||
@@ -324,7 +327,7 @@ class SmartDevice(Device):
|
||||
# It also ensures that devices like power strips do not add modules such as
|
||||
# firmware to the child devices.
|
||||
skip_parent_only_modules = False
|
||||
child_modules_to_skip = {}
|
||||
child_modules_to_skip: dict = {} # TODO: this is never non-empty
|
||||
if self._parent and self._parent.device_type != DeviceType.Hub:
|
||||
skip_parent_only_modules = True
|
||||
|
||||
@@ -333,17 +336,18 @@ class SmartDevice(Device):
|
||||
skip_parent_only_modules and mod in NON_HUB_PARENT_ONLY_MODULES
|
||||
) or mod.__name__ in child_modules_to_skip:
|
||||
continue
|
||||
if (
|
||||
mod.REQUIRED_COMPONENT in self._components
|
||||
or self.sys_info.get(mod.REQUIRED_KEY_ON_PARENT) is not None
|
||||
required_component = cast(str, mod.REQUIRED_COMPONENT)
|
||||
if required_component in self._components or (
|
||||
mod.REQUIRED_KEY_ON_PARENT
|
||||
and self.sys_info.get(mod.REQUIRED_KEY_ON_PARENT) is not None
|
||||
):
|
||||
_LOGGER.debug(
|
||||
"Device %s, found required %s, adding %s to modules.",
|
||||
self.host,
|
||||
mod.REQUIRED_COMPONENT,
|
||||
required_component,
|
||||
mod.__name__,
|
||||
)
|
||||
module = mod(self, mod.REQUIRED_COMPONENT)
|
||||
module = mod(self, required_component)
|
||||
if await module._check_supported():
|
||||
self._modules[module.name] = module
|
||||
|
||||
@@ -354,7 +358,7 @@ class SmartDevice(Device):
|
||||
):
|
||||
self._modules[Light.__name__] = Light(self, "light")
|
||||
|
||||
async def _initialize_features(self):
|
||||
async def _initialize_features(self) -> None:
|
||||
"""Initialize device features."""
|
||||
self._add_feature(
|
||||
Feature(
|
||||
@@ -575,11 +579,11 @@ class SmartDevice(Device):
|
||||
return str(self._info.get("device_id"))
|
||||
|
||||
@property
|
||||
def internal_state(self) -> Any:
|
||||
def internal_state(self) -> dict:
|
||||
"""Return all the internal state data."""
|
||||
return self._last_update
|
||||
|
||||
def _update_internal_state(self, info: dict) -> None:
|
||||
def _update_internal_state(self, info: dict[str, Any]) -> None:
|
||||
"""Update the internal info state.
|
||||
|
||||
This is used by the parent to push updates to its children.
|
||||
@@ -587,8 +591,8 @@ class SmartDevice(Device):
|
||||
self._info = info
|
||||
|
||||
async def _query_helper(
|
||||
self, method: str, params: dict | None = None, child_ids=None
|
||||
) -> Any:
|
||||
self, method: str, params: dict | None = None, child_ids: None = None
|
||||
) -> dict:
|
||||
res = await self.protocol.query({method: params})
|
||||
|
||||
return res
|
||||
@@ -610,22 +614,25 @@ class SmartDevice(Device):
|
||||
"""Return true if the device is on."""
|
||||
return bool(self._info.get("device_on"))
|
||||
|
||||
async def set_state(self, on: bool): # TODO: better name wanted.
|
||||
async def set_state(self, on: bool) -> dict:
|
||||
"""Set the device state.
|
||||
|
||||
See :meth:`is_on`.
|
||||
"""
|
||||
return await self.protocol.query({"set_device_info": {"device_on": on}})
|
||||
|
||||
async def turn_on(self, **kwargs):
|
||||
async def turn_on(self, **kwargs: Any) -> dict:
|
||||
"""Turn on the device."""
|
||||
await self.set_state(True)
|
||||
return await self.set_state(True)
|
||||
|
||||
async def turn_off(self, **kwargs):
|
||||
async def turn_off(self, **kwargs: Any) -> dict:
|
||||
"""Turn off the device."""
|
||||
await self.set_state(False)
|
||||
return await self.set_state(False)
|
||||
|
||||
def update_from_discover_info(self, info):
|
||||
def update_from_discover_info(
|
||||
self,
|
||||
info: dict,
|
||||
) -> None:
|
||||
"""Update state from info from the discover call."""
|
||||
self._discovery_info = info
|
||||
self._info = info
|
||||
@@ -633,7 +640,7 @@ class SmartDevice(Device):
|
||||
async def wifi_scan(self) -> list[WifiNetwork]:
|
||||
"""Scan for available wifi networks."""
|
||||
|
||||
def _net_for_scan_info(res):
|
||||
def _net_for_scan_info(res: dict) -> WifiNetwork:
|
||||
return WifiNetwork(
|
||||
ssid=base64.b64decode(res["ssid"]).decode(),
|
||||
cipher_type=res["cipher_type"],
|
||||
@@ -651,7 +658,9 @@ class SmartDevice(Device):
|
||||
]
|
||||
return networks
|
||||
|
||||
async def wifi_join(self, ssid: str, password: str, keytype: str = "wpa2_psk"):
|
||||
async def wifi_join(
|
||||
self, ssid: str, password: str, keytype: str = "wpa2_psk"
|
||||
) -> dict:
|
||||
"""Join the given wifi network.
|
||||
|
||||
This method returns nothing as the device tries to activate the new
|
||||
@@ -688,9 +697,12 @@ class SmartDevice(Device):
|
||||
except DeviceError:
|
||||
raise # Re-raise on device-reported errors
|
||||
except KasaException:
|
||||
_LOGGER.debug("Received an expected for wifi join, but this is expected")
|
||||
_LOGGER.debug(
|
||||
"Received a kasa exception for wifi join, but this is expected"
|
||||
)
|
||||
return {}
|
||||
|
||||
async def update_credentials(self, username: str, password: str):
|
||||
async def update_credentials(self, username: str, password: str) -> dict:
|
||||
"""Update device credentials.
|
||||
|
||||
This will replace the existing authentication credentials on the device.
|
||||
@@ -705,7 +717,7 @@ class SmartDevice(Device):
|
||||
}
|
||||
return await self.protocol.query({"set_qs_info": payload})
|
||||
|
||||
async def set_alias(self, alias: str):
|
||||
async def set_alias(self, alias: str) -> dict:
|
||||
"""Set the device name (alias)."""
|
||||
return await self.protocol.query(
|
||||
{"set_device_info": {"nickname": base64.b64encode(alias.encode()).decode()}}
|
||||
|
Reference in New Issue
Block a user