Unifi Presence (help with python)

I was using Unifi Presence with ST, and have nearly got it ported over to HE. I just have no idea about python and how to call the right commands.

I'm using the Maker API app for controlling the Virtual switch (since Maker API doesnt like presence, as per here: Maker API app error, and have change the 'home' and 'away' to 'on' and 'off'.

I've worked out the Unifi portion, and I've worked out what needs to be entered in HE, but I'm not sure how to send the command from python to HE.

This is the command that I need to send to HE. I've tested it in a web browser and it works, changing the switch from off to on.

http://[HUB ip]/apps/api/[maker API]/devices/[device ID]/on?access_token=[access token]

Previously, I've been using python to send a command to ST, but that code no longer works. How on earth do I send that command to HE in python. This is the snippet of code from python that I've modified. I realise that many of the definitions refer to ST, but that's mostly irrelevant, since in the configuration file whilst they are a ST naming convention, they point to HE. This is the snippet of code. The other parts of the code login to the Unifi controller and get presence based on mac address.

Original Code
def updateSmartThings(urlStShardbase, appID, AccessToken, currStatus):
if currStatus == 'Present':
status = 'home'
else:
status = 'away'
url = urlStShardbase + "/api/smartapps/installations/" + appID + "/Phone/" + status + "?access_token=" + AccessToken
# print url
try:
r = requests.get(url)
except requests.exceptions.RequestException as e:
print e
sys.exit("SmartThings Update Failed")
if not r.status_code == requests.codes.ok:
print datetime.datetime.now(), "SmartThings update failed with status code ", r.status_code
if int(r.status_code) == '500':
print datetime.datetime.now(), "Is this really a Virutal Presence Sensor?"

Modified code
def updateSmartThings(urlStShardbase, appID, AccessToken, currStatus):
if currStatus == 'Present':
status = 'on'
else:
status = 'off'
url = http://[HUB ip]/apps/api/[maker API]/devices/[device ID]/on?access_token=[access token]
# print url
try:
r = requests.get(url)
except requests.exceptions.RequestException as e:
print e
sys.exit("SmartThings Update Failed")
if not r.status_code == requests.codes.ok:
print datetime.datetime.now(), "SmartThings update failed with status code ", r.status_code
if int(r.status_code) == '500':
print datetime.datetime.now(), "Is this really a Virutal Presence Sensor?"

It even fails if I modify the line that begins url = ???? to url = http://[HUB ip]/apps/api/[maker API]/devices/[device ID]/on?access_token=[access token]

Any suggestions? All I really need, I think, it what code to change the url =

Paging @Navat604 since you indicated some interest in this project some months ago. But love help from anyone.

Just in case anyone is wondering:

urlStShardbase = 'http://local IPv4 address'

Should I include :39501 at the end? I tried that, but it didnt succeed.

Might it have anything to do with the url is a local IP address, not a URL?

If so, anyone point out a fix?

Got it working. Might tidy up the code and release it. First for me!

2 Likes

can you share your code?

Yep, let me tidy it up, it's full of references to SmartThings, and wont make sense to anyone. I'll do it over the weekend.

Requirements: 1. device to run a python script, 2. on same local network as the Ubiquiti controller (have not tested cloud keys, as I dont have one, might be possible though).

1 Like

What's the latest on this? Do you have this working?

Yes, try here.