Switch-Bot API integration

I can confirm! the lock is back in their v1.1 API just released today (2022-09-16)
The main change is how you authenticate… you must use a token and a secret now. There’s an example in the API documentation… which is wrong btw, there’s just one thing misssing in the documentation… the nonce header.
After adding that, the lock is working now :wink:

Just one question… since we have to generate a sign string for every request (it is valid for only 5min or so), how can we add this logic into HA? In python some additional libraries are required right? so, I wonder how can we achieve the same with the jinja templates, or we should move to an actual python script for that… do you know if there’s any integration for switchbot stuff in the HACS? if not, is there a “template” or “example” to start with Creating your first integration | Home Assistant Developer Docs? I will play with it over this weekend - my focus will be this lock.

3 Likes

I was looking at V1.1 as well after troubleshooting why my lock wasn’t working, when it was polling status just fine(so thanks for pointing this out). How did you end up doing the cryptography for the sign string. I know sign for the header has to be set with hmac (sha256) according to the API forums. I tried in Node Red, but with no ability to import crypto() methods.

Does anyone know how to access the SwitchBot Motion Sensor status from within the Contact sensor? i.e. not a dedicated motion sensor but the contact sensor has a built-in motion sensor that triggers to the APP and sends notifications, but I can’t find anything within the API about the actual motion sensor element in order to use it.

Hello,

I am totally new to home assistant and really not a coder either. I would like to add my lock to my home assistant but where do you add this file? I cannot figure out how to get this file where it need to be or what the name should be.

I have enabled a custom component for tplink and edited that so i am familiar with the process. Please, any help would be much appreciated.

I successfully added a bot to my setup, I’m able to see the switch in the dashboard (and it works), but I’m not able to use the device in Automation & Scences. After some digging, it does not register the switch as a device, did I miss something there?

Here’s my configuration:

rest_command:
  switchbot_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": "command"}'

switch:
  - platform: template
    switches:
      terrace_light:
        unique_id: terrace_light
        friendly_name: Terrace Light
        turn_on:
          service: rest_command.switchbot_command
          data:
            deviceId: !secret switchbot_terrace_light
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_command
          data:
            deviceId: !secret switchbot_terrace_light
            command: "turnOff"

I’m still working… but maybe this can be help you:

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 }}"}'

#<Curtains Position and other sensors here>
  - platform: rest
    name: 'Motionsensor JSON'
    resource: !secret switchbot_motioningresso_status_url
    method: GET
    scan_interval: 600
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: '{{ value_json.body }}'
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId 
      - deviceType
      - hubDeviceId
      - moveDetected
      - brightness
  - platform: template
    sensors:
      switchbot_motioningresso_motiondetected:
        friendly_name: "Motion Detected"
        value_template: '{{ states.sensor.motionsensor_json.attributes["moveDetected"] }}'
        
in secrets file you must enter:

switchbot_motioningresso_status_url: "https://api.switch-bot.com/v1.0/devices/YOURDEVICEID/status"

I am using the v1.0 API interface for my switchbot curtains. Ever since I updated my firmware on the Switchbot Mini Hub and the Switchbot curtains to v6, the API does not return the correct position of the curtains. It appears to be fixed values that it was last updated.

Does anyone have the same experience or is there a fix to this?

I looked that the v1.1 API interface, but as pointed out above, this new authorization method is time limited - so I guess the current methods used here can no longer be used.

Same problem, after update the firmware the position value is zero.

Did you ever figure out the lock via v1.1? Possible using jinja or python script? I am interested in this is well.

1 Like

I am also crossing my fingers for an API-based integration. I could adventure into the manual code but I have been working 7 days a week and spend most of my free time sleeping where I can. So not a lot of trouble-shooting time as of late :frowning: thus an easier solution would be great so that I can start controlling my SB locks.

1 Like

If you want to use it I created this repo, with my script, to use API 1.1.

:warning: It is not a simple home assistant script, you need pyscrypt from hacs, but mostly this script at the moment is Work In Progress :warning:

