mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
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:
committed by
Paulus Schoutsen
parent
c76a61ad16
commit
3bb5caabe2
91
homeassistant/components/climate/reproduce_state.py
Normal file
91
homeassistant/components/climate/reproduce_state.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Module that groups code required to handle state restore for component."""
|
||||
import asyncio
|
||||
from typing import Iterable, Optional
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_TEMPERATURE, SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON, STATE_OFF, STATE_ON)
|
||||
from homeassistant.core import Context, State
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from .const import (
|
||||
ATTR_AUX_HEAT,
|
||||
ATTR_AWAY_MODE,
|
||||
ATTR_TARGET_TEMP_HIGH,
|
||||
ATTR_TARGET_TEMP_LOW,
|
||||
ATTR_HOLD_MODE,
|
||||
ATTR_OPERATION_MODE,
|
||||
ATTR_SWING_MODE,
|
||||
ATTR_HUMIDITY,
|
||||
SERVICE_SET_AWAY_MODE,
|
||||
SERVICE_SET_AUX_HEAT,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
SERVICE_SET_HOLD_MODE,
|
||||
SERVICE_SET_OPERATION_MODE,
|
||||
SERVICE_SET_SWING_MODE,
|
||||
SERVICE_SET_HUMIDITY,
|
||||
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, [])
|
||||
|
||||
if ATTR_AUX_HEAT in state.attributes:
|
||||
await call_service(SERVICE_SET_AUX_HEAT, [ATTR_AUX_HEAT])
|
||||
|
||||
if ATTR_AWAY_MODE in state.attributes:
|
||||
await call_service(SERVICE_SET_AWAY_MODE, [ATTR_AWAY_MODE])
|
||||
|
||||
if (ATTR_TEMPERATURE in state.attributes) or \
|
||||
(ATTR_TARGET_TEMP_HIGH in state.attributes) or \
|
||||
(ATTR_TARGET_TEMP_LOW in state.attributes):
|
||||
await call_service(SERVICE_SET_TEMPERATURE,
|
||||
[ATTR_TEMPERATURE,
|
||||
ATTR_TARGET_TEMP_HIGH,
|
||||
ATTR_TARGET_TEMP_LOW])
|
||||
|
||||
if ATTR_HOLD_MODE in state.attributes:
|
||||
await call_service(SERVICE_SET_HOLD_MODE,
|
||||
[ATTR_HOLD_MODE])
|
||||
|
||||
if ATTR_OPERATION_MODE in state.attributes:
|
||||
await call_service(SERVICE_SET_OPERATION_MODE,
|
||||
[ATTR_OPERATION_MODE])
|
||||
|
||||
if ATTR_SWING_MODE in state.attributes:
|
||||
await call_service(SERVICE_SET_SWING_MODE,
|
||||
[ATTR_SWING_MODE])
|
||||
|
||||
if ATTR_HUMIDITY in state.attributes:
|
||||
await call_service(SERVICE_SET_HUMIDITY,
|
||||
[ATTR_HUMIDITY])
|
||||
|
||||
|
||||
@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])
|
||||
Reference in New Issue
Block a user