Require help in implementing python code output in automation

hey i wrote a code that connects my computer and raspberry home assistant and sends my display status to home assistant and i want my lights to go on when i switch to display 1 and off when i switch display 2
now i get the status in the home assistant visual studio code add in the terminal
how do i take the output in the terminal and make it a condition for the automation?
im quite new to home assistant.

thanks in advance.

How does it send the display data to Home Assistant? MQTT? REST API?

What do you mean with this?

the data is sent from my visual studio in windows 10 to the visual studio add on in home assistant and you can see it in the terminal of the visual studio add on in home assistant

and i use TCP i made my computer server and my home assistant a client

Sorry I don’t understand, how do you send data from visual studio?

Instead of receiving the information in the VSCode terminal (where there is no way to forward to home assistant) setup either a restful, tcp or command line sensor in home assistant to receive the information.

well it doesnt really matter since my question is how to direct terminal output to home assistant but i send via socket TCP i use this codes:

server.py:

import socket
import threading
import win32api
import time

monitors = win32api.EnumDisplayMonitors()
win32api.GetMonitorInfo(monitors[0][0])
info = win32api.GetMonitorInfo(monitors[0][0])
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
LOOP = 1
check = True
DISPLAY = "z"

def background():
    while check:
        global DISPLAY
        monitors = win32api.EnumDisplayMonitors()
        win32api.GetMonitorInfo(monitors[0][0])
        info = win32api.GetMonitorInfo(monitors[0][0])
        if info == {'Monitor': (0, 0, 1707, 960),'Work': (0, 0, 1707,960),'Flags': 1,'Device': '\\\\.\\DISPLAY1'}:
            DISPLAY = "DISPLAY 1 on"
            time.sleep(5)
        else:
            DISPLAY = "DISPLAY 4 on"
            time.sleep(5)

b = threading.Thread(name='background', target=background)

b.start()

    


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr):
    print(f"[NEW CONNETION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            print(f"[{addr}] {msg}")
            conn.send(DISPLAY.encode(FORMAT))
            time.sleep(5)
            

    conn.close()


def start ():
    server.listen()
    print(f"[LISTENING] server is listening on {SERVER}")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")

print ("[STARTING] server is starting...")
start()

client.py

import socket
import time

HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER =  "ip address"
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
connected = True

def send(msg):
    while connected:
        message = msg.encode(FORMAT)
        msg_length = len(message)
        send_length = str(msg_length).encode(FORMAT)
        send_length += b' '* (HEADER - len(send_length))
        client.send(send_length)
        client.send(message)
        print(client.recv(2048).decode(FORMAT))
        time.sleep(5)

send("data received")

client = home assistant visual studio code add on (raspberry pi 4)
server = computer visual studio code (windows 10)

my output in terminal will be either
“DISPLAY 1 ON”
“DISPLAY 4 ON”

Yeah that’s not going to work.

  1. There is no way to get data from the VSCode terminal into home assistant.

  2. All the sensor options I linked to above require a command / response. Not a data stream. You are going to have to change your code. Look into mqtt. It can push messages to home assistant via a broker addon.

2 Likes

Alright ill look into it kinda bummer that I wrote this code for nothing thanks anyway
ill try reading about MQTT.