{"id":238295,"date":"2022-07-08T03:25:00","date_gmt":"2022-07-08T07:25:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=238295"},"modified":"2025-05-30T07:24:53","modified_gmt":"2025-05-30T07:24:53","slug":"22-07-08-synclights","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/22-07-08-synclights\/","title":{"rendered":"Sync your Philips Hue Lights with the status of your Travis build"},"content":{"rendered":"\n<p>Let\u2019s 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"usage-and-prerequisites\">Usage and Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Philips Hue Lighting<\/li>\n\n\n\n<li>Raspberry-Pi<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s start with the Ruby version, there\u2019s also a Python version in the repository I\u2019ll provide at the end. Currently this is what&nbsp;<code>hue_lighting.rb<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>require 'hue'\nrequire 'json'\nrequire 'travis\/client'\n\nhue = nil\n\nunless File.exists?('settings.json')\n  puts \"Please setup which Hue lights you want to link on Travis CI first\"\n  exit\nend\n\nbegin\n  puts \"Locating the Hue bridge\"\n\n  hue = Hue::Client.new(\"travis-ci-on-the-piiii\")\n\n  puts \"Hue bridge found at #{client.bridge.ip}\\n\\n\"\nrescue Hue::NoBridgeFound => e\n  puts \"Sorry but no Hue bridge could be found\"\n  exit\nrescue => e\n\nend\n\nbegin\n  hue = Hue::Client.new(\"travis-philips-hue\")\n\n  puts \"Looks like the bridge knows who you are!\\n\\n\"\nrescue Hue::LinkButtonNotPressed => e\n  puts \"Please setup which Hue lights you want to link on Travis CI first\"\n  exit\nend\n\nTRANSITION_TIME = 2\nPASSED = { hue: 25500, brightness: 255, saturation: 255 }\nFAILED = { hue: 65535, brightness: 255, saturation: 255 }\n\nfile = File.read('settings.json')\nmonitored_lights = JSON.parse(file)&#91;'lights']\n\nmessage = \"setup to sync with Travis CI repos\"\n\ncase monitored_lights.size\nwhen 0\n  puts \"No lights #{message}\"\n  puts \"Please setup which Hue lights you want to link on Travis CI first\"\n  exit\nwhen 1\n  puts \"1 light #{message}\"\nelse\n  puts \"#{monitored_lights.size} lights #{message}\"\nend\n\ndef repo_status(travis, slug)\n  repo = travis.repo(slug)\n  master = repo.branch('master')\n  master.state\nrescue => e\n  puts \"had an error fetching the status of repo:#{slug}, moving on\"\n  raise\nend\n\ndef update_light(hue, light_id, state)\n  case state\n  when 'passed'\n    hue.light(light_id).set_state PASSED, TRANSITION_TIME\n  when 'failed'\n    hue.light(light_id).set_state FAILED, TRANSITION_TIME\n  end\nrescue => e\n  puts \"had an error updating light:#{light_id}, moving on\"\n  raise\nend\n\nputs \"Starting monitoring: #{monitored_lights.collect { |ml| ml&#91;\"repo\"] }}\\n\\n\"\ntravis = Travis::Client.new\nloop do\n  travis.clear_cache\n  monitored_lights.each do |ml|\n    begin\n      puts \"checking status of #{ml&#91;\"repo\"]}\"\n      state = repo_status(travis, ml&#91;\"repo\"])\n      puts \"the most recent master build on #{ml&#91;\"repo\"]} #{state}\"\n      update_light(hue, ml&#91;\"light_id\"], state)\n    rescue => e\n    end\n  end\n  sleep 3\n end<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"travis-ci\">Travis CI<\/h2>\n\n\n\n<p>Here\u2019s my&nbsp;<code>.travis.yml<\/code>, this is for Python, but you can swap Ruby in for this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dist: jammy\naddons:\n  apt:\n    packages:\n      - coreutils\nlanguage:\n- python\npython:\n- '3.9'\nscript:\n- chmod u+x \".\/hue.py\"\n- echo \"light changing to {$BUILDSTATUS}\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>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?<\/p>\n\n\n\n<p>As always if you have any questions, any questions at all, please email me at&nbsp;<a href=\"mailto:montana@travis-ci.org\">montana@travis-ci.org<\/a>.<\/p>\n\n\n\n<p>Happy building!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-238295","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=238295"}],"version-history":[{"count":1,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238295\/revisions"}],"predecessor-version":[{"id":242647,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238295\/revisions\/242647"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=238295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=238295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=238295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}