Skip to content

Commit

Permalink
Merge pull request #4 from AntoineGS/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AntoineGS authored Jul 5, 2022
2 parents 84632ab + 03a17d4 commit fab796d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 96 deletions.
132 changes: 66 additions & 66 deletions Get-TeamsStatus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -76,90 +80,86 @@ 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
}

Write-Host "Teams Status: $Status"
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
}
}
33 changes: 19 additions & 14 deletions Lang-en.ps1
Original file line number Diff line number Diff line change
@@ -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"
$csCameraOn = "On"
$csCameraOff = "Off"
33 changes: 19 additions & 14 deletions Lang-nl.ps1
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ $settingsHAUrl = "<HAUrl>" # 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...
Expand Down
5 changes: 5 additions & 0 deletions TSFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit fab796d

Please sign in to comment.