Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce an input boolean 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 (
11  ATTR_ENTITY_ID,
12  SERVICE_TURN_OFF,
13  SERVICE_TURN_ON,
14  STATE_OFF,
15  STATE_ON,
16 )
17 from homeassistant.core import Context, HomeAssistant, State
18 
19 from . import DOMAIN
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  state: State,
27  *,
28  context: Context | None = None,
29  reproduce_options: dict[str, Any] | None = None,
30 ) -> None:
31  """Reproduce input boolean states."""
32  if (cur_state := hass.states.get(state.entity_id)) is None:
33  _LOGGER.warning("Unable to find entity %s", state.entity_id)
34  return
35 
36  if state.state not in (STATE_ON, STATE_OFF):
37  _LOGGER.warning(
38  "Invalid state specified for %s: %s", state.entity_id, state.state
39  )
40  return
41 
42  if cur_state.state == state.state:
43  return
44 
45  service = SERVICE_TURN_ON if state.state == STATE_ON else SERVICE_TURN_OFF
46 
47  await hass.services.async_call(
48  DOMAIN,
49  service,
50  {ATTR_ENTITY_ID: state.entity_id},
51  context=context,
52  blocking=True,
53  )
54 
55 
57  hass: HomeAssistant,
58  states: Iterable[State],
59  *,
60  context: Context | None = None,
61  reproduce_options: dict[str, Any] | None = None,
62 ) -> None:
63  """Reproduce component states."""
64  await asyncio.gather(
65  *(
67  hass, state, context=context, reproduce_options=reproduce_options
68  )
69  for state in states
70  )
71  )
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)