Introduction to PythonScriptsPro for Home Assistant
PythonScriptsPro is an advanced custom component designed to enhance Home Assistant by streamlining the process of running Python scripts. It offers extended capabilities over the default python_script component, enabling users to execute scripts more efficiently and with greater flexibility.
Installation
PythonScriptsPro can be installed via the Home Assistant Community Store (HACS). Simply navigate to Integrations, select the three dots to access Custom repositories, and add the URL AlexxIT/PythonScriptsPro
under the Integration category. Alternatively, users can manually download the python_script
folder from the latest release on GitHub and place it in the custom_components
directory.
Configuration
To configure PythonScriptsPro, it’s essential to modify the configuration.yaml
file by including python_script:
(note the lack of an “S” at the end). For scripts that require additional Python libraries, these can be specified under the requirements
section within the YAML configuration.
Utilizing the python_script.exec
Service
The component introduces the python_script.exec
service, enabling the execution of:
- External Python scripts located in any directory by using the
file
parameter. - Inline Python code directly within the YAML file using the
source
parameter.
Unlike the standard python_script component, PythonScriptsPro allows importing any Python library, making it possible to integrate more complex functions and operations within scripts. Users can pass variables to scripts and benefit from compiled and cached code, ensuring faster execution on subsequent runs. If instant modifications to scripts are necessary, caching can be disabled with the cache: false
parameter.
From Home Assistant version 2023.7 onwards, the service returns script local variables with simple types as part of its response.
Running Python Scripts
From a Python File
Users can run scripts from files, such as displaying Home Assistant's startup time in a notification. This involves calling the service with a relative path to the script and additional parameters if necessary.
Inline Code Execution
Inline execution allows users to embed Python code directly in their YAML configuration. A common use case is fetching and displaying the user's IP address using the requests
library.
Advanced Examples
The project includes examples such as remotely executing SSH commands using the paramiko
library and interacting with the Home Assistant API to read states, invoke services, and fire events.
Integrating with Sensors
PythonScriptsPro enables the creation of custom sensors through Python scripts, providing options to load inline code or from files and configure sensor attributes like name
, icon
, unit_of_measurement
, and scan_interval
. The component compiles and caches the source code upon loading, necessitating a Home Assistant restart after changes to the Python files.
These sensors can perform varied tasks, such as reporting the user's IP address, database size, or determining the instance's external URL using Home Assistant's network utilities.
In summary, PythonScriptsPro enhances the scripting capabilities within Home Assistant, offering users more power and flexibility to customize their smart home environment with Python scripts. Its ease of installation, configuration, and extended functionalities make it a valuable tool for Home Assistant enthusiasts seeking more control and versatility.