Custom_integration sample for a Telnet style control?

Hi

Having to control a device through a telnet connection and realising HA terribly lacks a tool to allow such interface without developping an integration, I’m thinking to develop an integration. Not really being good in Python I would prefer to start from an existing integration I could adapt for my case. Once stabilised and working well I’ll share it back with community.
Does someone know an existing custom integration that use a telnet connection to control a device ?

Thanks for your ideas, suggestions

Vincèn

So the core integration does not work for your use case?

Nope as it’s mainly designed for retrieving data from devices not controlling them out of on/off :frowning: Here my use case is an audio matrix. I’m going to send some switching commands and volume so can’t be handled by Telnet integration from what I tested with it !

Well I think we both learned from this that using netcat in shell command or command line notify is out and the telnet binary isn’t available in HA’s container.

An integration is probably your best way if you can find/make one (and/or submit feature requests for the core telnet integration). Another option is to write a simple python script and call it from a shell command or command line notify. HA uses telnetlib in its telnet integration so you can write a python script that depends on that library, its available in HA’s docker container. Assuming that the library does what you need just the integration doesn’t.

If you don’t like that some other options:

  1. Install the SSH & Web Terminal Addon. Use its setup configuration options to make the binaries you need available. SSH to that container in your shell command or command line notify and run your script there
  2. Make your own addon or put a container somewhere else from an image that includes whatever dependencies you need and enable SSH to it then have your command SSH there.
  3. Install Node RED, AppDaemon or pyScript (depending on your tooling preference) and explore what options they have for interfacing with telnet devices

It’s a switch integration, it can most definitely control a device’s on/off state.

switch:
  - platform: telnet
    switches:
      projector:
        resource: THE_IP_ADDRESS
        port: 4002
        command_on: "PWR ON"  ##<-------- See this
        command_off: "PWR OFF" ##<-------- and this
        command_state: "PWR?"
        value_template: '{{ value == "PWR=01" }}'
        timeout: 0.9

Yep and netcat is not really a satisfying solution for doing that, not even speaking about debugging it that is a nightmare :see_no_evil:

After quite some time lost on trying to find a solution, will go with the integration for sure !

Tried also the python script system but it’s same nightmare as netcat :frowning:

Good point about Node RED as I have been able easily to control properly the device with Node RED :+1:

Yep but it’s the only think it can control so useless for “real” control of a device :smiley:

Hey dude, what was your solution in node red? I think I’m coming across a similar situation where the core integration is fine for on / off switches, but for commands beyond simple on off, it is lacking.

Just use “the node-red-contrib-telnet-client” palette in Nodered and you’ll be all good :slight_smile: similar process with devices that needs SSH connection for control :wink:

1 Like

Thanks for coming back. I did install this but couldn’t figure out how to actually use it. When I search I couldn’t find any documentation on its use on node red website. I actually in the end used the function node to form the message and a TCP request node to send that message. It worked.

Would you mind sharing one of your telnet node flows so I can see how you got those nodes working please?

Thanks

of course it’s quite simple :slight_smile:

  • Once added the node-red-contrib-telnet-client palette in nodered, you just drop a Send box in your workspace.
  • Double-click on that Send box and then you’ll get that dialog box:
    2023-01-04 12.21.10 49xweix8org6mgviiutxlwrvt01oa4b3.ui.nabu.casa 9f65ce422c82
    where you’ll be able to add a new telnet connection using the little pencil or select an existing one if there is already one setup using the Connection list.
  • After that you just add a set msg.payload box to build the string you wish to send like that:
    2023-01-04 12.24.12 49xweix8org6mgviiutxlwrvt01oa4b3.ui.nabu.casa e4708ac6a0e6
  • and then you do an usual process in NodeRed like that to get string sent on a button push or whatever you need:

You should be able also to handle strings back from device you control but I didn’t try it so far :wink:

2 Likes

Hi Vincen, the other option, which I eventually got to is pyscript.

I have a bunch of audio, HDMI and home theater devices that all use telnet for the command interface (along with other gadgets such as TVs which have all their own connections and integrations).

After struggling with complex HA Automations, shell scripts using nc (netcat) and various methods including json to update HA afterwards, I ended up with a relatively slow, un-maintainable and un-reliable solution.

I have some scripting experience but knew nothing about python. But in desperation, knowing that telnetlib for python existed (thank you ! ), and given that HA appears to be largely a python creation, I started (a) learning basic python (supported by Python tester - Test code online), and (b) working through the python alternatives.

I looked at HA’s python_script but it was going to be complex to get relatively simple things done. There were two other options whose names I can’t recall but were also going to be complex.

I eventually found pyscript, read the concise but very good documentation, and eventually migrated. It is well integrated into HA, provides global and persistent variables, has replaced all of my shell scripts (bash) and HA scripts, and almost all of my Automations. Total number of lines has been reduced by about 70%. Pyscript turned out to be easy learn - level is about VB script rather than C# .

I now have an HA for my environment that is much more maintainable and reliable, and is quicker.

David

1 Like

Dear Vincent,
I’m new to node-red, and I’m just trying to send the following command “x5AVx9” to my Atlona hdmi switcher through telnet through a button in HA.
But it doesn’t work. What am I doing wrong? Thanks !

image

image

image

Well did you add the CR character in properties of telnet connection ? as it’s mandatory according at Atlona protocol !
Screenshot_20230729_153709
Example here with Blustream HDMI matrix system that needs also a CR at end of transmission :wink:

1 Like

Thank you so much! I indeed just had to add a “\r” in the “Out end mark” of the telnet connection.
image