- 积分
- 305
- 金钱
- 247
- 威望
- 0
- 贡献
- 0
- HASS币
- 0
中级会员
- 积分
- 305
- 金钱
- 247
- HASS币
- 0
|
发表于 2024-4-2 14:30:29
|
显示全部楼层
本帖最后由 lj407 于 2024-4-2 14:32 编辑
本人太小白了,感觉还是全部贴出来好免得有大佬意想不到的低级错误……
import requests, functools
import json, asyncio
from urllib.parse import quote
from datetime import datetime, timezone
async def async_http_request(url, data):
search_url = url + '/api/fs/search'
# service.call("media_player", "media_pause", entity_id=["media_player.xiaomi_lx06_72af_play_control", "media_player.yun_yin_le_xiao_ai_yin_xiang_9476"])
media_player.media_pause(entity_id=["media_player.xiaomi_lx06_72af_play_control", "media_player.yun_yin_le_xiao_ai_yin_xiang_9476"])
await asyncio.sleep(1)
post_request = functools.partial(requests.post, search_url, data=data, headers={'Content-Type': 'application/json'})
response = await hass.async_add_executor_job(post_request)
res_data = response._content.decode('utf-8')
json_data = json.loads(res_data)
log.error(res_data)
json_u = json_data['data']['content']
if not json_u or len(json_u) == 0:
return 'search error'
music_url = url + '/d' + json_u[0]['parent'] + '/' + quote(json_u[0]['name'])
music_addr = music_url.strip()
media_content_id = music_addr
media_content_type = "music"
# service.call("media_player", "play_media", entity_id="media_player.xiao_ai_yin_xiang_9476", media_content_id=media_content_id, media_content_type=media_content_type)
log.error(media_content_id)
media_player.play_media(entity_id="media_player.xiao_ai_yin_xiang_9476", media_content_id=media_content_id, media_content_type=media_content_type)
@state_trigger("sensor.xiaomi_lx06_72af_conversation.timestamp")
async def get_conversation():
xiaoai_state_dict = state.getattr(sensor.xiaomi_lx06_72af_conversation)
if "content" not in xiaoai_state_dict:
log.error("获取内容失败")
return
if xiaoai_state_dict["timestamp"] is None:
return
utc_dt = datetime.fromisoformat(str(xiaoai_state_dict["timestamp"])).astimezone(timezone.utc)
state_timestamp = int(utc_dt.timestamp())
utc_now = datetime.now(timezone.utc)
now_timestamp = int(utc_now.timestamp())
if now_timestamp - state_timestamp > 15:
return
xiaoai_stt = xiaoai_state_dict["content"]
if xiaoai_stt is not None and "本地播放" in xiaoai_stt:
music_msg = xiaoai_stt.replace("本地播放","").replace("的"," ").replace("歌曲","").replace("歌","")
url = 'http://192.168.0.121:5244'
data = {"parent":"/","keywords":music_msg,"scope":0,"page":1,"per_page":100,"password":""}
json_data = json.dumps(data)
# await asyncio.sleep(1)
async_http_request(url, json_data)
|
|