jeeshofone / ha-apc-ecu-3

Get homeassistant to read APC ECU-3 data
https://www.123cloud.st/p/the-unexpectedly-direct-path-to-building
4 stars 1 forks source link

Python #3

Closed rohrsh closed 4 months ago

rohrsh commented 7 months ago

Hey buddy,

Thanks for putting this together. I've been hoping to find a local method for these old ECU-3 for a while.

What is best way to run python in the Home Assistant situation? I'm using the official Home Assistant OS installation.

  1. I tried with the default Terminal add-on, but that is locked down and doesn't include python
  2. I installed the advanced Terminal & SSH add-on. This had python but I had to pip install requests and beautifulsoup4 in order to run populate-configuration.py https://github.com/hassio-addons/addon-ssh/blob/main/README.md
  3. For the regular solarhtml2json.py automation script, I first tried the Python Scripts module, but it doesn't allow imports
  4. Next I tried pyscripts, but I think that is expecting modules instead of scripts? https://hacs-pyscript.readthedocs.io/en/stable/overview.html
Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)  # noqa: S102
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "solarhtml2json.py", line 1, in <module>
    import requests
ImportError: __import__ not found
Navya-Rawat999 commented 7 months ago

Hey, there! What is home assistant?

xiazhengtao commented 7 months ago

It looks like you are trying to run Python scripts within the Home Assistant environment, and you've encountered some challenges. Let's address the issues you mentioned and discuss the best approach.

  1. Default Terminal Add-on:

    • The default Terminal add-on may not include Python, which could limit your ability to run Python scripts directly.
  2. Advanced Terminal & SSH Add-on:

    • You installed the advanced Terminal & SSH add-on, which has Python. This is a good step.
    • You had to install additional packages (requests and beautifulsoup4) using pip. This is typical for installing Python packages in Home Assistant.
  3. Python Scripts Module:

    • The Python Scripts module in Home Assistant doesn't allow imports. This limitation is intentional for security reasons to prevent arbitrary code execution.
  4. Pyscript:

    • Pyscript is a more flexible scripting environment for Home Assistant, but it expects modules rather than standalone scripts.

Given your requirements, here are some recommendations:

Remember to carefully consider the security implications of running scripts within your Home Assistant environment. Always follow best practices to minimize potential risks.

If you provide more details about the specific requirements of your Python scripts, I can offer more targeted advice.

rohrsh commented 7 months ago

Thank you ChatGPT

jeeshofone commented 7 months ago

Hey @rohrsh sorry for the delay - I'll update the readme tonight with more specific instructions

jeeshofone commented 7 months ago

Hey, there! What is home assistant?

https://www.home-assistant.io

jeeshofone commented 7 months ago
  1. I tried with the default Terminal add-on, but that is locked down and doesn't include python

Ah, good to know. I haven't tried that add-on!

  1. I installed the advanced Terminal & SSH add-on. This had python but I had to pip install requests and beautifulsoup4 in order to run populate-configuration.py

Ah, since i only needed to run this once, I just ran it locally on my laptop.

  1. For the regular solarhtml2json.py automation script, I first tried the Python Scripts module, but it doesn't allow imports
  2. Next I tried pyscripts, but I think that is expecting modules instead of scripts? https://hacs-pyscript.readthedocs.io/en/stable/overview.html
Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)  # noqa: S102
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "solarhtml2json.py", line 1, in <module>
    import requests
ImportError: __import__ not found

I am also using the official Home Assistant OS. My understanding is that the terminal add-ons and modules all run as containers in the OS and don't share any library version. I used the File Editor add-on to just put the solarhtml2json.py file in /config/ which is a path shared with the main HAOS system.

Screenshot 2023-12-14 at 8 41 21 PM

HAOS ships with compatible versions of the python libraries I used in the script, so there should be nothing else to add.

Please try that and let me know if it works!

rohrsh commented 4 months ago

Can you share the shell_command.convert_solar_data please?

jeeshofone commented 4 months ago
shell_command:
  convert_solar_data: python solarhtml2json.py

@rohrsh In the configuration.yaml file add the above line to register the shell script for convert_solar_data

rohrsh commented 4 months ago

BAM

Thank you

image
jeeshofone commented 4 months ago

Glad it worked!