Hi Guys/Gals,
Hurray for the first post.
Problem: python_scripts has no access to specific features in python3. YMMV, but here is a patch that I made to give me access to os.system() calls (and any other os module function)
Solution: Patch your own code! Out of bands patches, whoop whoop! (Devs will not like us!)
— python_script-mod.py 2018-01-09 18:27:47.226150531 -0600
+++ /srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/python_script.py > 2018-01-09 18:26:26.360677898 -0600
@@ -140,6 +140,7 @@
builtins = safe_builtins.copy()
builtins.update(utility_builtins)
builtins[‘datetime’] = datetime
- builtins[‘os’] = os
builtins[‘sorted’] = sorted
builtins[‘time’] = TimeWrapper()
builtins[‘dt_util’] = dt_util
Change the bullet , into a + sign, silly formatting.
Save above in a file called python_scripts-mod.py.
Apply patch using:
patch < python_scripts-mod.py
And that will patch the file located here: /srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/python_script.py
If patch doesn’t work, try moving it into the components directory under /srv/homeassistant/…/components/
And you’ll now have access to:
os.system(), and other functions that home_assistant python_scripts should really not have access to!
You should probably really use the curl method as indicated below, it doesn’t meet my needs, so it wont work for what I want to do.
Original post banter:
I have a few different components that are working when I call a python_script, the only problem is I seem to lack some functionality in the Python. I’ve got some experience with python3, however it seems that hass is limited in which libraries are available for use when calling python_scripts/. Imports don’t work (as highlighted in other topics), and functions don’t seem to work properly either.
I assume the documentation for python_scripts feature is sparse likely because not many people are looking to write their own python scripts to do home automation…
Has anyone found a way to call child scripts from a python_script service? In ~/.homeassistant/ python_script /fork_py . py
I want to use as a general purpose handler for services and different events while using home assistant. I want to automated things using dynamic IRSEND commands: the HA configuration would get too convoluted to be able to do this properly (especially with integration with Amazon Alexa, and other services).os.system() doesn’t work, and system() doesn’t work. I’ve tried call() as well as run(), but none seem to work. Can someone point me in the right direction?
I’ve stumbled around the github home-assistant/home-assistant/blob/8267a21bfe74520c7c8e0a8e94114476106261d9/homeassistant/components/python_script.py
It seems to support os.* but I can’t call os.system() calls.People have gotten things to work with curl commands:
shell_command: “curl -K asdklajsdlkjsdkfjsdfklj/blah . php?var1=test&var2=apples” but I would rather not use <?php ?> if at all possible (less exposed services)
Regards,
James