API for local communication with TP-Link Smart Plugs devices over a LAN with asyncio.
Project description
# aiotplink
Library to communicate with TP-Link Smart Plugs and similar devices
## Features
* Supports TPLink Smart Switches and Lights
* Python API to interact with device at a low level using asyncio
## About this library
Based on the work done by [SoftSCheck](https://github.com/softScheck/tplink-smartplug)
and [GadgetReactor](https://github.com/GadgetReactor/pyHS100)
## Installation
```
$ sudo pip3 install aiotplink
```
At this point you should be able to use
You can try:
```
python3 -m aioyplink
```
And you should be able to turn On/Off your devices
## Using the library.
To make things simple, you create a class with 2 methods, "register" and "unregister"
class devices():
""" A simple class with a register and unregister methods
"""
def __init__(self):
self.devices={}
self.doi=None #device of interest
def register(self,info, addr):
if "mac" in info and info["mac"].lower() not in self.devices:
self.devices[info["mac"].lower()] = aiot.GetDevice(addr,info,hb=10)
else:
self.devices[info["mac"].lower()].addr = addr
def unregister(self,mac):
if mac.lower() in self.devices:
print ("%s is gone"% self.devices[mac.lower()].name)
self.devices[mac.lower()].stop()
del(self.devices[mac.lower()])
def stop(self):
for dev in self.devices.values():
dev.stop()
When registering, you get an dictionary with the flatten out info from a TP-Link device, and an address with
the format (ip address, port). You can pass those information directly to 'GetDevice' to get the correct object.
'GetDevice" is defined like so
GetDevice(addr,info,hb=HBTIMEOUT,on_change=lambda x: print(x))
With addr, the address pair, info, the flatten out informaton from discovery, hb, a heatbeat timeout in secs and
on_change a function that will react to whatever is produced by the heartbeat. Note that if the device has an energy meter,
those values will automatically be produced with the heartbeat.
After that it is quite simple.
MyDevices= devices()
loop = aio.get_event_loop()
discovery = aiot.TPLinkDiscovery(loop, MyDevices, repeat=15)
try:
loop.add_reader(sys.stdin,readin)
discovery.start()
print("Hit \"Enter\" to start")
print("Use Ctrl-C to quit")
loop.run_forever()
except:
print("Exiting at user's request.")
finally:
MyDevices.stop()
discovery.cleanup()
loop.remove_reader(sys.stdin)
loop.run_until_complete(aio.sleep(10))
loop.close()
Create a registrar instance
Create a TPLinkDiscovery instance passing the registrar and how often to run discovery
Start discovery, and you are on your merry way.
The various device object will have these methods available.
on(): Turning the device on
off(): Turning the device off
led_on(): Turn the LED light on (AKA "Night mode" off)
led_off : Turn the LED light off (AKA "Night mode" on)
set_name(name)
set_brighness)
set_temperature()
set_colour(hue, saturation, value)
depending on their capabilities.
Most commands are defined in the commands.py file.
## Troubleshooting
Open an issue and I'll try to help.
Library to communicate with TP-Link Smart Plugs and similar devices
## Features
* Supports TPLink Smart Switches and Lights
* Python API to interact with device at a low level using asyncio
## About this library
Based on the work done by [SoftSCheck](https://github.com/softScheck/tplink-smartplug)
and [GadgetReactor](https://github.com/GadgetReactor/pyHS100)
## Installation
```
$ sudo pip3 install aiotplink
```
At this point you should be able to use
You can try:
```
python3 -m aioyplink
```
And you should be able to turn On/Off your devices
## Using the library.
To make things simple, you create a class with 2 methods, "register" and "unregister"
class devices():
""" A simple class with a register and unregister methods
"""
def __init__(self):
self.devices={}
self.doi=None #device of interest
def register(self,info, addr):
if "mac" in info and info["mac"].lower() not in self.devices:
self.devices[info["mac"].lower()] = aiot.GetDevice(addr,info,hb=10)
else:
self.devices[info["mac"].lower()].addr = addr
def unregister(self,mac):
if mac.lower() in self.devices:
print ("%s is gone"% self.devices[mac.lower()].name)
self.devices[mac.lower()].stop()
del(self.devices[mac.lower()])
def stop(self):
for dev in self.devices.values():
dev.stop()
When registering, you get an dictionary with the flatten out info from a TP-Link device, and an address with
the format (ip address, port). You can pass those information directly to 'GetDevice' to get the correct object.
'GetDevice" is defined like so
GetDevice(addr,info,hb=HBTIMEOUT,on_change=lambda x: print(x))
With addr, the address pair, info, the flatten out informaton from discovery, hb, a heatbeat timeout in secs and
on_change a function that will react to whatever is produced by the heartbeat. Note that if the device has an energy meter,
those values will automatically be produced with the heartbeat.
After that it is quite simple.
MyDevices= devices()
loop = aio.get_event_loop()
discovery = aiot.TPLinkDiscovery(loop, MyDevices, repeat=15)
try:
loop.add_reader(sys.stdin,readin)
discovery.start()
print("Hit \"Enter\" to start")
print("Use Ctrl-C to quit")
loop.run_forever()
except:
print("Exiting at user's request.")
finally:
MyDevices.stop()
discovery.cleanup()
loop.remove_reader(sys.stdin)
loop.run_until_complete(aio.sleep(10))
loop.close()
Create a registrar instance
Create a TPLinkDiscovery instance passing the registrar and how often to run discovery
Start discovery, and you are on your merry way.
The various device object will have these methods available.
on(): Turning the device on
off(): Turning the device off
led_on(): Turn the LED light on (AKA "Night mode" off)
led_off : Turn the LED light off (AKA "Night mode" on)
set_name(name)
set_brighness)
set_temperature()
set_colour(hue, saturation, value)
depending on their capabilities.
Most commands are defined in the commands.py file.
## Troubleshooting
Open an issue and I'll try to help.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiotplink-0.1.0b1.tar.gz
(12.0 kB
view details)
Built Distribution
File details
Details for the file aiotplink-0.1.0b1.tar.gz
.
File metadata
- Download URL: aiotplink-0.1.0b1.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63bba3a5142f6223fcb03aa92e410320eb79550ee2d44ec6f6893ad2408350f4 |
|
MD5 | 207a7d964df6be1e3222cf228635e681 |
|
BLAKE2b-256 | 2c8e0da9fb04d757a39263687d4e3725e53dfecd73a853a5e4b8a19b4053f43a |
File details
Details for the file aiotplink-0.1.0b1-py3-none-any.whl
.
File metadata
- Download URL: aiotplink-0.1.0b1-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 223091c74c55f742836d0a8157c0b35b9ed16ad4c54ec5ce82fab30139280e8e |
|
MD5 | 2f806ce59e3d96c40f8754cca2899d98 |
|
BLAKE2b-256 | 2439f80f712d06b40d8b208613b3803d2c5926505100b9943143fbd58fcd32d3 |