Skip to content

Commit

Permalink
Extend media_player features setting (close dext0r#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Aug 30, 2022
1 parent 4607b46 commit 27df1e5
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
8 changes: 6 additions & 2 deletions custom_components/yandex_smart_home/capability_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,12 @@ def supported(self) -> bool:
"""Test if capability is supported."""
features = self.state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)

if self.state.domain == media_player.DOMAIN and features & media_player.MediaPlayerEntityFeature.SELECT_SOURCE:
return super().supported()
if self.state.domain == media_player.DOMAIN:
if const.MEDIA_PLAYER_FEATURE_SELECT_SOURCE in self.entity_config.get(const.CONF_FEATURES, []):
return True

if features & media_player.MediaPlayerEntityFeature.SELECT_SOURCE:
return super().supported()

return False

Expand Down
3 changes: 3 additions & 0 deletions custom_components/yandex_smart_home/capability_onoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ def supported(self) -> bool:
if const.CONF_TURN_ON in self.entity_config or const.CONF_TURN_OFF in self.entity_config:
return True

if const.MEDIA_PLAYER_FEATURE_TURN_ON_OFF in self.entity_config.get(const.CONF_FEATURES, []):
return True

return features & media_player.MediaPlayerEntityFeature.TURN_ON or \
features & media_player.MediaPlayerEntityFeature.TURN_OFF

Expand Down
8 changes: 6 additions & 2 deletions custom_components/yandex_smart_home/capability_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ def supported(self) -> bool:
features = self.state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)

if self.state.domain == media_player.DOMAIN:
return features & media_player.MediaPlayerEntityFeature.PAUSE and \
features & media_player.MediaPlayerEntityFeature.PLAY
if const.MEDIA_PLAYER_FEATURE_PLAY_PAUSE in self.entity_config.get(const.CONF_FEATURES, []):
return True

if features & media_player.MediaPlayerEntityFeature.PAUSE and \
features & media_player.MediaPlayerEntityFeature.PLAY:
return True

return False

Expand Down
7 changes: 6 additions & 1 deletion custom_components/yandex_smart_home/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,13 @@
MEDIA_PLAYER_FEATURE_VOLUME_MUTE = 'volume_mute'
MEDIA_PLAYER_FEATURE_VOLUME_SET = 'volume_set'
MEDIA_PLAYER_FEATURE_NEXT_PREVIOUS_TRACK = 'next_previous_track'
MEDIA_PLAYER_FEATURE_SELECT_SOURCE = 'select_source'
MEDIA_PLAYER_FEATURE_TURN_ON_OFF = 'turn_on_off'
MEDIA_PLAYER_FEATURE_PLAY_PAUSE = 'play_pause'
MEDIA_PLAYER_FEATURES = (
MEDIA_PLAYER_FEATURE_VOLUME_MUTE,
MEDIA_PLAYER_FEATURE_VOLUME_SET,
MEDIA_PLAYER_FEATURE_NEXT_PREVIOUS_TRACK
MEDIA_PLAYER_FEATURE_NEXT_PREVIOUS_TRACK,
MEDIA_PLAYER_FEATURE_SELECT_SOURCE,
MEDIA_PLAYER_FEATURE_TURN_ON_OFF,
)
11 changes: 8 additions & 3 deletions docs/config/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
## Поддерживаемые функции (media_player) { id=features }
> Параметр: `features` (только для `media_player`)
> Возможные значения: `volume_mute`, `volume_set`, `next_previous_track` (список, можно все сразу)
> Возможные значения: `volume_mute`, `volume_set`, `next_previous_track`, `select_source`[^1], `turn_on_off`[^1], `play_pause`[^1] (список, можно все сразу)
Используется для явного указания поддерживаемых устройством функций.
Необходим для устройств, которые меняют набор функций в зависимости от своего состояния (например Chrome Cast).
Необходим для устройств, которые меняют набор функций в зависимости от своего состояния (например Chrome Cast или Universal Mediaplayer).

!!! example "Пример"
```yaml
Expand All @@ -83,7 +83,12 @@
- volume_mute
- volume_set
- next_previous_track
```
- select_source
- turn_on_off
- play_pause
```

[^1]: Начиная с версии 0.6.1

## Выбор каналов (media_player) { id=support_set_channel }
> Параметр: `support_set_channel` (только для `media_player`)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_capability_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from custom_components.yandex_smart_home.error import SmartHomeError

from . import BASIC_CONFIG, BASIC_DATA, MockConfig
from .test_capability import assert_no_capabilities, get_exact_one_capability
from .test_capability import assert_exact_one_capability, assert_no_capabilities, get_exact_one_capability


class MockModeCapability(ModeCapability):
Expand Down Expand Up @@ -308,6 +308,16 @@ async def test_capability_mode_input_source(hass, caplog):
state = State('media_player.test', STATE_OFF)
assert_no_capabilities(hass, BASIC_CONFIG, state, CAPABILITIES_MODE, MODE_INSTANCE_INPUT_SOURCE)

state = State('media_player.test', STATE_ON)
config = MockConfig(entity_config={
state.entity_id: {
'features': [
'select_source'
]
}
})
assert_exact_one_capability(hass, config, state, CAPABILITIES_MODE, MODE_INSTANCE_INPUT_SOURCE)

state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.SELECT_SOURCE,
media_player.ATTR_INPUT_SOURCE_LIST: [f's{i}' for i in range(1, 15)]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_capability_onoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ async def test_capability_onoff_media_player(hass):
state = State('media_player.simple', STATE_ON)
assert_no_capabilities(hass, BASIC_CONFIG, state, CAPABILITIES_ONOFF, ON_OFF_INSTANCE_ON)

state = State('media_player.test', STATE_ON)
config = MockConfig(entity_config={
state.entity_id: {
'features': [
'turn_on_off'
]
}
})
assert_exact_one_capability(hass, config, state, CAPABILITIES_ONOFF, ON_OFF_INSTANCE_ON)

state = State('media_player.only_on', STATE_ON, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.TURN_OFF
})
Expand Down
10 changes: 10 additions & 0 deletions tests/test_capability_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ async def test_capability_pause_media_player(hass):
state = State('media_player.test', STATE_ON)
assert_no_capabilities(hass, BASIC_CONFIG, state, CAPABILITIES_TOGGLE, TOGGLE_INSTANCE_PAUSE)

state = State('media_player.test', STATE_ON)
config = MockConfig(entity_config={
state.entity_id: {
'features': [
'play_pause'
]
}
})
assert_exact_one_capability(hass, config, state, CAPABILITIES_TOGGLE, TOGGLE_INSTANCE_PAUSE)

for s in [media_player.STATE_IDLE, media_player.STATE_OFF]:
state = State('media_player.test', s, {
ATTR_SUPPORTED_FEATURES:
Expand Down

0 comments on commit 27df1e5

Please sign in to comment.