Skip to content

Instantly share code, notes, and snippets.

@ErikDeBruijn
Created March 12, 2017 20:52
Show Gist options
  • Save ErikDeBruijn/576f83b197ec1c8356cf2b01f5e5e9df to your computer and use it in GitHub Desktop.
Save ErikDeBruijn/576f83b197ec1c8356cf2b01f5e5e9df to your computer and use it in GitHub Desktop.
Fibaro to API.ai bridge for Google Home integration
require 'yaml'
require 'pp'
require 'sinatra'
require 'open-uri'
require 'json'
enable :sessions
$config = YAML::load_file('./config.yaml')
configure do
set :bind, $config[:bind_ip]
set :port, $config[:bind_port]
end
$user = nil
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if(@auth.provided? and @auth.basic? and @auth.credentials)
c = @auth.credentials
pp c
$user = $config[:users][c[0]]
$user[:password] == c[1]
end
end
end
get '/' do
pp params
message = ""
message = "#{params['message']}<br>" if params['message']
'Nothing to see here. Move along.<br><hr> '+ message
end
#get '/api/v1/cmd' do
# redirect '/', 301
#end
def fibaroRequest(requestURL)
requestURL = "#{$config[:fibaro_api]}/#{requestURL}"
puts requestURL
open(requestURL, http_basic_authentication: [$config[:fibaro_username],$config[:fibaro_password]]).read
end
def fibaroSceneControl (sceneID,action='start')
requestURL = "sceneControl?id=#{sceneID}&action=#{action}"
puts fibaroRequest(requestURL)
end
def fibaroDeviceAction (deviceID,name='turnOff',arg1)
requestURL = "callAction?deviceID=#{deviceID}&name=#{name}&arg1=#{arg1}"
puts fibaroRequest(requestURL)
end
def parsePercentage(percentageStr)
if percentageStr.to_i.between?(1,100)
percentage = percentageStr.to_i
puts "probably is a percentage"
else
percentage = case percentageStr
when 'partially' then 50
when 'slightly' then 20
when 'mostly' then 70
when 'half way' then 50
when 'halfway' then 50
else 100
end
puts "phrase percentage"
end
#percentage = 100 if percentageStr === ''
puts "Percentage is #{percentage.to_s}% (p str = #{percentageStr} to_i = #{percentageStr.to_i})."
percentage
end
post '/api/v1/webhook' do
protected!
puts "webhook"
payload = JSON.parse(request.body.read)
pp payload
if payload['result']
if payload['result']['parameters']
vars = payload['result']['parameters']
pp vars
case payload['result']['action']
when 'curtains.control'
percentage = parsePercentage(vars['percentage'])
case vars['CurtainShutterState']
when 'close'
fibaroDeviceAction(1330,'setValue',(100 - percentage).to_s)
speech = "Closing."
when 'open'
fibaroDeviceAction(1330,'setValue',percentage.to_s)
speech = "Opening."
else speech = "not clear what to do with the curtains."
end
when 'alarm.control'
case vars['alarmState']
when 'arm' then fibaroSceneControl(37)
when 'disarm' then fibaroSceneControl(38)
else speech = "Invalid alarmstate #{vars['alarmState']}."
end
when 'lights.control'
status = case vars['onOrOff']
when 'off' then 'turnOff'
when 'on' then 'turnOn'
else 'turnOn'
end
roomLights = $config[:rooms]
pp roomLights
deviceID = roomLights[vars['Rooms']]
if deviceID.nil?
if vars['Rooms'] == 'all'
if status == 'turnOff'
fibaroSceneControl(17)
speech = "Let there be darkness."
else speech = "Power bills too low? Why would you?"
end
else
speech = "No known light for room #{vars['Rooms']}."
end
else
if vars['percentage'].empty?
fibaroDeviceAction(deviceID,status,nil)
else
percentage = parsePercentage(vars['percentage'])
fibaroDeviceAction(deviceID,'setValue',percentage)
fibaroDeviceAction(deviceID,'setVolume',percentage)
end
speech = "Turning #{vars['onOrOff']} lights in #{vars['Rooms']}"
end
else
speech = "The action \"#{payload['result']['action']}\" is unfamiliar to me."
end
content_type :json
json_response = { :speech => speech, :displayText => speech, :data => {}, :contextOut => [], :source => "FibaroWeb" }.to_json
puts json_response
json_response
end
end
end
get '/api/v1/device' do
protected!
pp params
status = 'turnOff'
status = 'turnOn' if params['onoroff'] == 'on'
fibaroDeviceAction(params['deviceID'],status)
end
File.open('./config.yaml', 'w') {|f| f.write $config.to_yaml }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment