If you’re using BTT, it really is pretty easy as long as you have JSON Helper installed on your macbook. I’ve now got them to show/toggle my alarm system, lights, tv, speaker, aircon, house temp.
Just create a Long-Lived Access Token in your home assistant.
EDIT: In case anyone else decides to use this and wants to set up a lot of buttons/widgets, it’s actually alot easier to use persistent string variables and copy/paste everything, especially if you ever end up changing your HA domain or the auth codes
First run the following to set your variables:
tell application "BetterTouchTool" to set_persistent_string_variable hasite to "YOURDOMAIN"
tell application "BetterTouchTool" to set_persistent_string_variable authcode to "LONGLIVETOKEN"
I’ve actually created a button prompts for me to enter a new domain name and authcode incase I want to change either.
Once they’re set you just need to retrieve the variables, and set your entity_id & which service you want to use.
Get state of an entity:
-- change this
set entity to "YOURENTITY_ID" -- eg "switch.tv"
-- don't alter the rest of this
tell application "BetterTouchTool" to set hasite to get_string_variable "hasite"
tell application "BetterTouchTool" to set authcode to get_string_variable "authcode"
tell application "JSON Helper" to set EntityState to State of (read JSON from (do shell script "curl -X GET -H \"Authorization: Bearer " & authcode & "\" -H \"Content-Type: application/json\" " & hasite & "/api/states/" & entity))
return EntityState
Call Service:
-- change these 2
set entity to "YOURENTITY_ID" -- eg "switch.tv"
set service to "DOMAIN/SERVICE" -- eg "switch/toggle"
-- don't alter the rest of this
tell application "BetterTouchTool" to set hasite to get_string_variable "hasite"
tell application "BetterTouchTool" to set authcode to get_string_variable "authcode"
set service to do shell script "curl -X POST -H \"Authorization: Bearer " & authcode & "\" -H \"Content-Type: application/json\" -d '{\"entity_id\": \"" & entity & "\"}' " & hasite & "/api/services/" & service
Set an entity state (such as input_select):
-- change these 2
set entity to "YOURENTITY_ID" -- ie "input_select.tv_source"
set Option to "OPTION/NUMBER/Etc" -- ie "Netflix"
-- don't alter the rest of this
tell application "BetterTouchTool" to set hasite to get_string_variable "hasite"
tell application "BetterTouchTool" to set authcode to get_string_variable "authcode"
set service to do shell script "curl -X POST -H \"Authorization: Bearer " & authcode & "\" -H \"Content-Type: application/json\" -d '{\"state\": \"" & Option & "\"}' " & hasite & "/api/states/" & entity
As a full example, for my home alarm system, I’ve got some icons (sheild/bells) saved in a folder and use this to have a widget display them so I know the state of the alarm system:
set entity to "alarm_control_panel.security"
tell application "BetterTouchTool" to set hasite to get_string_variable "hasite"
tell application "BetterTouchTool" to set authcode to get_string_variable "authcode"
tell application "JSON Helper" to set states to State of (read JSON from (do shell script "curl -X GET -H \"Authorization: Bearer " & authcode & "\" -H \"Content-Type: application/json\" " & hasite & "/api/states/" & entity))
if states is "armed_home" then
set Alarm to "Home"
else if states is "armed_night" then
set Alarm to "Night"
else if states is "armed_away" then
set Alarm to "Away"
else if states is "disarmed" then
set Alarm to "Disarmed"
else
set Alarm to "Pending"
end if
set IconPath to ("Macintosh HD:Users:ME:Official:Accounts:BTT:BTTFilesKEEP:Alarm:" & Alarm & ".png") as string
return "{\"icon_path\":\"" & (POSIX path of IconPath as text) & "\", \"text\":\"" & " " & "\", }"
Then for the asigned action, I have a template switch which switches the alarm between disarmed and armed_home.
set entity to "switch.security_home" as string
set service to "switch/toggle"
tell application "BetterTouchTool" to set hasite to get_string_variable "hasite"
tell application "BetterTouchTool" to set authcode to get_string_variable "authcode"
set service to do shell script "curl -X POST -H \"Authorization: Bearer " & authcode & "\" -H \"Content-Type: application/json\" -d '{\"entity_id\": \"" & entity & "\"}' " & hasite & "/api/services/" & service