From 9ba565b4086ea91119ec3fe3789889e2eda0906e Mon Sep 17 00:00:00 2001 From: paradizelost Date: Mon, 7 Dec 2020 11:20:11 -0600 Subject: [PATCH] Get status and flipflop Added pull for current light status to toggle rather than using a random. --- Smartthings.ps1 | 63 ++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/Smartthings.ps1 b/Smartthings.ps1 index 33eb70c..cbe5ea9 100644 --- a/Smartthings.ps1 +++ b/Smartthings.ps1 @@ -3,36 +3,41 @@ $headers=@{ Authorization = "Bearer $token" } $url="https://api.smartthings.com/v1/devices" +$offcommandhash = @{ + "component"="main"; + "capability"="switch"; + "command"="off"; +} +$oncommandhash = @{ + "component"="main"; + "capability"="switch"; + "command"="on"; +} +$offcommand = "[$($offcommandhash | ConvertTo-Json)]" +$oncommand = "[$($oncommandhash | ConvertTo-Json)]" $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) +function toggle-lights(){ + param([string]$DeviceLabel) + foreach($thing in $things){ + if($thing.components.capabilities.id -contains "switch"){ + if($thing.label -eq $DeviceLabel){ + write-host $thing.label + $lightstatus = (Invoke-RestMethod -Method GET -uri "https://api.smartthings.com/v1/devices/$($thing.deviceid)/status" -headers $headers).components.main.switch.switch.value + $commandURL = "https://api.smartthings.com/v1/devices/$($thing.deviceid)/commands" + write-host $commandurl + write-host "-----" + if(($lightstatus -eq "off")){ + 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 + } + Invoke-RestMethod -Method GET -uri "https://api.smartthings.com/v1/devices/$($thing.deviceid)/status" -headers $headers + } } } } + + +do{toggle-lights -devicelabel "FamilyRoom South East 1"}while($true)