So now that I’m starting to the hang of this I was ecstatic to see that HA detects Roku devices like a champ so I figured I could move my automation from ST which only works for Plex to adjust my lights at night (dim/brighten) depending on the state of the roku. HA picks up the state just fine shows up in the attributes as playing or stopped (not sure about pause yet as it still shows it playing but the history says paused)
So anyways I wrote this
appdaemon.yaml
living_room_light_control_media_roku:
module: living_room_light_media
class: living_room_media_light_control
playerID: media_player.roku_yw000g669229
constrain_start_time: sunset
constrain_end_time: 00:00:00
###############################################
The App
import appdaemon.appapi as appapi
class living_room_media_light_control(appapi.AppDaemon):
def initialize(self):
self.log("Lets adjust the lights")
# Now here is our lights that we need to use
self.living_room_lights = 'light.living_room_lights'
# Now its time for the fun stuff
self.listen_state(self.adjust_light_function,(self.args["playerID"]))
# Found this cool function to convert the brightness to a percentage because well thats easier than doing math
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
# Now for our function
def adjust_light_function(self, entity, attribute, old, new, kwargs):
adjust_light_function = self.get_state((self.args["playerID"]))
self.log("Stepping into the function to adjust the lights")
if new == "playing":
self.log("The TV is playing so lets turn the lights down to 5%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif new == "paused":
self.log("The TV is paused so lets turn the lights to 50%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif new == "stopped":
self.log("The TV is stopped so lets turn the lights to 100%")
brightness = self.brightness_to_percent(100)
self.turn_on(self.living_room_lights, brightness=brightness)
But now for the life of me this is never called when the state changes. Does a roku pass in some other weird value for playing?
I really like the idea of this because it will work with any channel on the roku be it plex/netflix/amazon etc. Once I get this one hammered out I’ll have a new one to put together for the basement because I have 3 different light groups down there to control.
Thank you all for your awesome assitance with this. Yes I did search a lot on this and I can find references to a media_player but nothing specific about a roku. I originally had this written out to not pass in the entityid from the yaml file but then decided that since I will ultimately have 2 devices to control my living room lights that this was easier and cleaner. The 2nd device will be my retro-pi with Kodi on it because thats what I use to watch LiveTV with (yes legally)
And wow just wow I think now looking at this from a whole new perspective I might see it.
Might help maybe if I put in the function to listen too instead of new
import appdaemon.appapi as appapi
class living_room_media_light_control(appapi.AppDaemon):
def initialize(self):
self.log("Lets adjust the lights")
# Now here is our lights that we need to use
self.living_room_lights = 'light.living_room_lights'
# Now its time for the fun stuff
self.listen_state(self.adjust_light_function,(self.args["playerID"]))
# Found this cool function to convert the brightness to a percentage because well thats easier than doing math
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
# Now for our function
def adjust_light_function(self, entity, attribute, old, new, kwargs):
adjust_light_function = self.get_state((self.args["playerID"]))
self.log("Stepping into the function to adjust the lights")
if adjust_light_function == "playing":
self.log("The TV is playing so lets turn the lights down to 5%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif adjust_light_function == "paused":
self.log("The TV is paused so lets turn the lights to 50%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif adjust_light_function == "stopped":
self.log("The TV is stopped so lets turn the lights to 100%")
brightness = self.brightness_to_percent(100)
self.turn_on(self.living_room_lights, brightness=brightness)
you dont need the get state.
new should work as you used it, because new has the latest state.
to make sure you are getting the right things you could use the line
self.log(new)
to see what you are getting.
Will implement that right now and monitor it for the settings. Thank you for the debug trick
1 Like
So without further ado here is the working version of this app.
import appdaemon.appapi as appapi
class living_room_media_light_control(appapi.AppDaemon):
def initialize(self):
self.log("Lets adjust the lights")
# Now here is our lights that we need to use
self.living_room_lights = 'light.living_room_lights'
# Now its time for the fun stuff
self.listen_state(self.adjust_light_function,(self.args["playerID"]))
# Found this cool function to convert the brightness to a percentage because well thats easier than doing math
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
# Now for our function
def adjust_light_function(self, entity, attribute, old, new, kwargs):
self.log(new)
adjust_light_function = self.get_state((self.args["playerID"]))
self.log(new)
self.log("Stepping into the function to adjust the lights")
if new == "playing":
self.log("The TV is playing so lets turn the lights down to 5%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif new == "paused" or new == "idle":
self.log("The TV is paused so lets turn the lights to 50%")
brightness = self.brightness_to_percent(5)
self.turn_on(self.living_room_lights, brightness=brightness)
elif new == "stopped" or new == "home":
self.log("The TV is stopped so lets turn the lights to 100%")
brightness = self.brightness_to_percent(100)
self.turn_on(self.living_room_lights, brightness=brightness)
However one should note that once you enter a channel in the Roku it will change to a playing status even if it isn’t actually playing anything. I’m thinking about doing a delay on this or something. I’m also watching the various states that are called and see if something else changes when I select an item to watch.
1 Like