Python script in venv out of JS rule

Because tado is kicking me out, I decided to uninstall the binding, and go ahead with specific HTTP calls when needed.

I created this script, called tado-set-temperature.py:

import argparse
from libtado.api import Tado
# import webbrowser

############################################

### VARIABLES WHICH NEED TO BE SET ONCE: ###

# The path of the json file where the Tado refresh token is stored.
token_file_path = 'refresh_token.json'

# The temperature can only be set in Celsius, because that is wat libtado's API expects.
# If you want to integrate Farenheit, change the API, or integrate a calculation function.
# The below variable is therefore pointless:
# Set to 0 or 1. (0 = Celsius, 1 = Fahrenheit)
# temperature_unit_choice = 0

############################################


t = Tado(token_file_path=token_file_path)


# This variable is also pointless - see above.
# temperature_units = ["celsius", "fahrenheit"]


def argparse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--targettemp", nargs=1,
                        help="Enter the desired temperature", type=float, required=True)
    parser.add_argument("-z", "--zone", nargs=1,
                        help="Enter zone number", type=int, required=True)
    args = parser.parse_args()
    return args


def tadologin():
    status = t.get_device_activation_status()

    if status == "PENDING":
        url = t.get_device_verification_url()

        # to auto-open the browser (on a desktop device), un-comment the following line:
        # webbrowser.open_new_tab(url)

        t.device_activation()

        status = t.get_device_activation_status()

    if status == "COMPLETED":
        print("Login successful")
    else:
        print(f"Login status is {status}")


def set_temperature(zone, temperature):
    t.set_temperature(zone, temperature)


if __name__ == "__main__":
    args = argparse_args()
    tadologin()
    set_temperature(args.zone[0], args.targettemp[0])

I’m able to run it from the CLI:

openhab@MinipcLG2:~/bin/python/tado-manual-control$ ls -pal
totaal 44
drwxrwxr-x  4 openhab openhab 4096 sep 17 19:12 ./
drwxrwxr-x+ 4 erik    erik    4096 sep 17 18:15 ../
drwxrwxr-x  8 openhab openhab 4096 sep 17 18:13 .git/
-rw-rw-r--  1 openhab openhab 4688 sep 17 18:13 .gitignore
-rw-rw-r--  1 openhab openhab   21 sep 17 18:13 README.md
-rw-rw-r--  1 openhab openhab   85 sep 17 19:12 refresh_token.json
-rw-rw-r--  1 openhab openhab    7 sep 17 18:13 requirements.txt
-rw-rw-r--  1 openhab openhab 1689 sep 17 18:13 tado-back-to-schedule.py
-rw-rw-r--  1 openhab openhab 1871 sep 17 19:12 tado-set-temperature.py
drwxrwxr-x  5 openhab openhab 4096 sep 17 18:17 venv/
openhab@MinipcLG2:~/bin/python/tado-manual-control$ /var/lib/openhab/bin/python/tado-manual-control/venv/bin/python /var/lib/openhab/bin/python/tado-manual-control/tado-set-temperature.py -t 17.5 -z 2
Login successful

It works. I did have to cheat a bit, because it kept asking me to login. So I copied the refresh token from my Windows pc into refresh_token.json. But now it doesn’t ask for new logins.

But I can’t get it to work from Scratchpad (with JavaScript).

This:

var command = ['/var/lib/openhab/bin/python/tado-manual-control/venv/bin/python', '/var/lib/openhab/bin/python/tado-manual-control/tado-set-temperature.py', '-t',  '11.5', '-z', '2']
var tadomanualtempset = actions.Exec.executeCommandLine(time.Duration.ofSeconds(3), command)
console.log("tado-manual = "+tadomanualtempset)

returns:

19:19:30.052 [WARN ] [org.openhab.core.io.net.exec.ExecUtil] - Timeout occurred when executing commandLine '[/var/lib/openhab/bin/python/tado-manual-control/venv/bin/python, /var/lib/openhab/bin/python/tado-manual-control/tado-set-temperature.py, -t, 11.5, -z, 2]'
19:19:30.053 [INFO ] [enhab.automation.script.ui.scratchpad] - tado-manual = null

And what really boggles my mind: if I comment out the last two lines of the python script (so tadologin() and set_temperature(args.zone[0], args.targettemp[0])), this is the output when I run the script:

19:20:58.782 [INFO ] [enhab.automation.script.ui.scratchpad] - tado-manual = Please visit the following URL in your Web browser to log in to your Tado account: https://login.tado.com/oauth2/device?user_code=K85KSR
Waiting for you to complete logging in. You have until 2025-09-17 19:25:58

Why can’t it use the refresh token now? Furthermore, the temperature isn’t changed. Also, where’s the printed statement? What’s with the timeout if I don’t comment out tadologin()?

Thanks in advance for anyone’s guidance!

When you are running the command from the command line, are you running it as user openhab? If not that is one big different.

Another difference is you were in the tado-manual-control folder when you ran the script from the command line. But executeCommandLine runs from ~openhab which is usually /var/lib/openhab on a Linux installed system. It probably doesn’t see that refresh token file.

1 Like

Yes. As seen in the snippet :slight_smile:

Aha, I’ll change it to the absolute path in the code!

That did the trick!