Universal media player: ordered states (#68036)

pull/73804/head
Chris Browet 2022-06-29 07:03:56 +02:00 committed by GitHub
parent b7b8feda0f
commit 596f60bdb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -72,6 +72,8 @@ from homeassistant.const import (
STATE_IDLE,
STATE_OFF,
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
@ -94,8 +96,15 @@ CONF_ATTRS = "attributes"
CONF_CHILDREN = "children"
CONF_COMMANDS = "commands"
OFF_STATES = [STATE_IDLE, STATE_OFF, STATE_UNAVAILABLE, STATE_UNKNOWN]
STATES_ORDER = [
STATE_UNKNOWN,
STATE_UNAVAILABLE,
STATE_OFF,
STATE_IDLE,
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
]
ATTRS_SCHEMA = cv.schema_with_slug_keys(cv.string)
CMD_SCHEMA = cv.schema_with_slug_keys(cv.SERVICE_SCHEMA)
@ -614,9 +623,15 @@ class UniversalMediaPlayer(MediaPlayerEntity):
async def async_update(self):
"""Update state in HA."""
for child_name in self._children:
child_state = self.hass.states.get(child_name)
if child_state and child_state.state not in OFF_STATES:
self._child_state = child_state
return
self._child_state = None
for child_name in self._children:
if (child_state := self.hass.states.get(child_name)) and STATES_ORDER.index(
child_state.state
) >= STATES_ORDER.index(STATE_IDLE):
if self._child_state:
if STATES_ORDER.index(child_state.state) > STATES_ORDER.index(
self._child_state.state
):
self._child_state = child_state
else:
self._child_state = child_state

View File

@ -422,6 +422,12 @@ async def test_active_child_state(hass, mock_states):
await ump.async_update()
assert mock_states.mock_mp_1.entity_id == ump._child_state.entity_id
mock_states.mock_mp_1._state = STATE_PAUSED
mock_states.mock_mp_1.async_schedule_update_ha_state()
await hass.async_block_till_done()
await ump.async_update()
assert mock_states.mock_mp_2.entity_id == ump._child_state.entity_id
mock_states.mock_mp_1._state = STATE_OFF
mock_states.mock_mp_1.async_schedule_update_ha_state()
await hass.async_block_till_done()