Home Assistant Unofficial Reference 2024.12.1
intent.py
Go to the documentation of this file.
1 """Intents for the humidifier integration."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.const import ATTR_ENTITY_ID, ATTR_MODE, STATE_OFF
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers import intent
11 
12 from . import (
13  ATTR_AVAILABLE_MODES,
14  ATTR_HUMIDITY,
15  DOMAIN,
16  SERVICE_SET_HUMIDITY,
17  SERVICE_SET_MODE,
18  SERVICE_TURN_ON,
19  HumidifierEntityFeature,
20 )
21 
22 INTENT_HUMIDITY = "HassHumidifierSetpoint"
23 INTENT_MODE = "HassHumidifierMode"
24 
25 
26 async def async_setup_intents(hass: HomeAssistant) -> None:
27  """Set up the humidifier intents."""
28  intent.async_register(hass, HumidityHandler())
29  intent.async_register(hass, SetModeHandler())
30 
31 
32 class HumidityHandler(intent.IntentHandler):
33  """Handle set humidity intents."""
34 
35  intent_type = INTENT_HUMIDITY
36  description = "Set desired humidity level"
37  slot_schema = {
38  vol.Required("name"): intent.non_empty_string,
39  vol.Required("humidity"): vol.All(vol.Coerce(int), vol.Range(0, 100)),
40  }
41  platforms = {DOMAIN}
42 
43  async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
44  """Handle the hass intent."""
45  hass = intent_obj.hass
46  slots = self.async_validate_slots(intent_obj.slots)
47 
48  match_constraints = intent.MatchTargetsConstraints(
49  name=slots["name"]["value"],
50  domains=[DOMAIN],
51  assistant=intent_obj.assistant,
52  )
53  match_result = intent.async_match_targets(hass, match_constraints)
54  if not match_result.is_match:
55  raise intent.MatchFailedError(
56  result=match_result, constraints=match_constraints
57  )
58 
59  state = match_result.states[0]
60  service_data = {ATTR_ENTITY_ID: state.entity_id}
61 
62  humidity = slots["humidity"]["value"]
63 
64  if state.state == STATE_OFF:
65  await hass.services.async_call(
66  DOMAIN, SERVICE_TURN_ON, service_data, context=intent_obj.context
67  )
68  speech = f"Turned {state.name} on and set humidity to {humidity}%"
69  else:
70  speech = f"The {state.name} is set to {humidity}%"
71 
72  service_data[ATTR_HUMIDITY] = humidity
73  await hass.services.async_call(
74  DOMAIN,
75  SERVICE_SET_HUMIDITY,
76  service_data,
77  context=intent_obj.context,
78  blocking=True,
79  )
80 
81  response = intent_obj.create_response()
82 
83  response.async_set_speech(speech)
84  return response
85 
86 
87 class SetModeHandler(intent.IntentHandler):
88  """Handle set humidity intents."""
89 
90  intent_type = INTENT_MODE
91  description = "Set humidifier mode"
92  slot_schema = {
93  vol.Required("name"): intent.non_empty_string,
94  vol.Required("mode"): cv.string,
95  }
96  platforms = {DOMAIN}
97 
98  async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
99  """Handle the hass intent."""
100  hass = intent_obj.hass
101  slots = self.async_validate_slots(intent_obj.slots)
102  match_constraints = intent.MatchTargetsConstraints(
103  name=slots["name"]["value"],
104  domains=[DOMAIN],
105  assistant=intent_obj.assistant,
106  )
107  match_result = intent.async_match_targets(hass, match_constraints)
108  if not match_result.is_match:
109  raise intent.MatchFailedError(
110  result=match_result, constraints=match_constraints
111  )
112 
113  state = match_result.states[0]
114  service_data = {ATTR_ENTITY_ID: state.entity_id}
115 
116  intent.async_test_feature(state, HumidifierEntityFeature.MODES, "modes")
117  mode = slots["mode"]["value"]
118 
119  if mode not in (state.attributes.get(ATTR_AVAILABLE_MODES) or []):
120  raise intent.IntentHandleError(
121  f"Entity {state.name} does not support {mode} mode"
122  )
123 
124  if state.state == STATE_OFF:
125  await hass.services.async_call(
126  DOMAIN,
127  SERVICE_TURN_ON,
128  service_data,
129  context=intent_obj.context,
130  blocking=True,
131  )
132  speech = f"Turned {state.name} on and set {mode} mode"
133  else:
134  speech = f"The mode for {state.name} is set to {mode}"
135 
136  service_data[ATTR_MODE] = mode
137  await hass.services.async_call(
138  DOMAIN,
139  SERVICE_SET_MODE,
140  service_data,
141  context=intent_obj.context,
142  blocking=True,
143  )
144 
145  response = intent_obj.create_response()
146 
147  response.async_set_speech(speech)
148  return response
intent.IntentResponse async_handle(self, intent.Intent intent_obj)
Definition: intent.py:43
intent.IntentResponse async_handle(self, intent.Intent intent_obj)
Definition: intent.py:98
None async_setup_intents(HomeAssistant hass)
Definition: intent.py:26