Sync your Philips Hue Lights with the status of your Travis build

By Travis CI

Jul 8, 2022

Jul 8, 2022

Let’s have some fun this Friday! Sync the color of your build (red if failed, green if passing) with your Travis CI builds using your Philips Hue lights. Currently this has basic functionality, if the Travis CI build passes, your Hue lights will turn green.n your Travis CI build fails it will turn red.

Usage and Prerequisites

  • Philips Hue Lighting
  • Raspberry-Pi

Let’s start with the Ruby version, there’s also a Python version in the repository I’ll provide at the end. Currently this is what hue_lighting.rb:

require 'hue'
require 'json'
require 'travis/client'

hue = nil

unless File.exists?('settings.json')
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
end

begin
  puts "Locating the Hue bridge"

  hue = Hue::Client.new("travis-ci-on-the-piiii")

  puts "Hue bridge found at #{client.bridge.ip}\n\n"
rescue Hue::NoBridgeFound => e
  puts "Sorry but no Hue bridge could be found"
  exit
rescue => e

end

begin
  hue = Hue::Client.new("travis-philips-hue")

  puts "Looks like the bridge knows who you are!\n\n"
rescue Hue::LinkButtonNotPressed => e
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
end

TRANSITION_TIME = 2
PASSED = { hue: 25500, brightness: 255, saturation: 255 }
FAILED = { hue: 65535, brightness: 255, saturation: 255 }

file = File.read('settings.json')
monitored_lights = JSON.parse(file)['lights']

message = "setup to sync with Travis CI repos"

case monitored_lights.size
when 0
  puts "No lights #{message}"
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
when 1
  puts "1 light #{message}"
else
  puts "#{monitored_lights.size} lights #{message}"
end

def repo_status(travis, slug)
  repo = travis.repo(slug)
  master = repo.branch('master')
  master.state
rescue => e
  puts "had an error fetching the status of repo:#{slug}, moving on"
  raise
end

def update_light(hue, light_id, state)
  case state
  when 'passed'
    hue.light(light_id).set_state PASSED, TRANSITION_TIME
  when 'failed'
    hue.light(light_id).set_state FAILED, TRANSITION_TIME
  end
rescue => e
  puts "had an error updating light:#{light_id}, moving on"
  raise
end

puts "Starting monitoring: #{monitored_lights.collect { |ml| ml["repo"] }}\n\n"
travis = Travis::Client.new
loop do
  travis.clear_cache
  monitored_lights.each do |ml|
    begin
      puts "checking status of #{ml["repo"]}"
      state = repo_status(travis, ml["repo"])
      puts "the most recent master build on #{ml["repo"]} #{state}"
      update_light(hue, ml["light_id"], state)
    rescue => e
    end
  end
  sleep 3
 end

Now make a build that will pass, and make a build that will fail. Your Philips Hue light should be synced with the build status, meaning green if it passed and red it the build failed.

Travis CI

Here’s my .travis.yml, this is for Python, but you can swap Ruby in for this example:

dist: jammy
addons:
  apt:
    packages:
      - coreutils
language:
- python
python:
- '3.9'
script:
- chmod u+x "./hue.py"
- echo "light changing to {$BUILDSTATUS}"

Conclusion

Your lights should be red and green now depending on your build status! Who knew there was more to Travis than just builds, why not have some fun?

As always if you have any questions, any questions at all, please email me at [email protected].

Happy building!