Home Assistant Unofficial Reference 2024.12.1
reproduce_state.py
Go to the documentation of this file.
1 """Reproduce an Alarm control panel 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, Final
9 
10 from homeassistant.const import (
11  ATTR_ENTITY_ID,
12  SERVICE_ALARM_ARM_AWAY,
13  SERVICE_ALARM_ARM_CUSTOM_BYPASS,
14  SERVICE_ALARM_ARM_HOME,
15  SERVICE_ALARM_ARM_NIGHT,
16  SERVICE_ALARM_ARM_VACATION,
17  SERVICE_ALARM_DISARM,
18  SERVICE_ALARM_TRIGGER,
19 )
20 from homeassistant.core import Context, HomeAssistant, State
21 
22 from . import DOMAIN, AlarmControlPanelState
23 
24 _LOGGER: Final = logging.getLogger(__name__)
25 
26 VALID_STATES: Final[set[str]] = {
27  AlarmControlPanelState.ARMED_AWAY,
28  AlarmControlPanelState.ARMED_CUSTOM_BYPASS,
29  AlarmControlPanelState.ARMED_HOME,
30  AlarmControlPanelState.ARMED_NIGHT,
31  AlarmControlPanelState.ARMED_VACATION,
32  AlarmControlPanelState.DISARMED,
33  AlarmControlPanelState.TRIGGERED,
34 }
35 
36 
38  hass: HomeAssistant,
39  state: State,
40  *,
41  context: Context | None = None,
42  reproduce_options: dict[str, Any] | None = None,
43 ) -> None:
44  """Reproduce a single state."""
45  if (cur_state := hass.states.get(state.entity_id)) is None:
46  _LOGGER.warning("Unable to find entity %s", state.entity_id)
47  return
48 
49  if state.state not in VALID_STATES:
50  _LOGGER.warning(
51  "Invalid state specified for %s: %s", state.entity_id, state.state
52  )
53  return
54 
55  # Return if we are already at the right state.
56  if cur_state.state == state.state:
57  return
58 
59  service_data = {ATTR_ENTITY_ID: state.entity_id}
60 
61  if state.state == AlarmControlPanelState.ARMED_AWAY:
62  service = SERVICE_ALARM_ARM_AWAY
63  elif state.state == AlarmControlPanelState.ARMED_CUSTOM_BYPASS:
64  service = SERVICE_ALARM_ARM_CUSTOM_BYPASS
65  elif state.state == AlarmControlPanelState.ARMED_HOME:
66  service = SERVICE_ALARM_ARM_HOME
67  elif state.state == AlarmControlPanelState.ARMED_NIGHT:
68  service = SERVICE_ALARM_ARM_NIGHT
69  elif state.state == AlarmControlPanelState.ARMED_VACATION:
70  service = SERVICE_ALARM_ARM_VACATION
71  elif state.state == AlarmControlPanelState.DISARMED:
72  service = SERVICE_ALARM_DISARM
73  elif state.state == AlarmControlPanelState.TRIGGERED:
74  service = SERVICE_ALARM_TRIGGER
75 
76  await hass.services.async_call(
77  DOMAIN, service, service_data, context=context, blocking=True
78  )
79 
80 
82  hass: HomeAssistant,
83  states: Iterable[State],
84  *,
85  context: Context | None = None,
86  reproduce_options: dict[str, Any] | None = None,
87 ) -> None:
88  """Reproduce Alarm control panel states."""
89  await asyncio.gather(
90  *(
92  hass, state, context=context, reproduce_options=reproduce_options
93  )
94  for state in states
95  )
96  )
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)