TeamsStatusMacOS
TeamsStatusMacOS copied to clipboard
Send Microsoft Teams meeting status from MacOS to Home Assistant
Home Assistant Microsoft Teams Status for MacOS
Although the Home Assistant MacOS companion app allows you to use the state of your camera and microphone to determine if you are in a Teams meeting, this method doesn't work if you are in a meeting with both off. The aim of this project is to send your meeting status (In meeting, Not in meeting) to Home Assistant without relying on the status of your camera or microphone.
- If using the old Microsoft Teams application: This is accomplished by reading the Microsoft Teams log.
- If you are using the new version of Microsoft Teams (titled
Microsoft Teams (work or school)): Microsoft no longer writes the state to the log file. This script works around the limitation by looking at thepowerdprocess' logs to determine if/if not in a meeting.
Regardless of which of the two methods mentioned above applies to you, the logs are automatically checked every 2 seconds and the current state is sent to Home Assistant if it differs from the previous state.
Important Note
There are two different methods titled Local and External.
External
If you are having to connect to a work VPN, you likely don't have access to your local network. If this is the case, use the External method.
Note: This method requires that your Home Assistant instance is accessible externally and while connected to your employer's VPN.
Local
If you don't have to connect to your employer's VPN or, in the rare instance, if you are able to access to your local network while connected to your employer's VPN, use the Local method.
Prerequisites
Regardless of which of the two methods you are using, there are few things that will need to be done:
Enable Full Disk Access for sh
- On your Mac, open "System Preferences" and navigate to "Security & Privacy" and select the
Privacytab. - On the left-hand side select "Full Disk Access"
- On the right-hand side, ensure that
shis present and checked. If not, proceed to step 4. - Click on the lock icon in the lower left-hand corner and enter your password.
- If
shalready exists on the right-hand side, but isn't checked, check it to give it Full Disk Access. If it is not present, proceed to step 6. - Click on the
+button. Navigate to your hard drive, then thebinfolder, selectsh, and click "Open". If you don't see thebinfolder when clicking on your hard drive, make hidden folders visible with the following key combination:cmd+shift+. - Make sure
shis checked if it isn't already after adding it.
Create the Teams_Status Directory
- In your
Documentsfolder, create a new folder titledTeams_Status
Create Input Text Helper in Home Assistant
- In Home Assistant, navigate to the
Integrationspage. - At the top of the page, click on
Helpers. - In the lower right-hand corner, click on
+ CREATE HELPER. - Select
Textas your helper. - Name the helper
Microsoft Teams Status. *It is important to name the entity exactly this as the scripts rely on a specific entity_id.
Installation
Download the files from this repository. Depending on which method you are using, follow the corresponding guide below.
Local Method (click to expand)
Move the script file
- Place the file titled
ms-teams-status-local.shinto theTeams_Statusfolder that you previously created in yourDocumentsfolder. - Make the script executable:
- On your Mac, open a terminal window and execute the following command:
chmod +x ~/Documents/Teams_Status/ms-teams-status-local.sh
- Remove the quarantine attribute automatically added to the downloaded script:
xattr -d com.apple.quarantine ~/Documents/Teams_Status/ms-teams-status-local.sh
Move the plist file
- Place the file titled
com.homeassistant.MSTeamsStatusSender-Local.plistinto/Users/yourusername/Library/LaunchAgents
Note: The Library folder is a hidden folder. In order to see it, while inside the folder corresponding to your Mac username, press cmd + shift + .
Create a Long-Lived Token in Home Assistant
- In Home Assistant, click on your Profile (located in the lower left-hand corner).
- Scroll down to the section titled
Long-Lived Access Tokens. - Select
CREATE TOKEN. - Give the token a name and click on
Ok. - You will be presented with a long access token. Be sure to save it in a location as you will need it in the steps that follow.
Edit the script file
- Open the
ms-teams-status-local.shfile (located in your Teams_Status folder) in an editor of your choosing. - Change the
teams_versionvariable to EITHER "Old" (including quotation marks) if you are using the old MS Teams or "New" (including quotation marks) if you are using the new version of MS Teams, also referred to asMicrosoft Teams (work or school) - Place your token inside of the
tokenvariable - Place your local Home Assistant URL inside the
local_urlvariable, e.g.,"http://192.168.1.42:8123" - Save the changes.
Load the plist file
On your Mac, open a terminal window and execute the following command:
launchctl load -w ~/Library/LaunchAgents/com.homeassistant.MSTeamsStatusSender-Local.plist
That's it! Whenever you are logged in on your Mac, the script will run every 2 seconds and update the input_text.microsoft_teams_status entity if the current meeting state differs from the previous state.
External Method (click to expand)
Move the script file
- Place the file titled
ms-teams-status-external.shinto theTeams_Statusfolder that you previously created in yourDocumentsfolder. - Make the script executable:
- On your Mac, open a terminal window and execute the following command:
chmod +x ~/Documents/Teams_Status/ms-teams-status-external.sh
- Remove the quarantine attribute automatically added to the downloaded script:
xattr -d com.apple.quarantine ~/Documents/Teams_Status/ms-teams-status-external.sh
Move the plist file
- Place the file titled
com.homeassistant.MSTeamsStatusSender-External.plistinto/Users/yourusername/Library/LaunchAgents
Note: The Library folder is a hidden folder. In order to see it, while inside the folder corresponding to your Mac username, press cmd + shift + .
Create a Webhook in Home Assistant
- Go to the
Automationssection of Home Assistant. - Create a new automation.
- For the
TriggerselectWebhookand give it an ID. - Click on the gear icon next to the ID field and make sure ONLY the box next to
POSTis checked. - Add a new action and select
Call service. - Search for and select the
input_text.set_valueservice. - Select the
input_text.microsoft_teams_statusentity as the target. - Paste the following into the Value field:
"{{trigger.json.state}}" - Save the Automation.
- If you are a NabuCasa user, you will need to enable the webhook and get the webhook URL by navigating to the
Home Assistant Cloudmenu located in the settings.
For those that prefer YAML, this is the full YAML for the automation above:
- id: '1686539374529'
alias: Microsoft Teams Status
description: ''
trigger:
- platform: webhook
allowed_methods:
- POST
local_only: false
webhook_id: Microsoft_Teams_Status
condition: []
action:
- service: input_text.set_value
data:
value: '{{trigger.json.state}}'
target:
entity_id: input_text.microsoft_teams_status
mode: single
Edit the script file
- Open the
ms-teams-status-external.shfile (located in your Teams_Status folder) in an editor of your choosing. - Change the
teams_versionvariable to EITHER "Old" (including quotation marks) if you are using the old MS Teams or "New" (including quotation marks) if you are using the new version of MS Teams, also referred to asMicrosoft Teams (work or school) - Place your webhook url inside the
webhook_urlvariable - Save the changes.
Load the plist file
On your Mac, open a terminal window and execute the following command:
launchctl load -w ~/Library/LaunchAgents/com.homeassistant.MSTeamsStatusSender-External.plist
That's it! Whenever you are logged in on your Mac, the script will run every 2 seconds and update the input_text.microsoft_teams_status entity if the current meeting state differs from the previous state.
Experiencing Issues?
Any errors that occur while running the script are logged to the error log file ha.msteams-status.err. This file can be located in the tmp folder (hidden folder nested within the hard drive directory as seen below).
Stopping the script
If you no longer want the script to run on your Mac, simply open up a Terminal window and execute the following command (which one depends on your original installation method):
launchctl unload -w ~/Library/LaunchAgents/com.homeassistant.MSTeamsStatusSender-Local.plist
or
launchctl unload -w ~/Library/LaunchAgents/com.homeassistant.MSTeamsStatusSender-External.plist
