Add support for TP-Link smartbulbs (#30)

* Add support for new-style protocol

Newer devices (including my LB130) seem to include the request length in
the previously empty message header, and ignore requests that lack it. They
also don't send an empty packet as the final part of a response, which can
lead to hangs. Add support for this, with luck not breaking existing devices
in the process.

* Fix tests

We now include the request length in the encrypted packet header, so strip
the header rather than assuming that it's just zeroes.

* Create a SmartDevice parent class

Add a generic SmartDevice class that SmartPlug can inherit from, in
preparation for adding support for other device types.

* Add support for TP-Link smartbulbs

These bulbs use the same protocol as the smart plugs, but have additional
commands for controlling bulb-specific features. In addition, the bulbs
have their emeter under a different target and return responses that
include the energy unit in the key names.

* Add tests for bulbs

Not entirely comprehensive, but has pretty much the same level of testing
as plugs
This commit is contained in:
Matthew Garrett
2017-01-17 05:38:23 -08:00
committed by GadgetReactor
parent 04185706f8
commit 2d6376b597
7 changed files with 819 additions and 269 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import json
import socket
import struct
import logging
_LOGGER = logging.getLogger(__name__)
@@ -49,10 +50,16 @@ class TPLinkSmartHomeProtocol:
sock.send(TPLinkSmartHomeProtocol.encrypt(request))
buffer = bytes()
# Some devices send responses with a length header of 0 and
# terminate with a zero size chunk. Others send the length and
# will hang if we attempt to read more data.
length = -1
while True:
chunk = sock.recv(4096)
if length == -1:
length = struct.unpack(">I", chunk[0:4])[0]
buffer += chunk
if not chunk:
if (length > 0 and len(buffer) >= length + 4) or not chunk:
break
finally:
@@ -115,7 +122,7 @@ class TPLinkSmartHomeProtocol:
:return: ciphertext request
"""
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR
buffer = bytearray(4) # 4 nullbytes
buffer = bytearray(struct.pack(">I", len(request)))
for char in request:
cipher = key ^ ord(char)