Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce a Select 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_OPTION, ATTR_OPTIONS, DOMAIN, SERVICE_SELECT_OPTION
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  if state.state not in cur_state.attributes.get(ATTR_OPTIONS, []):
31  _LOGGER.warning(
32  "Invalid state specified for %s: %s", state.entity_id, state.state
33  )
34  return
35 
36  # Return if we are already at the right state.
37  if cur_state.state == state.state:
38  return
39 
40  await hass.services.async_call(
41  DOMAIN,
42  SERVICE_SELECT_OPTION,
43  {ATTR_ENTITY_ID: state.entity_id, ATTR_OPTION: state.state},
44  context=context,
45  blocking=True,
46  )
47 
48 
50  hass: HomeAssistant,
51  states: Iterable[State],
52  *,
53  context: Context | None = None,
54  reproduce_options: dict[str, Any] | None = None,
55 ) -> None:
56  """Reproduce multiple select states."""
57  await asyncio.gather(
58  *(
60  hass, state, context=context, reproduce_options=reproduce_options
61  )
62  for state in states
63  )
64  )
None _async_reproduce_state(HomeAssistant hass, State state, *Context|None context=None, dict[str, Any]|None reproduce_options=None)
None async_reproduce_states(HomeAssistant hass, Iterable[State] states, *Context|None context=None, dict[str, Any]|None reproduce_options=None)