Apple MacBook Pro Touch Bar Home Assistant-ifed

Thought some of you might be interested in seeing what I have been working on using Better Touch Tool on a 2017 15" Touch Bar MacBook Pro and some simple apple scripts.

(I’ll post the config as soon as I am done, but wanted to show what I have so far)

Essentially state info is pulled from the HA REST API and processed using AppleScript. What you can see here is my monthly kWh, current watts, current download and upload speed of the WAN port on my router, and state information for different rooms in my apartment. If a light is on the button is also a light yellow color to indicate the light is on. Touching the button toggles the lights for that room.

I’d love to hear from other Touch Bar users and see what else others would think would really take it to the next level.

12 Likes

Best use for the TouchBar yet! :stuck_out_tongue:
Definitely interested in this!

This is awesome! I’m really interested in how you’ve set this up.

I just got my 2018 MacBook Pro today, so I’ve been playing around attempting to set something similar up.

Can you please help post a sample config? I also wish to try this on my Touch Bar :slight_smile:

Any chance you can give a hint on how you did this?

I’ve got as far as being able to toggle a switch using a shell script widget, but I can’t figure out how to get a state. Tried using the info on https://developers.home-assistant.io/docs/en/external_api_rest.html, but it’s not working. Even copying the curl script for retrieving states, and puting in my own token, address and sensor name doesn’t work. I just get an error ending with “curl: (52) Empty reply from server” number 52"

Nice one! How have you progressed? And are you willing to share the code?

I’ve actually managed to get it working.

This is my code for retrieving the my Lounge Room Temp state.

tell application “JSON Helper”

set loungetempstates to do shell script “curl -X GET -H "Authorization: Bearer REALLYLONGTOKEN" -H "Content-Type: application/json" https://MYDOMAINNAME:PORT/api/states/sensor.temp_lounge”

set loungetempstates to read JSON from loungetempstates

set indoortemp to state of loungetempstates & "°C "

end tell

I’ve added it to my Dark Sky widget so I’ve got inside and outside temp showing. I managed the same for lights, but then realised the the delay between pressing them and the light turning off was annoyingly long. Ended up deleting them. Quicker to just add to Home and tell Siri to turn lights on/off. I planning on adding an alarm system button though. I just haven’t gotten around to it yet.

This is a basic Light Toggle in case someone needs help:

set lightOff to do shell script “curl -X POST -H "Authorization: Bearer REALLYLONGTOKEN" -H "Content-Type: application/json" -d ‘{"entity_id": "light.lounge"}’ https://MYDOMAINNAME:PORT/api/services/light/toggle”

1 Like

Do you by any chance have the xcodeproj file?
@PodPerson

What xcodeproj file?

@PodPerson didn’t you do this in xcode on a Mac?

Nope. Just BTT and script editor

@PodPerson @keatontaylor is there any update for this project? Thanks

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


3 Likes

I succeeded, but is it possible to get the status of a google home and change the song?

That I don’t know. If HA is able to see the song and change it with a service, probably. What do you usually use to view the song & change it?

For anyone that finds this topic. I’ve opened ❤️💻 Macbook Pro's Touch Bar is finally useful: displaying sensors and controlling devices where I post my repo with instructions on how to set everything up! :smiley:

2 Likes