Skip to content

Home Assistant Operating System

Adam edited this page Feb 22, 2024 · 2 revisions

Prepare the Virtual Environment:

sudo mkdir /config/custom_components
sudo mkdir /config/custom_components/ca350

Copy config and script from GIT https://github.com/adorobis/hacomfoairmqtt/ to /config/custom_components/ca350 /config/custom_components/ca350/ca350.py /config/custom_components/ca350/config.ini Edit config.ini as needed

python3 -m venv /config/custom_components/ca350/python3venv
source /config/custom_components/ca350/python3venv/bin/activate
pip3 install paho-mqtt==1.6.1
pip3 install pyserial
deactivate

Create /config/custom_components/ca350/ca350runner.py:

import os
import time

pid = os.fork()

if pid > 0 :
    print("I am parent process:")
    print("Process ID:", os.getpid())
    print("Child's process ID:", pid)
  
else :
    print("\nI am child process:")
    print("Process ID:", os.getpid())
    print("Parent's process ID:", os.getppid())
    
    while True:
        print("Run ca350.py")
        os.system('source /config/custom_components/ca350/python3venv/bin/activate && python3 /config/custom_components/ca350/ca350.py')
        time.sleep(15)

Edit configuration.yaml:

shell_command:
  start_ca350: python3 /config/custom_components/ca350/ca350runner.py &

Create a HA Automation to start ca350runner.py 2 minutes after HA startup:

alias: Start Pyton3 Shell Command ca350 on HA Start
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: shell_command.start_ca350
    data: {}
mode: single