Run a Python Script on the VM's host OS as part of automation

Hi all,

I’m really stumped with this one.

My current setup is HA installed as a VM using Oracle’s VirtualBox, which runs on an always on windows 7 machine.

I have written a Python script that uses argprase, which allows me to pass through a parameter when the script is called, which I would like to use in automations. It’s simply to change the input device on a Hyperion LED setup and I want this to link to my TV’s source.

I am planning to create an automation where by when the TV source changes the script is called and runs on the windows OS (I would run it within HA but it utilises library imports making things more complicated).

The part I am struggling with is linking a .py file to HA, I just want to be able to say when the TV changes to HDMI the following script runs, so in command prompt that would be:

python led_changer.py {source}

with the source being dynamic and also changing part of the script using argparse. The only source options would be LiveTV and HDMI.

I have tried setting up a shared folder with the py file in, in Virtualbox and mounting it but I cant find it in HA’s terminal.

To simplify, is there a way to run a python script as part of an automation in HA?

Thanks.

I’ve not used it, but there is a Python Scripts integration. However, there is also a Hyperion integration - will that not do what you want?

To run python commands, you can also use the “shell command” integration, and then you can pass the arguments in your automation too

To make sure the imports are working, test it first from the ssh addon

Hi both, thanks for the posts.

@michaelblight

Unfortunately the Python Scripts integration does not allow imports:

It is not possible to use Python imports with this integration.

I have Windows setup with all the packages (imports) I need installed (namely Selenium), but running the script with that integration would mean it runs within the VM, which wouldn’t work as Selenium needs to open up a web browser.

To give some more detail I have a fork of the Hyperion Integration installed via HACS, which is called HyperHDR. While this does expose certain attributes of the setup, it does not allow you to directly control the input device, I checked with the integration dev, and then also the dev of the original project and it appears its simply not built for a setup like mine.

@pergola.fabio

I have been playing with the Shell Command integration and I can pass the arguments through, but it’s the same issue, from what I understand all scripts would run within the VM, so that blocks Selenium from running. So I’m stuck with how can I install the library I need, I dont even know if the VM has pip installed.

Here is what I added to my config file:

shell_command:
  run_python_script: "python3 led_changer.py {{ source_name }}"

and here is the output in Dev Tools:

I guess my only options now would be building a custom integration (seems overkill), trigger the script using a HTTP request (seems unsecure), or trying to use an API.

I just thought that there would be a simple way to run a script outside of your VM or at least allow HA to mount shared folders within the VM…

Thanks both.

if the libabry is not present, you can add it, i do that to with an automation on restart HA everytime
with the command “apk add …” since its Alpine OS

you can try first from the SSH conatiner, try first there the "apk add … " command
if that works, you also need to add it to HA container, that can be done with an automation

Ohh interesting I didnt realise you could install libraries to HA directly, but from what your saying its not permanent, hence the need for an automation to run the command each time HA boots?

My docker/container knowledge is non-existent so will require so research on my part there. I’m doubtful this would work though, when my script runs it opens up Chrome within windows and does the browsing for you, its basically a very hacky way to change a setting in the HyperHDR web UI. So even if I could get the library installed I dont think there would be a way of running a web browser within the container.

Thanks for the post, though, really appreciate you taking a look at this.

Cheers, George.

its indeed not persistent, on a update its indeed gone, dont think its gone on a reboot
thats why i have the “apk add …” command in an automation on start event
Then my python commands are just working fine

not sure about browser, dont think it will work offcourse