-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomowy.py
111 lines (96 loc) · 3.28 KB
/
automowy.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# SPDX-License-Identifier: Unlicense
import requests
class AutomowyError(Exception):
pass
class AutomowySession:
AUTH_URL_BASE = 'https://iam-api.dss.husqvarnagroup.net/api/v3/token/'
URL_BASE = 'https://amc-api.dss.husqvarnagroup.net/app/v1/'
def __init__(self):
self.session = requests.Session()
self.user = None
self.token = None
self.mowers = None
def login(self, user, password):
response = self.session.post(
self.AUTH_URL_BASE,
json={'data': {
'type': 'token',
'attributes': {'username': user, 'password': password}
}}
)
response.raise_for_status()
self.user = user
json = response.json()
self.token = json['data']['id']
provider = json['data']['attributes']['provider']
self.session.headers.update({
'Authorization': 'Bearer ' + self.token,
'Authorization-Provider': provider
})
return self
def logout(self):
response = self.session.delete(self.AUTH_URL_BASE + self.token)
response.raise_for_status()
self.token = None
del (self.session.headers['Authorization'])
def list_mowers(self):
response = self.session.get(
self.URL_BASE + 'mowers'
)
response.raise_for_status()
json = response.json()
self.mowers = [
{'id': mower['id'], 'name': mower['name']}
for mower in json
]
return json
def find_mower(self, match=None):
return Automowy(self, match)
class Automowy:
def __init__(self, session, match=None):
self.session = session
if not session.mowers:
session.list_mowers()
if not len(self.session.mowers):
raise AutomowyError('No mower for %s' % session.user)
if match:
for mower in session.mowers + [None]:
if mower is None:
raise AutomowyError('No mower matching %s' % match)
if mower['id'] == match or mower['name'] == match:
break
else:
mower = session.mowers[0]
self.name = mower['name']
self.id = mower['id']
self.url = session.URL_BASE + 'mowers/%s/' % self.id
def query(self, command):
"""
:param command: can be 'status', 'geofence' or 'settings'
"""
response = self.session.session.get(
self.url + command
)
response.raise_for_status()
return response.json()
def control(self, command, param=None):
"""
:param command: can be 'start', 'start/override/period',
'pause', 'park', 'park/duration/timer'
:param param: must be {'period':minutes} for 'start/override/period'
"""
response = self.session.session.post(
self.url + 'control/%s' % command,
json=param
)
response.raise_for_status()
return response.json()
def set(self, key, value):
"""
:param key: examples: 'cuttingHeight', 'ecoMode'
"""
response = self.session.session.put(
self.url + 'settings',
json={'settings': {key: value}}
)
response.raise_for_status()