Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce an Input text state."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Iterable
7 import logging
8 from typing import Any
9 
10 from homeassistant.const import ATTR_ENTITY_ID
11 from homeassistant.core import Context, HomeAssistant, State
12 
13 from . import ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
19  hass: HomeAssistant,
20  state: State,
21  *,
22  context: Context | None = None,
23  reproduce_options: dict[str, Any] | None = None,
24 ) -> None:
25  """Reproduce a single state."""
26  # Return if we can't find the entity
27  if (cur_state := hass.states.get(state.entity_id)) is None:
28  _LOGGER.warning("Unable to find entity %s", state.entity_id)
29  return
30 
31  # Return if we are already at the right state.
32  if cur_state.state == state.state:
33  return
34 
35  # Call service
36  service = SERVICE_SET_VALUE
37  service_data = {ATTR_ENTITY_ID: state.entity_id, ATTR_VALUE: state.state}
38 
39  await hass.services.async_call(
40  DOMAIN, service, service_data, context=context, blocking=True
41  )
42 
43 
45  hass: HomeAssistant,
46  states: Iterable[State],
47  *,
48  context: Context | None = None,
49  reproduce_options: dict[str, Any] | None = None,
50 ) -> None:
51  """Reproduce Input text states."""
52  # Reproduce states in parallel.
53  await asyncio.gather(
54  *(
56  hass, state, context=context, reproduce_options=reproduce_options
57  )
58  for state in states
59  )
60  )
None async_reproduce_states(HomeAssistant hass, Iterable[State] states, *Context|None context=None, dict[str, Any]|None reproduce_options=None)
None _async_reproduce_state(HomeAssistant hass, State state, *Context|None context=None, dict[str, Any]|None reproduce_options=None)