Displaying Home Assistant entity states in touch bar and using as a button

EDIT: Spent yesterday altering my HA buttons/widgets to make it easy to create more and to fix if my home assistant domain or token needs to be changed.
For anyone who wants to try this themselves, to get your really long token to authorize the script, go to your HA profile, scroll to the bottem and create a Long-Lived Access Token. You need to have JSON Helper installed on your MacBook

First run the following to set your HA domain and token as persistant string 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 in case 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


Supercool @Doa!

Might be a stupid question, but would this also work with Nabu Casa (HA cloud)? I haven't exposed HA on my own IP.