Controlling a KVM Switch

I have this KVM Switch:

It has a wired remote for switching that connects with a USB Mini B connector. I had the bright idea of wiring that to an ESPHome device to allow HA control as well as automating around which PC I have active.

I’m still fairly a newb with ESPHome, but I thought this would be easy, as the voltmeter shows every USB pin is static except “Data -” which is 0.14v when on input 1 and 3.2v on input 2. And I can almost get it working as I want. I.e. I can connect a GPIO pin to the “Data -” and then:

  - platform: gpio
    name: "KVM"
    pin: GPIO25

This exposes a switch in HA that I can toggle to go from input 1 (when off) to input 2 (when on). However, the hardware button on the switch no longer functions…when I push it, the color changes but the input does not. I really want that to work so that I’m not totally dependent on HA/wifi/etc.

For grins, I tried using a binary_sensor:

  - platform: gpio
    name: "KVM"
    pin: GPIO25

With that the hardware switch works fine and the correct state is reported in HA (off when input 1 and on when input 2). Of course, I have no control from HA.

Is there something I’m missing to get the GPIO Switch to work correctly, without interfering with the hardware functionality? Or a better way to go about this?

hmm bit unclear,

KVM’s need to “detect” a signal on both ports in order to have it switch between A and B that could be your issue why its not switching…

if the button pop’s back when pushed its a non-latching switch, then your current code keeps the button pressed. I would advice to change the platform to a method that is used to blink a single led for a second or so.

if it’s latched switch → use a staircase wiring diagram (you could go 3 switches however i would keep it 2)

Both buttons (the remote and on the switch) are “clicky” so I assume that is non-latching. Also, if I hold either button down, the other will not switch the input. So it makes some sense.

Do you mean this kind of method?

If so I haven’t had any luck with that either. Too short a delay and it just doesn’t switch. Too long a delay and it switches to input 2 and then back to input 1.

I was thinking maybe I needed a relay but it would seem to do the same?

This is harder than expected…

actually that makes sense

What you are experiencing is probably an anti-ghosting / (de)bounce that prevents a simple tap … to switch … to long press = cycle through the connected system(s) usefull if you have 16 devices connected to the KVM …

think code below might help


  • platform: gpio
    pin: 25
    id: relay
    name: “Gate Remote”
    icon: “mdi:gate”
    • delay: 500ms
    • switch.turn_off: relay

found here GPIO Switch — ESPHome it turns itself of after 500ms (half a second)

o would define the switch and cycle myself … and if possible i would try to read the LED (which one is active) but they are nice to have …

I think you need to desolder the hardware button from the logic board.
Then connect the hardware button to the esp as you did with a binary sensor.

This means you can operate it as you did with the switch, and the binary sensor will sense if you use the hardware button and you can switch the switch in esp.
Not sure how the remote is connected but you probably need to intercept this also from getting to the kvm logic board.

The remote is connected via a USB Mini B cable (Male to Male on both ends – to the remote and to the KVM box). I kind of doubt it’s true USB/UART, but that’s a total guess.

That’s where I was hoping to just connect a USB Mini B Female to the Esphome device and swap it out with the remote. So far, close but no cigar.

My other thought is also adding some hardware switch to the Esphome…but I’m a little concerned that I might damage the KVM is I’m sending an incorrect signal all the time.

Yes that’s exactly what I tried. I can’t remember what the cutoff was where it actually switched, but I’ll try again.

Given this:

So just sending the signal on the D- works. But because the two buttons are hardwired to some logic that does not change the state of the ESP it does not work.
What you need to do is to read the button press event and with that toggle the switch.
What happens on D+ cable when you press the button?

I think the easiest is to detach the two buttons and make sure they send their commands to the ESP instead of the KWM.

i am thinking it also might be a pin low vs pin high (inverse=true) situation? although that might explain when the esp presses the button nothing happens. just grasping at straws here…

did you measure the continuity with the board powered off ? D- or D+ and the buttons leads might be connected or not . checking if each is connected to a ground plain might help in the troubleshooting

Finally figured this out. I was being a bit of a bonehead. I had very well tracked down the communication between the device and the remote switch (that is, D- indicates which input is active). I had not done the same for the communication between the remote switch and the device. I finally found by testing continuity that the ID pin (that I had been ignoring) is connected to ground when the remote button is pressed. So that is how the remote tells the device to change input.

So I have it working perfectly now as follows:

Pin connections (between ESP32 and the USB Mini B):

  • VIN connected to VUSB (provides power to ESP32…nice!)
  • GND to GND
  • GPIO25 is connected to D-
  • Nothing is connected to D+ (doesn’t seem to cause any issues)
  • GPIO26 is connected to ID
  - platform: gpio
    name: "Input 2 Active"
    pin: GPIO25
  - platform: gpio
    name: "Change Input"
    id: change_input
      number: GPIO26    
      inverted: true
    - delay: 500ms
    - switch.turn_off: change_input      

Throwing in a a “low-tech” option here: just use a HA-controlled SwitchBot to press the button.