Clean up accessing intent helpers via hass (#72028)

This commit is contained in:
Franck Nijhof 2022-05-17 20:33:51 +02:00 committed by GitHub
parent 0d94324d58
commit 69e622b327
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 32 deletions

View file

@ -11,13 +11,15 @@ INTENT_CLOSE_COVER = "HassCloseCover"
async def async_setup_intents(hass: HomeAssistant) -> None:
"""Set up the cover intents."""
hass.helpers.intent.async_register(
intent.async_register(
hass,
intent.ServiceIntentHandler(
INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER, "Opened {}"
)
),
)
hass.helpers.intent.async_register(
intent.async_register(
hass,
intent.ServiceIntentHandler(
INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER, "Closed {}"
)
),
)

View file

@ -187,11 +187,16 @@ class HangoutsBot:
if not (match := matcher.match(text)):
continue
if intent_type == INTENT_HELP:
return await self.hass.helpers.intent.async_handle(
DOMAIN, intent_type, {"conv_id": {"value": conv_id}}, text
return await intent.async_handle(
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,
intent_type,
{"conv_id": {"value": conv_id}}

View file

@ -22,8 +22,8 @@ INTENT_MODE = "HassHumidifierMode"
async def async_setup_intents(hass: HomeAssistant) -> None:
"""Set up the humidifier intents."""
hass.helpers.intent.async_register(HumidityHandler())
hass.helpers.intent.async_register(SetModeHandler())
intent.async_register(hass, HumidityHandler())
intent.async_register(hass, SetModeHandler())
class HumidityHandler(intent.IntentHandler):
@ -39,8 +39,8 @@ class HumidityHandler(intent.IntentHandler):
"""Handle the hass intent."""
hass = intent_obj.hass
slots = self.async_validate_slots(intent_obj.slots)
state = hass.helpers.intent.async_match_state(
slots["name"]["value"], hass.states.async_all(DOMAIN)
state = intent.async_match_state(
hass, slots["name"]["value"], hass.states.async_all(DOMAIN)
)
service_data = {ATTR_ENTITY_ID: state.entity_id}
@ -83,7 +83,8 @@ class SetModeHandler(intent.IntentHandler):
"""Handle the hass intent."""
hass = intent_obj.hass
slots = self.async_validate_slots(intent_obj.slots)
state = hass.helpers.intent.async_match_state(
state = intent.async_match_state(
hass,
slots["name"]["value"],
hass.states.async_all(DOMAIN),
)

View file

@ -19,20 +19,23 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass, DOMAIN, _async_process_intent
)
hass.helpers.intent.async_register(
intent.async_register(
hass,
intent.ServiceIntentHandler(
intent.INTENT_TURN_ON, HA_DOMAIN, SERVICE_TURN_ON, "Turned {} on"
)
),
)
hass.helpers.intent.async_register(
intent.async_register(
hass,
intent.ServiceIntentHandler(
intent.INTENT_TURN_OFF, HA_DOMAIN, SERVICE_TURN_OFF, "Turned {} off"
)
),
)
hass.helpers.intent.async_register(
intent.async_register(
hass,
intent.ServiceIntentHandler(
intent.INTENT_TOGGLE, HA_DOMAIN, SERVICE_TOGGLE, "Toggled {}"
)
),
)
return True

View file

@ -21,7 +21,7 @@ INTENT_SET = "HassLightSet"
async def async_setup_intents(hass: HomeAssistant) -> None:
"""Set up the light intents."""
hass.helpers.intent.async_register(SetIntentHandler())
intent.async_register(hass, SetIntentHandler())
def _test_supports_color(state: State) -> None:
@ -56,8 +56,8 @@ class SetIntentHandler(intent.IntentHandler):
"""Handle the hass intent."""
hass = intent_obj.hass
slots = self.async_validate_slots(intent_obj.slots)
state = hass.helpers.intent.async_match_state(
slots["name"]["value"], hass.states.async_all(DOMAIN)
state = intent.async_match_state(
hass, slots["name"]["value"], hass.states.async_all(DOMAIN)
)
service_data = {ATTR_ENTITY_ID: state.entity_id}

View file

@ -15,7 +15,7 @@ from homeassistant.const import (
STATE_OFF,
STATE_ON,
)
from homeassistant.helpers.intent import IntentHandleError
from homeassistant.helpers.intent import IntentHandleError, async_handle
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)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_HUMIDITY,
{"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)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_HUMIDITY,
{"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)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_MODE,
{"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)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_MODE,
{"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)
try:
await hass.helpers.intent.async_handle(
await async_handle(
hass,
"test",
intent.INTENT_MODE,
{"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)
try:
await hass.helpers.intent.async_handle(
await async_handle(
hass,
"test",
intent.INTENT_MODE,
{"name": {"value": "Bedroom humidifier"}, "mode": {"value": "eco"}},

View file

@ -2,7 +2,7 @@
from homeassistant.components import light
from homeassistant.components.light import ATTR_SUPPORTED_COLOR_MODES, ColorMode, intent
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
@ -16,7 +16,8 @@ async def test_intent_set_color(hass):
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_SET,
{"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)
try:
await hass.helpers.intent.async_handle(
await async_handle(
hass,
"test",
intent.INTENT_SET,
{"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)
await intent.async_setup_intents(hass)
result = await hass.helpers.intent.async_handle(
result = await async_handle(
hass,
"test",
intent.INTENT_SET,
{