mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-13 10:58:02 +00:00
Simplify API documentation by using doctests (#73)
* Add doctests to SmartBulb * Add SmartDevice doctests, cleanup README.md * add doctests for smartplug and smartstrip * add discover doctests * Fix bulb mock * add smartdimmer doctests * add sphinx-generated docs, cleanup readme a bit * remove sphinx-click as it does not work with asyncclick * in preparation for rtd hooking, move doc deps to be separate from dev deps * pytestmark needs to be applied separately for each and every file, this fixes the tests * use pathlib for resolving relative paths * Skip discovery doctest on python3.7 The code is just fine, but some reason the mocking behaves differently between 3.7 and 3.8. The latter seems to accept a discrete object for asyncio.run where the former expects a coroutine..
This commit is contained in:
@@ -23,53 +23,69 @@ TPLINK_KELVIN = {
|
||||
class SmartBulb(SmartDevice):
|
||||
"""Representation of a TP-Link Smart Bulb.
|
||||
|
||||
Usage example:
|
||||
```python
|
||||
p = SmartBulb("192.168.1.105")
|
||||
await p.update()
|
||||
To initialize, you have to await :func:`update()` at least once.
|
||||
This will allow accessing the properties using the exposed properties.
|
||||
|
||||
# print the devices alias
|
||||
print(p.alias)
|
||||
All changes to the device are done using awaitable methods,
|
||||
which will not change the cached values, but you must await :func:`update()` separately.
|
||||
|
||||
# change state of bulb
|
||||
await p.turn_on()
|
||||
await p.update()
|
||||
assert p.is_on
|
||||
await p.turn_off()
|
||||
|
||||
# query and print current state of plug
|
||||
print(p.state_information)
|
||||
|
||||
# check whether the bulb supports color changes
|
||||
if p.is_color:
|
||||
print("we got color!")
|
||||
# set the color to an HSV tuple
|
||||
await p.set_hsv(180, 100, 100)
|
||||
await p.update()
|
||||
# get the current HSV value
|
||||
print(p.hsv)
|
||||
|
||||
# check whether the bulb supports setting color temperature
|
||||
if p.is_variable_color_temp:
|
||||
# set the color temperature in Kelvin
|
||||
await p.set_color_temp(3000)
|
||||
await p.update()
|
||||
|
||||
# get the current color temperature
|
||||
print(p.color_temp)
|
||||
|
||||
# check whether the bulb is dimmable
|
||||
if p.is_dimmable:
|
||||
# set the bulb to 50% brightness
|
||||
await p.set_brightness(50)
|
||||
await p.update()
|
||||
|
||||
# check the current brightness
|
||||
print(p.brightness)
|
||||
```
|
||||
|
||||
Errors reported by the device are raised as SmartDeviceExceptions,
|
||||
Errors reported by the device are raised as :class:`SmartDeviceException`s,
|
||||
and should be handled by the user of the library.
|
||||
|
||||
Examples:
|
||||
>>> import asyncio
|
||||
>>> bulb = SmartBulb("127.0.0.1")
|
||||
>>> asyncio.run(bulb.update())
|
||||
>>> print(bulb.alias)
|
||||
KL130 office bulb
|
||||
|
||||
Bulbs, like any other supported devices, can be turned on and off:
|
||||
|
||||
>>> asyncio.run(bulb.turn_off())
|
||||
>>> asyncio.run(bulb.turn_on())
|
||||
>>> asyncio.run(bulb.update())
|
||||
>>> print(bulb.is_on)
|
||||
True
|
||||
|
||||
You can use the is_-prefixed properties to check for supported features
|
||||
>>> bulb.is_dimmable
|
||||
True
|
||||
>>> bulb.is_color
|
||||
True
|
||||
>>> bulb.is_variable_color_temp
|
||||
True
|
||||
|
||||
All known bulbs support changing the brightness:
|
||||
|
||||
>>> bulb.brightness
|
||||
30
|
||||
>>> asyncio.run(bulb.set_brightness(50))
|
||||
>>> asyncio.run(bulb.update())
|
||||
>>> bulb.brightness
|
||||
50
|
||||
|
||||
Bulbs supporting color temperature can be queried to know which range is accepted:
|
||||
|
||||
>>> bulb.valid_temperature_range
|
||||
(2500, 9000)
|
||||
>>> asyncio.run(bulb.set_color_temp(3000))
|
||||
>>> asyncio.run(bulb.update())
|
||||
>>> bulb.color_temp
|
||||
3000
|
||||
|
||||
Color bulbs can be adjusted by passing hue, saturation and value:
|
||||
|
||||
>>> asyncio.run(bulb.set_hsv(180, 100, 80))
|
||||
>>> asyncio.run(bulb.update())
|
||||
>>> bulb.hsv
|
||||
(180, 100, 80)
|
||||
|
||||
If you don't want to use the default transitions, you can pass `transition` in milliseconds.
|
||||
This applies to all transitions (turn_on, turn_off, set_hsv, set_color_temp, set_brightness).
|
||||
The following changes the brightness over a period of 10 seconds:
|
||||
|
||||
>>> asyncio.run(bulb.set_brightness(100, transition=10_000))
|
||||
|
||||
"""
|
||||
|
||||
LIGHT_SERVICE = "smartlife.iot.smartbulb.lightingservice"
|
||||
|
Reference in New Issue
Block a user