From 19f97a3e53ed99e85fb3778521aa0e4358229fbc Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 23 Jun 2024 17:09:57 -0400 Subject: [PATCH] LLM to handle decimal attributes (#120257) --- homeassistant/helpers/llm.py | 5 ++++- tests/helpers/test_llm.py | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/llm.py b/homeassistant/helpers/llm.py index 53ec092fda2..6673786e2e1 100644 --- a/homeassistant/helpers/llm.py +++ b/homeassistant/helpers/llm.py @@ -4,6 +4,7 @@ from __future__ import annotations from abc import ABC, abstractmethod from dataclasses import dataclass +from decimal import Decimal from enum import Enum from functools import cache, partial from typing import Any @@ -461,7 +462,9 @@ def _get_exposed_entities( info["areas"] = ", ".join(area_names) if attributes := { - attr_name: str(attr_value) if isinstance(attr_value, Enum) else attr_value + attr_name: str(attr_value) + if isinstance(attr_value, (Enum, Decimal)) + else attr_value for attr_name, attr_value in state.attributes.items() if attr_name in interesting_attributes }: diff --git a/tests/helpers/test_llm.py b/tests/helpers/test_llm.py index e62d9ffdbee..5389490b401 100644 --- a/tests/helpers/test_llm.py +++ b/tests/helpers/test_llm.py @@ -1,5 +1,6 @@ """Tests for the llm helpers.""" +from decimal import Decimal from unittest.mock import patch import pytest @@ -402,7 +403,11 @@ async def test_assist_api_prompt( suggested_object_id="living_room", device_id=device.id, ) - hass.states.async_set(entry1.entity_id, "on", {"friendly_name": "Kitchen"}) + hass.states.async_set( + entry1.entity_id, + "on", + {"friendly_name": "Kitchen", "temperature": Decimal("0.9")}, + ) hass.states.async_set(entry2.entity_id, "on", {"friendly_name": "Living Room"}) def create_entity(device: dr.DeviceEntry, write_state=True) -> None: @@ -510,6 +515,9 @@ async def test_assist_api_prompt( entry1.entity_id: { "names": "Kitchen", "state": "on", + "attributes": { + "temperature": "0.9", + }, }, entry2.entity_id: { "areas": "Test Area, Alternative name",