From 8024a170255b21f08664e0b5e521dd1698f30beb Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 14 Feb 2023 05:45:27 -0500 Subject: [PATCH] Handle device reg fields not being valid data in openai conversion (#88047) Handle device reg fields not being valid data --- .../components/openai_conversation/const.py | 2 +- tests/components/openai_conversation/test_init.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/openai_conversation/const.py b/homeassistant/components/openai_conversation/const.py index b5644915d91..294bbbd6e90 100644 --- a/homeassistant/components/openai_conversation/const.py +++ b/homeassistant/components/openai_conversation/const.py @@ -15,7 +15,7 @@ An overview of the areas and the devices in this smart home: {{ area.name }}: {%- set area_info.printed = true %} {%- endif %} -- {{ device_attr(device, "name") }}{% if device_attr(device, "model") and device_attr(device, "model") not in device_attr(device, "name") %} ({{ device_attr(device, "model") }}){% endif %} +- {{ device_attr(device, "name") }}{% if device_attr(device, "model") and (device_attr(device, "model") | string) not in (device_attr(device, "name") | string) %} ({{ device_attr(device, "model") }}){% endif %} {%- endif %} {%- endfor %} {%- endfor %} diff --git a/tests/components/openai_conversation/test_init.py b/tests/components/openai_conversation/test_init.py index b64f3322895..14d2015ace2 100644 --- a/tests/components/openai_conversation/test_init.py +++ b/tests/components/openai_conversation/test_init.py @@ -67,14 +67,21 @@ async def test_default_prompt(hass, mock_init_component): device_reg.async_update_device( device.id, disabled_by=device_registry.DeviceEntryDisabler.USER ) - device = device_reg.async_get_or_create( + device_reg.async_get_or_create( config_entry_id="1234", connections={("test", "9876-no-name")}, manufacturer="Test Manufacturer NoName", model="Test Model NoName", suggested_area="Test Area 2", ) - + device_reg.async_get_or_create( + config_entry_id="1234", + connections={("test", "9876-integer-values")}, + name=1, + manufacturer=2, + model=3, + suggested_area="Test Area 2", + ) with patch("openai.Completion.create") as mock_create: result = await conversation.async_converse(hass, "hello", None, Context()) @@ -93,6 +100,7 @@ Test Area 2: - Test Device 2 - Test Device 3 (Test Model 3A) - Test Device 4 +- 1 (3) Answer the users questions about the world truthfully.