本帖最后由 ha_wr5v8k 于 2024-11-28 07:40 编辑
### 参考https://bbs.hassbian.com/thread-22469-3-1.html
### self.before_state 小爱音箱播放器上次播放进度与上上次状态;self._attr_media_* 小爱音箱播放器当前播放进度;self.current_state 小爱音箱播放器上次状态;media_player.state 小爱音箱播放器当前状态;self._attr_state 云音乐播放器当前状态
def interval(self, now):
# 暂停时不更新
if self._attr_state == STATE_PAUSED:
return
media_player = self.media_player
if media_player is not None:
attrs = media_player.attributes
self._attr_media_position = attrs.get('media_position', 0)
self._attr_media_duration = attrs.get('media_duration', 0)
self._attr_media_position_updated_at = datetime.datetime.now()
# 判断是否下一曲
if self.before_state is not None:
# 判断音乐总时长
# if self.before_state['media_duration'] > 0 and self.before_state['media_duration'] - self.before_state['media_position'] <= 5:
# 判断源音乐播放器状态
# if self.before_state['state'] == STATE_PLAYING and self.current_state == STATE_IDLE:
# self.hass.create_task(self.async_media_next_track())
# self.before_state = None
# return
### 判断音乐总时长,但小爱音箱播放器更新间隔是二十二秒
import asyncio
if (self.before_state['media_duration'] > 0 and self.before_state['media_duration'] - self.before_state['media_position'] <= 22) or 'next_at' in self.before_state:
if ('next_at' not in self.before_state):
self.before_state['next_at'] = datetime.timedelta(seconds=max(self.before_state['media_duration'] - self.before_state['media_position'] - 5, 0)) + self._attr_media_position_updated_at
if (self._attr_media_position_updated_at >= self.before_state['next_at']):
# 判断源音乐播放器状态(空闲则下一首),但小爱音箱播放器状态是循环播放
if self.before_state['state'] == STATE_PLAYING and (self.current_state == STATE_IDLE or self.current_state == STATE_PLAYING):
asyncio.run_coroutine_threadsafe(self.async_media_next_track(), self.hass.loop)
self.before_state = None
return
else:
del self.before_state['next_at']
# 源播放器空闲 & 当前正在播放
# 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.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 or self.current_state == STATE_PAUSED) \
and self._attr_media_duration == 0 and self._attr_media_position == 0 and self._attr_state == STATE_PLAYING) or 'next_at' in self.before_state:
if ('next_at' not in self.before_state):
self.before_state['next_at'] = datetime.timedelta(seconds=22) + self._attr_media_position_updated_at
if (self._attr_media_position_updated_at >= self.before_state['next_at']):
if self.before_state['media_duration'] == 0 and self.before_state['media_position'] == 0 and (self.current_state == STATE_IDLE or media_player.state == STATE_PAUSED) \
and media_player.attributes.get('media_duration', 0) == 0 and self._attr_state == STATE_PLAYING:
asyncio.run_coroutine_threadsafe(self.async_media_next_track(), self.hass.loop)
self.before_state = None
return
else:
del self.before_state['next_at']
### 小爱音箱播放器暂停 & 当前正在播放
if (self.before_state['media_duration'] == 0 and self.before_state['media_position'] == 0 and self.current_state == STATE_PAUSED \
and self._attr_media_duration > 0 and self._attr_media_position > 0 and self._attr_state == STATE_PLAYING) or 'pause_at' in self.before_state:
if ('pause_at' not in self.before_state):
self.before_state['pause_at'] = datetime.timedelta(seconds=22) + self._attr_media_position_updated_at
if (self._attr_media_position_updated_at >= self.before_state['pause_at']):
if media_player.state == STATE_PAUSED and self._attr_state == STATE_PLAYING:
asyncio.run_coroutine_threadsafe(self.async_media_pause(), self.hass.loop)
self.before_state = None
return
else:
del self.before_state['pause_at']
self.before_state = {
'media_position': int(self._attr_media_position),
'media_duration': int(self._attr_media_duration),
'state': self.current_state
}
self.current_state = media_player.state
if hasattr(self, 'playlist'):
music_info = self.playlist[self.playindex]
self._attr_app_name = music_info.singer
self._attr_media_image_url = music_info.thumbnail
self._attr_media_album_name = music_info.album
self._attr_media_title = music_info.song
self._attr_media_artist = music_info.singer
|