Looking for a .py scripter to make my script better

Hi, in my house i have a domotica controller, i can send TCP commands to it to turn all my lights/switches on off, all is working perfectly , i have configured all templates lights/switches, that use a shell command…

i have also exposed my lights/switches to google assistant, all was working fine , untill this new improvement came :

before that PR: basicly what happened, is when you say to google like, turn all lights on , it sends the all commands in a sequence, so my light was turned on, one by one … no problem
After that PR/ when you say, turn all lights ON, HA just sends ON commands all together, and not in sequence anymore… my controller is not able to handle multiple requests together …

so i am tryng to write a code, like below, with some copy/paste stuff from google, but i cant figure it out, something is not right, with that try except stuff
the code is working though, when i turn 1 light on / off, but when turn on like 4 lights, only 2 are turned ON

how can i make this to work? :slight_smile:

or is there maybe another way to accomplish what i want todo ?

tnx and appreciated

#!/usr/bin/python3
import socket
import time
import binascii
import sys

host = '192.168.0.10'
port = 1001
message = binascii.a2b_hex ("ED43310000000000000000000000AFAF" + sys.argv[1].zfill(2) + sys.argv[2].zfill(2) + sys.argv[3].zfill(2))

# First try-except block -- create socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print ("Error creating socket")
    sys.exit(1)
# Second try-except block -- connect to given host/port

try:
	s.connect((host, port))
except socket.gaierror:
    print ("Address-related error connecting to server")
    s.connect((host, port))

except socket.error:
    print ("Connection error")
    s.connect((host, port))
	
except socket.timeout:
    print ("Connection timeout socket connect")
    s.connect((host, port))

# Third try-except block -- sending data
try:
    s.send(message)

except socket.error:

    print("Error sending data")
    s.send(message)
except socket.timeout:
    print ("Connection timeout send message")
    s.connect((host, port))

s.close()	
	
'''

Does each device have it’s own IP address and/or port?

No, the master controller just has an IP and a port, basically , I just send a message with netcat to it… Always on same ip and port…

That’s defined as host and port in my script… From HA I just pass arguments, those are my lights, that’s how I differentiate it

Maybe I’m being overly simple but I’ll give it a shot. I presume the sys.argv[1].zfill(2) etc are the address of the lights? If so, have you tried printing out or logging the variable values when it’s run? You can confirm the correct values are being passed in that way. I know that’s not a solution but it’s where I’d start looking if I were having the issue.

Hi, thx in advance…

Yeah those are indeed the addresses big my lights , coming from my template lights, those are correct,. Already using it 2 years… Still working also if I turn on lights one by one, or if i create a test script that turns on multiple lights, that they all turn on… Since that script is still using the sequence method…

It’s only not working from Google, since HA is using the parallel method, and my controller is not able to handle those at the same time…

For now I created a small workaround,. I have added a random delay at the start of my script, a delay between 0 and 2 sec… That Works, but it isn’t a proper solution offcourse…

Also tried script below, problem is that I don’t get a response from my controller if a message was succeeded, I can input whatever I want as the message… Otherwise I could read something with the socket, but that’s not possible… Normally the sendsll command gives back “None” when it was fired… So tried below, it works but still not in parallel…

Sometimes 2 lights, sometimes 4… It’s a little bit better


while success is not None:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.sendall(message)
    success = (s.sendall(message))
    s.close()