Skip to content

Commit

Permalink
Update alexa_talking_clock.py
Browse files Browse the repository at this point in the history
  • Loading branch information
UbhiTS authored May 11, 2020
1 parent 675386e commit e00fac4
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions apps/alexa_talking_clock/alexa_talking_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# alexas:
# - media_player.bedroom_alexa
# - media_player.kitchen_alexa
# google_homes:
# - media_player.bedroom_google_home
# - media_player.kitchen_google_home
# voice:
# volume_offset: 0 # -40 to 4, default 0
# pitch_offset: 0 # -33 to 50, default 0
Expand All @@ -31,8 +28,7 @@ class AlexaTalkingClock(hass.Hass):

def initialize(self):

self.alexas = self.args["alexas"] if "alexas" in self.args else []
self.google_homes = self.args["google_homes"] if "google_homes" in self.args else []
self.alexas = self.args["alexas"]

self.volume_offset = 0
self.pitch_offset = 0
Expand Down Expand Up @@ -143,16 +139,10 @@ def time_announce(self, kwargs):
minute = now.minute

time_speech = self.get_time_speech(hour, minute)
alexa_effects_speech = self.set_effects(time_speech)

effects_speech = self.set_effects(time_speech)
delay = 0
for alexa in self.alexas:
self.run_in(self.time_announce_alexa, delay, alexa = alexa, time_speech = time_speech, effects_speech = alexa_effects_speech)
delay = delay + 5

delay = 0
for google_home in self.google_homes:
self.run_in(self.time_announce_google_home, delay, google_home = google_home, time_speech = time_speech)
self.run_in(self.time_announce_alexa, delay, alexa = alexa, time_speech = time_speech, effects_speech = effects_speech)
delay = delay + 5


Expand All @@ -177,18 +167,26 @@ def time_announce_alexa(self, kwargs):
message = effects_speech

self.call_service("notify/alexa_media", data = {"type": announce, "method": method}, target = alexa, title = title, message = message)

self.log(f"TIME_ANNOUNCE {time_speech}: {alexa.split('.')[1]}")


def time_announce_google_home(self, kwargs):
google_home = kwargs['google_home']
message = kwargs['time_speech']

self.call_service("tts/google_say", entity_id = google_home, message = message)

self.log(f"TIME_ANNOUNCE {message}: {google_home.split('.')[1]}")
def set_effects(self, time_speech):
prefix = "<speak>"
postfix = "</speak>"

if self.whisper:
prefix = prefix + "<amazon:effect name='whispered'>"
postfix = "</amazon:effect>" + postfix

str_rate = str(self.rate)
str_pitch = "+" + str(self.pitch_offset) if self.pitch_offset >= 0 else str(self.pitch_offset)
str_volume = "+" + str(self.volume_offset) if self.volume_offset >= 0 else str(self.volume_offset)

prefix = prefix + "<prosody rate='" + str_rate + "%' pitch='" + str_pitch + "%' volume='" + str_volume + "dB'>"
postfix = "</prosody>" + postfix

return prefix + time_speech + postfix


def get_time_speech(self, hour, minute):

Expand Down Expand Up @@ -217,24 +215,6 @@ def get_time_speech(self, hour, minute):
return prefix + " " + time_speech + " " + postfix


def set_effects(self, time_speech):
prefix = "<speak>"
postfix = "</speak>"

if self.whisper:
prefix = prefix + "<amazon:effect name='whispered'>"
postfix = "</amazon:effect>" + postfix

str_rate = str(self.rate)
str_pitch = "+" + str(self.pitch_offset) if self.pitch_offset >= 0 else str(self.pitch_offset)
str_volume = "+" + str(self.volume_offset) if self.volume_offset >= 0 else str(self.volume_offset)

prefix = prefix + "<prosody rate='" + str_rate + "%' pitch='" + str_pitch + "%' volume='" + str_volume + "dB'>"
postfix = "</prosody>" + postfix

return prefix + time_speech + postfix


# https://stackoverflow.com/questions/20518122/python-working-out-if-time-now-is-between-two-times
def time_outside_range(self, now, start, end):

Expand Down

0 comments on commit e00fac4

Please sign in to comment.