前回はnode.jsベースのtplink-smarthome-apiというAPIを試したが、まだまだ理解できていないし、テレビの見過ぎを防止するために時間をカウントして消すという方法ができそうにない。他にPythonベースのAPIを見つけた。
Reverse Engineering the TP-Link HS110書いている内容が理解できないのは自分の英語のレベルが低いせいだけではない。タイトルにある通り、リバースエンジニアリングで解析しているとのことだ。やっていることはとても真似できそうにもないが、pythonで書かれているスクリプトは非常にわかりやすかった。
プラグへ通信データを送受信するときは0x85 (171)を先頭キーとして、暗号化しているとのことだ。また、スクリプトはPython2ベースとのこと。
コマンド名を入れるやり方とJSONで書くやり方2種類用意されている。
コマンド名指定で電源ON
$ python2.7 tplink_smartplug.py -t 192.168.0.2 -c on
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}
JSON指定で電源ON
$ python2.7 tplink_smartplug.py -t 192.168.0.2 -j '{"system":{"set_relay_state":{"state":1}}}'
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}
コマンドは以下のものがある。
Command | Description |
on | Turns on the plug |
off | Turns off the plug |
info | Returns device info |
cloudinfo | Returns cloud connectivity info |
wlanscan | Scan for nearby access points |
time | Returns the system time |
schedule | Lists configured schedule rules |
countdown | Lists configured countdown rules |
antitheft | Lists configured antitheft rules |
reboot | Reboot the device |
reset | Reset the device to factory settings |
energy | Return realtime voltage/current/power |
最後の項目にあるenargyなら、電流値を見てテレビがついているかわかるかと思ったが、HS105は対応していないらしい。
$ python2.7 tplink_smartplug.py -t 192.168.0.2 -c energy
Sent: {“emeter”:{“get_realtime”:{}}}
Received: {“emeter”:{“get_realtime”:{“err_code”:-1,”err_msg”:”module not support”}}}
ちなみにpyton3で試したら2と3との互換の関係でできなかった。原因がわかったらまた報告します。