Porting python 3 to groovy (2017)

Although i am not completely blind when trying to code stuff it usually takes a while for me to get my stuff to work, I have never programmed in groovy before so i thought i would seek some help. i have a python script i made that refreshes the connection between my cnct intelliplugs and amazon alexa. i have it set as a scheduled task on my pc to run every 12 hours, but i think it would work better if i could get smartthings to send this info for me so i could also add a virtual device to refresh the connection on demand if i need.

the code i use is here

import requests
import urllib
 
 
url = 'https://www.ikonkek2.com/KOAuthDemeter/Alley/authorize3'
 
login = "username@email.com here"
password = "password here"
client_id = 'client id here'
response_type = 'code'
 
values = {'client_id': client_id, 'user': login + "cnct", 'pwd': password, 'response_type': response_type}
 
r = requests.post(url, json=values)

data = r.json()

state = 'put state here'
scope = 'discoverOrControl'
uri = 'https://pitangui.amazon.com/api/skill/link/XXXXXXXXXXXX'


redir = uri + "?code=" + data['code'][0:32] + "&state=" + state + "&scope=" +scope

import urllib.request
with urllib.request.urlopen(redir) as response:
   html = response.read()

any help getting this process started would be greatly appreciated

Are you trying to launch something every 12h that your Alexa can activate?
if so, why not creating an Alexa routine directly with Alexa app and setup the recurrence inside?

Python 3 is not complete rewrite of Python; it still pretty much the good old Python you all love. But I am not going to lie. There are many changes in Python 3; many that will cause pain when you will port your code; and so many that I won’t be able to cover them all in this talk. That is why I will focus only on the changes that will need to know to port your code. If you want to learn about all new and shiny features, you will need to visit the python org’s website and the online documentation of Python 3.

Check this blog may be its could be useful to you :https://peadrop.com/blog/2009/04/05/porting-your-code-to-python-3/

You would need to make a smartapp to do what you are doing - look the Amazon dash button app for an example of handling http request/response. Not quite so straight forward and a lot of complexity for something that doesn’t look like has anything to do with ST.

TBH if you are doing this because ST hub box is always on and sitting on your LAN, and you don’t want to run your PC 24x7 to run a simple python script, then there are better and simpler approaches. @Philippe_Portes suggestion is the most logical one, or you could get $5 Raspberry Pi W and run your python code via a crown job on it and let it run 24x7 headless - just google how you do it headless and you won’t have to buy any accessories with it beyond a microsd card and microuab power dongle

1 Like

To have SmartThings trigger your Python script, create a SmartApp using the SmartThings Developer Workspace. In the SmartApp, use HTTP requests to send the required data to your Python script’s endpoint. Deploy the SmartApp and use it in your SmartThings app to refresh the connection when needed.

Because the original poster posted 6 years ago, they were looking at the old groovy architecture, which is completely different than the current 2023 platform. They would not have been able to do what you’re describing, but they would not have needed to because their code would have run in the Groovy cloud provided by smartthings. (Also, the person you are replying to has not even visited this forum since 2018.)

A huge amount has changed in the last six years, with most of the change coming in the last 12 months. I recommend not replying to posts that are older than that unless you know that your post will still be relevant. :thinking:

2 Likes