2023-12-04 18:50:05 +00:00
|
|
|
"""Module for the IOT legacy IOT KASA protocol."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2024-11-18 18:46:36 +00:00
|
|
|
from collections.abc import Callable
|
2024-07-17 17:57:09 +00:00
|
|
|
from pprint import pformat as pf
|
2024-11-18 18:46:36 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-11-13 17:50:21 +00:00
|
|
|
from ..deviceconfig import DeviceConfig
|
|
|
|
from ..exceptions import (
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
|
|
|
KasaException,
|
|
|
|
TimeoutError,
|
|
|
|
_ConnectionError,
|
|
|
|
_RetryableError,
|
2024-01-18 09:57:33 +00:00
|
|
|
)
|
2024-11-13 17:50:21 +00:00
|
|
|
from ..json import dumps as json_dumps
|
|
|
|
from ..transports import XorEncryption, XorTransport
|
2024-11-12 13:40:44 +00:00
|
|
|
from .protocol import BaseProtocol, mask_mac, redact_data
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2024-11-13 17:50:21 +00:00
|
|
|
from ..transports import BaseTransport
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-07-17 17:57:09 +00:00
|
|
|
REDACTORS: dict[str, Callable[[Any], Any] | None] = {
|
|
|
|
"latitude": lambda x: 0,
|
|
|
|
"longitude": lambda x: 0,
|
|
|
|
"latitude_i": lambda x: 0,
|
|
|
|
"longitude_i": lambda x: 0,
|
|
|
|
"deviceId": lambda x: "REDACTED_" + x[9::],
|
|
|
|
"id": lambda x: "REDACTED_" + x[9::],
|
|
|
|
"alias": lambda x: "#MASKED_NAME#" if x else "",
|
|
|
|
"mac": mask_mac,
|
|
|
|
"mic_mac": mask_mac,
|
|
|
|
"ssid": lambda x: "#MASKED_SSID#" if x else "",
|
|
|
|
"oemId": lambda x: "REDACTED_" + x[9::],
|
|
|
|
"username": lambda _: "user@example.com", # cnCloud
|
|
|
|
}
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-01-22 15:28:30 +00:00
|
|
|
class IotProtocol(BaseProtocol):
|
2023-12-04 18:50:05 +00:00
|
|
|
"""Class for the legacy TPLink IOT KASA Protocol."""
|
|
|
|
|
2024-01-18 09:57:33 +00:00
|
|
|
BACKOFF_SECONDS_AFTER_TIMEOUT = 1
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
*,
|
2023-12-19 14:11:59 +00:00
|
|
|
transport: BaseTransport,
|
2023-12-04 18:50:05 +00:00
|
|
|
) -> None:
|
2023-12-19 14:11:59 +00:00
|
|
|
"""Create a protocol object."""
|
2023-12-29 19:17:15 +00:00
|
|
|
super().__init__(transport=transport)
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
self._query_lock = asyncio.Lock()
|
2024-07-17 17:57:09 +00:00
|
|
|
self._redact_data = True
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def query(self, request: str | dict, retry_count: int = 3) -> dict:
|
2023-12-04 18:50:05 +00:00
|
|
|
"""Query the device retrying for retry_count on failure."""
|
|
|
|
if isinstance(request, dict):
|
|
|
|
request = json_dumps(request)
|
|
|
|
assert isinstance(request, str) # noqa: S101
|
|
|
|
|
|
|
|
async with self._query_lock:
|
|
|
|
return await self._query(request, retry_count)
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def _query(self, request: str, retry_count: int = 3) -> dict:
|
2023-12-04 18:50:05 +00:00
|
|
|
for retry in range(retry_count + 1):
|
|
|
|
try:
|
|
|
|
return await self._execute_query(request, retry)
|
2024-02-21 15:52:55 +00:00
|
|
|
except _ConnectionError as sdex:
|
2023-12-04 18:50:05 +00:00
|
|
|
if retry >= retry_count:
|
2023-12-19 14:11:59 +00:00
|
|
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
2024-01-18 09:57:33 +00:00
|
|
|
raise sdex
|
2023-12-04 18:50:05 +00:00
|
|
|
continue
|
2024-02-21 15:52:55 +00:00
|
|
|
except AuthenticationError as auex:
|
2024-01-23 22:15:18 +00:00
|
|
|
await self._transport.reset()
|
2023-12-19 14:11:59 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Unable to authenticate with %s, not retrying", self._host
|
|
|
|
)
|
2023-12-04 18:50:05 +00:00
|
|
|
raise auex
|
2024-02-21 15:52:55 +00:00
|
|
|
except _RetryableError as ex:
|
2024-01-23 22:15:18 +00:00
|
|
|
await self._transport.reset()
|
2024-01-18 09:57:33 +00:00
|
|
|
if retry >= retry_count:
|
|
|
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
|
|
raise ex
|
|
|
|
continue
|
2024-02-21 15:52:55 +00:00
|
|
|
except TimeoutError as ex:
|
2024-01-23 22:15:18 +00:00
|
|
|
await self._transport.reset()
|
2024-01-18 09:57:33 +00:00
|
|
|
if retry >= retry_count:
|
|
|
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
|
|
raise ex
|
|
|
|
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
|
|
|
|
continue
|
2024-02-21 15:52:55 +00:00
|
|
|
except KasaException as ex:
|
2024-01-23 22:15:18 +00:00
|
|
|
await self._transport.reset()
|
2023-12-20 17:08:04 +00:00
|
|
|
_LOGGER.debug(
|
2024-01-18 09:57:33 +00:00
|
|
|
"Unable to query the device: %s, not retrying: %s",
|
2023-12-20 17:08:04 +00:00
|
|
|
self._host,
|
|
|
|
ex,
|
|
|
|
)
|
|
|
|
raise ex
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
# make mypy happy, this should never be reached..
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Query reached somehow to unreachable")
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def _execute_query(self, request: str, retry_count: int) -> dict:
|
2024-07-17 17:57:09 +00:00
|
|
|
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
|
|
|
|
|
|
|
if debug_enabled:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"%s >> %s",
|
|
|
|
self._host,
|
|
|
|
request,
|
|
|
|
)
|
|
|
|
resp = await self._transport.send(request)
|
|
|
|
|
|
|
|
if debug_enabled:
|
|
|
|
data = redact_data(resp, REDACTORS) if self._redact_data else resp
|
|
|
|
_LOGGER.debug(
|
|
|
|
"%s << %s",
|
|
|
|
self._host,
|
|
|
|
pf(data),
|
|
|
|
)
|
|
|
|
return resp
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
async def close(self) -> None:
|
2024-01-23 22:15:18 +00:00
|
|
|
"""Close the underlying transport."""
|
2023-12-04 18:50:05 +00:00
|
|
|
await self._transport.close()
|
2024-01-26 09:11:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _deprecated_TPLinkSmartHomeProtocol(IotProtocol):
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-04-17 13:39:24 +00:00
|
|
|
host: str | None = None,
|
2024-01-26 09:11:31 +00:00
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
port: int | None = None,
|
|
|
|
timeout: int | None = None,
|
|
|
|
transport: BaseTransport | None = None,
|
2024-01-26 09:11:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Create a protocol object."""
|
|
|
|
if not host and not transport:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("host or transport must be supplied")
|
2024-01-26 09:11:31 +00:00
|
|
|
if not transport:
|
|
|
|
config = DeviceConfig(
|
|
|
|
host=host, # type: ignore[arg-type]
|
|
|
|
port_override=port,
|
|
|
|
timeout=timeout or XorTransport.DEFAULT_TIMEOUT,
|
|
|
|
)
|
|
|
|
transport = XorTransport(config=config)
|
|
|
|
super().__init__(transport=transport)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def encrypt(request: str) -> bytes:
|
|
|
|
"""Encrypt a request for a TP-Link Smart Home Device.
|
|
|
|
|
|
|
|
:param request: plaintext request data
|
|
|
|
:return: ciphertext to be send over wire, in bytes
|
|
|
|
"""
|
|
|
|
return XorEncryption.encrypt(request)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decrypt(ciphertext: bytes) -> str:
|
|
|
|
"""Decrypt a response of a TP-Link Smart Home Device.
|
|
|
|
|
|
|
|
:param ciphertext: encrypted response data
|
|
|
|
:return: plaintext response
|
|
|
|
"""
|
|
|
|
return XorEncryption.decrypt(ciphertext)
|