mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Make device initialisation easier by reducing required imports (#936)
Adds username and password arguments to discovery to remove the need to import Credentials. Creates TypeAliases in Device for connection configuration classes and DeviceType. Using the API with these changes will only require importing either Discover or Device depending on whether using Discover.discover() or Device.connect() to initialize and interact with the API.
This commit is contained in:
@@ -18,17 +18,32 @@ devices.
|
||||
|
||||
Discovery returns a dict of {ip: discovered devices}:
|
||||
|
||||
>>> import asyncio
|
||||
>>> from kasa import Discover, Credentials
|
||||
>>>
|
||||
>>> found_devices = await Discover.discover()
|
||||
>>> [dev.model for dev in found_devices.values()]
|
||||
['KP303(UK)', 'HS110(EU)', 'L530E', 'KL430(US)', 'HS220(US)']
|
||||
|
||||
You can pass username and password for devices requiring authentication
|
||||
|
||||
>>> devices = await Discover.discover(
|
||||
>>> username="user@example.com",
|
||||
>>> password="great_password",
|
||||
>>> )
|
||||
>>> print(len(devices))
|
||||
5
|
||||
|
||||
You can also pass a :class:`kasa.Credentials`
|
||||
|
||||
>>> creds = Credentials("user@example.com", "great_password")
|
||||
>>> devices = await Discover.discover(credentials=creds)
|
||||
>>> print(len(devices))
|
||||
5
|
||||
|
||||
Discovery can also be targeted to a specific broadcast address instead of
|
||||
the default 255.255.255.255:
|
||||
|
||||
>>> found_devices = await Discover.discover(target="127.0.0.255")
|
||||
>>> found_devices = await Discover.discover(target="127.0.0.255", credentials=creds)
|
||||
>>> print(len(found_devices))
|
||||
5
|
||||
|
||||
@@ -49,29 +64,16 @@ It is also possible to pass a coroutine to be executed for each found device:
|
||||
>>> await dev.update()
|
||||
>>> print(f"Discovered {dev.alias} (model: {dev.model})")
|
||||
>>>
|
||||
>>> devices = await Discover.discover(on_discovered=print_dev_info)
|
||||
>>> devices = await Discover.discover(on_discovered=print_dev_info, credentials=creds)
|
||||
Discovered Bedroom Power Strip (model: KP303(UK))
|
||||
Discovered Bedroom Lamp Plug (model: HS110(EU))
|
||||
Discovered Living Room Bulb (model: L530)
|
||||
Discovered Bedroom Lightstrip (model: KL430(US))
|
||||
Discovered Living Room Dimmer Switch (model: HS220(US))
|
||||
|
||||
You can pass credentials for devices requiring authentication
|
||||
|
||||
>>> devices = await Discover.discover(
|
||||
>>> credentials=Credentials("myusername", "mypassword"),
|
||||
>>> discovery_timeout=10
|
||||
>>> )
|
||||
>>> print(len(devices))
|
||||
5
|
||||
|
||||
Discovering a single device returns a kasa.Device object.
|
||||
|
||||
>>> device = await Discover.discover_single(
|
||||
>>> "127.0.0.1",
|
||||
>>> credentials=Credentials("myusername", "mypassword"),
|
||||
>>> discovery_timeout=10
|
||||
>>> )
|
||||
>>> device = await Discover.discover_single("127.0.0.1", credentials=creds)
|
||||
>>> device.model
|
||||
'KP303(UK)'
|
||||
|
||||
@@ -98,7 +100,11 @@ from kasa.device_factory import (
|
||||
get_device_class_from_sys_info,
|
||||
get_protocol,
|
||||
)
|
||||
from kasa.deviceconfig import ConnectionType, DeviceConfig, EncryptType
|
||||
from kasa.deviceconfig import (
|
||||
DeviceConfig,
|
||||
DeviceConnectionParameters,
|
||||
DeviceEncryptionType,
|
||||
)
|
||||
from kasa.exceptions import (
|
||||
KasaException,
|
||||
TimeoutError,
|
||||
@@ -296,6 +302,8 @@ class Discover:
|
||||
interface=None,
|
||||
on_unsupported=None,
|
||||
credentials=None,
|
||||
username: str | None = None,
|
||||
password: str | None = None,
|
||||
port=None,
|
||||
timeout=None,
|
||||
) -> DeviceDict:
|
||||
@@ -323,11 +331,16 @@ class Discover:
|
||||
:param discovery_packets: Number of discovery packets to broadcast
|
||||
:param interface: Bind to specific interface
|
||||
:param on_unsupported: Optional callback when unsupported devices are discovered
|
||||
:param credentials: Credentials for devices requiring authentication
|
||||
:param credentials: Credentials for devices that require authentication.
|
||||
username and password are ignored if provided.
|
||||
:param username: Username for devices that require authentication
|
||||
:param password: Password for devices that require authentication
|
||||
:param port: Override the discovery port for devices listening on 9999
|
||||
:param timeout: Query timeout in seconds for devices returned by discovery
|
||||
:return: dictionary with discovered devices
|
||||
"""
|
||||
if not credentials and username and password:
|
||||
credentials = Credentials(username, password)
|
||||
loop = asyncio.get_event_loop()
|
||||
transport, protocol = await loop.create_datagram_endpoint(
|
||||
lambda: _DiscoverProtocol(
|
||||
@@ -367,6 +380,8 @@ class Discover:
|
||||
port: int | None = None,
|
||||
timeout: int | None = None,
|
||||
credentials: Credentials | None = None,
|
||||
username: str | None = None,
|
||||
password: str | None = None,
|
||||
) -> Device:
|
||||
"""Discover a single device by the given IP address.
|
||||
|
||||
@@ -379,10 +394,15 @@ class Discover:
|
||||
:param discovery_timeout: Timeout in seconds for discovery
|
||||
:param port: Optionally set a different port for legacy devices using port 9999
|
||||
:param timeout: Timeout in seconds device for devices queries
|
||||
:param credentials: Credentials for devices that require authentication
|
||||
:param credentials: Credentials for devices that require authentication.
|
||||
username and password are ignored if provided.
|
||||
:param username: Username for devices that require authentication
|
||||
:param password: Password for devices that require authentication
|
||||
:rtype: SmartDevice
|
||||
:return: Object for querying/controlling found device.
|
||||
"""
|
||||
if not credentials and username and password:
|
||||
credentials = Credentials(username, password)
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
try:
|
||||
@@ -469,8 +489,9 @@ class Discover:
|
||||
device = device_class(config.host, config=config)
|
||||
sys_info = info["system"]["get_sysinfo"]
|
||||
if device_type := sys_info.get("mic_type", sys_info.get("type")):
|
||||
config.connection_type = ConnectionType.from_values(
|
||||
device_family=device_type, encryption_type=EncryptType.Xor.value
|
||||
config.connection_type = DeviceConnectionParameters.from_values(
|
||||
device_family=device_type,
|
||||
encryption_type=DeviceEncryptionType.Xor.value,
|
||||
)
|
||||
device.protocol = get_protocol(config) # type: ignore[assignment]
|
||||
device.update_from_discover_info(info)
|
||||
@@ -502,7 +523,7 @@ class Discover:
|
||||
type_ = discovery_result.device_type
|
||||
|
||||
try:
|
||||
config.connection_type = ConnectionType.from_values(
|
||||
config.connection_type = DeviceConnectionParameters.from_values(
|
||||
type_,
|
||||
discovery_result.mgt_encrypt_schm.encrypt_type,
|
||||
discovery_result.mgt_encrypt_schm.lv,
|
||||
|
Reference in New Issue
Block a user