Sending TCP packets to a device


#1

someone knows how or if its even possible to configure home assistant to send a TCP packet with a payload to an ip/port? i can’t even find something about it.


#2

shell command to netcat?


#3

Example?
What I would like to do is send a packet to turn device on.
Then ha checks status, then a switch for that device gets set to on.
The reverse for off


#4

Why TCP?

Have you looked at MQTT if you want a bus for sending and receiving messages?

I only ask as you seem to only want TCP for a way of sending messages to and from homeassistant.


#5

I think you need to define your problem a bit further. Sending a “packet” to a device is a pretty vague description.

Most devices will use a standard tcp or udp protocol, tell us details of what you are trying to do.


#6

Cool, here goes:
I own a new gas fireplace and it comes with wifi.
I’m pretty tech savvy so I’ve found out which wifi controller it uses. It’s a lantronix xpico and it’s listening on port 2000 for tcp packets.
The fireplace comes bundled with an android app, I’ve managed to capture the hex value of the packets being send to switch the relay on/off
I’ve used a pc to send those packets to see if it will turn the fireplace on and off. This works fine.
Now I would like to have some sort of switch in HA where I can switch it on and off, ultimately I want to be able to control that switch via Google home by calling its name.


#7

Put something like
shell_command: !include shell_commands.yaml
into your configuration.yaml and add the commands to shell_commands.yaml like so:

fireplace_on: echo -e "\xB1\xB2...." | nc <host> <port>

This allows you to call shell_command.fireplace_on from an automation.
The command uses netcat (nc) to open a TCP connection to the host/port and sends the byte sequence “B1,B2” to that port.

To have a switch in the UI, create an input_boolean and add an automation that triggers when its state changes.
In the automation’s action part, you call the shell command.

Sebastian


#8

or a slight enhancement - if it is an on/off switch you want … use a command line switch: https://www.home-assistant.io/components/switch.command_line/

Same principles but you can set on & off in the same component and it will appear as a switch.


#9

i used a combination of both ideas.
far easier than i thought.

i created a command line switch and made that send the byte sequence to port 2000 when i call the on or off state.

now all i need to find out is how to make that switch status aware.
since i also have an android app which can also turn on the fireplace and a RF remote control which can.
for ex: if i turn on the fireplace on or off by means of the phone app or the remote control, HA needs to know if the fireplace is either ON or OFF i am guessing this is done by polling the state of the device and then interpreting the received byte string.

right?


#10

Hi, first post here.
I write because i have similar problem and did same aproach.
I only need to send a command to a ip:port to open a door that automatically close, but happend that the comand in HEX contains non-printable caracters and when i try to send the command in HA it give me an error "expected valid JSON character at line "

ERROR:homeassistant.util.yaml:unacceptable character #x0001: special characters are not allowed

What can i do


#11

Please post your exact code as the crystal ball is not working this week.


#12

There’s no such thing as unprintable characters.
You need to let the underlying Linux console send the data to the ip:port with the net command.
It expects a hex bytesequence like a0/f3/e9
You’re trying to send hex data with a json query directly from HA. That doesn’t work.


#13

Here is a command state switch…

- platform: command_line
  scan_interval: 10
  switches:
    light_led:
      command_on: echo -n 11 | nc 192.168.1.100 6722
      command_off: echo -n 21 | nc 192.168.1.100 6722
      command_state: echo -n 00 | nc -q 1 <ip_addr> <port>
      value_template: "{{ value[0:1] == '1' }}"
      friendly_name: LED

#14

I am also attempting to accomplish something quite similar to what is being posted on this thread. I am trying to send a simple TCP/IP command to my TV via SSH.

I am able to use a putty session and make a raw connection to my TV to control it. This is a Sharp Aquos TV which supports TCP/IP control and or RS232 Serial control. I have been unable to get the Aquos component operational. I am able to load it but it only offers a power button. Further the power button when i turn off the TV only turns the TV into some form of standby and remains in a somewhat back lit state on the screen. The protocol Sharp makes available supports about any command I can think of except for 1: it will not power on the TV when TV is powered off.

