Skip to content

Instantly share code, notes, and snippets.

@syxanash
Created March 26, 2024 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syxanash/b7aee45517cc8ca2f13c479c57b85b50 to your computer and use it in GitHub Desktop.
Save syxanash/b7aee45517cc8ca2f13c479c57b85b50 to your computer and use it in GitHub Desktop.
require 'faye/websocket'
require 'eventmachine'
require 'rest-client'
SMART_BULB = {
ip: 'shelly_ip_addr',
user: 'shelly_user',
password: 'shelly_password'
}
EM.run {
ws = Faye::WebSocket::Client.new('wss://lightbulb.url')
ws.on :open do |event|
puts "Connected to Litebulb!"
end
ws.on :message do |event|
ws_data = event.data
if ws_data.start_with?("BULB")
bulb_status = ws_data.match(%r{^BULB\:(.*?)$})[1]
status_to_change = bulb_status == "1" ? 'on' : 'off'
RestClient::Request.new(
:method => 'get',
:url => "http://#{SMART_BULB[:ip]}/light/0/?turn=#{status_to_change}",
:user => SMART_BULB[:user],
:password => SMART_BULB[:password],
).execute
puts "Bulb has been turned #{status_to_change}"
end
end
ws.on :close do |event|
# p [:close, event.code, event.reason]
ws = nil
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment