Reproduce states by letting each component opt in on handling state recovery itself (#18700)
* Move group to it's own setup * Let each component to handle restore of state * Move constants for climate into const.py For now import all into __init__.py to keep backword compat * Move media plyaer constants to const.py file For now import all constants into __init__.py to keep backword compatibility * Move media player to it's own file * Move climate to it's own file * Remove ecobee service from common components BREAKING CHANGE * Add tests for climate * Add test for media_player * Make sure we clone timestamps of state * Add tests for groups * Remove old tests for media player, it's handled by other tests * Add tests for calls to component functions * Add docstring for climate const * Add docstring for media_player const * Explicitly import constants in climate * Explicitly import constants in media_player * Add period to climate const * Add period to media_player const * Fix some lint errors in climate * Fix some lint errors in media_player * Fix lint warnings on climate tests * Fix lint warnings on group tests * Fix lint warnings on media_player tests * Fix lint warnings on state tests * Adjust indent for state tests
This commit is contained in:
parent
c76a61ad16
commit
3bb5caabe2
13 changed files with 846 additions and 228 deletions
87
homeassistant/components/media_player/reproduce_state.py
Normal file
87
homeassistant/components/media_player/reproduce_state.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
"""Module that groups code required to handle state restore for component."""
|
||||
import asyncio
|
||||
from typing import Iterable, Optional
|
||||
|
||||
from homeassistant.const import (
|
||||
SERVICE_MEDIA_PAUSE, SERVICE_MEDIA_PLAY, SERVICE_MEDIA_SEEK,
|
||||
SERVICE_MEDIA_STOP, SERVICE_TURN_OFF, SERVICE_TURN_ON, SERVICE_VOLUME_MUTE,
|
||||
SERVICE_VOLUME_SET, STATE_IDLE, STATE_OFF, STATE_ON, STATE_PAUSED,
|
||||
STATE_PLAYING)
|
||||
from homeassistant.core import Context, State
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from .const import (
|
||||
ATTR_MEDIA_VOLUME_LEVEL,
|
||||
ATTR_MEDIA_VOLUME_MUTED,
|
||||
ATTR_MEDIA_SEEK_POSITION,
|
||||
ATTR_INPUT_SOURCE,
|
||||
ATTR_SOUND_MODE,
|
||||
ATTR_MEDIA_CONTENT_TYPE,
|
||||
ATTR_MEDIA_CONTENT_ID,
|
||||
ATTR_MEDIA_ENQUEUE,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
SERVICE_SELECT_SOUND_MODE,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
|
||||
async def _async_reproduce_states(hass: HomeAssistantType,
|
||||
state: State,
|
||||
context: Optional[Context] = None) -> None:
|
||||
"""Reproduce component states."""
|
||||
async def call_service(service: str, keys: Iterable):
|
||||
"""Call service with set of attributes given."""
|
||||
data = {}
|
||||
data['entity_id'] = state.entity_id
|
||||
for key in keys:
|
||||
if key in state.attributes:
|
||||
data[key] = state.attributes[key]
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, service, data,
|
||||
blocking=True, context=context)
|
||||
|
||||
if state.state == STATE_ON:
|
||||
await call_service(SERVICE_TURN_ON, [])
|
||||
elif state.state == STATE_OFF:
|
||||
await call_service(SERVICE_TURN_OFF, [])
|
||||
elif state.state == STATE_PLAYING:
|
||||
await call_service(SERVICE_MEDIA_PLAY, [])
|
||||
elif state.state == STATE_IDLE:
|
||||
await call_service(SERVICE_MEDIA_STOP, [])
|
||||
elif state.state == STATE_PAUSED:
|
||||
await call_service(SERVICE_MEDIA_PAUSE, [])
|
||||
|
||||
if ATTR_MEDIA_VOLUME_LEVEL in state.attributes:
|
||||
await call_service(SERVICE_VOLUME_SET, [ATTR_MEDIA_VOLUME_LEVEL])
|
||||
|
||||
if ATTR_MEDIA_VOLUME_MUTED in state.attributes:
|
||||
await call_service(SERVICE_VOLUME_MUTE, [ATTR_MEDIA_VOLUME_MUTED])
|
||||
|
||||
if ATTR_MEDIA_SEEK_POSITION in state.attributes:
|
||||
await call_service(SERVICE_MEDIA_SEEK, [ATTR_MEDIA_SEEK_POSITION])
|
||||
|
||||
if ATTR_INPUT_SOURCE in state.attributes:
|
||||
await call_service(SERVICE_SELECT_SOURCE, [ATTR_INPUT_SOURCE])
|
||||
|
||||
if ATTR_SOUND_MODE in state.attributes:
|
||||
await call_service(SERVICE_SELECT_SOUND_MODE, [ATTR_SOUND_MODE])
|
||||
|
||||
if (ATTR_MEDIA_CONTENT_TYPE in state.attributes) and \
|
||||
(ATTR_MEDIA_CONTENT_ID in state.attributes):
|
||||
await call_service(SERVICE_PLAY_MEDIA,
|
||||
[ATTR_MEDIA_CONTENT_TYPE,
|
||||
ATTR_MEDIA_CONTENT_ID,
|
||||
ATTR_MEDIA_ENQUEUE])
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_reproduce_states(hass: HomeAssistantType,
|
||||
states: Iterable[State],
|
||||
context: Optional[Context] = None) -> None:
|
||||
"""Reproduce component states."""
|
||||
await asyncio.gather(*[
|
||||
_async_reproduce_states(hass, state, context)
|
||||
for state in states])
|
Loading…
Add table
Add a link
Reference in a new issue