### Install Paramiko Library Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md Configure the component to automatically install the 'paramiko' library if it's not already present. This is useful for environments like Hass.io or Docker where external libraries might not be pre-installed. ```yaml python_script: requirements: - paramiko>=2.7.1 ``` -------------------------------- ### Run Python Script from File Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md Execute a Python script located in the Home Assistant configuration directory. This example demonstrates passing a variable 'time_val' to the script and disabling the cache for immediate updates. ```yaml script: test_file: sequence: - service: python_script.exec data_template: # use `data_template` if you have Jinja2 templates in params file: path_to/test_file.py # relative path from config folder cache: false # disable cache if you want change python file without reload HA title: Python from file test time_val: "{{ states('sensor.start_time')|round }}" ``` -------------------------------- ### Execute Remote SSH Command Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md Run a command on a remote SSH server. This example configures connection details and the command to execute, then logs the response. It requires the 'paramiko' library. ```yaml script: ssh_command: sequence: - service: python_script.exec data: file: path_to/ssh_command.py host: 192.168.1.123 # optional user: myusername # optional pass: mypassword # optional command: ls -la ``` -------------------------------- ### Run Python Script from Inline Source Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md Execute Python code directly within the YAML configuration. This example uses the 'requests' library to fetch the public IP address and display it in a notification. ```yaml script: test_source: sequence: - service: python_script.exec data: title: Python inline test source: | import requests r = requests.get('https://api.ipify.org?format=json') resp = r.json() logger.debug(resp) hass.services.call('persistent_notification', 'create', { 'title': data['title'], 'message': f"My IP: { resp['ip'] }" }) ``` -------------------------------- ### Create a Python Script Sensor for Database Size Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md This sensor configuration calculates the size of the Home Assistant database file in MB. It logs a debug message and updates hourly. Ensure the 'os' module is available. ```yaml - platform: python_script name: My DB size icon: mdi:database unit_of_measurement: MB scan_interval: '01:00:00' # optional source: | import os logger.debug("Update DB size") filename = self.hass.config.path('home-assistant_v2.db') self.state = round(os.stat(filename).st_size / 1_000_000, 1) ``` -------------------------------- ### Create a Python Script Sensor for Instance External URL Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md This sensor retrieves the external URL for the Home Assistant instance using the network helper. It includes error handling for cases where no URL is available and updates hourly. ```yaml - platform: python_script name: Instance external url # more info https://developers.home-assistant.io/docs/instance_url/ scan_interval: '01:00:00' # optional source: | from homeassistant.helpers import network try: self.state = network.get_url( self.hass, allow_internal=False, ) except network.NoURLAvailableError: raise MyInvalidValueError("Failed to find suitable URL for my integration") ``` -------------------------------- ### Create a Python Script Sensor for Public IP Address Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md This configuration defines a Home Assistant sensor that fetches and displays the public IP address using the requests library. It updates every 5 minutes. ```yaml sensor: - platform: python_script name: My IP address scan_interval: '00:05:00' # optional, default: 30s source: | import requests r = requests.get('https://api.ipify.org?format=json') self.state = r.json()['ip'] ``` -------------------------------- ### Read States, Call Services, and Fire Events with HASS API Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md This script demonstrates reading states and attributes, calling services, and firing events using the Home Assistant API within a Python script. It's useful for complex automations or custom logic. ```python state1 = hass.states.get('sensor.start_time').state name1 = hass.states.get('sensor.start_time').attributes['friendly_name'] if float(state1) < 30: hass.services.call('persistent_notification', 'create', { 'title': "My Python Script", 'message': "Home Assistant started very quickly" }) hass.states.set('sensor.start_time', state1, { 'friendly_name': f"Fast {name1}" }) else: hass.services.call('persistent_notification', 'create', { 'title': "My Python Script", 'message': "Home Assistant was running for a very long time" }) hass.states.set('sensor.start_time', state1, { 'friendly_name': f"Slow {name1}" }) hass.bus.fire('my_event_name', { 'param1': 'value1' }) ``` -------------------------------- ### Python Script for File Execution Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md A Python script that logs received data and creates a persistent notification in Home Assistant. It uses the 'hass' and 'data' objects available within the script's execution context. ```python logger.debug(data) hass.services.call('persistent_notification', 'create', { 'title': data['title'], 'message': f"Home Assistant starts in { data['time_val'] } seconds" }) out1 = 123 # some var for service respond ``` -------------------------------- ### Python Script for Remote SSH Command Execution Source: https://github.com/alexxit/pythonscriptspro/blob/master/README.md A Python script that connects to a remote host via SSH using 'paramiko', executes a given command, and logs the output. It retrieves connection details and the command from the 'data' object passed to the service. ```python from paramiko import SSHClient, AutoAddPolicy host = data.get('host', '172.17.0.1') port = data.get('port', 22) username = data.get('user', 'pi') password = data.get('pass', 'raspberry') command = data.get('command') client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) client.connect(host, port, username, password) stdin, stdout, stderr = client.exec_command(command) resp = stdout.read() stderr.read() client.close() logger.info(f"SSH response:\n{ resp.decode() }") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.