Supereg / homebridge-http-switch

Powerful http switch for Homebridge: https://github.com/homebridge/homebridge
ISC License
218 stars 36 forks source link

HowTo: to send a Curl API Request #114

Closed Stefan-83 closed 1 year ago

Stefan-83 commented 1 year ago

Hi, i want to switch the light of my winecooler via the manufacturer's API:

I can use swagger to switch the light with: Servers: https://publickitchenapi.casoapp.com/ POST: /api/v1.0/Winecooler/SetLight ApiKey: x-apikey xxxxxxxxxxxxxxx

Request body:

{
  "technicalDeviceId": "1001-ABCDEFGHI",
  "zone": 1,
  "lightOn": false
}

Responses:

curl -X POST "https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight" -H "accept: */*" -H "x-api-key: xxxxxxxxxxxxxxx" -H "Content-Type: text/json" -d "{\"technicalDeviceId\":\"1001-ABCDEFGHI\",\"zone\":1,\"lightOn\":false}"

Request URL https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight

Any ideas how to integrate this into http-switch?

Thanks, Stefan

mbierman commented 1 year ago

Something like this:

{
    "accessory": "HTTP-SWITCH",
    "name": "your light name",
    "switchType": "stateful",
    "serialNumber": "SW001",
    "pullInterval": 60000,
    "debug": true,
    "onUrl": {
        "url": "https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight",
        "method": "POST",
        "headers": {
           "ApiKey": "x-apikey xxxxxxxxxxxxxxx",
            "Content-Type": "application/json",
        },
        "body": {
             "technicalDeviceId": "1001-94c9601c7866",
            "zone": 1,
           "lightOn": true
        }
    },
    "offUrl": {
        "url": "https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight",
        "method": "POST",
        "headers": {
           "ApiKey": "x-apikey xxxxxxxxxxxxxxx",
            "Content-Type": "application/json",
        },
        "body": {
             "technicalDeviceId": "1001-94c9601c7866",
            "zone": 1,
           "lightOn": false
        }
    },
}

You may want to set a statusURL too if you want to know the current status of the light and the API has one.

See the https://github.com/Supereg/homebridge-http-switch

Stefan-83 commented 1 year ago

Thank you so much Michael 👍 with some little tweaks, it works! (no need for status --> switchType: toggle and the correct header is "x-api-key")

{
    "accessory": "HTTP-SWITCH",
    "name": "Weinkühlschrank",
    "switchType": "toggle",
    "serialNumber": "SW001",
    "pullInterval": 60000,
    "debug": true,
    "onUrl": {
        "url": "https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight",
        "method": "POST",
        "headers": {
            "x-api-key": "0000000000000000000000000000",
            "Content-Type": "application/json"
        },
        "body": {
            "technicalDeviceId": "1001-ABCDEFGHI",
            "zone": 0,
            "lightOn": true
        }
    },
    "offUrl": {
        "url": "https://publickitchenapi.casoapp.com/api/v1.0/Winecooler/SetLight",
        "method": "POST",
        "headers": {
            "x-api-key": "0000000000000000000000000000",
            "Content-Type": "application/json"
        },
        "body": {
            "technicalDeviceId": "1001-ABCDEFGHI",
            "zone": 0,
            "lightOn": false
        }
    }
}

(hopefully this example will help others in the future)

Best regards, Stefan