Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce a Text entity 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 .const 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  if (cur_state := hass.states.get(state.entity_id)) is None:
27  _LOGGER.warning("Unable to find entity %s", state.entity_id)
28  return
29 
30  # Return if we are already at the right state.
31  if cur_state.state == state.state:
32  return
33 
34  service = SERVICE_SET_VALUE
35  service_data = {ATTR_ENTITY_ID: state.entity_id, ATTR_VALUE: state.state}
36 
37  await hass.services.async_call(
38  DOMAIN, service, service_data, context=context, blocking=True
39  )
40 
41 
43  hass: HomeAssistant,
44  states: Iterable[State],
45  *,
46  context: Context | None = None,
47  reproduce_options: dict[str, Any] | None = None,
48 ) -> None:
49  """Reproduce multiple Text states."""
50  # Reproduce states in parallel.
51  await asyncio.gather(
52  *(
54  hass, state, context=context, reproduce_options=reproduce_options
55  )
56  for state in states
57  )
58  )
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)