Add reload support to intent_script (#93404)

* support live reload of intent_script

* add services.yaml

* update tesls for full code coverage

* Update based on feedback

* fix intent_script reload when no intent_script config

* Update homeassistant/helpers/intent.py

* update tests to handle no_existing better

---------

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Michael Benz 2023-06-04 04:02:23 +10:00 committed by GitHub
parent 65b62d877d
commit 305fa128fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 178 additions and 7 deletions

View file

@ -1,9 +1,14 @@
"""Test intent_script component."""
from unittest.mock import patch
from homeassistant import config as hass_config
from homeassistant.bootstrap import async_setup_component
from homeassistant.components.intent_script import DOMAIN
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import intent
from tests.common import async_mock_service
from tests.common import async_mock_service, get_fixture_path
async def test_intent_script(hass: HomeAssistant) -> None:
@ -134,3 +139,49 @@ async def test_intent_script_falsy_reprompt(hass: HomeAssistant) -> None:
assert response.card["simple"]["title"] == "Hello Paulus"
assert response.card["simple"]["content"] == "Content for Paulus"
async def test_reload(hass: HomeAssistant) -> None:
"""Verify we can reload intent config."""
config = {"intent_script": {"NewIntent1": {"speech": {"text": "HelloWorld123"}}}}
await async_setup_component(hass, "intent_script", config)
await hass.async_block_till_done()
intents = hass.data.get(intent.DATA_KEY)
assert len(intents) == 1
assert intents.get("NewIntent1")
yaml_path = get_fixture_path("configuration.yaml", "intent_script")
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
DOMAIN,
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert len(intents) == 1
assert intents.get("NewIntent1") is None
assert intents.get("NewIntent2")
yaml_path = get_fixture_path("configuration_no_entry.yaml", "intent_script")
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
DOMAIN,
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
# absence of intent_script from the configuration.yaml should delete all intents.
assert len(intents) == 0
assert intents.get("NewIntent1") is None
assert intents.get("NewIntent2") is None