sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
228 stars 121 forks source link

Set PowerState from remote device #337

Closed dradissoftware closed 10 months ago

dradissoftware commented 10 months ago

Is it possible to set the PowerState of a switch from a remote ESP device? For instance, if I wanted a to flip a switch to turn on all my individual lights (switches), I could get all the switch devices by ID and then call setPowerState to turn them all on or off. It seems like "sendPowerStateEvent" only notifies Sinric that the power state has changed, it doesn't actually fire the switches "onPowerState" function.

kakopappa commented 10 months ago

Hi @dradissoftware

sendPowerStateEvent is to let the server know about physical changes. Sending on/off state to a device can be done via API only.

To use the API,

  1. Generate an API key from the dashboard -> credentials
  2. Send a HTTP POST command: here's example curl
curl --location --request POST 'https://ifttt.sinric.pro/v1/actions'
--data-raw '{
"api_key" : "xxxx",
"device_id": "xxx",
"action": "setPowerState",
"value": { "state": "On" }
}'

Ref: https://github.com/sinricpro/esp8266-esp32-sdk/issues/70#issuecomment-644781907

dradissoftware commented 10 months ago

Thank you, that was just what I needed. I can now make calls to the API from my ESP01 with code like this...

char* host = "https://ifttt.sinric.pro/v1/actions"; int httpsPort = 443;

String data = "{ \"api_key\": \"ffffff-1111-2222-33333\", \"device_id\": \"123567890abcdef\", \"action\": \"setPowerState\", \"value\": { \"state\": \"On\" } }";

WiFiClientSecure client; client.setInsecure(); client.connect(host, httpsPort);

HTTPClient http; http.begin(client, host); http.addHeader("Content-Type", "application/json"); int httpCode = http.POST(data); http.end();