add typing hints to make it easier for 3rd party developers to use the library (#90)

* add typing hints to make it easier for 3rd party developers to use the library

* remove unused devicetype enum to support python3.3

* add python 3.3 to travis and tox, install typing module in setup.py
This commit is contained in:
Teemu R
2017-09-18 18:13:06 +02:00
committed by GitHub
parent 3ddd31f3c1
commit af90a36153
11 changed files with 163 additions and 135 deletions

View File

@@ -2,6 +2,7 @@ import json
import socket
import struct
import logging
from typing import Any, Dict, Union
_LOGGER = logging.getLogger(__name__)
@@ -24,7 +25,9 @@ class TPLinkSmartHomeProtocol:
DEFAULT_TIMEOUT = 5
@staticmethod
def query(host, request, port=DEFAULT_PORT):
def query(host: str,
request: Union[str, Dict],
port: int = DEFAULT_PORT) -> Any:
"""
Request information from a TP-Link SmartHome Device and return the
response.
@@ -76,7 +79,7 @@ class TPLinkSmartHomeProtocol:
return json.loads(response)
@staticmethod
def encrypt(request):
def encrypt(request: str) -> bytearray:
"""
Encrypt a request for a TP-Link Smart Home Device.
@@ -94,7 +97,7 @@ class TPLinkSmartHomeProtocol:
return buffer
@staticmethod
def decrypt(ciphertext):
def decrypt(ciphertext: bytes) -> str:
"""
Decrypt a response of a TP-Link Smart Home Device.
@@ -104,9 +107,9 @@ class TPLinkSmartHomeProtocol:
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR
buffer = []
ciphertext = ciphertext.decode('latin-1')
ciphertext_str = ciphertext.decode('latin-1')
for char in ciphertext:
for char in ciphertext_str:
plain = key ^ ord(char)
key = ord(char)
buffer.append(chr(plain))