Я знаю, что Alexa может принимать текст, потому что я могу вводить текст через приложение Alexa для Android и объявлять его на всех моих эхо-устройствах. Мне нужен способ отправлять текст из скриптов Python в приложение Alexa и запускать объявление.
Это видео на 5:20 ( ) показывает, что Echo объявляет пользовательский текст через Home Assistant, вместо этого я хотел бы сделать это через Python. К счастью, для этого Home Assistant использует два пакета Python: AlexaPy и alexa_media_player.
AlexaPy (https://alexapy.readthedocs.io/en/lates ... submodules). ) есть именно то, что мне нужно: метод send_announcment, который позволяет эхо-устройству объявлять текст («это тест») с помощью следующего фрагмента кода.
Код: Выделить всё
echo_device = alexapy.AlexaAPI(device, alexa_login) # stuck here
await echo_device.send_announcment(message='this is a test')
Код: Выделить всё
import asyncio
import alexapy
url = 'amazon.com'
name = 'amazon_login_name'
password = 'amazon_password'
def fun_outputpath(file: str):
return file
async def connect():
alexa_login = alexapy.AlexaLogin(url, name, password, fun_outputpath)
await alexa_login.login()
is_connected = await alexa_login.test_loggedin()
if is_connected:
print("Connected")
print(alexa_login.customer_id)
print(alexa_login.email)
print(alexa_login.password)
else:
print("Not connected")
await alexa_login.close()
def main():
asyncio.run(connect())
if __name__ == "__main__":
main()
Код: Выделить всё
class AlexaAPI:
# pylint: disable=too-many-public-methods
"""Class for accessing a specific Alexa device using rest API.
Args:
device (AlexaClient): Instance of an AlexaClient to access
login (AlexaLogin): Successfully logged in AlexaLogin
"""
devices: dict[str, Any] = {}
wake_words: dict[str, Any] = {}
_sequence_queue: dict[Any, list[dict[Any, Any]]] = {}
_sequence_lock: dict[Any, asyncio.Lock] = {}
def __init__(self, device, login: AlexaLogin):
"""Initialize Alexa device."""
self._device = device
Код: Выделить всё
class AlexaClient(MediaPlayerDevice, AlexaMedia):
"""Representation of a Alexa device."""
def __init__(self, device, login, second_account_index=0):
"""Initialize the Alexa device."""
super().__init__(self, login)
# Logged in info
self._authenticated = None
self._can_access_prime_music = None
self._customer_email = None
self._customer_id = None
self._customer_name = None
# Device info
self._device = device
Подробнее здесь: https://stackoverflow.com/questions/778 ... ouncements