Skip to content

Instantly share code, notes, and snippets.

@psyciknz
Last active January 24, 2024 23:51
Show Gist options
  • Save psyciknz/b8a5617ae760681b28900f11f5948a76 to your computer and use it in GitHub Desktop.
Save psyciknz/b8a5617ae760681b28900f11f5948a76 to your computer and use it in GitHub Desktop.
Power Shell script for updating Openhab, or Home Assistant via rest api. Uses a settings.json config to detect teams in a call or not
$SettingsObject = Get-Content -Path settings.json | ConvertFrom-Json
Write-Output "Start up"
Write-Output $SettingsObject
function Update-OpenHAB {
param([string]$item, [bool]$state)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
#$headers.Add("Authorization", "Bearer " + $SettingsObject.openhabtoken)
$headers.Add("Content-Type", "text/plain")
$body = If ($state -eq $True) { "ON" } Else { "OFF" }
#$url = $SettingsObject.openhabbasepath + $item + '/state'
$url = $SettingsObject.openhabbasepath + $item
#Write-Output $url
Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body
}
function Update-HomeAssistant{
param([string]$item, [bool]$state)
$headers = @{
'Authorization' = 'Bearer ' + $SettingsObject.homeassistanttoken
'Content-Type' = 'application/json'
}
$body = '{"entity_id": "' + $item + '"}'
if ($state -eq $True)
{
$url = $SettingsObject.homeassistantpath + "light/turn_on"
}
else
{
$url = $SettingsObject.homeassistantpath + "light/turn_off"
}
#Write-Output $url
Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body
}
function Check-Process {
param([string]$processname, [string]$openhabitem, [int]$offcallcount = 0)
$global:changeState =0
$process = Get-Process $processname -EA 0
if($process) {
$processCount = (Get-NetUDPEndpoint -OwningProcess ($process).Id -EA 0|Measure-Object).count
Write-Output "Checking process count for $process at $processCount is greater than $offcallcount"
if ($processCount -ge $offcallcount) {
Write-Output "Setting $openhabitem ON for ($process=($offcallcount) = $processCount)"
#Update-OpenHAB -item $openhabitem -state $True
Update-HomeAssistant -item $openhabitem -state $True
$global:changeState = 1
} else {
#Update-OpenHAB -item $openhabitem -state $False
Update-HomeAssistant -item $openhabitem -state $False
Write-Output "Setting $openhabitem OFF for ($process=($offcallcount) = $processCount)"
$global:changeState = 0
}
} else {
#Update-OpenHAB -item $openhabitem -state $False
Update-HomeAssistant -item $openhabitem -state $False
$global:changeState = 0
}
Remove-Variable process
}
While($True) {
$d = (get-date).ToString('T')
Write-Output "Check @ $d"
$changeState = 0
Foreach ($process in $SettingsObject.processes) {
#Write-Output "==== Checking $process"
#Check-Process -processname $process.processname -openhabitem $process.homeassistantitem -offcallcount $process.nocallprocesscount
Check-Process -processname $process.processname -openhabitem $process.homeassistantitem -offcallcount $process.nocallprocesscount
#Write-Output "==== Process $outProcess"
#Write-Output "--- $global:changeState"
if ($global:changeState.Equals(1)) {
Write-Output "Breaking out of loop as process is active $process.processname"
break
}
else {
Write-Output("Continue loop")
}
}
Start-Sleep -Seconds 30
}
@psyciknz
Copy link
Author

psyciknz commented Apr 12, 2022

Settings.json

{
	"openhabbasepath" : "<openhab url>:8080/rest/items/",
	"openhabtoken" : "",
	"homeassistantpath": "<homeassistant url>/api/services/",
	"homeassistanttoken": "<home assistant long lived token",
	"processes" : [
		{
			"processname" : "Teams",
			"openhabitem" : "<openhab item name to control>",
			"homeassistantitem" : "<home assistant entity id>",
			"nocallprocesscount" : 9
        },
        {
			"processname" : "Lync",
			"openhabitem" : "<openhab item name to control>",
			"homeassistantitem" : "<home assistant entity id>",
			"nocallprocesscount" : 2
		}
        
	]
}

@psyciknz
Copy link
Author

For "new" teams. Update the settings.json and add the new teams process details:

{
	"processname" : "ms-teams",
	"openhabitem" : "<openhab item name to control>",
	"homeassistantitem" : "<home assistant entity id>",
	"nocallprocesscount" : 9
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment