Morning/Evening Colors

Hello everyone for once I finally get to share a code example that I get to post without asking for clarification on something or something else.

I first must give mad thanks out to @aimc and @ReneTode for your wonderful help along my journey as well as everyone else I know I am forgetting some of you wonderful community members who have helped this noob with learning python.

So without anything further I took the tons of lines of code that originally made up my morning/evening colors and instead have it all slimmed down to this.

import appdaemon.plugins.hass.hassapi as hass
import datetime

class colors_music(hass.Hass):

    def initialize(self):
        time = datetime.time(7, 56, 00)
        self.media_player = 'media_player.living_room'
        self.run_daily(self.morning_colors_first_call, time)
        self.run_at_sunset(self.evening_colors_first_call, offset=int(self.args["sunset_offset"]))



    #Morning colors

    def morning_colors_first_call(self, kwargs):
        self.log("First Call to colors")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://www.marineband.marines.mil/Portals/175/Docs/Audio/Ceremonial/first_call.mp3", media_content_type = "music")
        self.run_in(self.attention, 300)
        self.run_in(self.national_anthem, 310)

    def national_anthem(self, kwargs):
        self.log("Playing National Anthem")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://192.168.7.2:8123/local/sounds/morning_colors/the_star-spangled_banner.mp3", media_content_type = "music")
        self.run_in(self.carry_on, 85)



# Evening colors

    def evening_colors_first_call(self, kwargs):
        self.log("Playing First Call")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://www.marineband.marines.mil/Portals/175/Docs/Audio/Ceremonial/first_call.mp3", media_content_type = "music")
        self.run_in(self.attention, 300)
        self.run_in(self.retreat, 310)

    def retreat(self, kwargs):
        self.log("Playing retreat")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://192.168.7.2:8123/local/sounds/evening_colors/Retreat.mp3", media_content_type = "music")
        self.run_in(self.carry_on, 85)

    #Multi Use
    def attention(self,kwargs):
        self.log("Attention")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://192.168.7.2:8123/local/sounds/morning_colors/attention.mp3", media_content_type = "music")

    def carry_on(self, kwargs):
        self.log("Carry On")
        self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 50)
        self.call_service("media_player/play_media", entity_id=self.media_player, media_content_id = "http://192.168.7.2:8123/local/sounds/morning_colors/attention.mp3", media_content_type = "music")

If you would like to grab the sound files that I use for this you can find them here

If you see something that could use some optimization feel free to make suggestions. :slight_smile:

2 Likes