From 69e622b3278c4d26a180969d746b9381756f7d15 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 17 May 2022 20:33:51 +0200 Subject: [PATCH] Clean up accessing intent helpers via hass (#72028) --- homeassistant/components/cover/intent.py | 10 ++++++---- .../components/hangouts/hangouts_bot.py | 11 +++++++--- homeassistant/components/humidifier/intent.py | 11 +++++----- homeassistant/components/intent/__init__.py | 15 ++++++++------ homeassistant/components/light/intent.py | 6 +++--- tests/components/humidifier/test_intent.py | 20 ++++++++++++------- tests/components/light/test_intent.py | 11 ++++++---- 7 files changed, 52 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/cover/intent.py b/homeassistant/components/cover/intent.py index 1fa353151ba..9174e8399f3 100644 --- a/homeassistant/components/cover/intent.py +++ b/homeassistant/components/cover/intent.py @@ -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 {}" - ) + ), ) diff --git a/homeassistant/components/hangouts/hangouts_bot.py b/homeassistant/components/hangouts/hangouts_bot.py index 11181c5b0ff..c3c363ef55e 100644 --- a/homeassistant/components/hangouts/hangouts_bot.py +++ b/homeassistant/components/hangouts/hangouts_bot.py @@ -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}} diff --git a/homeassistant/components/humidifier/intent.py b/homeassistant/components/humidifier/intent.py index aeeb18cceec..57f42f58fe0 100644 --- a/homeassistant/components/humidifier/intent.py +++ b/homeassistant/components/humidifier/intent.py @@ -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), ) diff --git a/homeassistant/components/intent/__init__.py b/homeassistant/components/intent/__init__.py index d626daa8c3b..c43643b0244 100644 --- a/homeassistant/components/intent/__init__.py +++ b/homeassistant/components/intent/__init__.py @@ -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 diff --git a/homeassistant/components/light/intent.py b/homeassistant/components/light/intent.py index 261cbbd973d..2ffd73a9792 100644 --- a/homeassistant/components/light/intent.py +++ b/homeassistant/components/light/intent.py @@ -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} diff --git a/tests/components/humidifier/test_intent.py b/tests/components/humidifier/test_intent.py index 66ff62872f3..ed207231bc6 100644 --- a/tests/components/humidifier/test_intent.py +++ b/tests/components/humidifier/test_intent.py @@ -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"}}, diff --git a/tests/components/light/test_intent.py b/tests/components/light/test_intent.py index 19b25efa772..0c837a49c42 100644 --- a/tests/components/light/test_intent.py +++ b/tests/components/light/test_intent.py @@ -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, {