Sorry - its not in the version in the repository - here is is:
import appdaemon.appapi as appapi
import random
import globals
class Alexa(appapi.AppDaemon):
def initialize(self):
self.register_endpoint(self.api_call)
def api_call(self, data):
intent = self.get_alexa_intent(data)
if intent is None:
self.log("Alexa error encountered: {}".format(self.get_alexa_error(data)))
return "", 201
intents = {
"AMAZON.CancelIntent": self.AmazonCancelIntent,
"BeerIntent": self.BeerIntent,
"StatusIntent": self.StatusIntent,
"LocateIntent": self.LocateIntent,
"MorningModeIntent": self.MorningModeIntent,
"DayModeIntent": self.DayModeIntent,
"EveningModeIntent": self.EveningModeIntent,
"NightModeIntent": self.NightModeIntent,
"NightModeQuietIntent": self.NightModeQuietIntent,
"SecureHouseIntent": self.SecureHouseIntent,
"QueryHouseIntent": self.QueryHouseIntent,
"QueryGarageDoorIntent": self.QueryGarageDoorIntent,
"QueryHeatIntent": self.QueryHeatIntent,
"ConditionsIntent": self.ConditionsIntent,
"TravelIntent": self.TravelIntent,
}
if intent in intents:
resp = intents[intent](data)
if resp["type"] == "answer":
response = self.format_alexa_response(speech = resp["speech"], card = resp["card"], title = resp["title"])
self.log("Recieved Alexa request: {}, answering: {}".format(intent, resp["speech"]))
elif resp["type"] == "delegate":
response = self.format_alexa_delegate_directive()
self.log("Recieved Alexa request, answering with follow-on")
else:
response = self.format_alexa_response(speech="Invalid response type".format(intent))
self.log("Invalid response type")
else:
response = self.format_alexa_response(speech = "I'm sorry, the {} does not exist within AppDaemon".format(intent))
#self.log(response)
return response, 200
def format_response(self, speech, card, title):
return {"type": "answer", "speech": speech, "card": card, "title": title}
def format_delegate(self, slots = None):
response = {"type": "delegate"}
return response
def format_alexa_delegate_directive(self, slots = None):
response = \
{
"version": "1.0",
"response":
{
"outputSpeech": None,
"card": None,
"directives": [{
"type": "Dialog.Delegate",
"updatedIntent": None
}],
"reprompt": None,
"shouldEndSession": False
}
}
#if slots:response["response"]["directives"][0]["updatedIntent"] = \
# {
# "name": "BeerIntent",
# "confirmationStatus": "NONE",
# "slots": {
# "type": {
# "name": "type",
# "value": "lager",
# "confirmationStatus": "NONE"
# },
# "time": {
# "name": "time",
# "value": 10,
# "confirmationStatus": "NONE"
# }
# }
#
# }
return response
def get_alexa_dialog_state(self, data):
if "request" in data and "dialogState" in data["request"]:
return (data["request"]["dialogState"])
else:
return None
def BeerIntent(self, data):
#pretty = appapi.Formatter()
#self.log(pretty(data))
dialog_state = self.get_alexa_dialog_state(data)
self.log(dialog_state)
if dialog_state == "STARTED":
return self.format_delegate()
elif dialog_state == "IN_PROGRESS":
return self.format_delegate()
elif dialog_state == "COMPLETED":
type = self.get_alexa_slot_value(data, slot="type")
time = self.get_alexa_slot_value(data, slot="time")
resp = "OK, holding your {} for the next {} minutes".format(type, time)
return self.format_response(speech=resp, card=resp,title="Beer Situation")
def LocateIntent(self, data):
user = self.get_alexa_slot_value(data, slot = "User")
if user is not None:
if user.lower() == "jack":
response = self.Jack()
elif user.lower() == "andrew":
response = self.Andrew()
elif user.lower() == "wendy":
response = self.Wendy()
elif user.lower() == "brett":
response = "I have no idea where Brett is, he never tells me anything"
else:
response = "I'm sorry, I don't know who {} is".format(user)
else:
response = "I'm sorry, I don't know who that is"
return self.format_response(response, response, "Where is {}?".format(user))
def AmazonCancelIntent(self, data):
response = "OK, cancelled"
return self.format_response(response, response, "Cancel")
def QueryHouseIntent(self, data):
security = self.get_app(self.args["apps"]["secure"])
secure, response = security.query_house({"type": "query", "caller": "alexa"})
return self.format_response(response, response, "Query House Security")
def SecureHouseIntent(self, data):
security = self.get_app(self.args["apps"]["secure"])
secure, response = security.query_house({"type": "secure", "caller": "alexa"})
return self.format_response(response, response, "Secure House")
def MorningModeIntent(self, data):
self.fire_event("MODE_CHANGE", mode = "Morning")
response = "Good Morning"
return self.format_response(response, response, "Morning Mode")
def DayModeIntent(self, data):
self.fire_event("MODE_CHANGE", mode = "Day")
response = "Good Day"
return self.format_response(response, response, "Day Mode")
def EveningModeIntent(self, data):
self.fire_event("MODE_CHANGE", mode = "Evening")
response = "Good Evening"
return self.format_response(response, response, "Evening Mode")
def NightModeIntent(self, data):
modes = self.get_app(self.args["apps"]["modes"])
response = modes.night(False, True)
return self.format_response(response, response, "Night Mode")
def NightModeQuietIntent(self, data):
modes = self.get_app(self.args["apps"]["modes"])
response = modes.night(True, True)
return self.format_response(response, response, "Night Mode Quiet")
def TravelIntent(self, data):
if self.now_is_between("05:00:00", "12:00:00"):
commute = self.entities.sensor.wendy_home_to_work.state
direction = "from home to work"
response = "Wendy's commute time {} is currently {} minutes".format(direction, commute)
elif self.now_is_between("12:00:01", "20:00:00"):
commute = self.entities.sensor.wendy_work_to_home.state
direction = "from work to home"
response = "Wendy's commute time {} is currently {} minutes".format(direction, commute)
else:
response = "Are you kidding me? Don't go to work now!"
return self.format_response(response, response, "Travel Time")
def StatusIntent(self, data):
response = self.HouseStatus()
return self.format_response(response, response, "House Status")
def ConditionsIntent(self, data):
temp = float(self.entities.sensor.side_temp_corrected.state)
if temp <= 70:
response = "It is {} degrees outside. The conditions have been met.".format(temp)
else:
response = "It is {} degrees outside. The conditions have not been met.".format(temp)
return self.format_response(response, response, "Conditions Query")
def QueryGarageDoorIntent(self, data):
response = self.Garage()
return self.format_response(response, response, "Is the garage open?")
def QueryHeatIntent(self, data):
response = self.Heat()
return self.format_response(response, response, "Is the heat on?")
def HouseStatus(self):
status = self.Heat()
status += "The downstairs temperature is {} degrees farenheit,".format(self.entities.sensor.downstairs_thermostat_temperature.state)
status += "The upstairs temperature is {} degrees farenheit,".format(self.entities.sensor.upstairs_thermostat_temperature.state)
status += "The outside temperature is {} degrees farenheit,".format(self.entities.sensor.side_temp_corrected.state)
status += self.Garage()
status += self.Wendy()
status += self.Andrew()
status += self.Jack()
return status
def Garage(self):
return "The garage door is {},".format(self.entities.cover.garage_door.state)
def Heat(self):
return "The heat is switched {},".format(self.entities.input_boolean.heating.state)
def Wendy(self):
location = self.get_state(globals.wendy_tracker)
if location == "home":
status = "Wendy is home,"
else:
status = "Wendy is away,"
return status
def Andrew(self):
location = self.get_state(globals.andrew_tracker)
if location == "home":
status = "Andrew is home,"
else:
status = "Andrew is away,"
return status
def Jack(self):
responses = [
"Jack is asleep on his chair",
"Jack just went out bowling with his kitty friends",
"Jack is in the hall cupboard",
"Jack is on the back of the den sofa",
"Jack is on the bed",
"Jack just stole a spot on daddy's chair",
"Jack is in the kitchen looking out of the window",
"Jack is looking out of the front door",
"Jack is on the windowsill behind the bed",
"Jack is out checking on his clown suit",
"Jack is eating his treats",
"Jack just went out for a walk in the neigbourhood",
"Jack is by his bowl waiting for treats"
]
return random.choice(responses)
def Response(self):
responses = [
"OK",
"Sure",
"If you insist",
"Done",
"No worries",
"I can do that",
"Leave it to me",
"Consider it done",
"As you wish",
"By your command",
"Affirmative",
"Yes oh revered one",
"I will",
"As you decree, so shall it be",
"No Problem"
]
return random.choice(responses)