Last active
June 21, 2024 13:25
-
-
Save mekaneck/429390e7a530d8002c606f25ce4aa959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Script to obtain meeting state and mute state from mutesync application (www.mutesync.com) | |
; This script will create 2 binary_sensor entities in Home Assistant; one for mute status and one for in_meeting status | |
; | |
; To obtain the status from Mutesync, 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.example.org" | |
; Other Customizeable settings. These can be changed as desired. | |
updatePeriodInSeconds := 10 | |
haMuteSensorEntityName := "mylaptop_mutesync_mute" | |
haMeetingSensorEntityName := "mylaptop_mutesync_meeting" | |
; These settings should not change | |
apiVersion := 1 | |
msURL := "http://127.0.0.1:8249/state" | |
msTokenText := "Token " msToken | |
haMuteURL := haServerURL "/api/states/binary_sensor." haMuteSensorEntityName | |
haMeetingURL := haServerURL "/api/states/binary_sensor." haMeetingSensorEntityName | |
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 | |
try | |
{ | |
oHttp.send() | |
; Wait for the response for 5 seconds | |
oHttp.WaitForResponse(5) | |
responseText := oHttp.responseText | |
;MsgBox % "initial response: " responseText | |
;Parse response for Meeting Status | |
inMeetingStatusLoc := Instr(responseText, """in_meeting"":") + 13 | |
inMeetingStatusRaw := SubStr(responseText, inMeetingStatusLoc, 4) | |
;MsgBox % "raw extract from inMeetingStatusRaw: " inMeetingStatusRaw | |
if (inMeetingStatusRaw = "true") | |
{ | |
;MsgBox In Meeting | |
meetingState := "on" | |
} | |
else | |
{ | |
;MsgBox Not In Meeting | |
meetingState := "off" | |
} | |
; Parse response for Mute Status | |
muteStatusLoc := Instr(responseText, """muted"":") + 8 | |
muteStatusRaw := SubStr(responseText, muteStatusLoc, 4) | |
;MsgBox % "raw extract from muteStatusRaw: " muteStatusRaw | |
if (muteStatusRaw = "true") | |
{ | |
;MsgBox Mute is On | |
muteState := "on" | |
} | |
else | |
{ | |
;MsgBox Mute is Off | |
muteState := "off" | |
} | |
} | |
catch e | |
{ | |
return | |
} | |
FormatTime, nowUTC, %A_NowUTC%, yyyy-MM-ddTHH:mm:ss+00:00 | |
mutePayload = {"state":"%muteState%","attributes":{"last_updated":"%nowUTC%"}} | |
meetingPayload = {"state":"%meetingState%","attributes":{"last_updated":"%nowUTC%"}} | |
; POST request | |
oHttp.Open("POST", haMuteURL, false) | |
; Add token header | |
oHttp.SetRequestHeader("authorization", haTokenText) | |
; Add content type header | |
oHttp.SetRequestHeader("content-type", "application/json") | |
; send Request with payload | |
try | |
{ | |
oHttp.send(mutePayload) | |
; wait for response from HA | |
oHttp.WaitForResponse(5) | |
} | |
catch e | |
{ | |
return | |
} | |
; POST request | |
oHttp.Open("POST", haMeetingURL, false) | |
; Add token header | |
oHttp.SetRequestHeader("authorization", haTokenText) | |
; Add content type header | |
oHttp.SetRequestHeader("content-type", "application/json") | |
; send Request with payload | |
try | |
{ | |
; send Request with payload | |
oHttp.send(meetingPayload) | |
; wait for response from HA | |
oHttp.WaitForResponse(5) | |
} | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment