本帖最后由 qqdwtypm 于 2023-8-21 10:49 编辑
实现步骤:
一、安装调试网易云音乐插件
二、自动替换小爱音箱中的vip歌曲
三、通过小爱音箱语音控制来播放网易云音乐
具体步骤:
一、安装调试网易云音乐插件
安装论坛里大佬的插件:
https://github.com/shaonianzhentan/ha_cloud_music
安装教程可以参考帖子:
https://bbs.hassbian.com/thread-20812-1-1.html
之后
1.修复低版本homeassistant安装后报错问题,修改__init__.py
# await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
2.在dlna上不会自动下一曲,修改media_player.py
(1)
# 定时器时间
TIME_BETWEEN_UPDATES = datetime.timedelta(seconds=10)
(2)
# 判断是否下一曲
if self.before_state is not None:
# 判断音乐总时长
if self.before_state['media_duration'] > 0 and self.before_state['media_duration'] - self.before_state['media_duration'] <= 5:
# 判断源音乐播放器状态
# if self.before_state['state'] == STATE_PLAYING and self.current_state == STATE_IDLE:
# if self.before_state['media_duration'] - self.before_state['media_position'] <= 10 and self.before_state['media_duration'] > 1 and self.before_state['media_duration'] - self.before_state['media_position'] >= 0:
if self._attr_media_duration - self._attr_media_position <= 19 and self._attr_media_duration > 1 and self._attr_media_duration - self._attr_media_position >= 0 :
self._attr_state = STATE_PAUSED
self.before_state = None
timeleft = self._attr_media_duration - self._attr_media_position
if timeleft > 13 :
time.sleep(timeleft-13)
self.hass.async_create_task(self.async_media_next_track())
self.before_state = None
return
# 源播放器空闲 & 当前正在播放
# if self.before_state['media_duration'] == 0 and self.before_state['media_position'] == 0 and self.current_state == STATE_IDLE \
# and self._attr_media_duration == 0 and self._attr_media_position == 0 and self._attr_state == STATE_PLAYING:
# self.hass.async_create_task(self.async_media_next_track())
# self.before_state = None
# return
if self.before_state['media_duration'] == 0 and self.before_state['media_position'] == 0 and self.current_state == STATE_PLAYING \
and self._attr_media_duration == 0 and self._attr_media_position == 0 and self._attr_state == STATE_PLAYING:
time.sleep(10)
if self.before_state['media_duration'] == 0 and self.before_state['media_position'] == 0 and self.current_state == STATE_PLAYING \
and self._attr_media_duration == 0 and self._attr_media_position == 0 and self._attr_state == STATE_PLAYING:
self.hass.async_create_task(self.async_media_next_track())
self.before_state = None
return
# 判断是否已暂停
if media_player.state == STATE_IDLE and self._attr_state != STATE_ON:
time.sleep(19)
if media_player.state == STATE_IDLE and self._attr_state != STATE_ON :
self.hass.async_create_task(self.async_media_pause())
self.before_state = None
return
3.返回的链接为https,小爱音箱无法跳转,修改为http,修改http.py
(1)
not_found_tips = quote(f'当前没有找到编号是{id},歌名为{song},作者是{singer}的播放链接')
play_url = f'http://fanyi.baidu.com/gettts?lan=zh&text={not_found_tips}&spd=5&source=web'
(2)
# 修改https为http
play_url2 = play_url.replace('https','http')
play_url = play_url2
print(play_url)
self.play_key = play_key
self.play_url = play_url
# 重定向到可播放链接
return web.HTTPFound(play_url)
二、自动替换小爱音箱中的vip歌曲
实现思路:监测到小爱回复中有“试听版”三个字触发,自动由网易云插件搜索播放
前提:已安装好miot那个插件,可以识别音箱对话;ha中的dlna已配置好音箱
homeassistant自动化:
alias: 本地播放-替换试听版
description: ''
trigger:
- platform: state
entity_id:
- sensor.xiaomi_s12_4c94_conversation
- platform: state
entity_id:
- sensor.xiaomi_lx06_fd69_conversation
enabled: true
- platform: state
entity_id:
- sensor.xiaomi_lx06_ebe4_conversation
enabled: true
condition:
- condition: template
value_template: |
{{ (trigger.to_state|default(0)) and as_timestamp(now()) -
as_timestamp(trigger.to_state.attributes.timestamp) < 30 }}
{# 限制30秒内的对话 #}
enabled: true
- condition: not
conditions:
- condition: template
value_template: '{{ ''本地播放'' in trigger.to_state.state }}'
action:
- if:
- condition: template
value_template: |
{{ "%E8%AF%95%E5%90%AC%E7%89%88" in
trigger.to_state.attributes.answers[0]|urlencode() }}
then:
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_s12_4c94_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_0095
target:
device_id: f8b39494266357981171acd9068de644
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_lx06_fd69_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_8269
target:
device_id: f8b39494266357981171acd9068de644
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_lx06_ebe4_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_8269
target:
device_id: f8b39494266357981171acd9068de644
- service: media_player.play_media
data:
media_content_id: cloudmusic://play/song?kv={{trigger.to_state.attributes.content}}
media_content_type: music
target:
device_id: f8b39494266357981171acd9068de644
- service: system_log.write
data:
level: info
logger: custom_components.xiaomi_miot.xiaoai
message: |
{{ trigger.entity_id }}: {{ trigger.to_state.state }}
enabled: false
mode: single
三、通过小爱音箱语音控制来播放网易云音乐
实现思路:监测小爱音箱对话中关键字,如果出现“本地播放”则触发,同时如果本地播放后面还跟有其他关键词则触发二级功能:
本地播放+热歌榜
+歌手 +(想传递去的搜索内容,比如歌手名)
+歌单 +(想传递去的搜索内容,比如歌单名)
+第三方 +(想传递去的搜索内容,比如歌手+歌名) 第三方音乐搜素,一首歌
+下一首
+随机播放
+顺序播放
+(不含以上搜索词,剩余部分想传递去的搜索内容,比如歌手+歌名)默认音乐搜索,一首歌
如直接对小爱说“本地播放歌手五月天”则会触发搜索该歌手的歌曲,“本地播放五月天”则会触发搜索五月天一首歌
ha的自动化代码:
<div>
</div><div>alias: 本地播放-网易云音乐
description: ''
trigger:
- platform: state
entity_id:
- sensor.xiaomi_s12_4c94_conversation
- platform: state
entity_id:
- sensor.xiaomi_lx06_fd69_conversation
enabled: true
- platform: state
entity_id:
- sensor.xiaomi_lx06_ebe4_conversation
enabled: true
condition:
- condition: template
value_template: |
{{ (trigger.to_state|default(0)) and as_timestamp(now()) -
as_timestamp(trigger.to_state.attributes.timestamp) < 30 }}
{# 限制30秒内的对话 #}
enabled: true
action:
- if:
- condition: template
value_template: |
{{ '本地播放' in trigger.to_state.state }}
then:
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_s12_4c94_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_0095
target:
device_id: f8b39494266357981171acd9068de644
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_lx06_fd69_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_8269
target:
device_id: f8b39494266357981171acd9068de644
- if:
- condition: template
value_template: |
{{ 'media_player.xiaomi_lx06_ebe4_play_control' in
trigger.to_state.attributes.parent_entity_id }}
then:
- service: media_player.select_sound_mode
data:
sound_mode: media_player.xiao_ai_yin_xiang_8269
target:
device_id: f8b39494266357981171acd9068de644
- choose:
- conditions:
- condition: template
value_template: '{{ ''热歌榜'' in trigger.to_state.state }}'
sequence:
- service: media_player.play_media
target:
entity_id: media_player.yun_yin_le
data:
media_content_id: >-
cloudmusic://163/playlist?title=%E7%83%AD%E6%AD%8C%E6%A6%9C&id=3778678
media_content_type: playlist
metadata:
title: 热歌榜
thumbnail: null
media_class: playlist
children_media_class: music
navigateIds:
- {}
- media_content_type: album
media_content_id: cloudmusic://163/toplist?title=%E6%A6%9C%E5%8D%95
- media_content_type: playlist
media_content_id: >-
cloudmusic://163/playlist?title=%E7%83%AD%E6%AD%8C%E6%A6%9C&id=3778678
- conditions:
- condition: template
value_template: '{{ ''歌手'' in trigger.to_state.state }}'
sequence:
- service: media_player.play_media
data:
media_content_id: >-
cloudmusic://play/singer?kv={{(trigger.to_state.state|replace("本地播放",""))|replace("歌手","")}}
media_content_type: music
target:
device_id: f8b39494266357981171acd9068de644
- conditions:
- condition: template
value_template: '{{ ''歌单'' in trigger.to_state.state }}'
sequence:
- service: media_player.play_media
data:
media_content_type: music
media_content_id: >-
cloudmusic://play/list?kv={{(trigger.to_state.state|replace("本地播放",""))|replace("歌单","")}}
target:
device_id: f8b39494266357981171acd9068de644
- conditions:
- condition: template
value_template: '{{ ''第三方'' in trigger.to_state.state }}'
sequence:
- service: media_player.play_media
data:
media_content_type: music
media_content_id: >-
cloudmusic://search/play?kv={{(trigger.to_state.state|replace("本地播放",""))|replace("第三方","")}}
target:
device_id: f8b39494266357981171acd9068de644
- conditions:
- condition: template
value_template: '{{ ''下一首'' in trigger.to_state.state }}'
sequence:
- service: media_player.media_next_track
data: {}
target:
device_id: f8b39494266357981171acd9068de644
- conditions:
- condition: template
value_template: '{{ ''随机播放'' in trigger.to_state.state }}'
sequence:
- service: media_player.shuffle_set
data:
shuffle: true
target:
device_id: f8b39494266357981171acd9068de644
- conditions:
- condition: template
value_template: '{{ ''顺序播放'' in trigger.to_state.state }}'
sequence:
- service: media_player.shuffle_set
data:
shuffle: false
target:
device_id: f8b39494266357981171acd9068de644
default:
- service: media_player.play_media
data:
media_content_type: music
media_content_id: >-
cloudmusic://play/song?kv={{trigger.to_state.state|replace("本地播放","")}}
target:
device_id: f8b39494266357981171acd9068de644
- service: system_log.write
data:
level: info
logger: custom_components.xiaomi_miot.xiaoai
message: |
{{ trigger.entity_id }}: {{ trigger.to_state.state }}
enabled: false
mode: single
</div>
已知问题:如果只有一首歌,小爱音箱会循环播放,小度音箱会自动停止,应该是小爱的固件原因。
|