Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce an Water heater 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  ATTR_TEMPERATURE,
13  SERVICE_TURN_OFF,
14  SERVICE_TURN_ON,
15  STATE_OFF,
16  STATE_ON,
17 )
18 from homeassistant.core import Context, HomeAssistant, State
19 
20 from . import (
21  ATTR_AWAY_MODE,
22  ATTR_OPERATION_MODE,
23  DOMAIN,
24  SERVICE_SET_AWAY_MODE,
25  SERVICE_SET_OPERATION_MODE,
26  SERVICE_SET_TEMPERATURE,
27  STATE_ECO,
28  STATE_ELECTRIC,
29  STATE_GAS,
30  STATE_HEAT_PUMP,
31  STATE_HIGH_DEMAND,
32  STATE_PERFORMANCE,
33 )
34 
35 _LOGGER = logging.getLogger(__name__)
36 
37 VALID_STATES = {
38  STATE_ECO,
39  STATE_ELECTRIC,
40  STATE_GAS,
41  STATE_HEAT_PUMP,
42  STATE_HIGH_DEMAND,
43  STATE_OFF,
44  STATE_ON,
45  STATE_PERFORMANCE,
46 }
47 
48 
50  hass: HomeAssistant,
51  state: State,
52  *,
53  context: Context | None = None,
54  reproduce_options: dict[str, Any] | None = None,
55 ) -> None:
56  """Reproduce a single state."""
57  if (cur_state := hass.states.get(state.entity_id)) is None:
58  _LOGGER.warning("Unable to find entity %s", state.entity_id)
59  return
60 
61  if state.state not in VALID_STATES:
62  _LOGGER.warning(
63  "Invalid state specified for %s: %s", state.entity_id, state.state
64  )
65  return
66 
67  # Return if we are already at the right state.
68  if (
69  cur_state.state == state.state
70  and cur_state.attributes.get(ATTR_TEMPERATURE)
71  == state.attributes.get(ATTR_TEMPERATURE)
72  and cur_state.attributes.get(ATTR_AWAY_MODE)
73  == state.attributes.get(ATTR_AWAY_MODE)
74  ):
75  return
76 
77  service_data = {ATTR_ENTITY_ID: state.entity_id}
78 
79  if state.state != cur_state.state:
80  if state.state == STATE_ON:
81  service = SERVICE_TURN_ON
82  elif state.state == STATE_OFF:
83  service = SERVICE_TURN_OFF
84  else:
85  service = SERVICE_SET_OPERATION_MODE
86  service_data[ATTR_OPERATION_MODE] = state.state
87 
88  await hass.services.async_call(
89  DOMAIN, service, service_data, context=context, blocking=True
90  )
91 
92  if (
93  state.attributes.get(ATTR_TEMPERATURE)
94  != cur_state.attributes.get(ATTR_TEMPERATURE)
95  and state.attributes.get(ATTR_TEMPERATURE) is not None
96  ):
97  await hass.services.async_call(
98  DOMAIN,
99  SERVICE_SET_TEMPERATURE,
100  {
101  ATTR_ENTITY_ID: state.entity_id,
102  ATTR_TEMPERATURE: state.attributes.get(ATTR_TEMPERATURE),
103  },
104  context=context,
105  blocking=True,
106  )
107 
108  if (
109  state.attributes.get(ATTR_AWAY_MODE) != cur_state.attributes.get(ATTR_AWAY_MODE)
110  and state.attributes.get(ATTR_AWAY_MODE) is not None
111  ):
112  await hass.services.async_call(
113  DOMAIN,
114  SERVICE_SET_AWAY_MODE,
115  {
116  ATTR_ENTITY_ID: state.entity_id,
117  ATTR_AWAY_MODE: state.attributes.get(ATTR_AWAY_MODE),
118  },
119  context=context,
120  blocking=True,
121  )
122 
123 
125  hass: HomeAssistant,
126  states: Iterable[State],
127  *,
128  context: Context | None = None,
129  reproduce_options: dict[str, Any] | None = None,
130 ) -> None:
131  """Reproduce Water heater states."""
132  await asyncio.gather(
133  *(
135  hass, state, context=context, reproduce_options=reproduce_options
136  )
137  for state in states
138  )
139  )
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)