1 """Module that groups code required to handle state restore for component."""
3 from __future__
import annotations
6 from collections.abc
import Iterable
19 from .const
import ATTR_HUMIDITY, DOMAIN, SERVICE_SET_HUMIDITY, SERVICE_SET_MODE
21 _LOGGER = logging.getLogger(__name__)
28 context: Context |
None =
None,
29 reproduce_options: dict[str, Any] |
None =
None,
31 """Reproduce component states."""
32 if (cur_state := hass.states.get(state.entity_id))
is None:
33 _LOGGER.warning(
"Unable to find entity %s", state.entity_id)
36 async
def call_service(service: str, keys: Iterable[str]) ->
None:
37 """Call service with set of attributes given."""
38 data = {
"entity_id": state.entity_id}
40 if key
in state.attributes:
41 data[key] = state.attributes[key]
43 await hass.services.async_call(
44 DOMAIN, service, data, blocking=
True, context=context
47 if state.state == STATE_OFF:
49 if cur_state.state != STATE_OFF:
50 await call_service(SERVICE_TURN_OFF, [])
53 if state.state != STATE_ON:
56 "Invalid state specified for %s: %s", state.entity_id, state.state
62 if cur_state.state != STATE_ON:
63 await call_service(SERVICE_TURN_ON, [])
65 if (cur_state := hass.states.get(state.entity_id))
is None:
66 _LOGGER.warning(
"Unable to find entity %s", state.entity_id)
71 if ATTR_MODE
in state.attributes
and state.attributes[
73 ] != cur_state.attributes.get(ATTR_MODE):
74 await call_service(SERVICE_SET_MODE, [ATTR_MODE])
77 if ATTR_HUMIDITY
in state.attributes
and state.attributes[
79 ] != cur_state.attributes.get(ATTR_HUMIDITY):
80 await call_service(SERVICE_SET_HUMIDITY, [ATTR_HUMIDITY])
85 states: Iterable[State],
87 context: Context |
None =
None,
88 reproduce_options: dict[str, Any] |
None =
None,
90 """Reproduce component states."""
94 hass, state, context=context, reproduce_options=reproduce_options
None async_reproduce_states(HomeAssistant hass, Iterable[State] states, *Context|None context=None, dict[str, Any]|None reproduce_options=None)
None _async_reproduce_states(HomeAssistant hass, State state, *Context|None context=None, dict[str, Any]|None reproduce_options=None)