def try_to_get_credentials(client_secrets): """Try to get credentials, or print an error and quit on failure.""" if os.path.exists(ASSISTANT_CREDENTIALS): return auth_helpers.load_credentials(ASSISTANT_CREDENTIALS) if not os.path.exists(VR_CACHE_DIR): os.mkdir(VR_CACHE_DIR) if not os.path.exists(client_secrets) and os.path.exists( OLD_CLIENT_SECRETS): client_secrets = OLD_CLIENT_SECRETS if not os.path.exists(client_secrets): print('You need client secrets to use the Assistant API.') print('Follow these instructions:') print( ' https://developers.google.com/api-client-library/python/auth/installed-app' '#creatingcred') print('and put the file at', client_secrets) sys.exit(1) if not os.getenv('DISPLAY') and not sys.stdout.isatty(): print(""" To use the Assistant API, manually start the application from the dev terminal. See the "Turn on the Assistant API" section of the Voice Recognizer User's Guide for more info.""") sys.exit(1) credentials = auth_helpers.credentials_flow_interactive(client_secrets) auth_helpers.save_credentials(ASSISTANT_CREDENTIALS, credentials) logging.info('OAuth credentials initialized: %s', ASSISTANT_CREDENTIALS) return credentials
def try_to_get_credentials(client_secrets): """Try to get credentials, or print an error and quit on failure.""" if os.path.exists(ASSISTANT_CREDENTIALS): return auth_helpers.load_credentials(ASSISTANT_CREDENTIALS) if not os.path.exists(VR_CACHE_DIR): os.mkdir(VR_CACHE_DIR) if not os.path.exists(client_secrets) and os.path.exists(OLD_CLIENT_SECRETS): client_secrets = OLD_CLIENT_SECRETS if not os.path.exists(client_secrets): print('You need client secrets to use the Assistant API.') print('Follow these instructions:') print(' https://developers.google.com/api-client-library/python/auth/installed-app' '#creatingcred') print('and put the file at', client_secrets) sys.exit(1) if not os.getenv('DISPLAY') and not sys.stdout.isatty(): print(""" To use the Assistant API, manually start the application from the dev terminal. See the "Turn on the Assistant API" section of the Voice Recognizer User's Guide for more info.""") sys.exit(1) credentials = auth_helpers.credentials_flow_interactive(client_secrets) auth_helpers.save_credentials(ASSISTANT_CREDENTIALS, credentials) logging.info('OAuth credentials initialized: %s', ASSISTANT_CREDENTIALS) return credentials
def initialize(self): self.ASSISTANT_API_ENDPOINT = 'embeddedassistant.googleapis.com' self.END_OF_UTTERANCE = embedded_assistant_pb2.ConverseResponse.END_OF_UTTERANCE self.DIALOG_FOLLOW_ON = embedded_assistant_pb2.ConverseResult.DIALOG_FOLLOW_ON self.CLOSE_MICROPHONE = embedded_assistant_pb2.ConverseResult.CLOSE_MICROPHONE api_endpoint=self.ASSISTANT_API_ENDPOINT credentials=os.path.join(click.get_app_dir(common_settings.ASSISTANT_APP_NAME), common_settings.ASSISTANT_CREDENTIALS_FILENAME) verbose=False self.audio_sample_rate=common_settings.DEFAULT_AUDIO_SAMPLE_RATE self.audio_sample_width=common_settings.DEFAULT_AUDIO_SAMPLE_WIDTH self.audio_iter_size=common_settings.DEFAULT_AUDIO_ITER_SIZE self.audio_block_size=common_settings.DEFAULT_AUDIO_DEVICE_BLOCK_SIZE self.audio_flush_size=common_settings.DEFAULT_AUDIO_DEVICE_FLUSH_SIZE self.grpc_deadline=common_settings.DEFAULT_GRPC_DEADLINE # Load credentials. try: creds = auth_helpers.load_credentials(credentials, scopes=[common_settings.ASSISTANT_OAUTH_SCOPE]) except Exception as e: self.error('Error loading credentials: %s', e) self.error('Run auth_helpers to initialize new OAuth2 credentials.') return # Create gRPC channel grpc_channel = auth_helpers.create_grpc_channel(api_endpoint, creds, ssl_credentials_file="", grpc_channel_options="") self.log('Connecting to google') # Create Google Assistant API gRPC client. self.assistant = embedded_assistant_pb2.EmbeddedAssistantStub(grpc_channel) # Configure audio source and sink. self.audio_device = None self.audio_source = self.audio_device = (self.audio_device or audio_helpers.SoundDeviceStream(sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) self.audio_sink = self.audio_device = (self.audio_device or audio_helpers.SoundDeviceStream(sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) # Create conversation stream with the given audio source and sink. self.conversation_stream = audio_helpers.ConversationStream(source=self.audio_source, sink=self.audio_sink, iter_size=self.audio_iter_size) self.conversation_state_bytes = None self.volume_percentage = 70 self.listen_state(self.startGH,self.args["activation_boolean"],new="on") self.log("App started. now listening to Homeassistant input")