diff --git a/Get-TeamsStatus.ps1 b/Get-TeamsStatus.ps1 index 3277ae4..374705b 100644 --- a/Get-TeamsStatus.ps1 +++ b/Get-TeamsStatus.ps1 @@ -26,9 +26,6 @@ Param($SetStatus) # Import Settings PowerShell script . ($PSScriptRoot + "\Settings.ps1") -################################################################ -# Don't edit the code below, unless you know what you're doing # -################################################################ $headers = @{"Authorization"="Bearer $HAToken";} $Enable = 1 @@ -45,54 +42,15 @@ If($null -ne $SetStatus){ $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" - break } -# Create function that will be used to set the status of the Windows Service -Function publishOnlineState () -{ - $params = @{ - "state"="updating"; - "attributes"= @{ - "friendly_name"="$entityHeartbeatName"; - "icon"="$iconMonitoring"; - "device_class"="connectivity"; - } - } - - $params = $params | ConvertTo-Json - Invoke-RestMethod -Uri "$HAUrl/api/states/$entityHeartbeat" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json" - - $params = @{ - "state"="on"; - "attributes"= @{ - "friendly_name"="$entityHeartbeatName"; - "icon"="$iconMonitoring"; - "device_class"="connectivity"; - } - } - - $params = $params | ConvertTo-Json - Invoke-RestMethod -Uri "$HAUrl/api/states/$entityHeartbeat" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json" -} - -# Start the stopwatch that will be monitoring the status of the Windows Service -$stopwatch = [system.diagnostics.stopwatch]::StartNew() -# Set the Windows Service heartbeat sensor at startup -publishOnlineState - +# Clear variables $CurrentStatus = "" $CurrentActivity = "" # Start monitoring the Teams logfile when no parameter is used to run the script DO { -# Set the Windows Service heartbeat sensor every 5 minutes and restart the stopwatch -If ([int]$stopwatch.Elapsed.Minutes -ge 4){ - $stopwatch.Restart() - publishOnlineState -} - # Get Teams Logfile and last icon overlay status $TeamsStatus = Get-Content -Path "C:\Users\$UserName\AppData\Roaming\Microsoft\Teams\logs.txt" -Tail 500 | Select-String -Pattern ` 'Setting the taskbar overlay icon -',` @@ -114,7 +72,7 @@ $TeamsProcess = Get-Process -Name Teams -ErrorAction SilentlyContinue If ($null -ne $TeamsProcess) { If ($TeamsStatus -like "*Setting the taskbar overlay icon - $lgAvailable*" -or ` $TeamsStatus -like "*StatusIndicatorStateService: Added Available*" -or ` - $TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Available -> NewActivity*") { + $TeamsStatus -like "*StatusIndicatorStateService: Added NewActivity (current state: Available -> NewActivity*") { $Status = $lgAvailable Write-Host $Status } diff --git a/README.md b/README.md index 8bbb1bb..cfd5bbd 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,9 @@ We're working a lot at our home office these days. Several people already found Microsoft provides the status of your account that is used in Teams via the Graph API. To access the Graph API, your organization needs to grant consent for the organization so everybody can read their Teams status. Since my organization didn't want to grant consent, I needed to find a workaround, which I found in monitoring the Teams client logfile for certain changes. -This script makes use of three sensors that are created in Home Assistant up front: +This script makes use of two sensors that are created in Home Assistant up front: * sensor.teams_status * sensor.teams_activity -* binary_sensor.teams_monitoring sensor.teams_status displays that availability status of your Teams client based on the icon overlay in the taskbar on Windows. sensor.teams_activity shows if you are in a call or not based on the App updates deamon, which is paused as soon as you join a call. @@ -23,9 +22,6 @@ input_text: teams_activity: name: Microsoft Teams activity icon: mdi:phone-off - teams_monitoring: - name: Microsoft Teams monitoring - icon: mdi:api sensor: - platform: template @@ -40,35 +36,20 @@ sensor: value_template: "{{states('input_text.teams_activity')}}" unique_id: sensor.teams_activity -binary_sensor: - - platform: template - sensors: - teams_monitoring: - friendly_name: "Microsoft Teams monitoring" - device_class: connectivity - value_template: >- - {% set lastChange = as_timestamp(states.binary_sensor.teams_monitoring.last_changed) %} - {% set now = as_timestamp(now()) %} - {% set noUpdateForMins = (now - lastChange) / 60 %} - {% if noUpdateForMins > 5 %} - off - {% else %} - {{states('input_text.teams_monitoring')}} - {% endif %} - icon_template: "{{state_attr('input_text.teams_monitoring','icon')}}" - unique_id: binary_sensor.teams_monitoring ``` * Generate a Long-lived access token ([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 * Restart Home Assistant to have the new sensors added * Download the files from this repository and save them to C:\Scripts -* Edit the Get-TeamsStatus.ps1 script and: +* Edit the Settings.ps1 file and: * Replace `` with the token you generated * Replace `` with the username that is logged in to Teams and you want to monitor * Replace `` with the URL to your Home Assistant server + * Adjust the language settings to your preferences * Start a elevated PowerShell prompt, browse to C:\Scripts and run the following command: ```powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned +Unblock-File .\Settings.ps1 Unblock-File .\Get-TeamsStatus.ps1 Start-Process -FilePath .\nssm.exe -ArgumentList 'install "Microsoft Teams Status Monitor" "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-command "& { . C:\Scripts\Get-TeamsStatus.ps1 }"" ' -NoNewWindow -Wait Start-Service -Name "Microsoft Teams Status Monitor"