mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
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:
@@ -1,4 +1,6 @@
|
||||
"""Tests for the intent helpers."""
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -184,3 +186,63 @@ async def test_cant_turn_on_lock(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result.response.response_type == intent.IntentResponseType.ERROR
|
||||
assert result.response.error_code == intent.IntentResponseErrorCode.NO_INTENT_MATCH
|
||||
|
||||
|
||||
def test_async_register(hass: HomeAssistant) -> None:
|
||||
"""Test registering an intent and verifying it is stored correctly."""
|
||||
handler = MagicMock()
|
||||
handler.intent_type = "test_intent"
|
||||
|
||||
intent.async_register(hass, handler)
|
||||
|
||||
assert hass.data[intent.DATA_KEY]["test_intent"] == handler
|
||||
|
||||
|
||||
def test_async_register_overwrite(hass: HomeAssistant) -> None:
|
||||
"""Test registering multiple intents with the same type, ensuring the last one overwrites the previous one and a warning is emitted."""
|
||||
handler1 = MagicMock()
|
||||
handler1.intent_type = "test_intent"
|
||||
|
||||
handler2 = MagicMock()
|
||||
handler2.intent_type = "test_intent"
|
||||
|
||||
with patch.object(intent._LOGGER, "warning") as mock_warning:
|
||||
intent.async_register(hass, handler1)
|
||||
intent.async_register(hass, handler2)
|
||||
|
||||
mock_warning.assert_called_once_with(
|
||||
"Intent %s is being overwritten by %s", "test_intent", handler2
|
||||
)
|
||||
|
||||
assert hass.data[intent.DATA_KEY]["test_intent"] == handler2
|
||||
|
||||
|
||||
def test_async_remove(hass: HomeAssistant) -> None:
|
||||
"""Test removing an intent and verifying it is no longer present in the Home Assistant data."""
|
||||
handler = MagicMock()
|
||||
handler.intent_type = "test_intent"
|
||||
|
||||
intent.async_register(hass, handler)
|
||||
intent.async_remove(hass, "test_intent")
|
||||
|
||||
assert "test_intent" not in hass.data[intent.DATA_KEY]
|
||||
|
||||
|
||||
def test_async_remove_no_existing_entry(hass: HomeAssistant) -> None:
|
||||
"""Test the removal of a non-existing intent from Home Assistant's data."""
|
||||
handler = MagicMock()
|
||||
handler.intent_type = "test_intent"
|
||||
intent.async_register(hass, handler)
|
||||
|
||||
intent.async_remove(hass, "test_intent2")
|
||||
|
||||
assert "test_intent2" not in hass.data[intent.DATA_KEY]
|
||||
|
||||
|
||||
def test_async_remove_no_existing(hass: HomeAssistant) -> None:
|
||||
"""Test the removal of an intent where no config exists."""
|
||||
|
||||
intent.async_remove(hass, "test_intent2")
|
||||
# simply shouldn't cause an exception
|
||||
|
||||
assert intent.DATA_KEY not in hass.data
|
||||
|
||||
Reference in New Issue
Block a user