mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Fix universal media player state after reload (#195717)
This commit is contained in:
@@ -27,7 +27,6 @@ from homeassistant.const import (
|
||||
CONF_STATE,
|
||||
CONF_STATE_TEMPLATE,
|
||||
CONF_UNIQUE_ID,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
@@ -47,6 +46,7 @@ from homeassistant.helpers.event import (
|
||||
)
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
from homeassistant.helpers.service import async_call_from_config
|
||||
from homeassistant.helpers.start import async_at_start
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
ATTR_ACTIVE_CHILD = "active_child"
|
||||
@@ -185,11 +185,10 @@ class UniversalMediaPlayer(MediaPlayerEntity):
|
||||
track_templates,
|
||||
_async_on_template_update,
|
||||
)
|
||||
self.hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_START, callback(lambda _: result.async_refresh())
|
||||
)
|
||||
|
||||
self.async_on_remove(result.async_remove)
|
||||
self.async_on_remove(
|
||||
async_at_start(self.hass, callback(lambda _: result.async_refresh()))
|
||||
)
|
||||
|
||||
depend = copy(self._children)
|
||||
for entity in self._attrs.values():
|
||||
@@ -200,6 +199,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
|
||||
self.hass, list(set(depend)), _async_on_dependency_update
|
||||
)
|
||||
)
|
||||
self._async_update()
|
||||
|
||||
def _entity_lkp(self, entity_id, state_attr=None):
|
||||
"""Look up an entity state."""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""The tests for the Universal Media player platform."""
|
||||
|
||||
from copy import copy
|
||||
from typing import Any
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from probatio.error import MultipleInvalid
|
||||
@@ -27,7 +28,7 @@ from homeassistant.const import (
|
||||
STATE_PLAYING,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import Context, HomeAssistant, callback
|
||||
from homeassistant.core import Context, CoreState, HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity import EntityPlatformState
|
||||
from homeassistant.helpers.event import async_track_state_change_event
|
||||
@@ -1091,6 +1092,7 @@ async def test_service_call_to_command(hass: HomeAssistant, mock_states) -> None
|
||||
|
||||
async def test_state_template(hass: HomeAssistant) -> None:
|
||||
"""Test with a simple valid state template."""
|
||||
hass.set_state(CoreState.not_running)
|
||||
hass.states.async_set("sensor.test_sensor", STATE_ON)
|
||||
|
||||
await async_setup_component(
|
||||
@@ -1115,6 +1117,31 @@ async def test_state_template(hass: HomeAssistant) -> None:
|
||||
assert hass.states.get("media_player.tv").state == STATE_OFF
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "expected"),
|
||||
[
|
||||
({"state_template": "{{ states.sensor.test_sensor.state }}"}, STATE_ON),
|
||||
({"children": ["media_player.mock1"]}, STATE_PLAYING),
|
||||
],
|
||||
)
|
||||
async def test_state_after_start(
|
||||
hass: HomeAssistant, config: dict[str, Any], expected: str
|
||||
) -> None:
|
||||
"""Test the state is correct right away when set up after Home Assistant started."""
|
||||
hass.states.async_set("sensor.test_sensor", STATE_ON)
|
||||
hass.states.async_set("media_player.mock1", STATE_PLAYING)
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"media_player",
|
||||
{"media_player": {"platform": "universal", "name": "tv", **config}},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("media_player.tv").state == expected
|
||||
|
||||
|
||||
async def test_browse_media(hass: HomeAssistant) -> None:
|
||||
"""Test browse media."""
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
|
||||
Reference in New Issue
Block a user