I am seeing red (lights reacting to my test suite)
TLDR; Im controlling the color of my room lights based on my tests suite using a gem that I wrote while procastinating https://github.com/matiasmoya/tuyaiotlights/
Update: It seems that the API keys expire after 1 month of trial. I had to request a 6-month extension, but after that, it will be 3.5USD per million request. Well, I will be playing with my lights in my test suite until then lol. There's a way to run local requests but it requires booting up Home Assistant, which for some reason is an operative system... I have no RAM to spare in a VM for that.
Project idea
I was bored and I wanted to make my tests more "dramatic". I also just got some Chinese RGB lights and I figured that I could do some fun, productive procrastination exercise with them.
So I researched their integration capabilities and choices that I had to control them.
Honestly, I don’t remember how or where in their app (SmartLife) I found a link to the IoT platform "tuya.com", and found something that looked like an API for my device!
I used ChatGPT to go through the documentation, had to correct it a little bit myself with stuff that I found in my Tuya cloud dashboard, but all-in-all it was pretty straightforward! I had a small Ruby script that connected to my device and changed the lights to red in a few minutes.
The gem and going out-of-scope
Later that day I figured that would be cool to make it a Gem executable, then I ended up pushing it to Rubygems because why not. Then I figured that the original idea was to use it in a hook in my test suite and I was all over the place already lol.
If you want to play around with the idea and you have some of these Chinese light bulbs, the readme in my repo should be enough to get you started
Adding this to my test suite
Here is the snippet that does this in my minitest suite
credentials = Rails.application.credentials.tuya_lights
controller = TuyaIotLights::SmartLightController.new(
credentials[:client_id],
credentials[:secret],
credentials[:device_id]
)
$minitest_failed = false
class Minitest::Test
def after_teardown
if failure
$minitest_failed = true
end
super
end
end
Minitest.after_run do
if $minitest_failed
controller.change_color('red')
else
controller.switch_to_white_mode
end
end
for rspec is a little bit of more ceremony having to register a formatter:
RSpec.configure do |config|
credentials = Rails.application.credentials.tuya_lights
controller = TuyaIotLights::SmartLightController.new(
credentials[:client_id],
credentials[:secret],
credentials[:device_id]
)
failed = false
config.reporter.register_listener(
Class.new {
RSpec::Core::Formatters.register self, :example_failed, :dump_summary
define_method(:initialize) do |output|
@failed = false
end
define_method(:example_failed) do |_notification|
@failed = true
end
define_method(:dump_summary) do |_summary|
if @failed
controller.change_color("red")
else
controller.switch_to_white_mode
end
end
}.new($stdout),
:example_failed,
:dump_summary
)
end
These lights are cheap and fun to use, if you got one you can give it a try using my gem https://github.com/matiasmoya/tuyaiotlights/ (any light that supports SmartLife or Tuya will work)
I will move this feature to a cron job to check my servers status in the next fun-productive procastination session, may look into adding Phillips Hue to the mix too