Archive

Which Laptop is connected to my screen?

I've installed an LED strip behind my monitor in my home office to reduce the contrast between the wall and the screen. A timer to turn it on and off does not work well since I am frequently using the same monitor with my private Macbook in the evening so I needed to be able to figure out if a running laptop is connected to my USB-C monitor.

At first I planned to use a ping sensor, however my work laptop is a Chromebook and does not respond to pings so I needed to have a better option. I thought that it would be nice to detect which laptop is connected and change the background color based on that.

My work laptop has a single port exposed so I could use nmap to detect whether or not my work laptop is connected. On my private Macbook I am running sshd which is also detectable using a port scan. Here is the sensor that I cam up with and returns either "Chromebook", "Macbook" or "None" depending on what signature it detects on the IP address of my monitor.

sensor:
  - platform: command_line
    name: Docking Station
    command: nmap -p22,5555 192.168.1.123 | grep -e tcp -e down
    scan_interval: 10
    value_template: >-
      {% if value|regex_search("open +freeciv") %}Chromebook{% elif value|regex_search("open +ssh") %}Macbook{% else %}None{% endif %}

I am using grep to limit the output of nmap to keep it below 255 characters which is a requirement that the command_line sensor has. I have added 'down' as well since grep returns an error if it does not find anything which command_line sensors do not like.

The following automation sets the light to different colors depending on whether I am working or my private laptop is connected.

automations:
  - id: '1610140797120'
    alias: Control Office Screen Light
    description: ''
    trigger:
    - platform: state
        entity_id: sensor.docking_station
    - platform: homeassistant
        event: start
    - platform: state
        entity_id: binary_sensor.screen_light_status
        from: 'unavailable'
    condition: []
    action:
    - choose:
        - conditions:
        - condition: state
            entity_id: sensor.docking_station
            state: Chromebook
        sequence:
        - service: light.turn_on
            data:
            rgb_color:
            - 108
            - 255
            - 108
            entity_id: light.screen_light
        - conditions:
        - condition: state
            entity_id: sensor.docking_station
            state: Macbook
        sequence:
        - service: light.turn_on
            data:
            rgb_color:
            - 255
            - 145
            - 200
            entity_id: light.screen_light
        default:
        - service: light.turn_off
        data: {}
        entity_id: light.screen_light
    mode: single

As light I am using the PCB that I created a long time ago to drive an WS2811 strip as seen in this video. It is powered through the monitors USB port and running ESPHOME.