Skip to content

Instantly share code, notes, and snippets.

@plopp
Last active July 31, 2023 15:20
Show Gist options
  • Save plopp/d16615deea55e8500dab4ca76349e9f3 to your computer and use it in GitHub Desktop.
Save plopp/d16615deea55e8500dab4ca76349e9f3 to your computer and use it in GitHub Desktop.
Improved ADAX example
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# install dependencies with: pip install requests sanction
import requests
import sanction
CLIENT_ID = "112395"
# replace with your client ID (see Adax WiFi app,Account Section)
CLIENT_SECRET = "6imtpX63D5WoRyKh"
# replace with your client ID (see Adax WiFi app,Account Section)
API_URL = "https://api-1.adax.no/client-api"
def get_token():
# Authenticate and obtain JWT token
oauthClient = sanction.Client(token_endpoint=API_URL + "/auth/token")
oauthClient.request_token(
grant_type="password", username=CLIENT_ID, password=CLIENT_SECRET
)
return oauthClient.access_token
def set_room_target_temperature(roomId, temperature, token):
# Sets target temperature of the room
headers = {"Authorization": "Bearer " + token}
json = {"rooms": [{"id": roomId, "targetTemperature": str(temperature)}]}
requests.post(API_URL + "/rest/v1/control/", json=json, headers=headers)
def get_homes_info(token):
headers = {"Authorization": "Bearer " + token}
response = requests.get(API_URL + "/rest/v1/content/", headers=headers)
if response.status_code == 200:
json = response.json()
for room in json["rooms"]:
roomName = room["name"]
targetTemperature = room["targetTemperature"] / 100.0
currentTemperature = 0
if "temperature" in room:
currentTemperature = room["temperature"] / 100.0
print(
"Room: %15s, Target: %5.2fC, Temperature: %5.2fC, id: %5d"
% (roomName, targetTemperature, currentTemperature, room["id"])
)
elif response.status_code == 429:
print("Error: Too many requests to API. Wait before making a new query.")
else:
print("Unknown response code", response.status_code)
token = get_token()
if token:
# Change the temperature to 24 C in the room with an Id of 196342
# Replace the 196342 with the room id from the get_homes_info output
set_room_target_temperature(196342, 2400, token)
get_homes_info(token)
@bborissov
Copy link

gives 400 error

@NikhilTank35
Copy link

it is working. @bborissov did you replace client id and client secret?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment