mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
05a6bbb145
* 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.
1.3 KiB
1.3 KiB
pyHS100
Python Library to control TPLink Switch (HS100 / HS110)
Usage
For all available API functions run help(SmartPlug)
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
print("Current time: %s" % plug.time)
print("Timezone: %s" % plug.timezone)
Getting and setting the name
print("Alias: %s" % plug.alias)
plug.alias = "My New Smartplug"
State & switching
print("Current state: %s" % plug.state)
plug.turn_off()
plug.turn_on()
or
plug.state = "ON"
plug.state = "OFF"
Getting emeter status (on HS110)
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
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 -m examples.cli <ip>