Home Assistant Unofficial Reference 2024.12.1
intent.py
Go to the documentation of this file.
1 """Intents for the client integration."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import intent
9 
10 from . import DOMAIN, INTENT_GET_TEMPERATURE
11 
12 
13 async def async_setup_intents(hass: HomeAssistant) -> None:
14  """Set up the climate intents."""
15  intent.async_register(hass, GetTemperatureIntent())
16 
17 
18 class GetTemperatureIntent(intent.IntentHandler):
19  """Handle GetTemperature intents."""
20 
21  intent_type = INTENT_GET_TEMPERATURE
22  description = "Gets the current temperature of a climate device or entity"
23  slot_schema = {
24  vol.Optional("area"): intent.non_empty_string,
25  vol.Optional("name"): intent.non_empty_string,
26  }
27  platforms = {DOMAIN}
28 
29  async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
30  """Handle the intent."""
31  hass = intent_obj.hass
32  slots = self.async_validate_slots(intent_obj.slots)
33 
34  name: str | None = None
35  if "name" in slots:
36  name = slots["name"]["value"]
37 
38  area: str | None = None
39  if "area" in slots:
40  area = slots["area"]["value"]
41 
42  match_constraints = intent.MatchTargetsConstraints(
43  name=name, area_name=area, domains=[DOMAIN], assistant=intent_obj.assistant
44  )
45  match_result = intent.async_match_targets(hass, match_constraints)
46  if not match_result.is_match:
47  raise intent.MatchFailedError(
48  result=match_result, constraints=match_constraints
49  )
50 
51  response = intent_obj.create_response()
52  response.response_type = intent.IntentResponseType.QUERY_ANSWER
53  response.async_set_states(matched_states=match_result.states)
54  return response
intent.IntentResponse async_handle(self, intent.Intent intent_obj)
Definition: intent.py:29
None async_setup_intents(HomeAssistant hass)
Definition: intent.py:13