The procedure is well explained (I think) but I’m Italian so, there might be something lost in translation.

For any problems open an issue.

2 Likes

Hi Guys,

I am new to HA, reading threads like crazy… i was reading gdeboos’s instructions on how to integrate the curtains to my enviroment, i have succesfully configured but when the curtains are “closed” - they show as “open” and vice versa - under the cover state.

Is there a quick fix for this? many thanks for your support!

Can anyone provide additional instructions on how the use the IR blaster on the Switchbot hub. I looked at some of the instructions above but was not able to get it to work. I’m trying to control a fan, so I need On/Off, speed control (just a single button that cycles through 3 different speeds), and a toggle for oscillation. Any help would be greatly appreciated!

For anyone with a similar issue to what I previously asked, I have found a solution.

As previously stated, you will need the rest command. I’ve included mine here, but it’s the same as all the other ones posted here.

rest_command:
  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"}'

Assuming you are adding more than one IR command, you will need to add multiple “switches” for each command. In my example below, I have a total of three “switches” (commands), power, speed control, and oscillation. They all need to go under the single “Switch” line.

switch:
  - platform: template
    switches:
      fan_power:
        friendly_name: Fan_Power
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Power"
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Power"
            
      fan_speed:
        friendly_name: Fan_Speed
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Speed_Toggle"
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Speed_Toggle"
            
      fan_oscillate:
        friendly_name: Fan_Oscillate
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Oscillate"
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_fan_deviceId
            command: "Oscillate"

Finally, on the Switchbot app, make a custom button for each one of these, ensuring that the name of the button matches the command name. I.E. the command “Power” only works because I have a custom button programmed in the Switchbot app named “Power”.

3 Likes

Hi, I’m tried to use your method above to integrate the Switchbot virtual remote to switch on my aircon by modyfing the code as below.

However I’m getting an error: ‘Failed to call service switch/turn_on. Unable to find service rest_command.switchbot_irdevice_command’

Grateful for any pointers

Edit: I’m an idiot - HA needed a full restart of the server to get the Service to be recognised, not just reloading of YAML configuration. All good now

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": "{{ parameter }}", "commandType": "{{ commandType }}"}'

switch:
  - platform: template
    switches:
      bedroom_aircon:
        friendly_name: "Bedroom Air Con Virtual Remote"
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret bedroom_aircon_deviceId
            command: "turnOn"
            commandType: ""
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret bedroom_aircon_deviceId
            command: "turnOff"
            commandType: "command"

With the completion of Add Battery and Firmware Info to Device status · Issue #11 · OpenWonderLabs/SwitchBotAPI · GitHub to expose battery of switchbot devices, what’s the most updated way to use API v1.1?

Using device/status for a regular bot I can only see: {‘deviceId’: ‘xxx’, ‘deviceType’: ‘Bot’, ‘hubDeviceId’: ‘xxx’, ‘power’: ‘on’}

Created a way to use API v1.1 with a standalone python script.

This example below works for just a switchbot bot device, but can be easily expanded for other devices. Feel free to submit any pull requests for additional commands/improvements.

Hi all :slight_smile:

I used “Smart Matching” when i added my portable ac, switchbot found many buttons and the display with current temp. When i curl to get device id i get this on the AC:
Why is the id long like that??
“,{“deviceId”:“01-202304211748-55496909”,“deviceName”:“AC”,“remoteType”:“Air Conditioner”,“hubDeviceId”:“ED*******3F4”}]}”

Is it not possible to use this one with HA using rest_command? :frowning:

Hi! How do your device id look like on the ac?

mine is like this “01-202304241929-74326444”
It doesent work for me :frowning:

https://api.switch-bot.com/v1.0/devices/01-202304241929-74326444/status
gives me this: {“statusCode”:190,“body”:{},“message”:“wrong deviceId”}
01-202201281415-69304742

Bumb :confused: Anyone ?