here is my config, i am not a programmer at all, so if your code is better, please post your result 
anyway, i my house i have an IP controller board to turn on lights/switches/covers/ventilation, some kind of domitica system
so i wanted to control this also with HA, so thats why i needed to get the state of switches/stuff, so it was als represented in HA
i dont think you can retrieve a status with netcat, its only to send, so thats why i use a python for that
anyway, here is an example of what i did
# SENDING CONFIG
# i need to send a long HEX string to turn something on/off; see the last bits 00 or 01
- platform: template
switches:
sc_living:
friendly_name: Living
command_on: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x01" | nc 192.168.0.10 1001
command_off: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x00" | nc 192.168.0.10 1001
#RECEIVING config
# created a py file here
sensor:
- platform: command_line
command: python3 /config/dobissreceive.py
name: Prog
scan_interval: 10
#so now my sensor contains the state of my relias
#so in my template lights/switches the state will change, or you can use automations whatever with a value template
#fox example, if you want to know if 001100000 , the 3th and 4th bit is ON ? 11 =>
#in template light/switch =>
value_template: "{{ states.sensor.Prog.state[2:4] == '11' }}"
#or in automations:
- entity_id: sensor.Prog
platform: state
condition:
- condition: template
value_template: "{{ states.sensor.Prog.state[2:4] == '11' }}"
script below :(dobissreceive.py)
#!/usr/bin/python3
import socket
import codecs
import binascii
host = '192.168.0.10'
port = 1001
message = binascii.a2b_hex ("your string here")
mySocket = socket.socket()
mySocket.connect((host,port))
mySocket.send(message)
data = mySocket.recv(1024)
data = binascii.hexlify(data).decode()
print (data)
mySocket.close()