Created
June 1, 2022 19:15
-
-
Save Anderen2/eb2fc7bceb91142b19a47d63fcf9087e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wakemeup.py | |
import appdaemon.plugins.hass.hassapi as hass | |
class WakeMeUp(hass.Hass): | |
def initialize(self): | |
self.set_state(entity="sensor.sleepalarm", state="Not set") | |
self.listen_state(self.time_set, "sensor.sleepalarm") | |
self.log("Initialized WakeMeUp") | |
# This runs whenever the alarm has been set | |
def time_set(self, entity, attribute, old, new, kwargs): | |
if new == "Not set": | |
return | |
# Timings | |
LIGHTS_OFF_AFTER_ALARM_SET = 25 | |
TIME_BEFORE_ALARM = 3600 | |
COFFEETIME_AFTER_ALARM = 30 | |
RADIOTIME_BEFORE_ALARM = 1200 | |
RADIOTIME_MID_BEFORE_ALARM = 1000 | |
RADIOTIME_MORE_MID_BEFORE_ALARM = 600 | |
RADIOTIME_FULL_BEFORE_ALARM = 300 | |
self.log("Alarm set to %r" % new) | |
seconds_until = self.entities.sensor.sleepalarm.attributes.seconds_until.replace(",",".") | |
self.log("Seconds until: %r" % seconds_until) | |
self.log("Seconds until -pre: %r" % (int(float(seconds_until))-TIME_BEFORE_ALARM)) | |
self.time_handle = self.run_in(self.alarm_pre_off, int(float(seconds_until))-TIME_BEFORE_ALARM) | |
self.coffee_time_handle = self.run_in(self.coffee_pre, int(float(seconds_until))+COFFEETIME_AFTER_ALARM) | |
self.radio_time_handle = self.run_in(self.radio_pre, int(float(seconds_until))-RADIOTIME_BEFORE_ALARM) | |
self.radio_time_mid_handle = self.run_in(self.radio_pre_mid, int(float(seconds_until))-RADIOTIME_MID_BEFORE_ALARM) | |
self.radio_time_more_mid_handle = self.run_in(self.radio_pre_more_mid, int(float(seconds_until))-RADIOTIME_MORE_MID_BEFORE_ALARM) | |
self.radio_time_full_handle = self.run_in(self.radio_pre_full, int(float(seconds_until))-RADIOTIME_FULL_BEFORE_ALARM) | |
if self.entities.light.bedroom.state == "on": | |
self.turn_on("light.bedroom", brightness=10, transition=10) | |
self.run_in(self.wait_sleep_start, LIGHTS_OFF_AFTER_ALARM_SET) | |
# This runs after LIGHTS_OFF_AFTER_ALARM_SET | |
def wait_sleep_start(self, kwargs): | |
self.turn_off("light.bedroom") | |
self.log("wait_sleep_start: Light turned off") | |
# This runs TIME_BEFORE_ALARM | |
def alarm_pre_off(self, kwargs): | |
self.turn_on("light.bedroom", brightness=10) | |
self.log("alarm_pre_off: Light turned on") | |
self.turn_on("light.bedroom", brightness=255, transition=1200) | |
self.log("alarm_pre_off: Light faded up") | |
# This runs COFFEETIME_AFTER_ALARM | |
def coffee_pre(self, kwargs): | |
self.turn_on("script.brew_coffee") | |
self.log("alarm_pre: Coffee brewing") | |
# This runs RADIOTIME_BEFORE_ALARM | |
def radio_pre(self, kwargs): | |
# Set Bedroom volume | |
self.call_service("media_player/volume_set", entity_id="media_player.snapcast_client_55_2", volume_level="0.3") | |
self.call_service("media_player/volume_mute", entity_id="media_player.snapcast_client_55_2", is_volume_muted=False) | |
# Mute all other snapcast players | |
self.call_service("media_player/volume_mute", entity_id="media_player.snapcast_client_9_a4", is_volume_muted=True) #Patio | |
self.call_service("media_player/volume_mute", entity_id="media_player.snapcast_client_8_30", is_volume_muted=True) #Livingroom | |
self.call_service("media_player/volume_mute", entity_id="media_player.snapcast_client_a7_2", is_volume_muted=True) #Hobbyroom | |
self.call_service("media_player/volume_mute", entity_id="media_player.snapcast_client_4f_2", is_volume_muted=True) #Phone | |
# Set MPD to play radio | |
try: | |
self.call_service("media_player/play_media", entity_id="media_player.mpd", media_content_type="playlist", media_content_id="NRK P3") | |
except Exception as err: | |
self.log("alarm_pre: MPD being unreliable ({0}), assuming OK state".format(err)) | |
# Set Snapcast to MPD | |
self.call_service("media_player/select_source", entity_id="media_player.snapcast_group_8a6235a3_9207_41c7_c140_af04c5e0b2fb", source="MPD") | |
self.log("alarm_pre: Radio on") | |
def radio_pre_mid(self, kwargs): | |
# Set Bedroom volume | |
self.call_service("media_player/volume_set", entity_id="media_player.snapcast_client_55_2", volume_level="0.4") | |
self.log("alarm_pre: Radio volume mid") | |
def radio_pre_more_mid(self, kwargs): | |
# Set Bedroom volume | |
self.call_service("media_player/volume_set", entity_id="media_player.snapcast_client_55_2", volume_level="0.55") | |
self.log("alarm_pre: Radio volume mid+") | |
def radio_pre_full(self, kwargs): | |
# Set Bedroom volume | |
self.call_service("media_player/volume_set", entity_id="media_player.snapcast_client_55_2", volume_level="0.7") | |
self.log("alarm_pre: Radio volume full") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment