mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-13 10:58:02 +00:00
Add commands to control the wifi settings (#45)
* Add commands to control the wifi settings Enables initial provisioning and changing the wifi network later on without the official app * new api to smartdevice: wifi_scan() and wifi_join(ssid, password, keytype) * cli: new subcommand 'wifi' with two commands: scan and join * update readme to initial setup * improvements based on code review, f-strings++
This commit is contained in:
@@ -14,9 +14,10 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from kasa.protocol import TPLinkSmartHomeProtocol
|
||||
|
||||
@@ -33,6 +34,14 @@ class DeviceType(Enum):
|
||||
Unknown = -1
|
||||
|
||||
|
||||
@dataclass
|
||||
class WifiNetwork:
|
||||
"""Wifi network container."""
|
||||
|
||||
ssid: str
|
||||
key_type: int
|
||||
|
||||
|
||||
class SmartDeviceException(Exception):
|
||||
"""Base exception for device errors."""
|
||||
|
||||
@@ -564,6 +573,22 @@ class SmartDevice:
|
||||
"""
|
||||
return self.mac
|
||||
|
||||
async def wifi_scan(self) -> List[WifiNetwork]:
|
||||
"""Scan for available wifi networks."""
|
||||
info = await self._query_helper("netif", "get_scaninfo", {"refresh": 1})
|
||||
if "ap_list" not in info:
|
||||
raise SmartDeviceException("Invalid response for wifi scan: %s" % info)
|
||||
|
||||
return [WifiNetwork(**x) for x in info["ap_list"]]
|
||||
|
||||
async def wifi_join(self, ssid, password, keytype=3):
|
||||
"""Join the given wifi network.
|
||||
|
||||
If joining the network fails, the device will return to AP mode after a while.
|
||||
"""
|
||||
payload = {"ssid": ssid, "password": password, "key_type": keytype}
|
||||
return await self._query_helper("netif", "set_stainfo", payload)
|
||||
|
||||
@property
|
||||
def device_type(self) -> DeviceType:
|
||||
"""Return the device type."""
|
||||
|
Reference in New Issue
Block a user