Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Module that groups code required to handle state restore for component."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Iterable
7 from typing import Any
8 
9 from homeassistant.const import ATTR_TEMPERATURE
10 from homeassistant.core import Context, HomeAssistant, State
11 
12 from .const import (
13  ATTR_FAN_MODE,
14  ATTR_HUMIDITY,
15  ATTR_HVAC_MODE,
16  ATTR_PRESET_MODE,
17  ATTR_SWING_HORIZONTAL_MODE,
18  ATTR_SWING_MODE,
19  ATTR_TARGET_TEMP_HIGH,
20  ATTR_TARGET_TEMP_LOW,
21  DOMAIN,
22  HVAC_MODES,
23  SERVICE_SET_FAN_MODE,
24  SERVICE_SET_HUMIDITY,
25  SERVICE_SET_HVAC_MODE,
26  SERVICE_SET_PRESET_MODE,
27  SERVICE_SET_SWING_HORIZONTAL_MODE,
28  SERVICE_SET_SWING_MODE,
29  SERVICE_SET_TEMPERATURE,
30 )
31 
32 
34  hass: HomeAssistant,
35  state: State,
36  *,
37  context: Context | None = None,
38  reproduce_options: dict[str, Any] | None = None,
39 ) -> None:
40  """Reproduce component states."""
41 
42  async def call_service(
43  service: str, keys: Iterable, data: dict[str, Any] | None = None
44  ) -> None:
45  """Call service with set of attributes given."""
46  data = data or {}
47  data["entity_id"] = state.entity_id
48  for key in keys:
49  if (value := state.attributes.get(key)) is not None:
50  data[key] = value
51 
52  await hass.services.async_call(
53  DOMAIN, service, data, blocking=True, context=context
54  )
55 
56  if state.state in HVAC_MODES:
57  await call_service(SERVICE_SET_HVAC_MODE, [], {ATTR_HVAC_MODE: state.state})
58 
59  if (
60  (ATTR_TEMPERATURE in state.attributes)
61  or (ATTR_TARGET_TEMP_HIGH in state.attributes)
62  or (ATTR_TARGET_TEMP_LOW in state.attributes)
63  ):
64  await call_service(
65  SERVICE_SET_TEMPERATURE,
66  [ATTR_TEMPERATURE, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW],
67  )
68 
69  if (
70  ATTR_PRESET_MODE in state.attributes
71  and state.attributes[ATTR_PRESET_MODE] is not None
72  ):
73  await call_service(SERVICE_SET_PRESET_MODE, [ATTR_PRESET_MODE])
74 
75  if (
76  ATTR_SWING_MODE in state.attributes
77  and state.attributes[ATTR_SWING_MODE] is not None
78  ):
79  await call_service(SERVICE_SET_SWING_MODE, [ATTR_SWING_MODE])
80 
81  if (
82  ATTR_SWING_HORIZONTAL_MODE in state.attributes
83  and state.attributes[ATTR_SWING_HORIZONTAL_MODE] is not None
84  ):
85  await call_service(
86  SERVICE_SET_SWING_HORIZONTAL_MODE, [ATTR_SWING_HORIZONTAL_MODE]
87  )
88 
89  if (
90  ATTR_FAN_MODE in state.attributes
91  and state.attributes[ATTR_FAN_MODE] is not None
92  ):
93  await call_service(SERVICE_SET_FAN_MODE, [ATTR_FAN_MODE])
94 
95  if ATTR_HUMIDITY in state.attributes:
96  await call_service(SERVICE_SET_HUMIDITY, [ATTR_HUMIDITY])
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 component states."""
107  await asyncio.gather(
108  *(
110  hass, state, context=context, reproduce_options=reproduce_options
111  )
112  for state in states
113  )
114  )
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)