Skip to content

Instantly share code, notes, and snippets.

@MrPickles2009
Last active August 18, 2022 00:12
Show Gist options
  • Save MrPickles2009/4abf7a019fdbbbe0545ad67556eb872a to your computer and use it in GitHub Desktop.
Save MrPickles2009/4abf7a019fdbbbe0545ad67556eb872a to your computer and use it in GitHub Desktop.
Hue Teams Presence indicator

Hue Teams Presence indicator

A powershell script to grab your Microsoft Teams presence and change a Philips Hue light to its colour.

Setup

  1. Inside hue-bridge.json, fill in your bridge's values:
{
  "id": "<BRIDGE_ID_HERE>",
  "internalipaddress": "<BRIDGE_IP_HERE>",
  "port": 443,
  "username": "<AUTH_USERNAME_HERE>",
  "selectedLight": {
    "id": "<LIGHT_ID_HERE>",
    "name": "<LIGHT_NAME_HERE>"
  }
}
  1. Inside Hue-Teams-Presence.ps1, find and set the following variable values for the refresh time (default = 5 seconds), the credentials of your registered app in Azure Portal and the User Id for the presence status you are trying to retrieve (how to register an app):
$refreshTime = <REFRESH_TIME_HERE>
<TENANT_ID_HERE>
<CLIENT_ID_HERE>
<CLIENT_SECRET_HERE>
<INSERT_USER_ID_HERE>

Additional information

  1. Make sure your registered app has User.Read.All and Presence.Read permissions enabled or else the script will be unable to retrieve any user's presence
  2. In some cases you may receive an UnauthorizedAccess error when attempting to run, to avoid this you must sign the script with a certificate. Information on how to do this can be found here
{
"id": "<BRIDGE_ID_HERE>",
"internalipaddress": "<BRIDGE_IP_HERE>",
"port": 443,
"username": "<AUTH_USERNAME_HERE>",
"selectedLight": {
"id": "<LIGHT_ID_HERE>",
"name": "<LIGHT_NAME_HERE>"
}
}
$refreshTime = 5 # seconds
Write-Host("Reading configuration file...")
function getToken {
$tokenCredentials = @{
'client_id' = '<CLIENT_ID_HERE>'
'client_secret' = '<CLIENT_SECRET_HERE>'
'grant_type' = 'client_credentials'
'scope' = 'https://graph.microsoft.com/.default'
}
$tenantId = "<TENANT_ID_HERE>"
$tokenUrl = "https://login.microsoftonline.com/" + $tenantId + "/oauth2/v2.0/token"
Invoke-RestMethod -Method "POST" -Uri $tokenUrl -Body $tokenCredentials -OutFile token.json
$tokenFile = Get-Content -Path "token.json" | ConvertFrom-Json
$tokenFile | Add-Member -Name "timestamp" -Value ([DateTimeOffset]::Now.ToUnixTimeSeconds()) -MemberType NoteProperty -Force
$tokenFile | ConvertTo-Json | Set-Content -Path "token.json"
}
if (Test-Path "hue-bridge.json") {
Write-Host("Configuration file found.")
$hueBridge = Get-Content -Path "hue-bridge.json" | ConvertFrom-Json
$username = $hueBridge.username
$selectedLight = $hueBridge.selectedLight
Write-Host "Selected hue bridge:" $hueBridge.internalipaddress
Write-Host "Selected light:" $selectedLight.name
Write-Host "Authenticating with Microsoft Graph API..."
if (Test-Path "token.json") {
Write-Host "Getting user presense... (press ctrl+c to exit)"
while ($true) {
$token = Get-Content -Path "token.json" | ConvertFrom-Json
$tokenExpired = ($token.timestamp + $token.expires_in) -lt [DateTimeOffset]::Now.ToUnixTimeSeconds()
if ($tokenExpired) {
Write-Host "Refreshing token..."
getToken
}
else {
$accessToken = $token.access_token
$headers = @{
"Authorization" = "Bearer " + $accessToken
}
$userId = "<INSERT_USER_ID_HERE>"
$presenceUrl = "https://graph.microsoft.com/v1.0/users/" + $userId + "/presence"
Invoke-RestMethod -Method "Get" -Uri $presenceUrl -Headers $headers -OutFile "teams-presence.json"
if (Test-Path "teams-presence.json") {
$teamsPresence = Get-Content -Path "teams-presence.json" | ConvertFrom-Json
Write-Host "User presence:" $teamsPresence.availability
$hueLightState = switch ($teamsPresence.availability) {
"Available" {@{
# Green
"on" = $true
"xy" = 0.1684212314323201, 0.6702906384586004
"bri" = 254
}; break}
"Busy" {@{
# Red
"on" = $true
"xy" = 0.6846051939721574, 0.31285746035737805
"bri" = 254
}; break}
"DoNotDisturb" {@{
# Purple
"on" = $true
"xy" = 0.3121628782561196, 0.14202813046842053
"bri" = 254
}; break}
"BeRightBack" {@{
# Yellow
"on" = $true
"xy" = 0.4441628013823352, 0.5165661629456695
"bri" = 254
}; break}
"Away" {@{
# Yellow
"on" = $true
"xy" = 0.4441628013823352, 0.5165661629456695
"bri" = 254
}; break}
"Offline" {@{
# Off
"on" = $false
}; break}
default {@{
# Off
"on" = $false
}; break}
}
$hueBridgeApiUrl = "http://" + $hueBridge.internalipaddress + "/api/" + $username + "/lights/" + $selectedLight.id + "/state"
Invoke-RestMethod -Method "PUT" -Uri $hueBridgeApiUrl -Body ($hueLightState | ConvertTo-Json) | Out-Null
}
else {
Write-Host("Something went wrong. Unable to find teams status file.")
}
}
start-sleep -seconds $refreshTime
}
}
else {
getToken
}
}
else {
Write-Host("Configuration file not found. Please create a file named hue-bridge.json")
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment