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:
Steven B
2024-06-03 21:06:54 +03:00
committed by GitHub
parent bfba7a347f
commit be5202ccb7
12 changed files with 263 additions and 90 deletions

View File

@@ -9,9 +9,16 @@ from datetime import datetime
from typing import TYPE_CHECKING, Any, Mapping, Sequence
from warnings import warn
from .credentials import Credentials
from typing_extensions import TypeAlias
from .credentials import Credentials as _Credentials
from .device_type import DeviceType
from .deviceconfig import DeviceConfig
from .deviceconfig import (
DeviceConfig,
DeviceConnectionParameters,
DeviceEncryptionType,
DeviceFamily,
)
from .emeterstatus import EmeterStatus
from .exceptions import KasaException
from .feature import Feature
@@ -51,6 +58,22 @@ class Device(ABC):
or :func:`Discover.discover_single()`.
"""
# All types required to create devices directly via connect are aliased here
# to avoid consumers having to do multiple imports.
#: The type of device
Type: TypeAlias = DeviceType
#: The credentials for authentication
Credentials: TypeAlias = _Credentials
#: Configuration for connecting to the device
Config: TypeAlias = DeviceConfig
#: The family of the device, e.g. SMART.KASASWITCH.
Family: TypeAlias = DeviceFamily
#: The encryption for the device, e.g. Klap or Aes
EncryptionType: TypeAlias = DeviceEncryptionType
#: The connection type for the device.
ConnectionParameters: TypeAlias = DeviceConnectionParameters
def __init__(
self,
host: str,
@@ -166,7 +189,7 @@ class Device(ABC):
return self.protocol._transport._port
@property
def credentials(self) -> Credentials | None:
def credentials(self) -> _Credentials | None:
"""The device credentials."""
return self.protocol._transport._credentials