-
-
Notifications
You must be signed in to change notification settings - Fork 239
Add tutorial doctest module and enable top level await #919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4805220
Add tutorial doctest module and enable top level await
sdb9696 7ecb95d
Add asyncio REPL info
sdb9696 c700e1c
Update post review and fix broken links
sdb9696 18f33dd
Try to fix readthedocs
sdb9696 23b28fc
Merge branch 'master' into janitor/add_tutorial
sdb9696 e494239
Apply suggestions from code review
sdb9696 c3254ca
Add tutorial link to the readme
sdb9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,9 @@ | |
|
|
||
| Home <self> | ||
| cli | ||
| tutorial | ||
| discover | ||
| smartdevice | ||
| device | ||
| design | ||
| contribute | ||
| smartbulb | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Tutorial | ||
|
|
||
| ```{eval-rst} | ||
| .. automodule:: tutorial | ||
| :members: | ||
| :inherited-members: | ||
| :undoc-members: | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # ruff: noqa | ||
| """ | ||
| The kasa library is fully async and methods that perform IO need to be run inside an async couroutine. | ||
|
|
||
| These examples assume you are following the tutorial inside `asyncio REPL` (python -m asyncio) or the code | ||
| is running inside an async function (`async def`). | ||
|
|
||
|
|
||
| The main entry point for the API is :meth:`~kasa.Discover.discover` and | ||
| :meth:`~kasa.Discover.discover_single` which return Device objects. | ||
|
|
||
| Most newer devices require your TP-Link cloud username and password, but this can be omitted for older devices. | ||
|
|
||
| >>> from kasa import Device, Discover, Credentials | ||
|
|
||
| :func:`~kasa.Discover.discover` returns a list of devices on your network: | ||
|
|
||
| >>> devices = await Discover.discover(credentials=Credentials("user@example.com", "great_password")) | ||
| >>> for dev in devices: | ||
| >>> await dev.update() | ||
| >>> print(dev.host) | ||
| 127.0.0.1 | ||
| 127.0.0.2 | ||
|
|
||
| :meth:`~kasa.Discover.discover_single` returns a single device by hostname: | ||
|
|
||
| >>> dev = await Discover.discover_single("127.0.0.1", credentials=Credentials("user@example.com", "great_password")) | ||
| >>> await dev.update() | ||
| >>> dev.alias | ||
| Living Room | ||
| >>> dev.model | ||
| L530 | ||
| >>> dev.rssi | ||
| -52 | ||
| >>> dev.mac | ||
| 5C:E9:31:00:00:00 | ||
|
|
||
| You can update devices by calling different methods (e.g., ``set_``-prefixed ones). | ||
| Note, that these do not update the internal state, but you need to call :meth:`~kasa.Device.update()` to query the device again. | ||
| back to the device. | ||
|
|
||
| >>> await dev.set_alias("Dining Room") | ||
| >>> await dev.update() | ||
| >>> dev.alias | ||
| Dining Room | ||
|
|
||
| Different groups of functionality are supported by modules which you can access via :attr:`~kasa.Device.modules` with a typed | ||
| key from :class:`~kasa.Module`. | ||
|
|
||
| Modules will only be available on the device if they are supported but some individual features of a module may not be available for your device. | ||
| You can check the availability using ``is_``-prefixed properties like `is_color`. | ||
|
|
||
| >>> from kasa import Module | ||
| >>> Module.Light in dev.modules | ||
| True | ||
| >>> light = dev.modules[Module.Light] | ||
| >>> light.brightness | ||
| 100 | ||
| >>> await light.set_brightness(50) | ||
| >>> await dev.update() | ||
| >>> light.brightness | ||
| 50 | ||
| >>> light.is_color | ||
| True | ||
| >>> if light.is_color: | ||
| >>> print(light.hsv) | ||
| HSV(hue=0, saturation=100, value=50) | ||
|
|
||
| You can test if a module is supported by using `get` to access it. | ||
|
|
||
| >>> if effect := dev.modules.get(Module.LightEffect): | ||
| >>> print(effect.effect) | ||
| >>> print(effect.effect_list) | ||
| >>> if effect := dev.modules.get(Module.LightEffect): | ||
| >>> await effect.set_effect("Party") | ||
| >>> await dev.update() | ||
| >>> print(effect.effect) | ||
| Off | ||
| ['Off', 'Party', 'Relax'] | ||
| Party | ||
|
|
||
| Individual pieces of functionality are also exposed via features which you can access via :attr:`~kasa.Device.features` and will only be present if they are supported. | ||
|
|
||
| Features are similar to modules in that they provide functionality that may or may not be present. | ||
|
|
||
| Whereas modules group functionality into a common interface, features expose a single function that may or may not be part of a module. | ||
|
|
||
| The advantage of features is that they have a simple common interface of `id`, `name`, `value` and `set_value` so no need to learn the module API. | ||
|
|
||
| They are useful if you want write code that dynamically adapts as new features are added to the API. | ||
|
|
||
| >>> if auto_update := dev.features.get("auto_update_enabled"): | ||
| >>> print(auto_update.value) | ||
| False | ||
| >>> if auto_update: | ||
| >>> await auto_update.set_value(True) | ||
| >>> await dev.update() | ||
| >>> print(auto_update.value) | ||
| True | ||
| >>> for feat in dev.features.values(): | ||
| >>> print(f"{feat.name}: {feat.value}") | ||
| Device ID: 0000000000000000000000000000000000000000\nState: True\nSignal Level: 2\nRSSI: -52\nSSID: #MASKED_SSID#\nOverheated: False\nBrightness: 50\nCloud connection: True\nHSV: HSV(hue=0, saturation=100, value=50)\nColor temperature: 2700\nAuto update enabled: True\nUpdate available: False\nCurrent firmware version: 1.1.6 Build 240130 Rel.173828\nAvailable firmware version: 1.1.6 Build 240130 Rel.173828\nLight effect: Party\nSmooth transition on: 2\nSmooth transition off: 2\nTime: 2024-02-23 02:40:15+01:00 | ||
| """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.