Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce an Vacuum 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_IDLE,
15  STATE_OFF,
16  STATE_ON,
17  STATE_PAUSED,
18 )
19 from homeassistant.core import Context, HomeAssistant, State
20 
21 from . import (
22  ATTR_FAN_SPEED,
23  DOMAIN,
24  SERVICE_PAUSE,
25  SERVICE_RETURN_TO_BASE,
26  SERVICE_SET_FAN_SPEED,
27  SERVICE_START,
28  SERVICE_STOP,
29  STATE_CLEANING,
30  STATE_DOCKED,
31  STATE_RETURNING,
32 )
33 
34 _LOGGER = logging.getLogger(__name__)
35 
36 VALID_STATES_TOGGLE = {STATE_ON, STATE_OFF}
37 VALID_STATES_STATE = {
38  STATE_CLEANING,
39  STATE_DOCKED,
40  STATE_IDLE,
41  STATE_PAUSED,
42  STATE_RETURNING,
43 }
44 
45 
47  hass: HomeAssistant,
48  state: State,
49  *,
50  context: Context | None = None,
51  reproduce_options: dict[str, Any] | None = None,
52 ) -> None:
53  """Reproduce a single state."""
54  if (cur_state := hass.states.get(state.entity_id)) is None:
55  _LOGGER.warning("Unable to find entity %s", state.entity_id)
56  return
57 
58  if not (state.state in VALID_STATES_TOGGLE or state.state in VALID_STATES_STATE):
59  _LOGGER.warning(
60  "Invalid state specified for %s: %s", state.entity_id, state.state
61  )
62  return
63 
64  # Return if we are already at the right state.
65  if cur_state.state == state.state and cur_state.attributes.get(
66  ATTR_FAN_SPEED
67  ) == state.attributes.get(ATTR_FAN_SPEED):
68  return
69 
70  service_data = {ATTR_ENTITY_ID: state.entity_id}
71 
72  if cur_state.state != state.state:
73  # Wrong state
74  if state.state == STATE_ON:
75  service = SERVICE_TURN_ON
76  elif state.state == STATE_OFF:
77  service = SERVICE_TURN_OFF
78  elif state.state == STATE_CLEANING:
79  service = SERVICE_START
80  elif state.state in [STATE_DOCKED, STATE_RETURNING]:
81  service = SERVICE_RETURN_TO_BASE
82  elif state.state == STATE_IDLE:
83  service = SERVICE_STOP
84  elif state.state == STATE_PAUSED:
85  service = SERVICE_PAUSE
86 
87  await hass.services.async_call(
88  DOMAIN, service, service_data, context=context, blocking=True
89  )
90 
91  if cur_state.attributes.get(ATTR_FAN_SPEED) != state.attributes.get(ATTR_FAN_SPEED):
92  # Wrong fan speed
93  service_data["fan_speed"] = state.attributes[ATTR_FAN_SPEED]
94  await hass.services.async_call(
95  DOMAIN, SERVICE_SET_FAN_SPEED, service_data, context=context, blocking=True
96  )
97 
98 
100  hass: HomeAssistant,
101  states: Iterable[State],
102  *,
103  context: Context | None = None,
104  reproduce_options: dict[str, Any] | None = None,
105 ) -> None:
106  """Reproduce Vacuum states."""
107  # Reproduce states in parallel.
108  await asyncio.gather(
109  *(
111  hass, state, context=context, reproduce_options=reproduce_options
112  )
113  for state in states
114  )
115  )
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)