This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mower_control.py
78 lines (68 loc) · 2.8 KB
/
mower_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from threading import Thread
import time
import sys
import requests
import json
# account specific values
USERNAME = 'USERNAME_GARDENA'
PASSWORD = 'PASSWORD_GARDENA'
API_KEY = 'API_KEY_GARDENA'
# other constants
AUTHENTICATION_HOST = 'https://api.authentication.husqvarnagroup.dev'
SMART_HOST = 'https://api.smart.gardena.dev'
# action options for mower
# START_SECONDS_TO_OVERRIDE - Manual operation, use 'seconds' attribute to define duration.
# START_DONT_OVERRIDE - Automatic operation.
# PARK_UNTIL_NEXT_TASK - Cancel the current operation and return to charging station.
# PARK_UNTIL_FURTHER_NOTICE - Cancel the current operation, return to charging station, ignore schedule.
DEVICE_ACTION = sys.argv[1]
def action_mower(mower, action):
payload = {
"data": {
"type": "MOWER_CONTROL",
"attributes": {
"command": action
},
"id": "does-not-matter"
}
}
r = requests.put('{}/v1/command/{}'.format(SMART_HOST, device['id']), json=payload, headers=headers)
assert r.status_code == 202, r
print("Command {} excecuted...".format(action))
response = r.json()
print(response)
if __name__ == "__main__":
payload = {'grant_type': 'password', 'username': USERNAME, 'password': PASSWORD,
'client_id': API_KEY}
print("Logging into authentication system...")
r = requests.post('{}/v1/oauth2/token'.format(AUTHENTICATION_HOST), data=payload)
assert r.status_code == 200, r
auth_token = r.json()["access_token"]
headers = {
"Content-Type": "application/vnd.api+json",
"x-api-key": API_KEY,
"Authorization-Provider": "husqvarna",
"Authorization": "Bearer " + auth_token
}
r = requests.get('{}/v1/locations'.format(SMART_HOST), headers=headers)
assert r.status_code == 200, r
assert len(r.json()["data"]) > 0, 'location missing - user has not setup system'
location_id = r.json()["data"][0]["id"]
r = requests.get('{}/v1/locations/{}'.format(SMART_HOST, location_id), headers=headers)
assert r.status_code == 200, r
assert len(r.json()["included"][0]['relationships']['services']['data']) > 0, 'no devices setup'
devices = r.json()["included"][0]['relationships']['services']['data']
for device in devices:
if(device['type'] == "MOWER"):
payload = {
"data": {
"type": "MOWER_CONTROL",
"attributes": {
"command": DEVICE_ACTION
},
"id": "does-not-matter"
}
}
r = requests.put('{}/v1/command/{}'.format(SMART_HOST, device['id']), json=payload, headers=headers)
assert r.status_code == 202, r
print("Command {} excecuted...".format(DEVICE_ACTION))