Last active
July 2, 2022 15:52
-
-
Save ttscoff/a120fe2154704dcada186dd735966aed to your computer and use it in GitHub Desktop.
Indigo control for BetterTouchTool
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
#!/usr/bin/env ruby | |
# Indigo control for BetterTouchTool | |
# | |
# Usage: | |
# | |
# To query a device status | |
# /path/to/btt_indigo.rb status "Device Name" | |
# | |
# To toggle a device: | |
# /path/to/btt_indigo.rb toggle "Device Name to Query for state" "Action On Group Name" "Action Off Group Name" | |
# | |
# To run an action | |
# /path/to/btt_indigo.rb action "Action Group Name" | |
require 'json' | |
require 'cgi' | |
require 'uri' | |
require 'net/https' | |
USER = 'xxx' | |
PASSWORD = 'xxx' | |
action, device, toggle = nil | |
case ARGV[0] | |
when /status/ | |
device = ARGV[1] | |
when /action/ | |
action = ARGV[1] | |
when /toggle/ | |
raise StandardError, "Toggle requires 3 arguments (device, on action, off action)", [] if ARGV.count != 4 | |
toggle = [ARGV[1], ARGV[2], ARGV[3]] | |
end | |
def api_call(query) | |
query.sub!(/^\//,'') | |
url = URI.parse("http://192.168.1.9:8177/#{query}") | |
res = Net::HTTP.start(url.host, url.port, :use_ssl => false) do |http| | |
request = Net::HTTP::Get.new url.request_uri | |
request.basic_auth USER, PASSWORD | |
res = http.request request | |
res.body.force_encoding('utf-8') | |
end | |
res | |
end | |
def status(name) | |
on_color = '165,218,120,255' | |
off_color = '95,106,129,255' | |
res = api_call("devices/#{escape(name)}.json") | |
data = JSON.parse(res) | |
is_on = data['isOn'] | |
brightness = res['brightness'] | |
print "{ \"BTTStreamDeckBackgroundColor\": \"#{is_on ? on_color : off_color }\"}" | |
end | |
def escape(string) | |
string.gsub(/ /, '%20') | |
end | |
def action(name) | |
api_call("actions/#{escape(name)}?_method=execute") | |
end | |
def toggle(name, on_action, off_action) | |
res = api_call("devices/#{escape(name)}.json") | |
data = JSON.parse(res) | |
is_on = data['isOn'] | |
if is_on | |
action(off_action) | |
else | |
action(on_action) | |
end | |
end | |
if device | |
status(device) | |
elsif toggle | |
toggle(*toggle) | |
else | |
action(action) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment