1
1
"""Tests for the OpenAI integration."""
2
2
from unittest .mock import patch
3
3
4
+ from openai import error
5
+
4
6
from homeassistant .components import conversation
5
7
from homeassistant .core import Context
6
- from homeassistant .helpers import device_registry
8
+ from homeassistant .helpers import area_registry , device_registry , intent
7
9
8
10
9
11
async def test_default_prompt (hass , mock_init_component ):
10
12
"""Test that the default prompt works."""
11
13
device_reg = device_registry .async_get (hass )
14
+ area_reg = area_registry .async_get (hass )
15
+
16
+ for i in range (3 ):
17
+ area_reg .async_create (f"{ i } Empty Area" )
12
18
13
19
device_reg .async_get_or_create (
14
20
config_entry_id = "1234" ,
@@ -18,20 +24,30 @@ async def test_default_prompt(hass, mock_init_component):
18
24
model = "Test Model" ,
19
25
suggested_area = "Test Area" ,
20
26
)
27
+ for i in range (3 ):
28
+ device_reg .async_get_or_create (
29
+ config_entry_id = "1234" ,
30
+ connections = {("test" , f"{ i } abcd" )},
31
+ name = "Test Service" ,
32
+ manufacturer = "Test Manufacturer" ,
33
+ model = "Test Model" ,
34
+ suggested_area = "Test Area" ,
35
+ entry_type = device_registry .DeviceEntryType .SERVICE ,
36
+ )
21
37
device_reg .async_get_or_create (
22
38
config_entry_id = "1234" ,
23
39
connections = {("test" , "5678" )},
24
40
name = "Test Device 2" ,
25
41
manufacturer = "Test Manufacturer 2" ,
26
- model = "Test Model 2" ,
42
+ model = "Device 2" ,
27
43
suggested_area = "Test Area 2" ,
28
44
)
29
45
device_reg .async_get_or_create (
30
46
config_entry_id = "1234" ,
31
47
connections = {("test" , "9876" )},
32
48
name = "Test Device 3" ,
33
49
manufacturer = "Test Manufacturer 3" ,
34
- model = "Test Model 3 " ,
50
+ model = "Test Model 3A " ,
35
51
suggested_area = "Test Area 2" ,
36
52
)
37
53
@@ -40,24 +56,33 @@ async def test_default_prompt(hass, mock_init_component):
40
56
41
57
assert (
42
58
mock_create .mock_calls [0 ][2 ]["prompt" ]
43
- == """You are a conversational AI for a smart home named test home.
44
- If a user wants to control a device, reject the request and suggest using the Home Assistant UI.
59
+ == """This smart home is controlled by Home Assistant.
45
60
46
61
An overview of the areas and the devices in this smart home:
47
62
48
63
Test Area:
49
-
50
- - Test Device (Test Model by Test Manufacturer)
64
+ - Test Device (Test Model)
51
65
52
66
Test Area 2:
67
+ - Test Device 2
68
+ - Test Device 3 (Test Model 3A)
53
69
54
- - Test Device 2 (Test Model 2 by Test Manufacturer 2)
55
- - Test Device 3 (Test Model 3 by Test Manufacturer 3)
70
+ Answer the users questions about the world truthfully.
56
71
72
+ If the user wants to control a device, reject the request and suggest using the Home Assistant UI.
57
73
58
74
Now finish this conversation:
59
75
60
76
Smart home: How can I assist?
61
77
User: hello
62
78
Smart home: """
63
79
)
80
+
81
+
82
+ async def test_error_handling (hass , mock_init_component ):
83
+ """Test that the default prompt works."""
84
+ with patch ("openai.Completion.create" , side_effect = error .ServiceUnavailableError ):
85
+ result = await conversation .async_converse (hass , "hello" , None , Context ())
86
+
87
+ assert result .response .response_type == intent .IntentResponseType .ERROR , result
88
+ assert result .response .error_code == "unknown" , result
0 commit comments