Last weekend I put a WiFi RGB LED strip on the back of my TV. I use my Chromecast on my TV to cast Spotify to pretty frequently for listening to music. One night my roommate said that it would be cool if the lights behind my TV changed color based on what album I was playing through Spotify. So, I started looking into ways to do this.
Thanks to AppDaemon and a Python library called Colorthief, I was able to come up with a solution. Below is the code for my AppDaemon app that changes the color of my LED strip based on my Chromecast’s current album art.
music_lights: module: music_lights class: music_lights ha_url: !secret ha_url media_player: "media_player.adams_tv" photo_attribute: "entity_picture" light: "light.tv_leds"
import appdaemon.appapi as appapi import sys if sys.version_info < (3, 0): from urllib2 import urlopen else: from urllib.request import urlopen import io from colorthief import ColorThief class music_lights(appapi.AppDaemon): def initialize(self): self.listen_state(self.change_led_color, self.args["media_player"], attribute = self.args["photo_attribute"]) def change_led_color(self, entity, attribute, old, new, kwargs): if new != old: rgb_color = self.get_color(self.args["ha_url"] + new) self.turn_on(self.args["light"], rgb_color=[rgb_color, rgb_color, rgb_color]) def get_color(self, url): fd = urlopen(url) f = io.BytesIO(fd.read()) color_thief = ColorThief(f) return color_thief.get_color(quality=3)
Now whenever I’m casting music to my TV through Chromecast, my LEDs change with the album cover that is playing. The app is pretty generic so I could set this up with pretty much any other media player and light combo that I wanted (if the light is RGB and the media player has album art support).
If you’re looking into setting up something like this I suggest you check out the documentation for AppDaemon including @ReneTode’s AppDaemon For Beginner tutorial. Before looking into this I had no idea what AppDaemon was capable of with HA. I had only used it to setup HADashboard before.
If anyone has suggestions for how to improve this or do it in a simpler way, please comment. My lights look super nice