2019-11-11 21:14:34 +00:00
|
|
|
"""Python interface for TP-Link's smart home devices.
|
2017-05-26 14:11:03 +00:00
|
|
|
|
2024-05-16 16:13:44 +00:00
|
|
|
All common, shared functionalities are available through `Device` class::
|
2017-05-26 14:11:03 +00:00
|
|
|
|
2024-05-16 16:13:44 +00:00
|
|
|
>>> from kasa import Discover
|
|
|
|
>>> x = await Discover.discover_single("192.168.1.1")
|
|
|
|
>>> print(x.model)
|
2017-05-26 14:11:03 +00:00
|
|
|
|
2024-05-16 16:13:44 +00:00
|
|
|
For device type specific actions `modules` and `features` should be used instead.
|
2017-05-26 14:11:03 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
Module-specific errors are raised as `KasaException` and are expected
|
2017-05-26 14:11:03 +00:00
|
|
|
to be handled by the user of the library.
|
|
|
|
"""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2023-05-17 18:33:02 +00:00
|
|
|
from importlib.metadata import version
|
2024-11-10 18:55:13 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2024-01-26 09:11:31 +00:00
|
|
|
from warnings import warn
|
2021-03-18 18:22:10 +00:00
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
from kasa.credentials import Credentials
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.device import Device
|
|
|
|
from kasa.device_type import DeviceType
|
2023-12-29 19:17:15 +00:00
|
|
|
from kasa.deviceconfig import (
|
|
|
|
DeviceConfig,
|
2024-06-03 18:06:54 +00:00
|
|
|
DeviceConnectionParameters,
|
|
|
|
DeviceEncryptionType,
|
|
|
|
DeviceFamily,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2019-12-18 08:11:18 +00:00
|
|
|
from kasa.discover import Discover
|
2021-09-23 15:58:19 +00:00
|
|
|
from kasa.emeterstatus import EmeterStatus
|
2023-09-13 13:46:38 +00:00
|
|
|
from kasa.exceptions import (
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
|
|
|
DeviceError,
|
|
|
|
KasaException,
|
|
|
|
TimeoutError,
|
|
|
|
UnsupportedDeviceError,
|
2023-09-13 13:46:38 +00:00
|
|
|
)
|
2024-04-24 16:38:52 +00:00
|
|
|
from kasa.feature import Feature
|
2024-05-22 13:33:55 +00:00
|
|
|
from kasa.interfaces.light import HSV, ColorTempRange, Light, LightState
|
2024-01-26 09:11:31 +00:00
|
|
|
from kasa.iotprotocol import (
|
|
|
|
IotProtocol,
|
|
|
|
_deprecated_TPLinkSmartHomeProtocol, # noqa: F401
|
|
|
|
)
|
2024-05-10 18:29:28 +00:00
|
|
|
from kasa.module import Module
|
2024-11-12 13:40:44 +00:00
|
|
|
from kasa.protocol import BaseProtocol
|
2023-12-04 18:50:05 +00:00
|
|
|
from kasa.smartprotocol import SmartProtocol
|
2024-11-12 13:40:44 +00:00
|
|
|
from kasa.transports import BaseTransport
|
2019-11-11 18:26:06 +00:00
|
|
|
|
2020-05-12 10:11:47 +00:00
|
|
|
__version__ = version("python-kasa")
|
|
|
|
|
|
|
|
|
2019-11-11 18:26:06 +00:00
|
|
|
__all__ = [
|
|
|
|
"Discover",
|
2024-01-22 15:28:30 +00:00
|
|
|
"BaseProtocol",
|
2024-11-01 18:17:18 +00:00
|
|
|
"BaseTransport",
|
2023-12-04 18:50:05 +00:00
|
|
|
"IotProtocol",
|
|
|
|
"SmartProtocol",
|
2024-05-19 10:20:18 +00:00
|
|
|
"LightState",
|
2022-10-27 15:40:54 +00:00
|
|
|
"TurnOnBehaviors",
|
|
|
|
"TurnOnBehavior",
|
2019-11-11 18:26:06 +00:00
|
|
|
"DeviceType",
|
2024-02-15 15:25:08 +00:00
|
|
|
"Feature",
|
2019-11-11 18:26:06 +00:00
|
|
|
"EmeterStatus",
|
2024-02-04 15:20:08 +00:00
|
|
|
"Device",
|
2024-05-11 18:40:08 +00:00
|
|
|
"Light",
|
2024-05-22 13:33:55 +00:00
|
|
|
"ColorTempRange",
|
|
|
|
"HSV",
|
2024-02-04 15:20:08 +00:00
|
|
|
"Plug",
|
2024-05-10 18:29:28 +00:00
|
|
|
"Module",
|
2024-02-21 15:52:55 +00:00
|
|
|
"KasaException",
|
|
|
|
"AuthenticationError",
|
|
|
|
"DeviceError",
|
|
|
|
"UnsupportedDeviceError",
|
|
|
|
"TimeoutError",
|
2023-09-13 13:46:38 +00:00
|
|
|
"Credentials",
|
2023-12-29 19:17:15 +00:00
|
|
|
"DeviceConfig",
|
2024-06-03 18:06:54 +00:00
|
|
|
"DeviceConnectionParameters",
|
|
|
|
"DeviceEncryptionType",
|
|
|
|
"DeviceFamily",
|
2019-11-11 18:26:06 +00:00
|
|
|
]
|
2024-01-26 09:11:31 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from . import iot
|
2024-05-19 10:20:18 +00:00
|
|
|
from .iot.modules.lightpreset import IotLightPreset
|
2024-02-04 15:20:08 +00:00
|
|
|
|
2024-01-26 09:11:31 +00:00
|
|
|
deprecated_names = ["TPLinkSmartHomeProtocol"]
|
2024-02-04 15:20:08 +00:00
|
|
|
deprecated_smart_devices = {
|
|
|
|
"SmartDevice": iot.IotDevice,
|
|
|
|
"SmartPlug": iot.IotPlug,
|
|
|
|
"SmartBulb": iot.IotBulb,
|
|
|
|
"SmartLightStrip": iot.IotLightStrip,
|
|
|
|
"SmartStrip": iot.IotStrip,
|
|
|
|
"SmartDimmer": iot.IotDimmer,
|
2024-05-19 10:20:18 +00:00
|
|
|
"SmartBulbPreset": IotLightPreset,
|
2024-02-04 15:20:08 +00:00
|
|
|
}
|
2024-06-03 18:06:54 +00:00
|
|
|
deprecated_classes = {
|
2024-02-21 15:52:55 +00:00
|
|
|
"SmartDeviceException": KasaException,
|
|
|
|
"UnsupportedDeviceException": UnsupportedDeviceError,
|
|
|
|
"AuthenticationException": AuthenticationError,
|
|
|
|
"TimeoutException": TimeoutError,
|
2024-06-03 18:06:54 +00:00
|
|
|
"ConnectionType": DeviceConnectionParameters,
|
|
|
|
"EncryptType": DeviceEncryptionType,
|
|
|
|
"DeviceFamilyType": DeviceFamily,
|
2024-02-21 15:52:55 +00:00
|
|
|
}
|
2024-01-26 09:11:31 +00:00
|
|
|
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def __getattr__(name: str) -> Any:
|
2024-01-26 09:11:31 +00:00
|
|
|
if name in deprecated_names:
|
2024-10-31 13:12:17 +00:00
|
|
|
warn(f"{name} is deprecated", DeprecationWarning, stacklevel=2)
|
2024-01-26 09:11:31 +00:00
|
|
|
return globals()[f"_deprecated_{name}"]
|
2024-02-04 15:20:08 +00:00
|
|
|
if name in deprecated_smart_devices:
|
|
|
|
new_class = deprecated_smart_devices[name]
|
|
|
|
package_name = ".".join(new_class.__module__.split(".")[:-1])
|
|
|
|
warn(
|
|
|
|
f"{name} is deprecated, use {new_class.__name__} "
|
|
|
|
+ f"from package {package_name} instead or use Discover.discover_single()"
|
|
|
|
+ " and Device.connect() to support new protocols",
|
|
|
|
DeprecationWarning,
|
2024-10-31 13:12:17 +00:00
|
|
|
stacklevel=2,
|
2024-02-04 15:20:08 +00:00
|
|
|
)
|
|
|
|
return new_class
|
2024-06-03 18:06:54 +00:00
|
|
|
if name in deprecated_classes:
|
2024-11-10 18:55:13 +00:00
|
|
|
new_class = deprecated_classes[name] # type: ignore[assignment]
|
2024-02-21 15:52:55 +00:00
|
|
|
msg = f"{name} is deprecated, use {new_class.__name__} instead"
|
2024-10-31 13:12:17 +00:00
|
|
|
warn(msg, DeprecationWarning, stacklevel=2)
|
2024-02-21 15:52:55 +00:00
|
|
|
return new_class
|
2024-01-26 09:11:31 +00:00
|
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
SmartDevice = Device
|
|
|
|
SmartBulb = iot.IotBulb
|
|
|
|
SmartPlug = iot.IotPlug
|
|
|
|
SmartLightStrip = iot.IotLightStrip
|
|
|
|
SmartStrip = iot.IotStrip
|
|
|
|
SmartDimmer = iot.IotDimmer
|
2024-05-19 10:20:18 +00:00
|
|
|
SmartBulbPreset = IotLightPreset
|
2024-02-21 15:52:55 +00:00
|
|
|
|
|
|
|
SmartDeviceException = KasaException
|
|
|
|
UnsupportedDeviceException = UnsupportedDeviceError
|
|
|
|
AuthenticationException = AuthenticationError
|
|
|
|
TimeoutException = TimeoutError
|
2024-06-03 18:06:54 +00:00
|
|
|
ConnectionType = DeviceConnectionParameters
|
|
|
|
EncryptType = DeviceEncryptionType
|
|
|
|
DeviceFamilyType = DeviceFamily
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
# Instanstiate all classes so the type checkers catch abstract issues
|
|
|
|
from . import smart
|
|
|
|
|
|
|
|
smart.SmartDevice("127.0.0.1")
|
|
|
|
iot.IotDevice("127.0.0.1")
|
|
|
|
iot.IotPlug("127.0.0.1")
|
|
|
|
iot.IotBulb("127.0.0.1")
|
|
|
|
iot.IotLightStrip("127.0.0.1")
|
|
|
|
iot.IotStrip("127.0.0.1")
|
|
|
|
iot.IotDimmer("127.0.0.1")
|