def on_alexa_discovery_discover()

in smart-mirror-full/extracted/device/script/src/agt/alexa_gadget.py [0:0]


    def on_alexa_discovery_discover(self, directive):
        """
        Called when Gadget receives Alexa.Discovery.Discover directive from the Echo device.
        """
        # Get values from the self.simple_gadget_config and set them to variables
        model_name = self._get_value_from_config(_GADGET_SETTINGS, _MODEL_NAME)
        if not model_name:
            model_name = _DEFAULT_MODEL_NAME

        device_token_encryption_type = self._get_value_from_config(_GADGET_SETTINGS, _DEVICE_TOKEN_ENCRYPTION_TYPE)
        if not device_token_encryption_type:
            device_token_encryption_type = _DEFAULT_DEVICE_TOKEN_ENCRYPTION_TYPE

        firmware_version = self._get_value_from_config(_GADGET_SETTINGS, _FIRMWARE_VERSION)
        if not firmware_version:
            firmware_version = _DEFAULT_FIRMWARE_VERSION

        # Automatically generate the device token using endpoint_id and device_type_secret
        device_token = self._generate_token(self.endpoint_id, self.device_type_secret)

        manufacturer_name = self._get_value_from_config(_GADGET_SETTINGS, _MANUFACTURER_NAME)
        if not manufacturer_name:
            manufacturer_name = _DEFAULT_MANUFACTURER_NAME

        description = self._get_value_from_config(_GADGET_SETTINGS, _DESCRIPTION)
        if not description:
            description = _DEFAULT_DESCRIPTION

        # Generate the Discover.Response Protocol Buffer Message
        pb_event = proto.DiscoverResponseEvent()
        pb_event.header.namespace = 'Alexa.Discovery'
        pb_event.header.name = 'Discover.Response'
        pb_event.header.messageId = ''

        # Populate the endpoint of the response payload
        pb_endpoint = pb_event.payload.endpoints.add()
        pb_endpoint.endpointId = self.endpoint_id
        pb_endpoint.manufacturerName = manufacturer_name
        pb_endpoint.description = description
        pb_endpoint.friendlyName = self.friendly_name

        pb_endpoint.additionalIdentification.modelName = model_name
        pb_endpoint.additionalIdentification.deviceTokenEncryptionType = device_token_encryption_type
        pb_endpoint.additionalIdentification.firmwareVersion = firmware_version
        pb_endpoint.additionalIdentification.amazonDeviceType = self.device_type
        pb_endpoint.additionalIdentification.radioAddress = self.radio_address
        pb_endpoint.additionalIdentification.deviceToken = device_token

        for section in self.gadget_config.sections():
            if section == _GADGET_CAPABILITIES:
                for (k, v) in self.gadget_config.items(section):
                    pb_capability = pb_endpoint.capabilities.add()
                    pb_capability.interface = k
                    pb_capability.type = 'AlexaInterface'
                    """
                    If capability is something like:
                        Alexa.Gadget.StateListener = 1.0 - timeinfo, timers, alarms, reminders, wakeword
                    Then we will split on '-' and add supported types

                    Otherwise, it should be in this format:
                        Alerts = 1.1
                    In which casse we simple pass only the version
                    """
                    if '-' in v:
                        v = v.split('-')
                        pb_capability.version = v[0].strip()
                        if len(v) == 2:
                            for st in v[1].split(','):
                                supported_types = pb_capability.configuration.supportedTypes.add()
                                supported_types.name = st.strip()
                    else:
                        pb_capability.version = v.strip()

        self.send_event(pb_event)