Hi!
I’m stuck at using pyscript with command line sensor.
The overall goal is to login at remote resourse with POST, get the cookie, then use the cookie to GET required data, then logout and pass this data to this sensor:
sensor:
- platform: command_line
name: My_sensor
command: "python3 /config/pyscript/test.py"
The script is the following:
log.warning("start script")
import requests
s = requests.Session()
task.executor(s.post, 'https://<MY_URL>/api/login', data='{"username": "MYUSER", "password": "MYPASS"}', verify=False, timeout=10)
response = task.executor(s.get, 'https://<MY_URL>/stat/sysinfo', timeout=10, verify=False)
task.executor(s.post, 'https://<MY_URL>/api/logout', timeout=10, verify=False)
log.warning(response)
print(response.json()["data"][0]["version"])
During initial compilation with debug messages it works and I get the required data, but when this script is called from sensor - the goes an error:
2021-10-22 11:08:15 WARNING (MainThread) [custom_components.pyscript.file.test] start script
2021-10-22 11:08:15 WARNING (MainThread) [custom_components.pyscript.file.test] <Response [200]>
2021-10-22 11:08:15 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/test.py
2021-10-22 11:08:17 ERROR (SyncWorker_5) [homeassistant.components.command_line] Command failed: python3 /config/pyscript/test.py
After some debugging I found out that log.warning and task.executor both cause “Command failed” error, although log.warning obviously works during reloading.
So, for example, this script works fine and sensor gets “Success” value:
import requests
s = requests.Session()
print("Success")
So python works, import also works, but pyscript extensions are not compiled when called from command line sensor.
That should be something simple, please help.