mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Clean up accessing intent helpers via hass (#72028)
This commit is contained in:
parent
0d94324d58
commit
69e622b327
@ -11,13 +11,15 @@ INTENT_CLOSE_COVER = "HassCloseCover"
|
|||||||
|
|
||||||
async def async_setup_intents(hass: HomeAssistant) -> None:
|
async def async_setup_intents(hass: HomeAssistant) -> None:
|
||||||
"""Set up the cover intents."""
|
"""Set up the cover intents."""
|
||||||
hass.helpers.intent.async_register(
|
intent.async_register(
|
||||||
|
hass,
|
||||||
intent.ServiceIntentHandler(
|
intent.ServiceIntentHandler(
|
||||||
INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}"
|
INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}"
|
||||||
)
|
),
|
||||||
)
|
)
|
||||||
hass.helpers.intent.async_register(
|
intent.async_register(
|
||||||
|
hass,
|
||||||
intent.ServiceIntentHandler(
|
intent.ServiceIntentHandler(
|
||||||
INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}"
|
INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}"
|
||||||
)
|
),
|
||||||
)
|
)
|
||||||
|
@ -187,11 +187,16 @@ class HangoutsBot:
|
|||||||
if not (match := matcher.match(text)):
|
if not (match := matcher.match(text)):
|
||||||
continue
|
continue
|
||||||
if intent_type == INTENT_HELP:
|
if intent_type == INTENT_HELP:
|
||||||
return await self.hass.helpers.intent.async_handle(
|
return await intent.async_handle(
|
||||||
DOMAIN, intent_type, {"conv_id": {"value": conv_id}}, text
|
self.hass,
|
||||||
|
DOMAIN,
|
||||||
|
intent_type,
|
||||||
|
{"conv_id": {"value": conv_id}},
|
||||||
|
text,
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self.hass.helpers.intent.async_handle(
|
return await intent.async_handle(
|
||||||
|
self.hass,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
intent_type,
|
intent_type,
|
||||||
{"conv_id": {"value": conv_id}}
|
{"conv_id": {"value": conv_id}}
|
||||||
|
@ -22,8 +22,8 @@ INTENT_MODE = "HassHumidifierMode"
|
|||||||
|
|
||||||
async def async_setup_intents(hass: HomeAssistant) -> None:
|
async def async_setup_intents(hass: HomeAssistant) -> None:
|
||||||
"""Set up the humidifier intents."""
|
"""Set up the humidifier intents."""
|
||||||
hass.helpers.intent.async_register(HumidityHandler())
|
intent.async_register(hass, HumidityHandler())
|
||||||
hass.helpers.intent.async_register(SetModeHandler())
|
intent.async_register(hass, SetModeHandler())
|
||||||
|
|
||||||
|
|
||||||
class HumidityHandler(intent.IntentHandler):
|
class HumidityHandler(intent.IntentHandler):
|
||||||
@ -39,8 +39,8 @@ class HumidityHandler(intent.IntentHandler):
|
|||||||
"""Handle the hass intent."""
|
"""Handle the hass intent."""
|
||||||
hass = intent_obj.hass
|
hass = intent_obj.hass
|
||||||
slots = self.async_validate_slots(intent_obj.slots)
|
slots = self.async_validate_slots(intent_obj.slots)
|
||||||
state = hass.helpers.intent.async_match_state(
|
state = intent.async_match_state(
|
||||||
slots["name"]["value"], hass.states.async_all(DOMAIN)
|
hass, slots["name"]["value"], hass.states.async_all(DOMAIN)
|
||||||
)
|
)
|
||||||
|
|
||||||
service_data = {ATTR_ENTITY_ID: state.entity_id}
|
service_data = {ATTR_ENTITY_ID: state.entity_id}
|
||||||
@ -83,7 +83,8 @@ class SetModeHandler(intent.IntentHandler):
|
|||||||
"""Handle the hass intent."""
|
"""Handle the hass intent."""
|
||||||
hass = intent_obj.hass
|
hass = intent_obj.hass
|
||||||
slots = self.async_validate_slots(intent_obj.slots)
|
slots = self.async_validate_slots(intent_obj.slots)
|
||||||
state = hass.helpers.intent.async_match_state(
|
state = intent.async_match_state(
|
||||||
|
hass,
|
||||||
slots["name"]["value"],
|
slots["name"]["value"],
|
||||||
hass.states.async_all(DOMAIN),
|
hass.states.async_all(DOMAIN),
|
||||||
)
|
)
|
||||||
|
@ -19,20 +19,23 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
hass, DOMAIN, _async_process_intent
|
hass, DOMAIN, _async_process_intent
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.helpers.intent.async_register(
|
intent.async_register(
|
||||||
|
hass,
|
||||||
intent.ServiceIntentHandler(
|
intent.ServiceIntentHandler(
|
||||||
intent.INTENT_TURN_ON, HA_DOMAIN, SERVICE_TURN_ON, "Turned {} on"
|
intent.INTENT_TURN_ON, HA_DOMAIN, SERVICE_TURN_ON, "Turned {} on"
|
||||||
)
|
),
|
||||||
)
|
)
|
||||||
hass.helpers.intent.async_register(
|
intent.async_register(
|
||||||
|
hass,
|
||||||
intent.ServiceIntentHandler(
|
intent.ServiceIntentHandler(
|
||||||
intent.INTENT_TURN_OFF, HA_DOMAIN, SERVICE_TURN_OFF, "Turned {} off"
|
intent.INTENT_TURN_OFF, HA_DOMAIN, SERVICE_TURN_OFF, "Turned {} off"
|
||||||
)
|
),
|
||||||
)
|
)
|
||||||
hass.helpers.intent.async_register(
|
intent.async_register(
|
||||||
|
hass,
|
||||||
intent.ServiceIntentHandler(
|
intent.ServiceIntentHandler(
|
||||||
intent.INTENT_TOGGLE, HA_DOMAIN, SERVICE_TOGGLE, "Toggled {}"
|
intent.INTENT_TOGGLE, HA_DOMAIN, SERVICE_TOGGLE, "Toggled {}"
|
||||||
)
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -21,7 +21,7 @@ INTENT_SET = "HassLightSet"
|
|||||||
|
|
||||||
async def async_setup_intents(hass: HomeAssistant) -> None:
|
async def async_setup_intents(hass: HomeAssistant) -> None:
|
||||||
"""Set up the light intents."""
|
"""Set up the light intents."""
|
||||||
hass.helpers.intent.async_register(SetIntentHandler())
|
intent.async_register(hass, SetIntentHandler())
|
||||||
|
|
||||||
|
|
||||||
def _test_supports_color(state: State) -> None:
|
def _test_supports_color(state: State) -> None:
|
||||||
@ -56,8 +56,8 @@ class SetIntentHandler(intent.IntentHandler):
|
|||||||
"""Handle the hass intent."""
|
"""Handle the hass intent."""
|
||||||
hass = intent_obj.hass
|
hass = intent_obj.hass
|
||||||
slots = self.async_validate_slots(intent_obj.slots)
|
slots = self.async_validate_slots(intent_obj.slots)
|
||||||
state = hass.helpers.intent.async_match_state(
|
state = intent.async_match_state(
|
||||||
slots["name"]["value"], hass.states.async_all(DOMAIN)
|
hass, slots["name"]["value"], hass.states.async_all(DOMAIN)
|
||||||
)
|
)
|
||||||
|
|
||||||
service_data = {ATTR_ENTITY_ID: state.entity_id}
|
service_data = {ATTR_ENTITY_ID: state.entity_id}
|
||||||
|
@ -15,7 +15,7 @@ from homeassistant.const import (
|
|||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.intent import IntentHandleError
|
from homeassistant.helpers.intent import IntentHandleError, async_handle
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_mock_service
|
||||||
|
|
||||||
@ -29,7 +29,8 @@ async def test_intent_set_humidity(hass):
|
|||||||
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_HUMIDITY,
|
intent.INTENT_HUMIDITY,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "humidity": {"value": "50"}},
|
{"name": {"value": "Bedroom humidifier"}, "humidity": {"value": "50"}},
|
||||||
@ -56,7 +57,8 @@ async def test_intent_set_humidity_and_turn_on(hass):
|
|||||||
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_HUMIDITY,
|
intent.INTENT_HUMIDITY,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "humidity": {"value": "50"}},
|
{"name": {"value": "Bedroom humidifier"}, "humidity": {"value": "50"}},
|
||||||
@ -97,7 +99,8 @@ async def test_intent_set_mode(hass):
|
|||||||
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_MODE,
|
intent.INTENT_MODE,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
||||||
@ -134,7 +137,8 @@ async def test_intent_set_mode_and_turn_on(hass):
|
|||||||
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
turn_on_calls = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_MODE,
|
intent.INTENT_MODE,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
||||||
@ -168,7 +172,8 @@ async def test_intent_set_mode_tests_feature(hass):
|
|||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await hass.helpers.intent.async_handle(
|
await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_MODE,
|
intent.INTENT_MODE,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "away"}},
|
||||||
@ -196,7 +201,8 @@ async def test_intent_set_unknown_mode(hass):
|
|||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await hass.helpers.intent.async_handle(
|
await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_MODE,
|
intent.INTENT_MODE,
|
||||||
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "eco"}},
|
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "eco"}},
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
from homeassistant.components import light
|
from homeassistant.components import light
|
||||||
from homeassistant.components.light import ATTR_SUPPORTED_COLOR_MODES, ColorMode, intent
|
from homeassistant.components.light import ATTR_SUPPORTED_COLOR_MODES, ColorMode, intent
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
||||||
from homeassistant.helpers.intent import IntentHandleError
|
from homeassistant.helpers.intent import IntentHandleError, async_handle
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_mock_service
|
||||||
|
|
||||||
@ -16,7 +16,8 @@ async def test_intent_set_color(hass):
|
|||||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_SET,
|
intent.INTENT_SET,
|
||||||
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
|
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
|
||||||
@ -40,7 +41,8 @@ async def test_intent_set_color_tests_feature(hass):
|
|||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await hass.helpers.intent.async_handle(
|
await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_SET,
|
intent.INTENT_SET,
|
||||||
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
|
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
|
||||||
@ -61,7 +63,8 @@ async def test_intent_set_color_and_brightness(hass):
|
|||||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||||
await intent.async_setup_intents(hass)
|
await intent.async_setup_intents(hass)
|
||||||
|
|
||||||
result = await hass.helpers.intent.async_handle(
|
result = await async_handle(
|
||||||
|
hass,
|
||||||
"test",
|
"test",
|
||||||
intent.INTENT_SET,
|
intent.INTENT_SET,
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user