2016-07-07 09:11:45 +00:00
|
|
|
# pyHS100
|
|
|
|
Python Library to control TPLink Switch (HS100 / HS110)
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
|
|
|
For all available API functions run ```help(SmartPlug)```
|
|
|
|
|
|
|
|
```python
|
|
|
|
from pyHS100 import SmartPlug
|
|
|
|
from pprint import pformat as pf
|
|
|
|
|
|
|
|
plug = SmartPlug("192.168.250.186")
|
|
|
|
print("Alias, type and supported features: %s" % (plug.identify(),))
|
|
|
|
print("Hardware: %s" % pf(plug.hw_info))
|
|
|
|
print("Full sysinfo: %s" % pf(plug.get_sysinfo())) # this prints lots of information about the device
|
|
|
|
```
|
|
|
|
|
|
|
|
## Time information
|
|
|
|
```python
|
|
|
|
print("Current time: %s" % plug.time)
|
|
|
|
print("Timezone: %s" % plug.timezone)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Getting and setting the name
|
|
|
|
```python
|
|
|
|
print("Alias: %s" % plug.alias)
|
|
|
|
plug.alias = "My New Smartplug"
|
|
|
|
```
|
|
|
|
|
|
|
|
## State & switching
|
|
|
|
```python
|
|
|
|
print("Current state: %s" % plug.state)
|
|
|
|
plug.turn_off()
|
|
|
|
plug.turn_on()
|
|
|
|
```
|
|
|
|
or
|
|
|
|
```python
|
|
|
|
plug.state = "ON"
|
|
|
|
plug.state = "OFF"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Getting emeter status (on HS110)
|
|
|
|
```python
|
|
|
|
print("Current consumption: %s" % plug.get_emeter_realtime())
|
|
|
|
print("Per day: %s" % plug.get_emeter_daily(year=2016, month=12))
|
|
|
|
print("Per month: %s" % plug.get_emeter_monthly(year=2016))
|
|
|
|
```
|
|
|
|
|
|
|
|
## Switching the led
|
|
|
|
```python
|
|
|
|
print("Current LED state: %s" % plug.led)
|
|
|
|
plug.led = 0 # turn off led
|
|
|
|
print("New LED state: %s" % plug.led)
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
# Example
|
|
|
|
There is also a simple tool for testing connectivity in examples, to use:
|
|
|
|
```python
|
|
|
|
python -m examples.cli <ip>
|
|
|
|
```
|