Switch-Bot API integration

Hi, after playing with the switchbot API documentation and just adapting what we know it works, I was able to call custom IR commands doing something like this:

rest_command:
  switchbot_device_command:
    url: 'https://api.switch-bot.com/v1.0/devices/{{ deviceId }}/commands'
    method: post
    content_type: 'application/json'
    headers:
      Authorization: !secret switchbot_api
    payload: '{"command": "{{ command }}","parameter": "{{ parameter }}"}'
  switchbot_irdevice_command:
    url: 'https://api.switch-bot.com/v1.0/devices/{{ deviceId }}/commands'
    method: post
    content_type: 'application/json'
    headers:
      Authorization: !secret switchbot_api
    payload: '{"command": "{{ command }}","parameter": "default", "commandType": "customize"}'

I add a second res_command function that pretty much only change the payload for custom IR commands, they need “commandType” set as “customize”…probably the first function can be improve to deal with this…

So, as a simple test I add a “fake switch” to trigger the custom IR command…

switch:
  - platform: template
    switches:
...other switches...
      bedroom_tv_qmenu:
        friendly_name: Bedroom TV QMenu
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_bedroomtv_deviceId
            command: "Q.Menu"
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_bedroomtv_deviceId
            command: "Q.Menu"

and it works!..TV shows the quick menu, which is a custom command of my bedroom tv IR device …Of course this is not the proper way since each custom button will be an “On/Off” switch, …but from here some additional templates-entities can be write to add all the custom IR commands that you want to call…

…hope it helps…

3 Likes