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