Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Calling Pyton script from Command line sensor #258

Unanswered
Ogurtsov-D asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

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).

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /