This is my first post here, if my post dont follow the "standard" you know why. And iam realy new to python and programming, i am trying to learn as i go.
I am using a script thats Controls my Husqvarna Automower
In that script there is a line that i dont understand and i would like to change the outcome of.
print(dict(mow.status()['mowerInfo']))
When i run the script i get an print out like this
{u'storedTimestamp': u'1472541846629', u'hdop': u'0.0', u'latitude': u'57.57320833333333', u'lastErrorCode': u'0', u'nextStartTimestamp': u'1472587200', u'mowerStatus': u'PARKED_TIMER', u'cachedSettingsUUID': u'c1029c29-ecd5-48bd-a27b-fa98c6985ff0', u'hostMessage': u'0', u'configChangeCounter': u'846', u'longitude': u'12.04773', u'nextStartSource': u'WEEK_TIMER', u'secondsOld': u'-1471069304597', u'gpsStatus': u'USING_GPS_MAP', u'gsmRssi': u'0', u'batteryPercent': u'100', u'connected': u'true', u'operatingMode': u'AUTO', u'lastErrorCodeTimestamp': u'0'}
I understands that this line executes the "status" function and prints the outcome, but i dont realy understand the dict and the ['mowerInfo'] and why i cant find any referens to ['mowerInfo'] anywere else in the script. As i understand there should be a dictonary in the script. But i cant find it.
And now to accual the question
Insteed of the print command, a would like to get som of the information parsed inte variables insteed.
For examples would i like to have a variable called mowerStatus and it should have the value PARKED_TIMER and a variable called batteryPercent and it should have the value 100
The script is runned by a smarthomesolution called Indigodomo and is runed on a mac using python 2.6
Anyone knows how to do that?
I have modified the script from the original
here is my modified script (with my credential XXed out)
import requests
import xmltodict
class API:
_API_IM = 'https://tracker-id-ws.husqvarna.net/imservice/rest/'
_API_TRACK = 'https://tracker-api-ws.husqvarna.net/services/'
def __init__(self):
self.session = requests.Session()
self.device_id = None
self.push_id = None
def login(self, login, password):
request = ("<login>"
" <email>%s</email>"
" <password>%s</password><language>fr-FR</language>"
"</login>") % (login, password)
response = self.session.post(self._API_IM + 'im/login',
data=request, headers={'Content type': 'application/xml'})
response.raise_for_status()
self.session.headers.update({'Session-Token': response.headers['Session-Token']})
self.select_first_robot()
def logout(self):
response = self.session.post(self._API_IM + 'im/logout')
response.raise_for_status()
self.device_id = None
del (self.session.headers['Session-Token'])
def list_robots(self):
response = self.session.get(self._API_TRACK + 'pairedRobots_v2')
response.raise_for_status()
result = xmltodict.parse(response.content)
return result
def select_first_robot(self):
result = self.list_robots()
self.device_id = result['robots']['robot']['deviceId']
def status(self):
response = self.session.get(self._API_TRACK + 'robot/%s/status_v2/' % self.device_id)
response.raise_for_status()
result = xmltodict.parse(response.content)
return result
def geo_status(self):
response = self.session.get(self._API_TRACK + 'robot/%s/geoStatus/' % self.device_id)
response.raise_for_status()
result = xmltodict.parse(response.content)
return result
def get_mower_settings(self):
request = ("<settings>"
" <autoTimer/><gpsSettings/><drivePastWire/>"
" <followWireOut><startPositionId>1</startPositionId></followWireOut>"
" <followWireOut><startPositionId>2</startPositionId></followWireOut>"
" <followWireOut><startPositionId>3</startPositionId></followWireOut>"
" <followWireOut><startPositionId>4</startPositionId></followWireOut>"
" <followWireOut><startPositionId>5</startPositionId></followWireOut>"
" <followWireIn><loopWire>RIGHT_BOUNDARY_WIRE</loopWire></followWireIn>"
" <followWireIn><loopWire>GUIDE_1</loopWire></followWireIn>"
" <followWireIn><loopWire>GUIDE_2</loopWire></followWireIn>"
" <followWireIn><loopWire>GUIDE_3</loopWire></followWireIn>"
" <csRange/>"
" <corridor><loopWire>RIGHT_BOUNDARY_WIRE</loopWire></corridor>"
" <corridor><loopWire>GUIDE_1</loopWire></corridor>"
" <corridor><loopWire>GUIDE_2</loopWire></corridor>"
" <corridor><loopWire>GUIDE_3</loopWire></corridor>"
" <exitAngles/><subareaSettings/>"
"</settings>")
response = self.session.post(self._API_TRACK + 'robot/%s/settings/' % self.device_id,
data=request, headers={'Content-type': 'application/xml'})
response.raise_for_status()
result = xmltodict.parse(response.content)
return result
def settingsUUID(self):
response = self.session.get(self._API_TRACK + 'robot/%s/settingsUUID/' % self.device_id)
response.raise_for_status()
result = xmltodict.parse(response.content)
return result
def control(self, command):
if command not in ['PARK', 'STOP', 'START']:
raise Exception("Unknown command")
request = ("<control>"
" <action>%s</action>"
"</control>") % command
response = self.session.put(self._API_TRACK + 'robot/%s/control/' % self.device_id,
data=request, headers={'Content-type': 'application/xml'})
response.raise_for_status()
def add_push_id(self, id):
request = "id=%s&platform=iOS" % id
response = self.session.post(self._API_TRACK + 'addPushId', data=request,
headers={'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
response.raise_for_status()
self.push_id = id
def remove_push_id(self):
request = "id=%s&platform=iOS" % id
response = self.session.post(self._API_TRACK + 'removePushId', data=request,
headers={'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
response.raise_for_status()
self.push_id = None
if __name__ == '__main__':
retry = 5
while retry > 0:
try:
mow = API()
mow.login("[email protected]", "xxxxxx")
print(dict(mow.status()['mowerInfo']))
retry = 0
except Exception as ex:
retry -= 1
if retry == 0:
print("[ERROR] Retrying to send the command")
else:
print("[ERROR] Failed to send the command")
exit(1)
print("Done")
mow.logout()
exit(0)
The orgiginal Project and script can bee find here
https://github.com/chrisz/pyhusmow
Thanx Martin
printbased on whatever thestatusfunction yields. The values you want to have, you actually do have in the form of dictionary keys and you can access them like so:a_dict = dict(mow.status()['mowerInfo'])and thena_dict['PARKED_TIMER']