Skip to content
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

Add production sample script #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ hc.get_token(auth_result)
# list the existing appliances
hc.get_appliances()
```
## Examples
### Simulator example

To test with the simulator, you can just use the sample file [examples/simulator_events.py](examples/simulator_events.py). First you need to prepare your Home Connect Developer Account as described [here](https://api-docs.home-connect.com/quickstart/?#authorization). Check out this repository and make sure you have the library installed in your python environment (`pip install -e .`). Afterwards you can run the `simulator_events.py` file. It will ask you for the Client ID, insert the one for `API Web Client` from the Applications page in your developer dashboard. A login page will be opened in your browser that requires you to login to your Home Connect account and grant access. After your approval, it will forward you to the quickstart guide. From this page you need to copy the full URL (it includes the authorization code) and paste it to the console that is running the `simulator_events.py`. That's already it. In the Home Connect Dashboard you can switch to the "Simulators" tab, play with the devices and see the events coming in your python console.

Note: In case the script fails with a message like "HomeAppliance is offline" make sure your devices in the simulator are marked as "Connected" and retry.

### Production environment example

If you want to test with your real devices, checkout the [examples/prod_events.py](examples/prod_events.py) script. You will also need a developer account (as mentioned above) as well as a regular homeconnect account (singlekey-id). In your developer dashboard you should create a new new application of the type "Authorization Code Grant Flow". Run the script and enter your project credentials, all data from your devices will be printed on the console.

## Disclaimer

Expand Down
28 changes: 28 additions & 0 deletions examples/prod_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from homeconnect import HomeConnect
import webbrowser


def print_status(app):
print(app.name, app.status)

if __name__ == '__main__':
client_id = input("Please enter the client ID: ")
client_secret = input("Please enter the client secret: ")
redirect_uri = input("Please enter your redirecturl (empty -> https://example.com): ")
if redirect_uri == "":
redirect_uri = 'https://example.com'

hc = HomeConnect(client_id, client_secret, redirect_uri)

webbrowser.open(hc.get_authurl())

auth_result = input("Please enter the URL redirected to: ")

hc.get_token(auth_result)

appliances = hc.get_appliances()

for app in appliances:
app.get_status()
print_status(app)
app.listen_events(callback=print_status)
8 changes: 5 additions & 3 deletions examples/simulator_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
import webbrowser
import time

SIMULATOR_API = 'https://simulator.home-connect.com'

def print_status(app):
print(app.name, app.status)



if __name__ == '__main__':
client_id = input("Please enter the client ID: ")
client_secret = input("Please enter the client secret: ")
redirect_uri = input("Please enter the redirect URI: ")
client_secret = '' # not necessary for the simulator
redirect_uri = 'https://api-docs.home-connect.com/quickstart/' # required by the api, all other values are blocked

hc = HomeConnect(client_id, client_secret, redirect_uri, simulate=True)
hc = HomeConnect(client_id, client_secret, redirect_uri, api_url=SIMULATOR_API)

webbrowser.open(hc.get_authurl())

Expand Down