Skip to content

Instantly share code, notes, and snippets.

@mekaneck
Created January 23, 2022 17:43
Show Gist options
  • Save mekaneck/5dab1b5c49e41f6920b58b20ada6ea33 to your computer and use it in GitHub Desktop.
Save mekaneck/5dab1b5c49e41f6920b58b20ada6ea33 to your computer and use it in GitHub Desktop.
This is an AutoHotKey script which will push the status of the "mutesync" application to a sensor in Home Assistant
; Script to obtain meeting state and mute state from mutesync application (www.mutesync.com)
;
; To obtain the Mutesync status, a token must be obtained from the Mutesync application.
; 1. Choose mutesync preferences, authentication tab, and check "allow external app"
; 2. Open a browser and navigate to http://127.0.0.1:8249/authenticate
; 3. Copy the 16-character token and paste it into this next line between the quotes.
msToken := "ABCDEFGHIJKLMNOP"
; For communicating with Home Assistant, a long-lived access token is needed.
; 1. Open up Home Assistant and select your user name
; 2. Scroll to the bottom of the screen and select "create token".
; 3. Name it something like Mutesynk Autohotkey, and copy it in the next line between the quotes.
haToken := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
; Also to communicate with Home Assistant, we need its URL. If you want this
; script to work remotely or when on VPN, this must be accessible remotely.
haServerURL := "https://yourHAserver.duckdns.org"
; Other Customizeable settings. These can be changed as desired.
updatePeriodInSeconds := 10
haSensorEntityName := "mylaptop_mutesync"
; These settings should not change
apiVersion := 1
msURL := "http://127.0.0.1:8249/state"
msTokenText := "Token " msToken
haURL := haServerURL "/api/states/sensor." haSensorEntityName
haTokenText := "Bearer " haToken
StringCaseSense Off
; End of configuration settings
; ------------------------------------------------------------------------------
#Persistent
; Execute the script once immediately upon launch
Gosub UpdateStatus
; Repeat the execution periodically
updatePeriodInMilliseconds := updatePeriodInSeconds * 1000
SetTimer, UpdateStatus, %updatePeriodInMilliseconds%
return
UpdateStatus:
; Create oHttp object
oHttp := ComObjCreate("WinHttp.Winhttprequest.5.1")
; GET request, synchronous mode
oHttp.Open("GET", msURL, false)
; Add token header
oHttP.SetRequestHeader("Authorization", msTokenText)
; Add API version header
oHttP.SetRequestHeader("x-mutesync-api-version", apiVersion)
; send Request
oHttp.send()
; Wait for the response for 5 seconds
oHttp.WaitForResponse(5, boolResponse)
responseText := oHttp.responseText
; Format the payload so HA will accept it
; Replace the data key with the attributes key
payload := StrReplace(responseText, """data"":", """attributes"":")
; Strip off the last two brackets
payload := SubStr(payload, 1, -2)
; Format the current UTC date/time in HA's preferred format
FormatTime, nowUTC, %A_NowUTC%, yyyy-MM-ddTHH:mm:ss+00:00
; Within the attributes data, add a "last updated" key with the UTC time as the value
payload := Format("{1}, ""last_updated"":""{2}""", payload, nowUTC)
; Add a state (outside of attributes) because it is required for a sensor in HA
payload := Format("{1}}, ""state"":""on"" }", payload)
; POST request
oHttp.Open("POST", haURL, false)
; Add token header
oHttP.SetRequestHeader("authorization", haTokenText)
; Add content type header
oHttP.SetRequestHeader("content-type", "application/json")
; send Request with payload
oHttp.send(payload)
; wait for response from HA
oHttp.WaitForResponse(5, boolResponse)
;responseText := oHttp.responseText
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment