Announcement

Collapse
No announcement yet.

Switchbot no longer working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Switchbot no longer working

    Has anyone managed to develop scripts to control a switchbot?

    Until today I've used a Node-Red node but it no longer works. It looks like it uses v1.0 of their API (just a token, no secret) so I guess they've turned off v1.0 support although I've not seen anything anywhere to prove that.

    I've not used the HS4 plugin and I guess it is also now broken.

    I've had a look at the Switchbot API documentation and got a little way to writing a script but failed and now I'm stumped.

    There must be a solution out there that works with the v1.1 API - any ideas?

    #2
    Do you use Alexa? Since you were using Node-Red, you might be able to control Switchbot devices through the Alexa Node. I'm using alexa-cakebaked, but I think the newest version is alexa-applestrudel. I'll have a look over the weekend- I have the plugin but normally control through Alexa.

    Comment


      #3
      Originally posted by Kevb View Post
      Do you use Alexa? Since you were using Node-Red, you might be able to control Switchbot devices through the Alexa Node. I'm using alexa-cakebaked, but I think the newest version is alexa-applestrudel. I'll have a look over the weekend- I have the plugin but normally control through Alexa.
      Thank you, that's a good idea. I'll take a look at the Alexa node.

      Comment


        #4
        With plenty of trial and error I've managed to write some vb.net scripts to get my Switchbot bot to "press". Its not pretty but it works.
        I'll post something here when I've finished it.

        Comment


          #5
          The Switchbot API v1.1 documentaion is here: https://github.com/OpenWonderLabs/SwitchBotAPI

          Note that you need the token and secret which are in the Switchbot app (>=v6.14). To generate a signature, you need to use the HMAC-SHA256 cryptographic has function (Yes, I know what you're thinking - I thought the same). So you need to include this at the top of your script:

          Imports System.Security.Cryptography

          You will also need to reference this in your HS4 settings.ini (remember to edit settings.ini when HS4 is not running). Your ScriptingReferences should look something like this:

          ScriptingReferences=Newtonsoft.Json;bin\homeseer\Newtonsoft. Json.dll,System.Web.Script.Serialization;System.Web.Extensio ns.dll,System.ServiceProcess;System.ServiceProcess.dll,Syste m.Security.Cryptography;System.Security.dll

          Your first quest is to generate a signature which you need to get your device ids. Note that the signature expires pretty quickly so you need to generate it and then use it. This script will return json containing details of all your devices. From here you can pick out the deviceId for the device you want to control:



          Dim Debug as Boolean = True

          Try

          Dim token as String = "my token"
          Dim secret as String = "my secret"

          Dim timespan as String = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds).ToString
          Dim nonce as String = ""
          Dim data as String = token & timespan & nonce
          Dim signature as String

          Dim Auth as String
          Dim sign as String
          Dim t as String
          Dim strURL as String

          Dim myWebReq as HttpWebRequest
          Dim myWebResp as HttpWebResponse
          Dim sr as StreamReader

          Dim Encoding = New Text.UTF8Encoding
          Dim KeyByte as Byte() = Encoding.GetBytes(Secret)
          Dim MessageBytes as Byte() = Encoding.GetBytes(data)
          Using Hmacsha256 = New HMACSHA256(KeyByte)
          Dim HashBytes as Byte() = Hmacsha256.ComputeHash(MessageBytes)
          signature = Convert.ToBase64String(HashBytes).ToUpper()
          End Using

          System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

          strURL = "https://api.switch-bot.com/v1.1/devices"
          Auth = "Authorization: " & token
          sign = "sign: " & signature
          t = "t: " & timespan
          nonce = "nonce: " & nonce

          myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
          myWebReq.Method = "GET"
          myWebReq.Headers.Add(Auth)
          myWebReq.Headers.Add(sign)
          myWebReq.Headers.Add(t)
          myWebReq.Headers.Add(nonce)

          myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)

          sr = New StreamReader(myWebResp.GetResponseStream())
          Dim jsonText As String = sr.ReadToEnd()

          Dim deserialized = JsonConvert.DeserializeObject(jsonText)
          Dim json as JObject = JObject.Parse(jsonText)
          Dim results as List(Of JToken) = json.Children().ToList

          If Debug then
          hs.writelog("Switchbot","Authorization: " & token)
          hs.writelog("Switchbot","sign: " & signature)
          hs.writelog("Switchbot","t: " & timespan)
          hs.writelog("Switchbot","nonce: " & nonce)
          End if

          hs.writelog("Switchbot","jsonText: " & jsonText)
          Devices = jsonText

          Catch ex As Exception
          hs.WriteLog("Switchbot", "Error: " & ex.Message.ToString)
          End Try



          In my case this produces (all I have is a hub and just 1 bot)

          {
          "statusCode": 100,
          "body": {
          "deviceList": [
          {
          "deviceId": "C024A166405E",
          "deviceName": "Switchbot Hub Mini 5E",
          "deviceType": "Hub Mini",
          "enableCloudService": false,
          "hubDeviceId": "000000000000"
          },
          {
          "deviceId": "C82CE8266AD9",
          "deviceName": "Bot D9",
          "deviceType": "Bot",
          "enableCloudService": true,
          "hubDeviceId": "C024A166405E"
          }
          ],
          "infraredRemoteList": []
          },
          "message": "success"
          }




          Now you have the deviceId you can control it. This script 'presses'​​ the bot:



          Dim Debug as Boolean = True

          Try

          Dim token as String = "my token"
          Dim secret as String = "mysecret"
          Dim DeviceId as String = "C82CE8266AD9"


          Dim timespan as String = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds).ToString
          Dim nonce as String = ""
          Dim data as String = token & timespan & nonce
          Dim signature as String

          Dim Auth as String
          Dim sign as String
          Dim t as String
          Dim strURL as String

          Dim myWebReq as HttpWebRequest
          Dim myWebResp as HttpWebResponse
          Dim sr as StreamReader

          Dim Encoding = New Text.UTF8Encoding
          Dim KeyByte as Byte() = Encoding.GetBytes(Secret)
          Dim MessageBytes as Byte() = Encoding.GetBytes(data)
          Using Hmacsha256 = New HMACSHA256(KeyByte)
          Dim HashBytes as Byte() = Hmacsha256.ComputeHash(MessageBytes)
          signature = Convert.ToBase64String(HashBytes).ToUpper()
          End Using

          System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

          Auth = "Authorization: " & token
          sign = "sign: " & signature
          t = "t: " & timespan
          nonce = "nonce: " & nonce

          Dim json As String = "{" & chr(34) & "command" & chr(34) & ":" & chr(34) & "press" & chr(34) & "," & chr(34) & "parameter" & chr(34) & ":" & chr(34) & "default" & chr(34) & "," & chr(34) & "commandType" & chr(34) & ":" & chr(34) & "command" & chr(34) & "}"

          strURL = "https://api.switch-bot.com/v1.1/devices/" & DeviceId & "/commands"

          Dim params As Byte() = encoding.GetBytes(json)
          myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
          myWebReq.ContentType = "application/json; charset=utf-8"
          myWebReq.ContentLength = params.Length
          myWebReq.Method = "POST"
          myWebReq.Headers.Add(Auth)
          myWebReq.Headers.Add(sign)
          myWebReq.Headers.Add(t)
          myWebReq.Headers.Add(nonce)

          Dim myStream As Stream = myWebReq.GetRequestStream()

          myStream.Write(params, 0, params.Length)
          myStream.Close()

          myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
          sr = New StreamReader(myWebResp.GetResponseStream())
          Dim jsonText As String = sr.ReadToEnd()
          Dim deserialized = JsonConvert.DeserializeObject(jsonText)
          SwitchbotPress = deserialized("message")

          If Debug then
          hs.writelog("Switchbot","json: " & json)
          hs.writelog("Switchbot","jsonText: " & jsonText)
          hs.writelog("Switchbot","Authorization: " & token)
          hs.writelog("Switchbot","sign: " & signature)
          hs.writelog("Switchbot","t: " & timespan)
          hs.writelog("Switchbot","nonce: " & nonce)
          hs.writelog("Switchbot","SwitchbotPress: " & SwitchbotPress)
          End if

          Catch ex As Exception
          hs.WriteLog("Switchbot", "Error: " & ex.Message.ToString)
          End Try



          As I said, its not pretty but at least it works. I hope this helps someone!​

          Comment


            #6
            Originally posted by Jez View Post
            Has anyone managed to develop scripts to control a switchbot?

            Until today I've used a Node-Red node but it no longer works. It looks like it uses v1.0 of their API (just a token, no secret) so I guess they've turned off v1.0 support although I've not seen anything anywhere to prove that.

            I've not used the HS4 plugin and I guess it is also now broken.

            I've had a look at the Switchbot API documentation and got a little way to writing a script but failed and now I'm stumped.

            There must be a solution out there that works with the v1.1 API - any ideas?
            Just noticed v1 of the api stopped working yesterday. I've been using Big5 to control my curtain-bot but since yesterday, while my script works without error, it doesn't move the curtain. Is anyone besides us two noticing this? No warnings that v1.0 is being shutdown. I do know about Michael's solution and was going to move there eventually. I guess now sooner than I expected.
            HS3PRO 3.0.0.500 as a Fire Daemon service, Windows 2016 Server Std Intel Core i5 PC HTPC Slim SFF 4GB, 120GB SSD drive, WLG800, RFXCom, TI103,NetCam, UltraNetcam3, BLBackup, CurrentCost 3P Rain8Net, MCsSprinker, HSTouch, Ademco Security plugin/AD2USB, JowiHue, various Oregon Scientific temp/humidity sensors, Z-Net, Zsmoke, Aeron Labs micro switches, Amazon Echo Dots, WS+, WD+ ... on and on.

            Comment

            Working...
            X