Get status and flipflop

Added pull for current light status to toggle rather than using a random.
This commit is contained in:
paradizelost 2020-12-07 11:20:11 -06:00 committed by GitHub
parent 281190e0e2
commit 9ba565b408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,36 +3,41 @@ $headers=@{
Authorization = "Bearer $token" Authorization = "Bearer $token"
} }
$url="https://api.smartthings.com/v1/devices" $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 $things = (invoke-RestMethod -Method Get -Uri $url -Headers $headers).items
foreach($thing in $things){ function toggle-lights(){
if($thing.components.capabilities.id -contains "switch"){ param([string]$DeviceLabel)
write-host $thing.label foreach($thing in $things){
$offcommandhash = @{ if($thing.components.capabilities.id -contains "switch"){
"component"="main"; if($thing.label -eq $DeviceLabel){
"capability"="switch"; write-host $thing.label
"command"="off"; $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"
$oncommandhash = @{ write-host $commandurl
"component"="main"; write-host "-----"
"capability"="switch"; if(($lightstatus -eq "off")){
"command"="on"; write-host "ON"
} invoke-RestMethod -Method POST -Uri $commandURL -Headers $headers -Body $oncommand
$offcommand = "[$($offcommandhash | ConvertTo-Json)]" } else {
$oncommand = "[$($oncommandhash | ConvertTo-Json)]" write-host "OFF"
if($thing.label -eq "FamilyRoom South East 1"){ invoke-RestMethod -Method POST -Uri $commandURL -Headers $headers -Body $offcommand
$commandURL = "https://api.smartthings.com/v1/devices/$($thing.deviceid)/commands" }
write-host $commandurl Invoke-RestMethod -Method GET -uri "https://api.smartthings.com/v1/devices/$($thing.deviceid)/status" -headers $headers
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)
} }
} }
} }
do{toggle-lights -devicelabel "FamilyRoom South East 1"}while($true)