Controoling HusqvarnaAutomower

Posted on
Sun Jul 31, 2016 10:08 am
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Controoling HusqvarnaAutomower

Hi
I have Automower 430x with the connect option and i found this
https://github.com/chrisz/pyhusmow

So i tested it using the terminal and i got it to work, now i would like to integrate it to Indigo but i have some questions

I have no experience with Python so bare with me.

As i understand we have a python file with some information husmow.py and then we have some python ”commands” and the commands links to the husmow.py file

for example
python husmow.py --login yourmaillogin --password yourpassword control START

First of all is it possible to use these commands as embedded python scripts or should i use another approach?

Does it matters were i put the husmow.py file and how do i convert the command so the file is found?

I tried everything i can think of, but i can't get it to work, i always get embedded script invalid syntax

And if i get STOP, PARK, START to work i was thinking of getting the status of different data into variables, there is a command STATUS were when i run it in terminal i get an answer were all the data comes up.

{ u'batteryPercent': u'100',
u'cachedSettingsUUID': u'83225f5b-2c0d-4626-b156-73add99d65d3',
u'configChangeCounter': u'422',
u'connected': u'true',
u'gpsStatus': u'USING_GPS_MAP',
u'gsmRssi': u'0',
u'hdop': u'0.0',
u'hostMessage': u'0',
u'lastErrorCode': u'0',
u'lastErrorCodeTimestamp': u'0',
u'latitude': u'56.573405',
u'longitude': u'11.047763333333334',
u'mowerStatus': u'PARKED_TIMER',
u'nextStartSource': u'WEEK_TIMER',
u'nextStartTimestamp': u'1470081600',
u'operatingMode': u'AUTO',
u'secondsOld': u'-1468507263308',
u'storedTimestamp': u'1469977240556'}

Is there a way to turn this is to variables?

Thanx for all the help i can get

Martin

Posted on
Mon Aug 08, 2016 4:59 am
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Re: Controoling HusqvarnaAutomower

Hi again
I have been Reading trying to solve this my self, but i still need some help.

If i understand correctly, this script uses argparse and thats a program used to make a script easy to Control using the terminal, so its not possible to use this in a embedded script

So is it possible to make Indigo write a Terminal line? Like this

python2.6 husmow.py --login mail@mail.com --password xxxxxx Control START

or can i translate this somehow so i can use this in a script?

EDIT i found the answer, using apple script

Code: Select all
do Shell script "python2.6 husmow.py --login mail@mail.com --password xxxxxx Control START"


did the trick



Thanx /Martin

Posted on
Mon Aug 08, 2016 6:27 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Controoling HusqvarnaAutomower

Glad you figured out your issue. I may have to hate you just a bit out of jealousy, I really want one of those mowers but just cannot justify the cost of them.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Mon Aug 08, 2016 11:42 am
roussell offline
User avatar
Posts: 1108
Joined: Aug 18, 2008
Location: Alabama

Re: Controoling HusqvarnaAutomower

Colorado4Wheeler wrote:
Glad you figured out your issue. I may have to hate you just a bit out of jealousy, I really want one of those mowers but just cannot justify the cost of them.

Same here - I have this exact mower on my wishlist, but life keeps getting in the way. Hopefully one of these days...

Gusten wrote:
I have Automower 430x with the connect option and i found this...

Keep posting your updates and maybe a review of the mower. I'm really interested in your progress!

Terry

Posted on
Tue Aug 09, 2016 4:06 am
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Re: Controoling HusqvarnaAutomower

I can now start, stop and park the Automower.

But i do not get any feedback so i cant get the status in to indigo.

If anyone can point me in the right direction how to get the feedback lines so i can try to get the status in to indigo.

When i enter the commands in terminal i can see the answer from the device, but when i run the script i dont get any feedback.


I will report back with an rewiew when i have run the mower for a while to give you pro and cons (I have only had it for a couple of weeks)

/Martin

Posted on
Sun Aug 28, 2016 10:48 am
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Re: Controoling HusqvarnaAutomower

Hi again i need som help
I Ihave now figured out this work, and i have converted the script so i have one script for every function i would like to do.

The script runs in terminal, but i get a warning

Code: Select all
Library/Python/2.6/site-packages/requests-2.9.1-py2.6.egg/requests/packages/urllib3/util/ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/Library/Python/2.6/site-packages/requests-2.9.1-py2.6.egg/requests/packages/urllib3/util/ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/Library/Python/2.6/site-packages/requests-2.9.1-py2.6.egg/requests/packages/urllib3/util/ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning


After the warning the script does it job as it should.

When running the script in indigo (Using execute script from file)

i get this error in the indigo log

Code: Select all
Script Error                    Error in plugin execution compileExecuteSource:

Traceback (most recent call last):
  File "plugin.py", line 208, in compileExecuteSource
  File "plugin.py", line 188, in _compileExecuteSource
  File "<string>", line 157, in _dynamicFunc
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.py", line 337, in __call__
SystemExit: 0



But it starts the mower so the command get sent away.

BUT i don't get the DONE printed in the log.


Anyone know why i get this error in Indigo, has it anything todo with the error i get in terminal? and how do i fix it?

Here is the code (with my credential XXed out)

Code: Select all
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("xxxxxxx@xxxxx.com", "xxxxxxx")

            mow.control("START")
           
            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)

    indigo.server.log("Done")

    mow.logout()

    exit(0)


Thanx for any help


/Martin

Posted on
Mon Aug 29, 2016 4:07 am
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Re: Controoling HusqvarnaAutomower

It seems like if i remowe the last line

exit(0)

Efterything is working without any error.

What does exit(0) acctual do?

/Martin

Posted on
Mon Aug 29, 2016 6:06 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Controoling HusqvarnaAutomower

I guess it's terminating the python script (which would normally be what you want) but Indigo isn't expecting the script to terminate itself so throws an error.


Sent from my iPhone using Tapatalk

Posted on
Mon Sep 05, 2016 1:49 pm
Gusten offline
Posts: 171
Joined: Dec 30, 2015
Location: Sweden, Gothenburg

Re: Controoling HusqvarnaAutomower

I put up my solution here
viewtopic.php?f=123&t=16784

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests