Skip to content

Commit

Permalink
Update to work with new API_KEY and KEY_ID requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenly committed Aug 17, 2023
1 parent 343e976 commit e519a42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application
COPY . .

# Set environment variables for your username, password, and key
# Set environment variables for your username, password, api key, key id, and key
ENV USERNAME=""
ENV PASSWORD=""
ENV API_KEY=""
ENV KEY_ID=""
ENV TOTP=""
ENV ALWAYS_REFRESH = ""
ENV KEY=""
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# docker-wyze-plug-api
A docker container containing an API for interacting with Wyze Plugs

```
📝 As of July 2023, Wyze now requires an API_KEY and KEY_ID from the Developer API Console (see below) to be passed with all requests. If you were using this tool before then you'll need to update your container. This key/id expires every year so you'll have to manually update it for now.
```

## Overview
The following gives a brief overview of this simple project and shows how to get started, and if something doesn't make sense, the code is very simple.

Expand All @@ -9,6 +13,8 @@ Big thank you to [Shaun Tarves](https://github.com/shauntarves) and [all the con
An overview of the Docker variables used:
- USERNAME: Your Wyze username
- PASSWORD: Your Wyze password
- API_KEY: The API key given to you by Wyze from the [Developer API Console](https://developer-api-console.wyze.com/#/apikey/view), it will only show once, so make sure you save it. Note: This is not the same as the KEY which is used to authenticate incoming requests to your API.
- KEY_ID: The KEY ID given to you by Wyze from the [Developer API Console](https://developer-api-console.wyze.com/#/apikey/view)
- TOTP: The totp key given to you by Wyze when you configure an authenticator app (this program acts like an authenticator app, don't worry you can still use your authenticator app)
- ALWAYS_REFRESH: Always refresh the token each time a request is made? Defaults to True. *(optional)*
- KEY: A short key used to authenticate incoming requests to provide a small amount of security. Defaults to "mykey". *(optional)*
Expand Down
23 changes: 15 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from flask import Flask, request, jsonify
from wyze_sdk import Client
from wyze_sdk.errors import WyzeApiError
from wyze_sdk.errors import WyzeClientConfigurationError
import os

app = Flask(__name__)

# Get the API key from the environment variables
api_key = os.getenv('KEY') or 'mykey'
# Get the API password key from the environment variables
api_password = os.getenv('KEY') or 'mykey'
always_refresh = bool(os.getenv('ALWAYS_REFRESH')) or True
client = Client()

# create client
try:
print("Using environment variables for USERNAME and PASSWORD")
print("Using environment variables for USERNAME, PASSWORD, API_KEY, KEY_ID, and TOTP")
print("Get an API key/id from: https://developer-api-console.wyze.com/#/apikey/view")
username = os.getenv('USERNAME')
password = os.getenv('PASSWORD')
print("Note: Make sure the TOTP key is correct")
api_key = os.getenv('API_KEY')
key_id = os.getenv('KEY_ID')
totp = os.getenv('TOTP')
client = Client(email=username, password=password, totp_key=totp)
client = Client(email=username, password=password, totp_key=totp, api_key=api_key, key_id=key_id)

# test the API
out_test = {}
Expand All @@ -26,11 +29,15 @@
print("Connected to Wyze API")
except WyzeApiError as error:
print(f'Got an error: {error}')
except WyzeClientConfigurationError as error:
print(f'Got an error. May be caused by invalid API KEY/ID: {error}')
except Exception as error:
print(f'Got an error: {error}')


@app.route('/plug/on', methods=['GET'])
def plug_on():
if request.args.get('key') == api_key:
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
Expand All @@ -57,7 +64,7 @@ def plug_on():

@app.route('/plug/off', methods=['GET'])
def plug_off():
if request.args.get('key') == api_key:
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
Expand Down Expand Up @@ -85,7 +92,7 @@ def plug_off():
@app.route('/plug', methods=['GET'])
def plug_list():
# Check the 'X-Api-Key' header of the request
if request.args.get('key') == api_key:
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
Expand Down

1 comment on commit e519a42

@mackenly
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #2

Please sign in to comment.