Updated to work with the new Teams client

main
Danny de Vries 6 days ago
parent 7fc3cc5d99
commit fa99696698

@ -41,102 +41,88 @@ If($null -ne $SetStatus){
} }
$params = $params | ConvertTo-Json $params = $params | ConvertTo-Json
Invoke-RestMethod -Uri "$HAUrl/api/states/$entityStatus" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json" try {
Invoke-RestMethod -Uri "$HAUrl/api/states/$entityStatus" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json"
Write-Host "Status set successfully."
} catch {
Write-Error "Failed to set status: $_"
}
break break
} }
# Start monitoring the Teams logfile when no parameter is used to run the script # Start monitoring the Teams logfile when no parameter is used to run the script
DO { DO {
# Get latest MSTeams_ logfile
$latestLogfile = Get-ChildItem -Path "C:\Users\$LocalUsername\AppData\Local\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams\Logs" -Name "MSTeams_*" | Select-Object -Last 1
$MSTeamsLog = Get-Content -Path "C:\Users\$LocalUsername\AppData\Local\Packages\MSTeams_8wekyb3d8bbwe\LocalCache\Microsoft\MSTeams\Logs\$latestLogfile" -Tail 100
# Get Teams Logfile and last icon overlay status # Get Teams Logfile and last icon overlay status
$TeamsStatus = Get-Content -Path $env:APPDATA"\Microsoft\Teams\logs.txt" -Tail 1000 | Select-String -Pattern ` $TeamsStatus = $MSTeamsLog | Select-String -Pattern `
'Setting the taskbar overlay icon -',` 'SetBadge Setting badge:',`
'StatusIndicatorStateService: Added' | Select-Object -Last 1 'SetTaskbarIconOverlay' | Select-Object -Last 1
# Get Teams Logfile and last app update deamon status # Get Teams Logfile and last app update deamon status
$TeamsActivity = Get-Content -Path $env:APPDATA"\Microsoft\Teams\logs.txt" -Tail 1000 | Select-String -Pattern ` $TeamsActivity = $MSTeamsLog | Select-String -Pattern `
'Resuming daemon App updates',` 'NotifyCallActive',`
'Pausing daemon App updates',` 'NotifyCallAccepted',`
'SfB:TeamsNoCall',` 'NotifyCallEnded' | Select-Object -Last 1
'SfB:TeamsPendingCall',`
'SfB:TeamsActiveCall',`
'name: desktop_call_state_change_send, isOngoing' | Select-Object -Last 1
# Get Teams application process # Get Teams application process
$TeamsProcess = Get-Process -Name Teams -ErrorAction SilentlyContinue $TeamsProcess = Get-Process -Name ms-teams -ErrorAction Break
# Check if Teams is running and start monitoring the log if it is # Check if Teams is running and start monitoring the log if it is
If ($null -ne $TeamsProcess) { If ($null -ne $TeamsProcess) {
If($TeamsStatus -eq $null){ } If($null -eq $TeamsStatus){ }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgAvailable*" -or ` ElseIf ($TeamsStatus -like "*available*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added Available*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Available -> NewActivity*") {
$Status = $lgAvailable $Status = $lgAvailable
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgBusy*" -or ` ElseIf ($TeamsStatus -like "*busy*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added Busy*" -or `
$TeamsStatus -like "*Setting the taskbar overlay icon - $lgOnThePhone*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added OnThePhone*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Busy -> NewActivity*") {
$Status = $lgBusy $Status = $lgBusy
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgAway*" -or ` ElseIf ($TeamsStatus -like "*away*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added Away*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Away -> NewActivity*") {
$Status = $lgAway $Status = $lgAway
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgBeRightBack*" -or ` ElseIf ($TeamsStatus -like "*doNotDistrb*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added BeRightBack*" -or ` $TeamsStatus -like "*Do not disturb*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: BeRightBack -> NewActivity*") {
$Status = $lgBeRightBack
Write-Host $Status
}
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgDoNotDisturb *" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added DoNotDisturb*" -or `
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: DoNotDisturb -> NewActivity*") {
$Status = $lgDoNotDisturb $Status = $lgDoNotDisturb
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgFocusing*" -or ` # Dummy - Not tested yet
$TeamsStatus -like "*StatusIndicatorStateService: Added Focusing*" -or ` ElseIf ($TeamsStatus -like "*focusing*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Focusing -> NewActivity*") {
$Status = $lgFocusing $Status = $lgFocusing
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgPresenting*" -or ` # Dummy - Not tested yet
$TeamsStatus -like "*StatusIndicatorStateService: Added Presenting*" -or ` ElseIf ($TeamsStatus -like "*presenting*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Presenting -> NewActivity*") {
$Status = $lgPresenting $Status = $lgPresenting
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgInAMeeting*" -or ` # Dummy - Not tested yet
$TeamsStatus -like "*StatusIndicatorStateService: Added InAMeeting*" -or ` ElseIf ($TeamsStatus -like "*inameeting*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: InAMeeting -> NewActivity*") {
$Status = $lgInAMeeting $Status = $lgInAMeeting
Write-Host $Status Write-Host $Status
} }
ElseIf ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgOffline*" -or ` ElseIf ($TeamsStatus -like "*offline*") {
$TeamsStatus -like "*StatusIndicatorStateService: Added Offline*") {
$Status = $lgOffline $Status = $lgOffline
Write-Host $Status Write-Host $Status
} }
If($TeamsActivity -eq $null){ } If($null -eq $TeamsActivity){ }
ElseIf ($TeamsActivity -like "*Resuming daemon App updates*" -or ` ElseIf ($TeamsActivity -like "*NotifyCallActive*" -or `
$TeamsActivity -like "*SfB:TeamsNoCall*" -or ` $TeamsActivity -like "*NotifyCallAccepted*") {
$TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: false*") {
$Activity = $lgNotInACall
$ActivityIcon = $iconNotInACall
Write-Host $Activity
}
ElseIf ($TeamsActivity -like "*Pausing daemon App updates*" -or `
$TeamsActivity -like "*SfB:TeamsActiveCall*" -or `
$TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: true*") {
$Activity = $lgInACall $Activity = $lgInACall
$ActivityIcon = $iconInACall $ActivityIcon = $iconInACall
Write-Host $Activity Write-Host $Activity
Write-Host $ActivityIcon
}
ElseIf ($TeamsActivity -like "*NotifyCallEnded*") {
$Activity = $lgNotInACall
$ActivityIcon = $iconNotInACall
Write-Host $Activity
Write-Host $ActivityIcon
} }
} }
# Set status to Offline when the Teams application is not running # Set status to Offline when the Teams application is not running
@ -149,7 +135,7 @@ Else {
} }
# Call Home Assistant API to set the status and activity sensors # Call Home Assistant API to set the status and activity sensors
If ($CurrentStatus -ne $Status -and $Status -ne $null) { If ($CurrentStatus -ne $Status -and $null -ne $Status) {
$CurrentStatus = $Status $CurrentStatus = $Status
$params = @{ $params = @{

@ -1,5 +1,5 @@
> [!NOTE] > [!NOTE]
> This solution only works for the legacy version of Microsoft Teams. The new version does not contain a logs.txt file which can be read by the script. Therefor this repository is not maintained anymore. > This solution only works for the new version of Microsoft Teams.
# Introduction # Introduction
We're working a lot at our home office these days. Several people already found inventive solutions to make working in the home office more comfortable. One of these ways is to automate activities in your home automatation system based on your status on Microsoft Teams. We're working a lot at our home office these days. Several people already found inventive solutions to make working in the home office more comfortable. One of these ways is to automate activities in your home automatation system based on your status on Microsoft Teams.
@ -16,7 +16,7 @@ sensor.teams_status displays that availability status of your Teams client based
This solution is created to work with Home Assistant. It will work with any home automation platform that provides an API, but you probably need to change the PowerShell code. This solution is created to work with Home Assistant. It will work with any home automation platform that provides an API, but you probably need to change the PowerShell code.
# Requirements # Requirements
* Create the three Teams sensors in the Home Assistant configuration.yaml file * Create the two Teams sensors in the Home Assistant configuration.yaml file
```yaml ```yaml
input_text: input_text:
teams_status: teams_status:
@ -40,7 +40,7 @@ sensor:
unique_id: sensor.teams_activity unique_id: sensor.teams_activity
``` ```
* Generate a Long-lived access token ([see HA documentation](https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token)) * Generate a Long-lived access token via `https://<HA URL>/profile/security` ([see HA documentation](https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token))
* Copy and temporarily save the token somewhere you can find it later * Copy and temporarily save the token somewhere you can find it later
* Restart Home Assistant to have the new sensors added * Restart Home Assistant to have the new sensors added
* Download the files from this repository and save them to C:\Scripts * Download the files from this repository and save them to C:\Scripts

@ -1,12 +1,12 @@
# Configure the variables below that will be used in the script # Configure the variables below that will be used in the script
$HAToken = "<Insert token>" # Example: eyJ0eXAiOiJKV1... $HAToken = "<Insert token>" # Example: eyJ0eXAiOiJKV1...
$UserName = "<UserName>" # When not sure, open a command prompt and type: echo %USERNAME% $HAUrl = "<HA URL>" # Example: https://yourha.duckdns.org
$HAUrl = "<HAUrl>" # Example: https://yourha.duckdns.org $LocalUsername = "<UserName>" # Example: your.username
# Set language variables below # Set language variables below
$lgAvailable = "Available" $lgAvailable = "Availabble"
$lgBusy = "Busy" $lgBusy = "Busy"
$lgOnThePhone = "On the phone" $lgOnThePhone = "In a call"
$lgAway = "Away" $lgAway = "Away"
$lgBeRightBack = "Be right back" $lgBeRightBack = "Be right back"
$lgDoNotDisturb = "Do not disturb" $lgDoNotDisturb = "Do not disturb"
@ -20,12 +20,9 @@ $lgInACall = "In a call"
# Set icons to use for call activity # Set icons to use for call activity
$iconInACall = "mdi:phone-in-talk-outline" $iconInACall = "mdi:phone-in-talk-outline"
$iconNotInACall = "mdi:phone-off" $iconNotInACall = "mdi:phone-off"
$iconMonitoring = "mdi:api"
# Set entities to post to # Set entities to post to
$entityStatus = "sensor.teams_status" $entityStatus = "sensor.teams_status"
$entityStatusName = "Microsoft Teams status" $entityStatusName = "Microsoft Teams status"
$entityActivity = "sensor.teams_activity" $entityActivity = "sensor.teams_activity"
$entityActivityName = "Microsoft Teams activity" $entityActivityName = "Microsoft Teams activity"
$entityHeartbeat = "binary_sensor.teams_monitoring"
$entityHeartbeatName = "Microsoft Teams monitoring"

Loading…
Cancel
Save