Exemple #1
0
 def get_client(self):
     client = Sleepyq(secrets.SLEEP_IQ_USER, secrets.SLEEP_IQ_PASSWORD)
     client.login()
     return client
Exemple #2
0
def intent_request(request, session, canFulfill):
    print("Intent Request on %s." % request["timestamp"])

    # NOTE: dialogState is only set if configured for interactive skill
    #dialogState = request["dialogState"]
    dialogState = "STARTED"

    session_attributes = {}
    if dialogState == "STARTED":
        intentName = request["intent"]["name"]
        confirmationStatus = request["intent"]["confirmationStatus"]
        intentSlots = request["intent"]["slots"]

        # INFO - get info about the bed
        # MODIFY - modify sleep number bed settings
        # FIND - find and set new sleep number favorite (unimplemented)
        if intentName == "INFO":
            client = Sleepyq(USER, PASS)
            status = client.login()
            bedId = client.beds()[0].bedId
            fs = client.bed_family_status()[0]
            fav = client.get_favsleepnumber(bedId)
            curLeft = {
                "sleep_number": fs.leftSide['sleepNumber'],
                "in_bed": fs.leftSide['isInBed'],
                "fav": fav.left
            }
            curRight = {
                "sleep_number": fs.rightSide['sleepNumber'],
                "in_bed": fs.rightSide['isInBed'],
                "fav": fav.right
            }
            card_title = "Sleep Number Skill - %s" % VERSION

            # This is ugly, but my string manupulation-fu is a bit rusty. I
            # should clean this up below once things are working.
            speech_output = "Here is your sleep number information for your bed. " + "The left side is set to %d, " % curLeft[
                "sleep_number"] + "and there %s in bed " % (
                    "is someone" if curLeft["in_bed"] else "is no one"
                ) + "at this time. The right side is set to %d, and " % curRight[
                    "sleep_number"] + "there %s in bed at this time. " % (
                        "is someone" if curRight["in_bed"] else "is no one")
            speech_output += "Your favorites for the left and right side are %d and %d, respectively." % (
                curLeft['fav'], curRight['fav'])
            reprompt_text = ""

            should_end_session = True
            return build_response(
                session_attributes,
                build_speechlet_response(card_title, speech_output,
                                         reprompt_text, should_end_session))
        elif intentName == "MODIFY":
            card_title = "Sleep Number Skill - %s" % VERSION
            client = Sleepyq(USER, PASS)
            status = False
            try:
                status = client.login()
            except:
                status = False

            if not status:
                # we should bail
                print("LOGIN failed")

            # Get any info we need
            bedId = client.beds()[0].bedId
            settings = {}
            for (name, obj) in iter(intentSlots.items()):
                settings[name] = obj.get('value')

            operation = settings.get('modify_operation')
            if operation == 'fill':
                # If we're being asked to fill, we simply set both sides to 100
                status = False
                for side in ['left', 'right']:
                    status = client.set_sleepnumber(bedId, side, 100)
                    if not status:
                        break
                if status:
                    speech_output = "Successfully filling the bed."
                else:
                    speech_output = "Sorry, I had trouble filling the bed."
            elif operation == 'set':
                # Set sleep number for the specified side
                if not all(k in settings for k in ("side", "setpoint")):
                    speech_output = "Sorry, you must specify both a side " + \
                                    "and set point."
                else:
                    number = None
                    side = settings.get('side')
                    bed = settings.get('bed')
                    if "favorite" in settings['setpoint']:
                        fav = client.get_favsleepnumber(bedId)
                        number = getattr(fav, side)
                    else:
                        # Alexa skill checks for range of 1 to 100 otherwise
                        number = int(settings['setpoint'])
                    if client.set_sleepnumber(bedId, side, number):
                        speech_output = "Setting %s side to %d" % (side,
                                                                   number)
                    else:
                        # something went wrong
                        speech_output = "Sorry. I had trouble setting your " + \
                                        "sleep number."
            elif operation == 'favorite':
                # Set favorite sleep number for the specified side
                if not all(k in settings for k in ("side", "setpoint")):
                    speech_output = "Sorry, you must specify both a side " + \
                                    "and set point."
                else:
                    number = number = int(settings['setpoint'])
                    side = settings.get('side')
                    bed = settings.get('bed')
                    if client.set_favsleepnumber(bedId, side, number):
                        speech_output = "Setting favorite for %s side " + \
                                        "to %d" % (side, number)
                    else:
                        # something went wrong
                        speech_output = "Sorry. I had trouble setting your " + \
                                        "sleep number favorite."
            else:
                speech_output = "Sorry, I'm not sure what you want me to " + \
                                "modify."

            # Build up response
            reprompt_text = ""
            should_end_session = True
            return build_response(
                session_attributes,
                build_speechlet_response(card_title, speech_output,
                                         reprompt_text, should_end_session))
        else:
            # this should drive a dialog, versus just erroring out
            raise ValueError("Invalid intentName")
    elif dialogState == "IN_PROGRESS":
        # NOTE: this isn't in use right now as I don't have dialog enabled

        # continue by using session_attributes from before
        session_attributes = session["session_attributes"]
    elif dialogState == "COMPLETED":
        # NOTE: this isn't in use right now as I don't have dialog enabled
        session_ended_request(request, session)
    else:
        raise ValueError("Invalid dialogState")