Home Assistant Unofficial Reference 2024.12.1
service_functions.py
Go to the documentation of this file.
1 """Support for IHC devices."""
2 
3 import voluptuous as vol
4 
5 from homeassistant.core import HomeAssistant
7 
8 from .const import (
9  ATTR_CONTROLLER_ID,
10  ATTR_IHC_ID,
11  ATTR_VALUE,
12  DOMAIN,
13  IHC_CONTROLLER,
14  IHC_CONTROLLER_INDEX,
15  SERVICE_PULSE,
16  SERVICE_SET_RUNTIME_VALUE_BOOL,
17  SERVICE_SET_RUNTIME_VALUE_FLOAT,
18  SERVICE_SET_RUNTIME_VALUE_INT,
19 )
20 from .util import async_pulse, async_set_bool, async_set_float, async_set_int
21 
22 SET_RUNTIME_VALUE_BOOL_SCHEMA = vol.Schema(
23  {
24  vol.Required(ATTR_IHC_ID): cv.positive_int,
25  vol.Required(ATTR_VALUE): cv.boolean,
26  vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int,
27  }
28 )
29 
30 SET_RUNTIME_VALUE_INT_SCHEMA = vol.Schema(
31  {
32  vol.Required(ATTR_IHC_ID): cv.positive_int,
33  vol.Required(ATTR_VALUE): vol.Coerce(int),
34  vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int,
35  }
36 )
37 
38 SET_RUNTIME_VALUE_FLOAT_SCHEMA = vol.Schema(
39  {
40  vol.Required(ATTR_IHC_ID): cv.positive_int,
41  vol.Required(ATTR_VALUE): vol.Coerce(float),
42  vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int,
43  }
44 )
45 
46 PULSE_SCHEMA = vol.Schema(
47  {
48  vol.Required(ATTR_IHC_ID): cv.positive_int,
49  vol.Optional(ATTR_CONTROLLER_ID, default=0): cv.positive_int,
50  }
51 )
52 
53 
54 def setup_service_functions(hass: HomeAssistant) -> None:
55  """Set up the IHC service functions."""
56 
57  def _get_controller(call):
58  controller_index = call.data[ATTR_CONTROLLER_ID]
59  for controller_id in hass.data[DOMAIN]:
60  controller_conf = hass.data[DOMAIN][controller_id]
61  if controller_conf[IHC_CONTROLLER_INDEX] == controller_index:
62  return controller_conf[IHC_CONTROLLER]
63  # if not found the controller_index is ouf of range
64  raise ValueError("The controller index is out of range")
65 
66  async def async_set_runtime_value_bool(call):
67  """Set a IHC runtime bool value service function."""
68  ihc_id = call.data[ATTR_IHC_ID]
69  value = call.data[ATTR_VALUE]
70  ihc_controller = _get_controller(call)
71  await async_set_bool(hass, ihc_controller, ihc_id, value)
72 
73  async def async_set_runtime_value_int(call):
74  """Set a IHC runtime integer value service function."""
75  ihc_id = call.data[ATTR_IHC_ID]
76  value = call.data[ATTR_VALUE]
77  ihc_controller = _get_controller(call)
78  await async_set_int(hass, ihc_controller, ihc_id, value)
79 
80  async def async_set_runtime_value_float(call):
81  """Set a IHC runtime float value service function."""
82  ihc_id = call.data[ATTR_IHC_ID]
83  value = call.data[ATTR_VALUE]
84  ihc_controller = _get_controller(call)
85  await async_set_float(hass, ihc_controller, ihc_id, value)
86 
87  async def async_pulse_runtime_input(call):
88  """Pulse a IHC controller input function."""
89  ihc_id = call.data[ATTR_IHC_ID]
90  ihc_controller = _get_controller(call)
91  await async_pulse(hass, ihc_controller, ihc_id)
92 
93  hass.services.register(
94  DOMAIN,
95  SERVICE_SET_RUNTIME_VALUE_BOOL,
96  async_set_runtime_value_bool,
97  schema=SET_RUNTIME_VALUE_BOOL_SCHEMA,
98  )
99  hass.services.register(
100  DOMAIN,
101  SERVICE_SET_RUNTIME_VALUE_INT,
102  async_set_runtime_value_int,
103  schema=SET_RUNTIME_VALUE_INT_SCHEMA,
104  )
105  hass.services.register(
106  DOMAIN,
107  SERVICE_SET_RUNTIME_VALUE_FLOAT,
108  async_set_runtime_value_float,
109  schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA,
110  )
111  hass.services.register(
112  DOMAIN, SERVICE_PULSE, async_pulse_runtime_input, schema=PULSE_SCHEMA
113  )
asyncio.Future[bool] async_set_float(HomeAssistant hass, IHCController ihc_controller, int ihc_id, float value)
Definition: util.py:42
None async_pulse(HomeAssistant hass, IHCController ihc_controller, int ihc_id)
Definition: util.py:12
asyncio.Future[bool] async_set_int(HomeAssistant hass, IHCController ihc_controller, int ihc_id, int value)
Definition: util.py:32
asyncio.Future[bool] async_set_bool(HomeAssistant hass, IHCController ihc_controller, int ihc_id, bool value)
Definition: util.py:22