请选择 进入手机版 | 继续访问电脑版

『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 7339|回复: 23

[经验分享] 小爱音箱dlna+网易云音乐插件实现自动替换vip歌曲和语音播放

  [复制链接]

2

主题

10

帖子

149

积分

论坛分享达人

积分
149
金钱
139
HASS币
10
发表于 2023-8-19 22:07:34 | 显示全部楼层 |阅读模式
本帖最后由 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>

已知问题:如果只有一首歌,小爱音箱会循环播放,小度音箱会自动停止,应该是小爱的固件原因。




评分

参与人数 7金钱 +51 HASS币 +10 收起 理由
ha_czyj + 2 感谢楼主分享!
wangyiyuan + 10 感谢楼主分享!
cat_li + 2 论坛有你更精彩!
札幌蹦极花果 + 2 论坛有你更精彩!
Smart_wp + 5 高手,这是高手!
+ 20 + 10 感谢楼主分享!
隔壁的王叔叔 + 10 墙都不扶,就服楼主!

查看全部评分

回复

使用道具 举报

2

主题

53

帖子

706

积分

高级会员

Rank: 4

积分
706
金钱
653
HASS币
0
发表于 2023-8-20 01:08:27 | 显示全部楼层
感觉非常有用,先收藏
回复

使用道具 举报

9

主题

168

帖子

1616

积分

金牌会员

Rank: 6Rank: 6

积分
1616
金钱
1448
HASS币
10
发表于 2023-8-20 10:57:01 | 显示全部楼层
没有周杰伦
回复

使用道具 举报

15

主题

228

帖子

1498

积分

金牌会员

Rank: 6Rank: 6

积分
1498
金钱
1270
HASS币
0
发表于 2023-8-20 13:06:41 | 显示全部楼层
我家小爱万能版遥控带时钟的  没有dlan   可以不
回复

使用道具 举报

7

主题

1149

帖子

3648

积分

论坛元老

Rank: 8Rank: 8

积分
3648
金钱
2499
HASS币
0
发表于 2023-8-21 09:48:29 | 显示全部楼层
ha的自动化写的溜啊
回复

使用道具 举报

2

主题

10

帖子

149

积分

论坛分享达人

积分
149
金钱
139
HASS币
10
 楼主| 发表于 2023-8-21 10:51:33 | 显示全部楼层

有的,插件作者直接在里面进行了专门处理
回复

使用道具 举报

2

主题

10

帖子

149

积分

论坛分享达人

积分
149
金钱
139
HASS币
10
 楼主| 发表于 2023-8-21 10:55:28 | 显示全部楼层
cw4774 发表于 2023-8-20 13:06
我家小爱万能版遥控带时钟的  没有dlan   可以不

识别tts部分主要是看miot是否支持,放歌的部分dlna只是一个手段,也可以考虑在ha上通过蓝牙连接目标音箱,或者直接在ha上声卡输出
回复

使用道具 举报

1

主题

43

帖子

468

积分

中级会员

Rank: 3Rank: 3

积分
468
金钱
425
HASS币
0
发表于 2023-8-27 12:00:07 | 显示全部楼层
请教楼主 device_id是指云音乐的么
回复

使用道具 举报

2

主题

10

帖子

149

积分

论坛分享达人

积分
149
金钱
139
HASS币
10
 楼主| 发表于 2023-8-29 20:27:43 | 显示全部楼层
252376009 发表于 2023-8-27 12:00
请教楼主 device_id是指云音乐的么

是的。可以通过自动化ui界面来编写,然后切换为yaml就可以获得。
回复

使用道具 举报

2

主题

77

帖子

575

积分

高级会员

Rank: 4

积分
575
金钱
498
HASS币
0
发表于 2023-8-30 21:28:43 | 显示全部楼层
大佬,能出个视频不,看的云里雾里的。哈哈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-7-25 15:45 , Processed in 0.092469 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表