-
Notifications
You must be signed in to change notification settings - Fork 58
Calling Pyton script from Command line sensor #258
-
Hi!
I'm trying to use pyscript with command line sensor.
The overall goal is to to do several HTTP requests :login at remote resourse with POST, get the cookie, then use this cookie to GET required data, then logout and pass this data to this sensor.
Sensor code in Home assistant:
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 both obviously work during reloading.
For example, this script works fine and sensor gets "Success" value:
import requests
s = requests.Session()
print("Success")
So python works, import also seems to work. But the python script cannot be called from sensor for some reason.
That should be something simple or I miss someting. Please help.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Pyscript scripts cannot run under plain Python. They require all of the infrastructure that the Pyscript Custom Component provides to run as expected.
If you want to create a sensor with pyscript, you'll have to do it entirely in pyscript (not using a command line sensor).
Beta Was this translation helpful? Give feedback.