在上面基础上将录音内容保存到homeassistant里面
要将录音内容保存到Home Assistant中,您可以使用Home Assistant的REST API来实现。
首先,您需要确保Home Assistant已经配置了REST API。您可以在Home Assistant的配置文件中添加以下内容来启用REST API:
rest:
然后,您可以使用以下代码将录音内容保存到Home Assistant中:
import requests
def save_recording_to_homeassistant(audio_file_path):
url = 'http://<home_assistant_ip>:<port>/api/services/persistent_notification/create'
headers = {
'Authorization': 'Bearer <access_token>',
'Content-Type': 'application/json'
}
data = {
'message': 'New recording',
'title': 'Recording',
'notification_id': 'recording',
'data': {
'attachment': {
'content_type': 'audio/wav',
'url': 'http://<your_server_ip>:<your_server_port>/path/to/audio_file'
}
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print('Recording saved successfully')
else:
print('Failed to save recording')
# 调用函数并传入录音文件的路径
save_recording_to_homeassistant('/path/to/recording.wav')
请注意,上述代码中的<home_assistant_ip>和<port>需要替换为Home Assistant的IP地址和端口号;<access_token>需要替换为您的Home Assistant的访问令牌;<your_server_ip>和<your_server_port>需要替换为您存储录音文件的服务器的IP地址和端口号。
在这个例子中,我们使用Home Assistant的persistent_notification服务来创建一个持久通知,并将录音文件作为附件添加到通知中。
如果保存成功,您将在Home Assistant的通知面板中看到一条新的通知,其中包含您的录音文件作为附件。
请注意,为了能够将录音文件从您的服务器上传到Home Assistant,您的服务器需要公开可访问的URL,并且Home Assistant能够通过该URL下载录音文件。
原文地址: https://www.cveoy.top/t/topic/i5JX 著作权归作者所有。请勿转载和采集!