支持多个媒体播放器切换

This commit is contained in:
shaonianzhetnan 2023-02-02 23:44:38 +08:00
parent 9152f3eed0
commit a01ba3458b
4 changed files with 25 additions and 12 deletions

View File

@ -54,7 +54,10 @@ homeassistant:
customize.yaml
```yaml
media_player.yun_yin_le:
media_player: media_player.源实体
media_player:
- media_player.源实体1
- media_player.源实体2
- media_player.源实体3
```
## 关联项目

View File

@ -19,7 +19,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data['cloud_music'] = CloudMusic(hass, api_url)
hass.http.register_view(HttpView)
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))
return True

View File

@ -1,7 +1,7 @@
{
"domain": "ha_cloud_music",
"name": "\u4E91\u97F3\u4E50",
"version": "2023.2.2",
"version": "2023.2.3",
"config_flow": true,
"documentation": "https://github.com/shaonianzhentan/ha_cloud_music",
"requirements": [],

View File

@ -82,6 +82,7 @@ class CloudMusicMediaPlayer(MediaPlayerEntity):
# default attribute
self._attr_source_list = []
self._attr_sound_mode = None
self._attr_sound_mode_list = []
self._attr_name = manifest.name
self._attr_unique_id = manifest.documentation
@ -132,11 +133,8 @@ class CloudMusicMediaPlayer(MediaPlayerEntity):
@property
def media_player(self):
if self.entity_id is not None:
state = self.hass.states.get(self.entity_id)
entity_id = state.attributes.get('media_player')
if entity_id is not None and entity_id != self.entity_id and entity_id.startswith('media_player.'):
return self.hass.states.get(entity_id)
if self.entity_id is not None and self._attr_sound_mode is not None:
return self.hass.states.get(self._attr_sound_mode)
@property
def device_info(self):
@ -161,9 +159,10 @@ class CloudMusicMediaPlayer(MediaPlayerEntity):
if self._attr_source_list.count(source) > 0:
self._attr_source = source
async def async_select_sound_mode(self, mode):
if self._attr_sound_mode_list.count(mode) > 0:
self._attr_sound_mode = mode
async def async_select_sound_mode(self, sound_mode):
if self._attr_sound_mode_list.count(sound_mode) > 0:
await self.async_media_pause()
self._attr_sound_mode = sound_mode
async def async_volume_up(self):
await self.async_call('volume_up')
@ -234,7 +233,18 @@ class CloudMusicMediaPlayer(MediaPlayerEntity):
# 更新属性
async def async_update(self):
pass
if self.entity_id is not None:
state = self.hass.states.get(self.entity_id)
entities = state.attributes.get('media_player')
if entities is not None:
# 兼容初版
if isinstance(entities, str):
entities = [ entities ]
if len(entities) > 0:
self._attr_sound_mode_list = entities
if self._attr_sound_mode is None:
self._attr_sound_mode = entities[0]
# 调用服务
async def async_call(self, service, service_data={}):