diff --git a/Get-TeamsStatus.ps1 b/Get-TeamsStatus.ps1 index 4005c3e..5d67c55 100644 --- a/Get-TeamsStatus.ps1 +++ b/Get-TeamsStatus.ps1 @@ -2,9 +2,9 @@ .NOTES Name: Get-TeamsStatus.ps1 Original Author: Danny de Vries - Maintainer: + Maintainer: Antoine G Simard Requires: PowerShell v2 or higher - Version History: https://github.com/EBOOZ/TeamsStatus/commits/main + GitHub: https://github.com/AntoineGS/TeamsStatusV2 .SYNOPSIS Sets the status of the Microsoft Teams client to Home Assistant. .DESCRIPTION @@ -34,26 +34,30 @@ $HAToken = if ([string]::IsNullOrEmpty($env:TSHATOKEN)) {$settingsHAToken} else $HAUrl = if ([string]::IsNullOrEmpty($env:TSHAURL)) {$settingsHAUrl} else {$env:TSHAURL} $headers = @{"Authorization"="Bearer $HAToken";} -$statusActivityHash = @{ - $lgAvailable = "Available" - $lgBusy = "Busy" - $lgAway = "Away" - $lgBeRightBack = "BeRightBack" - $lgDoNotDisturb = "DoNotDisturb" - $lgFocusing = "Focusing" - $lgPresenting = "Presenting" - $lgInAMeeting = "InAMeeting" - $lgOffline = "Offline" +$teamsStatusHash = @{ + # Teams short name = @{Teams long name = HA display name} + # Can be set manually in Teams + "Available" = @{"Available" = $tsAvailable} + "Busy" = @{"Busy" = $tsBusy} + "Away" = @{"Away" = $tsAway} + "BeRightBack" = @{"Be right back" = $tsBeRightBack} + "DoNotDisturb" = @{"Do not disturb" = $tsDoNotDisturb} + "Offline" = @{"Offline" = $tsOffline} + # Automated statuses + "Focusing" = @{"Focusing" = $tsFocusing} + "Presenting" = @{"Presenting" = $tsPresenting} + "InAMeeting" = @{"In a meeting" = $tsInAMeeting} + "OnThePhone" = @{"On the phone" = $tsOnThePhone} } # Ensure these are initialized to null so the first hit triggers an update in HA -$CurrentStatus = $null -$CurrentActivity = $null -$CurrentWebcamStatus = $null +$currentStatus = $null +$currentActivity = $null +$currentCamStatus = $null # Some defaults -$WebcamStatus = $lgCameraOff -$WebcamIcon = "mdi:camera-off" +$camStatus = $csCameraOff +$camIcon = "mdi:camera-off" $defaultIcon = "mdi:microsoft-teams" # Run the script when a parameter is used and stop when done @@ -76,46 +80,47 @@ Get-Content -Path "C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Teams\logs.t 'SfB:TeamsNoCall',` 'SfB:TeamsPendingCall',` 'SfB:TeamsActiveCall',` - 'name: desktop_call_state_change_send, isOngoing' | Select-Object -Last 1 + 'name: desktop_call_state_change_send, isOngoing',` + 'Attempting to play audio for notification type 1' | Select-Object -Last 1 # Get Teams application process $TeamsProcess = Get-Process -Name Teams -ErrorAction SilentlyContinue # Check if Teams is running and start monitoring the log if it is If ($null -ne $TeamsProcess) { - If($TeamsStatus -eq $null){ } - ElseIf($TeamsStatus -like "*Setting the taskbar overlay icon - $lgOnThePhone*" -or ` - $TeamsStatus -like "*StatusIndicatorStateService: Added OnThePhone*") { - $Status = $lgBusy - } - Else { - $statusActivityHash.GetEnumerator() | ForEach-Object { - If ($TeamsStatus -like "*Setting the taskbar overlay icon - $($_.key)*" -or ` - $TeamsStatus -like "*StatusIndicatorStateService: Added $($_.value)*" -or ` - $TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: $($_.value) -> NewActivity*") { - $Status = $($_.key) + If($null -ne $TeamsStatus) { + $teamsStatusHash.GetEnumerator() | ForEach-Object { + If ($TeamsStatus -like "*Setting the taskbar overlay icon - $($_.value.keys[0])*" -or ` + $TeamsStatus -like "*StatusIndicatorStateService: Added $($_.key)*" -or ` + $TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: $($_.key) -> NewActivity*") { + $Status = $($_.value.values[0]) } } } - If($TeamsActivity -eq $null){ } - ElseIf ($TeamsActivity -like "*Resuming daemon App updates*" -or ` - $TeamsActivity -like "*SfB:TeamsNoCall*" -or ` - $TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: false*") { - $Activity = $lgNotInACall - $ActivityIcon = $iconNotInACall - } - 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 - $ActivityIcon = $iconInACall + If($null -ne $TeamsActivity){ + If ($TeamsActivity -like "*Resuming daemon App updates*" -or ` + $TeamsActivity -like "*SfB:TeamsNoCall*" -or ` + $TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: false*") { + $Activity = $taNotInACall + $ActivityIcon = $iconNotInACall + } + ElseIf ($TeamsActivity -like "*Pausing daemon App updates*" -or ` + $TeamsActivity -like "*SfB:TeamsActiveCall*" -or ` + $TeamsActivity -like "*name: desktop_call_state_change_send, isOngoing: true*") { + $Activity = $taInACall + $ActivityIcon = $iconInACall + } + ElseIf ($TeamsActivity -like "*Attempting to play audio for notification type 1*") { + $Activity = $taIncomingCall + $ActivityIcon = $iconInACall + } } } # Set status to Offline when the Teams application is not running Else { - $Status = $lgOffline - $Activity = $lgNotInACall + $Status = $tsOffline + $Activity = $taNotInACall $ActivityIcon = $iconNotInACall } @@ -123,43 +128,38 @@ Get-Content -Path "C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Teams\logs.t Write-Host "Teams Activity: $Activity" # Webcam support (sensor.teams_cam_status) - # While in a call, we poke the registry for cam status (maybe too often), but I could not find a log to maches a trigger - # to turn on and off the camera so it might be hit or miss, moreso when leaving a call to ensure something - # triggers the log to set it to Off - If($Activity -eq $lgInACall -or $WebcamStatus -eq $lgCameraOn) { + # While in a call, we poke the registry for cam status (maybe too often), but I could not find a log entry to use as a trigger + # to know when to check the camera status so it might be hit or miss. + # When leaving a call it maybe not trigger as something non-camera related needs to get logged to trigger the check. + If($Activity -eq $taInACall -or $camStatus -eq $csCameraOn) { $registryPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged\" + "C:#Users#$env:USERNAME#AppData#Local#Microsoft#Teams#current#Teams.exe" $webcam = Get-ItemProperty -Path $registryPath -Name LastUsedTimeStop | select LastUsedTimeStop - if ($webcam.LastUsedTimeStop -eq 0) { - $WebcamStatus = $lgCameraOn - $WebcamIcon = "mdi:camera" + If ($webcam.LastUsedTimeStop -eq 0) { + $camStatus = $csCameraOn + $camIcon = "mdi:camera" } - else { - $WebcamStatus = $lgCameraOff - $WebcamIcon = "mdi:camera-off" + Else { + $camStatus = $csCameraOff + $camIcon = "mdi:camera-off" } } # Call Home Assistant API to set the status and activity sensors - If ($CurrentStatus -ne $Status -and $Status -ne $null) { - $CurrentStatus = $Status - - # Use default credentials in the case of a proxy server - $Wcl = new-object System.Net.WebClient - $Wcl.Headers.Add("user-agent", "PowerShell Script") - $Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials - InvokeHA -state $CurrentStatus -friendlyName $entityStatusName -icon $defaultIcon -entityId $entityStatusId + If ($currentStatus -ne $Status -and $Status -ne $null) { + $currentStatus = $Status + InvokeHA -state $currentStatus -friendlyName $entityStatusName -icon $defaultIcon -entityId $entityStatusId } - If ($CurrentActivity -ne $Activity) { - $CurrentActivity = $Activity + If ($currentActivity -ne $Activity) { + $currentActivity = $Activity InvokeHA -state $Activity -friendlyName $entityActivityName -icon $ActivityIcon -entityId $entityActivityId } - If ($null -ne $WebcamStatus -and $CurrentWebcamStatus -ne $WebcamStatus) { - $CurrentWebcamStatus = $WebcamStatus - InvokeHA -state $WebcamStatus -friendlyName $entityCamStatusName -icon $WebcamIcon -entityId $entityCamStatusId + If ($null -ne $camStatus -and $currentCamStatus -ne $camStatus) { + $currentCamStatus = $camStatus + InvokeHA -state $camStatus -friendlyName $entityCamStatusName -icon $camIcon -entityId $entityCamStatusId } } \ No newline at end of file diff --git a/Lang-en.ps1 b/Lang-en.ps1 index 6fb9b8b..476b741 100644 --- a/Lang-en.ps1 +++ b/Lang-en.ps1 @@ -1,15 +1,20 @@ -$lgAvailable = "Available" -$lgBusy = "Busy" -$lgOnThePhone = "On the phone" -$lgAway = "Away" -$lgBeRightBack = "Be right back" -$lgDoNotDisturb = "Do not disturb" -$lgPresenting = "Presenting" -$lgFocusing = "Focusing" -$lgInAMeeting = "In a meeting" -$lgOffline = "Offline" -$lgNotInACall = "Not in a call" -$lgInACall = "In a call" +# sensor.teams_activity +$taNotInACall = "Not in a call" +$taIncomingCall = "Incoming call" +$taInACall = "In a call" + +# sensor.teams_status +$tsAvailable = "Available" +$tsBusy = "Busy" +$tsAway = "Away" +$tsBeRightBack = "Be right back" +$tsDoNotDisturb = "Do not disturb" +$tsOffline = "Offline" +$tsFocusing = "Focusing" +$tsPresenting = "Presenting" +$tsInAMeeting = "In a meeting" +$tsOnThePhone = "On the phone" + # sensor.teams_cam_status -$lgCameraOn = "On" -$lgCameraOff = "Off" \ No newline at end of file +$csCameraOn = "On" +$csCameraOff = "Off" \ No newline at end of file diff --git a/Lang-nl.ps1 b/Lang-nl.ps1 index 79247e8..4d6072a 100644 --- a/Lang-nl.ps1 +++ b/Lang-nl.ps1 @@ -1,15 +1,20 @@ -$lgAvailable = "Beschikbaar" -$lgBusy = "Bezet" -$lgOnThePhone = "Aan de telefoon" -$lgAway = "Afwezig" -$lgBeRightBack = "Zo terug" -$lgDoNotDisturb = "Niet storen" -$lgPresenting = "Presenteren" -$lgFocusing = "Focussen" -$lgInAMeeting = "In een vergadering" -$lgOffline = "Offline" -$lgNotInACall = "Niet in gesprek" -$lgInACall = "In gesprek" +# sensor.teams_activity +$taNotInACall = "Niet in gesprek" +$taIncomingCall = "Binnenkomend telefoongesprek" +$taInACall = "In gesprek" + +# sensor.teams_status +$tsAvailable = "Beschikbaar" +$tsBusy = "Bezet" +$tsAway = "Afwezig" +$tsBeRightBack = "Zo terug" +$tsDoNotDisturb = "Niet storen" +$tsOffline = "Offline" +$tsFocusing = "Focussen" +$tsPresenting = "Presenteren" +$tsInAMeeting = "In een vergadering" +$tsOnThePhone = "Aan de telefoon" + # sensor.teams_cam_status -$lgCameraOn = "Aan" -$lgCameraOff = "Uit" +$csCameraOn = "Aan" +$csCameraOff = "Uit" \ No newline at end of file diff --git a/Settings.ps1 b/Settings.ps1 index cc59c24..125008d 100644 --- a/Settings.ps1 +++ b/Settings.ps1 @@ -6,9 +6,9 @@ $settingsHAUrl = "" # Example: https://yourha.duckdns.org or http://192.1 $Lang = "en" # Set icons to use for call activity -$iconInACall = "mdi:phone-in-talk-outline" +$iconInACall = "mdi:phone-in-talk" +$iconIncomingCall = "mdi:phone-incoming" $iconNotInACall = "mdi:phone-off" -$iconMonitoring = "mdi:api" # Set entities to post to # Friendly names are required or they get reset in HA through the API... diff --git a/TSFunctions.ps1 b/TSFunctions.ps1 index 3317ff3..9aad634 100644 --- a/TSFunctions.ps1 +++ b/TSFunctions.ps1 @@ -7,6 +7,11 @@ function InvokeHA { param ([string]$state, [string]$friendlyName, [string]$icon, [string]$entityId) + # Use default credentials in the case of a proxy server, not sure if this is doing anything as $Wcl is not used anywhere + $Wcl = new-object System.Net.WebClient + $Wcl.Headers.Add("user-agent", "PowerShell Script") + $Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials + Write-Host ("Setting <"+$friendlyName+"> to <"+$state+">:") $params = @{ "state"="$state";