Fix exposure checks on some intents (#118988)
* Check exposure in climate intent * Check exposure in todo list * Check exposure for weather * Check exposure in humidity intents * Add extra checks to weather tests * Add more checks to todo intent test * Move climate intents to async_match_targets * Update test_intent.py * Update test_intent.py * Remove patch
This commit is contained in:
parent
8c025ea1f7
commit
87114bf19b
9 changed files with 453 additions and 205 deletions
|
@ -6,10 +6,8 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers import intent
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
|
||||
from . import DOMAIN, WeatherEntity
|
||||
from . import DOMAIN
|
||||
|
||||
INTENT_GET_WEATHER = "HassGetWeather"
|
||||
|
||||
|
@ -24,7 +22,7 @@ class GetWeatherIntent(intent.IntentHandler):
|
|||
|
||||
intent_type = INTENT_GET_WEATHER
|
||||
description = "Gets the current weather"
|
||||
slot_schema = {vol.Optional("name"): cv.string}
|
||||
slot_schema = {vol.Optional("name"): intent.non_empty_string}
|
||||
platforms = {DOMAIN}
|
||||
|
||||
async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
|
||||
|
@ -32,43 +30,21 @@ class GetWeatherIntent(intent.IntentHandler):
|
|||
hass = intent_obj.hass
|
||||
slots = self.async_validate_slots(intent_obj.slots)
|
||||
|
||||
weather: WeatherEntity | None = None
|
||||
weather_state: State | None = None
|
||||
component: EntityComponent[WeatherEntity] = hass.data[DOMAIN]
|
||||
entities = list(component.entities)
|
||||
|
||||
name: str | None = None
|
||||
if "name" in slots:
|
||||
# Named weather entity
|
||||
weather_name = slots["name"]["value"]
|
||||
name = slots["name"]["value"]
|
||||
|
||||
# Find matching weather entity
|
||||
matching_states = intent.async_match_states(
|
||||
hass, name=weather_name, domains=[DOMAIN]
|
||||
match_constraints = intent.MatchTargetsConstraints(
|
||||
name=name, domains=[DOMAIN], assistant=intent_obj.assistant
|
||||
)
|
||||
match_result = intent.async_match_targets(hass, match_constraints)
|
||||
if not match_result.is_match:
|
||||
raise intent.MatchFailedError(
|
||||
result=match_result, constraints=match_constraints
|
||||
)
|
||||
for maybe_weather_state in matching_states:
|
||||
weather = component.get_entity(maybe_weather_state.entity_id)
|
||||
if weather is not None:
|
||||
weather_state = maybe_weather_state
|
||||
break
|
||||
|
||||
if weather is None:
|
||||
raise intent.IntentHandleError(
|
||||
f"No weather entity named {weather_name}"
|
||||
)
|
||||
elif entities:
|
||||
# First weather entity
|
||||
weather = entities[0]
|
||||
weather_name = weather.name
|
||||
weather_state = hass.states.get(weather.entity_id)
|
||||
|
||||
if weather is None:
|
||||
raise intent.IntentHandleError("No weather entity")
|
||||
|
||||
if weather_state is None:
|
||||
raise intent.IntentHandleError(f"No state for weather: {weather.name}")
|
||||
|
||||
assert weather is not None
|
||||
assert weather_state is not None
|
||||
weather_state = match_result.states[0]
|
||||
|
||||
# Create response
|
||||
response = intent_obj.create_response()
|
||||
|
@ -77,8 +53,8 @@ class GetWeatherIntent(intent.IntentHandler):
|
|||
success_results=[
|
||||
intent.IntentResponseTarget(
|
||||
type=intent.IntentResponseTargetType.ENTITY,
|
||||
name=weather_name,
|
||||
id=weather.entity_id,
|
||||
name=weather_state.name,
|
||||
id=weather_state.entity_id,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue