From 281190e0e20b725f282134d616db7079f0bf9d24 Mon Sep 17 00:00:00 2001 From: paradizelost Date: Mon, 7 Dec 2020 08:52:53 -0600 Subject: [PATCH] Initial Commit of Powershell script to work with smartthings rest api --- Smartthings.ps1 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Smartthings.ps1 diff --git a/Smartthings.ps1 b/Smartthings.ps1 new file mode 100644 index 0000000..33eb70c --- /dev/null +++ b/Smartthings.ps1 @@ -0,0 +1,38 @@ +$token="PERSONAL_ACCESS_TOKEN" +$headers=@{ + Authorization = "Bearer $token" +} +$url="https://api.smartthings.com/v1/devices" +$things = (invoke-RestMethod -Method Get -Uri $url -Headers $headers).items +foreach($thing in $things){ + if($thing.components.capabilities.id -contains "switch"){ + write-host $thing.label + $offcommandhash = @{ + "component"="main"; + "capability"="switch"; + "command"="off"; + } + $oncommandhash = @{ + "component"="main"; + "capability"="switch"; + "command"="on"; + } + $offcommand = "[$($offcommandhash | ConvertTo-Json)]" + $oncommand = "[$($oncommandhash | ConvertTo-Json)]" + if($thing.label -eq "FamilyRoom South East 1"){ + $commandURL = "https://api.smartthings.com/v1/devices/$($thing.deviceid)/commands" + write-host $commandurl + do{ + write-host "-----" + if((get-random -Minimum 0 -Maximum 2)){ + write-host "ON" + invoke-RestMethod -Method POST -Uri $commandURL -Headers $headers -Body $oncommand + } else { + write-host "OFF" + invoke-RestMethod -Method POST -Uri $commandURL -Headers $headers -Body $offcommand + } + start-sleep 3 + } while($true) + } + } +}