1 """Module that groups code required to handle state restore for component."""
3 from __future__
import annotations
6 from collections.abc
import Iterable
10 ATTR_SUPPORTED_FEATURES,
29 ATTR_MEDIA_CONTENT_ID,
30 ATTR_MEDIA_CONTENT_TYPE,
31 ATTR_MEDIA_VOLUME_LEVEL,
32 ATTR_MEDIA_VOLUME_MUTED,
36 SERVICE_SELECT_SOUND_MODE,
37 SERVICE_SELECT_SOURCE,
38 MediaPlayerEntityFeature,
46 context: Context |
None =
None,
47 reproduce_options: dict[str, Any] |
None =
None,
49 """Reproduce component states."""
50 cur_state = hass.states.get(state.entity_id)
51 features = cur_state.attributes[ATTR_SUPPORTED_FEATURES]
if cur_state
else 0
53 async
def call_service(service: str, keys: Iterable[str]) ->
None:
54 """Call service with set of attributes given."""
55 data = {
"entity_id": state.entity_id}
57 if key
in state.attributes:
58 data[key] = state.attributes[key]
60 await hass.services.async_call(
61 DOMAIN, service, data, blocking=
True, context=context
64 if state.state == STATE_OFF:
65 if features & MediaPlayerEntityFeature.TURN_OFF:
66 await call_service(SERVICE_TURN_OFF, [])
79 and features & MediaPlayerEntityFeature.TURN_ON
81 await call_service(SERVICE_TURN_ON, [])
83 cur_state = hass.states.get(state.entity_id)
84 features = cur_state.attributes[ATTR_SUPPORTED_FEATURES]
if cur_state
else 0
88 ATTR_INPUT_SOURCE
in state.attributes
89 and features & MediaPlayerEntityFeature.SELECT_SOURCE
91 await call_service(SERVICE_SELECT_SOURCE, [ATTR_INPUT_SOURCE])
94 ATTR_SOUND_MODE
in state.attributes
95 and features & MediaPlayerEntityFeature.SELECT_SOUND_MODE
97 await call_service(SERVICE_SELECT_SOUND_MODE, [ATTR_SOUND_MODE])
100 ATTR_MEDIA_VOLUME_LEVEL
in state.attributes
101 and features & MediaPlayerEntityFeature.VOLUME_SET
103 await call_service(SERVICE_VOLUME_SET, [ATTR_MEDIA_VOLUME_LEVEL])
106 ATTR_MEDIA_VOLUME_MUTED
in state.attributes
107 and features & MediaPlayerEntityFeature.VOLUME_MUTE
109 await call_service(SERVICE_VOLUME_MUTE, [ATTR_MEDIA_VOLUME_MUTED])
111 already_playing =
False
113 if (ATTR_MEDIA_CONTENT_TYPE
in state.attributes)
and (
114 ATTR_MEDIA_CONTENT_ID
in state.attributes
116 if features & MediaPlayerEntityFeature.PLAY_MEDIA:
119 [ATTR_MEDIA_CONTENT_TYPE, ATTR_MEDIA_CONTENT_ID],
121 already_playing =
True
125 and state.state
in (STATE_BUFFERING, STATE_PLAYING)
126 and features & MediaPlayerEntityFeature.PLAY
128 await call_service(SERVICE_MEDIA_PLAY, [])
129 elif state.state == STATE_IDLE:
130 if features & MediaPlayerEntityFeature.STOP:
131 await call_service(SERVICE_MEDIA_STOP, [])
132 elif state.state == STATE_PAUSED:
133 if features & MediaPlayerEntityFeature.PAUSE:
134 await call_service(SERVICE_MEDIA_PAUSE, [])
139 states: Iterable[State],
141 context: Context |
None =
None,
142 reproduce_options: dict[str, Any] |
None =
None,
144 """Reproduce component states."""
145 await asyncio.gather(
148 hass, state, context=context, reproduce_options=reproduce_options