Listen on a tcp port for commands?

I have a device that sends commands on a TCP port and then they need to be interpreted for an action.

Is this possible with HA? I found sensor.tcp but I’m trying to do the reverse. Sensor.tcp tells HA to make the connection. Instead I want HA to listen.

1 Like

Also interested in this. Got currently unsupported security cameras that don’t work directly with HASS api.

You could probably put something together in AppDaemon. You would probably need to permanently tie up one or more worker threads but if you do it right that can be managed.

You could develop a sensor-component which uses socketserver.TCPServer, which fires events based on the input you get. To those you could respond with automations.

my python is hack-ish. Any idea how to get started doing this?

Well, usually it is preferred to create an external package that does the “protocol-specific” stuff (in this case a package that starts and stops the tcp server and somehow handles the data), and then you create a sensor-platform/component that imports that package, starts the server, and then somehow handles the data.

I don’t know which of the existing components do it in a similar fashion. I know HomeMatic does (since I’m one of the developers), but that’s a really complex component and it might be hard to figure out which parts of the code are relevant, because for the sensor that’s much less needed.
Maybe the FirtzBox Call Monitor is similar. I believe that one is connecting to a telnet session, and whenever it receives data, it does something in HASS. Should be much less complex and probably the easier route to get started with. This will however probably change the state of an entity, not fire an event. And I personally think for this TCP sensor an event is better suited that messing around with entities.

Although, the first thing you should think of is what this module should actually do. The nature of such functionality would require it to be very flexible. What does the data you receive look like? And how is that useful for other possible devices? Should the output somehow be parsed? If so, could it be done using templates? JSON? Or just pass the received data directly to the event bus as a string to be then parsed by an automation?

Essentially you first have to know what you want to do with the sensor, given this should actually be a sensor and not a component on it’s own.

It looks like FirtzBox Call Monitor also initiates the connection.

Maybe I can search github for some python function for listening. I have to run somewhere but will try some more later.

It may initiate the connection, but I’m pretty sure it does not poll to see if anyhing has happened. Instead there probably is a callback function that fires as soon as the fritz box spits out information about an incoming call so HASS directly gets that information and can handle it. That’s the point. At least I believe you wan’t HASS to instantly react instead os saying “there’s been an event somewhere during the last minute”.

This would be an example of an async TCP server: https://github.com/python/asyncio/blob/master/examples/simple_tcp_server.py

1 Like

I listed this under feature requests. So far my efforts haven’t been very fruitful but still learning.

@danielperna84 Thanks for the help!

@bbrendan - what causes the device to send a command? Is it a change in state, or something regular/continuous?

Anyway my implementation would be to use a script to write a small daemon that listened on that port and fed the received information into a file.

You could then use the command line sensor to read that file.

In my case its a remote control that sends tcp strings. So channel up/down, volume, mute, play,stop, etc. Then HA would track the state of all of it and send it out to the IR blasters or whatever else.

So it has to be instantaneous. I’d I could use a daemon to send rest commands or other things but it would be cleaner to do it all within HA.

Yes I see the need for it to be instantaneous.

Thinking about it. What is this remote?

What about a daemon that passes the message to your mqtt broker?

I don’t have experience with mqtt but I’m guessing that could work. @aimc mentioned appdaemon as well. Oh, its an RTI remote.

I used another raspberry pi to do something similar, it would be great if this can be added to HA as a component. https://github.com/jebentancour/TH-HomeAssistant/tree/master/cameras

Hi everyone. I’am new here. I’ve been working with haas for over 2 months and I am finally getting involved in the development of components. Right now I am trying to develop one for the lutron caseta and the pico remotes. I have very much the same situation. I solved it by creating a daemon on the pi that connects to the telnet port of the lutron hub, when the daemon detects a telnet command it fires an event through the hass rest api. It works great, however, just like bbrendon I would like to develop the component inside hass.
I believe a sensor is the right way, but what next? I created a sensor that reads on the update mehtod the entry of the telnect connection, however this makes the update wait for very long if no input has been thrown by the lutron hub. so @bbrendon, were you able to solve anything?

I wrote a script that runs as a daemon and listens for commands via TCP. It then uses the HASS API and triggers stuff in HASS. Not ideal, but it worked.

There might be some new components now that solve this better. I haven’t looked into it again.

I finally solved it. If you are interested, what I did is read FirtzBox code and what actually worked for me is the part of calling a new thread with the daemon. Now it is not a daemon on the linux server but a thread on home assistant. This works great, the side thread runs an infinit loop that reads the telnet connection and if it gets something an hass event is fired.
I did all this in a component instead of a sensor. Now the sensor is not really needed.
If you’d like more help I’ll be glad to help. Have a good one.

3 Likes

@jmartinez Do you mind sharing how you did this? I find myself in a similar situation and need to read a TCP stream.

Yea. His solution sounds great. I’d rip his code off in a second and convert mine. Plz share it !