Skip to content

Instantly share code, notes, and snippets.

@plopp
Last active July 31, 2023 15:20
Show Gist options
  • Select an option

  • Save plopp/d16615deea55e8500dab4ca76349e9f3 to your computer and use it in GitHub Desktop.

Select an option

Save plopp/d16615deea55e8500dab4ca76349e9f3 to your computer and use it in GitHub Desktop.

Revisions

  1. plopp revised this gist Apr 1, 2021. 1 changed file with 22 additions and 17 deletions.
    39 changes: 22 additions & 17 deletions adax.py
    Original file line number Diff line number Diff line change
    @@ -34,26 +34,31 @@ def set_room_target_temperature(roomId, temperature, token):
    def get_homes_info(token):
    headers = {"Authorization": "Bearer " + token}
    response = requests.get(API_URL + "/rest/v1/content/", headers=headers)
    json = response.json()

    for room in json["rooms"]:
    roomName = room["name"]
    targetTemperature = room["targetTemperature"] / 100.0
    if response.status_code == 200:
    json = response.json()

    currentTemperature = 0
    if "temperature" in room:
    currentTemperature = room["temperature"] / 100.0
    for room in json["rooms"]:
    roomName = room["name"]
    targetTemperature = room["targetTemperature"] / 100.0

    print(
    "Room: %15s, Target: %5.2fC, Temperature: %5.2fC, id: %5d"
    % (roomName, targetTemperature, currentTemperature, room["id"])
    )
    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()

    # Change the temperature to 24 C in the room with an Id of 196342
    set_room_target_temperature(196342, 2400, token)

    # Replace the 196342 with the room id from the get_homes_info output
    get_homes_info(token)
    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)
  2. plopp revised this gist Apr 1, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions adax.py
    Original file line number Diff line number Diff line change
    @@ -17,11 +17,11 @@

    def get_token():
    # Authenticate and obtain JWT token
    oauthClinet = sanction.Client(token_endpoint=API_URL + "/auth/token")
    oauthClinet.request_token(
    oauthClient = sanction.Client(token_endpoint=API_URL + "/auth/token")
    oauthClient.request_token(
    grant_type="password", username=CLIENT_ID, password=CLIENT_SECRET
    )
    return oauthClinet.access_token
    return oauthClient.access_token


    def set_room_target_temperature(roomId, temperature, token):
  3. plopp revised this gist Apr 1, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions adax.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    # install dependencies with: pip install requests sanction
    import requests
    import sanction
  4. plopp created this gist Apr 1, 2021.
    56 changes: 56 additions & 0 deletions adax.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # 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
    oauthClinet = sanction.Client(token_endpoint=API_URL + "/auth/token")
    oauthClinet.request_token(
    grant_type="password", username=CLIENT_ID, password=CLIENT_SECRET
    )
    return oauthClinet.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)
    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"])
    )


    token = get_token()

    # Change the temperature to 24 C in the room with an Id of 196342
    set_room_target_temperature(196342, 2400, token)

    # Replace the 196342 with the room id from the get_homes_info output
    get_homes_info(token)