2016-07-07 09:11:45 +00:00
|
|
|
# pyHS100
|
2017-03-25 22:04:32 +00:00
|
|
|
|
|
|
|
Python Library to control TPLink smart plugs (HS100, HS105, HS110, HS200) and TPLink smart bulbs (LB1xx).
|
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
|
|
|
|
|
2017-03-25 22:04:32 +00:00
|
|
|
The package is shipped with a console tool named pyhs100, please refer to ```pyhs100 --help``` for detailed usage.
|
|
|
|
<b>Note: The tool does not currently support bulb-specific commands, please feel free to prepare a pull request!</b>
|
|
|
|
|
|
|
|
## Discovering devices
|
|
|
|
|
|
|
|
```
|
|
|
|
$ pyhs100 discover
|
|
|
|
|
|
|
|
Discovering devices for 5 seconds
|
|
|
|
Found device: {'ip': '192.168.250.186',
|
|
|
|
'port': 9999,
|
|
|
|
'sys_info': {'emeter': {'get_realtime': {'current': 0.013309,
|
|
|
|
<snip>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Querying the state
|
|
|
|
```
|
|
|
|
$ pyhs100 --ip 192.168.250.186
|
|
|
|
|
|
|
|
== My Smart Plug - HS110(EU) ==
|
|
|
|
Device state: OFF
|
|
|
|
LED state: False
|
|
|
|
Time: 1970-01-01 01:52:35
|
|
|
|
On since: 2017-03-19 17:09:16.408657
|
|
|
|
Hardware: 1.0
|
|
|
|
Software: 1.0.8 Build 151101 Rel.24452
|
|
|
|
MAC (rssi): 50:C7:BF:XX:XX:XX (-61)
|
|
|
|
Location: {'longitude': XXXX, 'latitude': XXXX}
|
|
|
|
== Emeter ==
|
|
|
|
Current state: {'power': 0, 'total': 0.001, 'current': 0.013552, 'voltage': 223.394238}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Library usage
|
|
|
|
|
|
|
|
For all available API functions run ```help(SmartPlug)``` or ```help(SmartBulb)```.
|
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
|
|
|
|
|
|
|
```python
|
2017-03-25 22:04:32 +00:00
|
|
|
from pyHS100 import SmartPlug, SmartBulb
|
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
|
|
|
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)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
plug.led = False # turn off led
|
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
|
|
|
print("New LED state: %s" % plug.led)
|
|
|
|
|
|
|
|
```
|