I have tested the operation of all the commands I wish to use and they work wonderfully using putty from a mac using a RAW connection. It fails any other method attempted which includes: Telnet, Rlogin, SSH and Serial. I am using the assigned ip address of the Sharp TV and the port of 10002 which is default and I have kept it that in the TV settings. I put all of this information in homes I avoid the suggestions to use the Sharp Aquos component as this hasn’t worked for me.

My goal is to mainly allow the switching of inputs from HDMI 1 to HDMI 2 to HDMI 3 as needed. This is mainly the only functionality I am seeking as I use a soundbar on one TV and a yamaha receiver on the other. I am able to force the TV on using the Yamaha receiver through HDMI CEC on one TV and the same method with an Apple TV on the other TV. So I have a sure fire way of automating the power on feature of the TV. So once it is on I want to use command_line to send the TCP/IP protocol to switch inputs or other settings I may wish to use.

The IP protocol for the Sharp for setting input is 8 characters. "IAVD# ".
The # would represent the number of the corresponding input and it follows with 3 spaces.

I wish to send: "IAVD3 " via ssh. I have attempted:

echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc 192.xxx.x.xxx 10002
This continually got me a .connection closed which i have gotten through
a direct connection before using an invalid connection method.

I did some reading and came across the idea to use the -u flag:
echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc -u 192.xxx.x.xxx10002
this had no effect but it appeared like the command worked from my SSH
connection into my hass.io port 22. It was ready for me to send my next command. However there was no change to the TV.

Now the good news I can report is it appears it is connected successfully to the TV. I attempted to open a new putty session to connect direct to the TV. It is showing Service Busy. Connection closed. This usually occurs when something is already using the port as only one connection is allowed.

As shown above I am using hex for the command inside quotes. I have also tried raw text such as "IAVD " instead of hex. Of course this works with a direct raw connection from putty to TV with just normal typing. Lastly, I just wanted to verify that enter command is automatic or if this may be a missing piece to the puzzle. Do I need to somehow account for that? I wasn’t sure if I should add a hex for carriage return or something.

Can anyone offer guidance on how I may send the command from another SSH instance?

FYI: I am attempting this directly typing into Putty which is connected to my Hass.io port 22. I do have private/public keys setup for my putty connections for both port 22 and 22222 though I am currently testing this through 22 connection. The TV currently has no username or password set. I have had the defaults set in the past as admin, password. When using a RAW connection when set, It prompted me for username and then password which I had to enter/login before sending commands. Without a username/password set, I am able to just send my first command such as "IAVD1 " and it works great.

Sorry for all of the information but wanted to ensure I paint a complete picture so we can get to solutions if one exists. Thanks for reading this far if you have.


#15

I will have a look at my code later, will post it…
But I also sometimes need netcat to get a value, not to send it … that doesn’t work for me from hassio… so instead I created a small .py file… And I cust call the python script file from my switch…

So maybe you can make your commands in a python file, and just call that file?


#16

See here what I did to receive from netcat, you can use it as a send command


#17

Why? you said ssh doesn’t work?

That is not ssh, it is a raw tcp connection, which I think you do want. However I think you are very confused about what ssh is.


#18

Nickrout,

I have a very basic understanding. When I say I want to send it via SSH I do realize that seems silly. When later I am indicating I am trying to send RAW TCP. I am trying to accomplish this through HASSIO installation and send RAW TCP command from the HASSIO SSH. Or at least that’s what I was thinking I wanted.

is command_line not using the SSH built into the SSH? Or is it just using the basic command_line of the HASSOS?

I am not a programmer but I am willing to try try and try some more. Which is why I finally began asking questions since nothing I was coming across was applicable. I guess I was asking if SSH can send raw commands through a setting.

I will test some other things to test my theory. Thanks for helping me look at it in a different light.


#19

Nickrout,

Can i run a command such as below direct from config.yaml?

- platform: command_line
  scan_interval: 10
  switches:
    light_led:
      command_HDMI3: echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc 192.xxx.x.xxx 10002
      friendly_name: LED

Obviously I would still need to tweak this for my configuration and something it could be called from?


#20

try

echo "IAVD3   " | nc 192.x.x.x 10002

Do not be confused by the fact that you use ssh to connect to your hassio instance. Once you have ssh’d in, it is the same as if you are sitting in front of the hassio machine looking at a monitor.