From 6fcc21acb6175c4c05eec6ba6ffab5438207744e Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 13 Mar 2025 14:24:55 +0100 Subject: [PATCH] Add start and docker intents for lawn mower --- homeassistant/components/lawn_mower/intent.py | 35 ++++++ tests/components/lawn_mower/test_intent.py | 103 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 homeassistant/components/lawn_mower/intent.py create mode 100644 tests/components/lawn_mower/test_intent.py diff --git a/homeassistant/components/lawn_mower/intent.py b/homeassistant/components/lawn_mower/intent.py new file mode 100644 index 00000000000..ca06ed2e238 --- /dev/null +++ b/homeassistant/components/lawn_mower/intent.py @@ -0,0 +1,35 @@ +"""Intents for the lawn mower integration.""" + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import intent + +from . import DOMAIN, SERVICE_DOCK, SERVICE_START_MOWING + +INTENT_LANW_MOWER_START_MOWING = "HassLawnMowerStartMowing" +INTENT_LANW_MOWER_DOCK = "HassLawnMowerDock" + + +async def async_setup_intents(hass: HomeAssistant) -> None: + """Set up the lawn mower intents.""" + intent.async_register( + hass, + intent.ServiceIntentHandler( + INTENT_LANW_MOWER_START_MOWING, + DOMAIN, + SERVICE_START_MOWING, + description="Starts a lawn mower", + required_domains={DOMAIN}, + platforms={DOMAIN}, + ), + ) + intent.async_register( + hass, + intent.ServiceIntentHandler( + INTENT_LANW_MOWER_DOCK, + DOMAIN, + SERVICE_DOCK, + description="Sends a lawn mower to dock", + required_domains={DOMAIN}, + platforms={DOMAIN}, + ), + ) diff --git a/tests/components/lawn_mower/test_intent.py b/tests/components/lawn_mower/test_intent.py new file mode 100644 index 00000000000..f673833d756 --- /dev/null +++ b/tests/components/lawn_mower/test_intent.py @@ -0,0 +1,103 @@ +"""The tests for the lawn mower platform.""" + +from homeassistant.components.lawn_mower import ( + DOMAIN, + SERVICE_DOCK, + SERVICE_START_MOWING, + LawnMowerActivity, + intent as lawn_mower_intent, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import intent + +from tests.common import async_mock_service + + +async def test_start_lawn_mower_intent(hass: HomeAssistant) -> None: + """Test HassLawnMowerStartMowing intent for lawn mowers.""" + await lawn_mower_intent.async_setup_intents(hass) + + entity_id = f"{DOMAIN}.test_lawn_mower" + hass.states.async_set(entity_id, LawnMowerActivity.DOCKED) + calls = async_mock_service(hass, DOMAIN, SERVICE_START_MOWING) + + response = await intent.async_handle( + hass, + "test", + lawn_mower_intent.INTENT_LANW_MOWER_START_MOWING, + {"name": {"value": "test lawn mower"}}, + ) + await hass.async_block_till_done() + + assert response.response_type == intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + call = calls[0] + assert call.domain == DOMAIN + assert call.service == SERVICE_START_MOWING + assert call.data == {"entity_id": entity_id} + + +async def test_start_lawn_mower_without_name(hass: HomeAssistant) -> None: + """Test starting a lawn mower without specifying the name.""" + await lawn_mower_intent.async_setup_intents(hass) + + entity_id = f"{DOMAIN}.test_lawn_mower" + hass.states.async_set(entity_id, LawnMowerActivity.DOCKED) + calls = async_mock_service(hass, DOMAIN, SERVICE_START_MOWING) + + response = await intent.async_handle( + hass, "test", lawn_mower_intent.INTENT_LANW_MOWER_START_MOWING, {} + ) + await hass.async_block_till_done() + + assert response.response_type == intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + call = calls[0] + assert call.domain == DOMAIN + assert call.service == SERVICE_START_MOWING + assert call.data == {"entity_id": entity_id} + + +async def test_stop_lawn_mower_intent(hass: HomeAssistant) -> None: + """Test HassLawnMowerDock intent for lawn mowers.""" + await lawn_mower_intent.async_setup_intents(hass) + + entity_id = f"{DOMAIN}.test_lawn_mower" + hass.states.async_set(entity_id, LawnMowerActivity.MOWING) + calls = async_mock_service(hass, DOMAIN, SERVICE_DOCK) + + response = await intent.async_handle( + hass, + "test", + lawn_mower_intent.INTENT_LANW_MOWER_DOCK, + {"name": {"value": "test lawn mower"}}, + ) + await hass.async_block_till_done() + + assert response.response_type == intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + call = calls[0] + assert call.domain == DOMAIN + assert call.service == SERVICE_DOCK + assert call.data == {"entity_id": entity_id} + + +async def test_stop_lawn_mower_without_name(hass: HomeAssistant) -> None: + """Test stopping a lawn mower without specifying the name.""" + await lawn_mower_intent.async_setup_intents(hass) + + entity_id = f"{DOMAIN}.test_lawn_mower" + hass.states.async_set(entity_id, LawnMowerActivity.MOWING) + calls = async_mock_service(hass, DOMAIN, SERVICE_DOCK) + + response = await intent.async_handle( + hass, "test", lawn_mower_intent.INTENT_LANW_MOWER_DOCK, {} + ) + await hass.async_block_till_done() + + assert response.response_type == intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + call = calls[0] + assert call.domain == DOMAIN + assert call.service == SERVICE_DOCK + assert call.data == {"entity_id": entity_id}