Use the entity temperature unit in climate device trigger capabilities (#191690)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-11-10 12:00:52 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 34ca25c802
commit 82eb1220cc
2 changed files with 137 additions and 40 deletions
@@ -2,7 +2,10 @@
import probatio
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.device_automation import (
DEVICE_TRIGGER_BASE_SCHEMA,
async_get_entity_registry_entry_or_raise,
)
from homeassistant.components.homeassistant.triggers import (
numeric_state as numeric_state_trigger,
state as state_trigger,
@@ -23,11 +26,18 @@ from homeassistant.const import (
PERCENTAGE,
)
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity import get_capability
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, HVAC_MODES, ClimateEntityStateAttribute
from .const import (
DOMAIN,
HVAC_MODES,
ClimateEntityCapabilityAttribute,
ClimateEntityStateAttribute,
)
TRIGGER_TYPES = {
"current_temperature_changed",
@@ -193,20 +203,28 @@ async def async_get_trigger_capabilities(
)
}
unit_of_measurement: str | None = PERCENTAGE
if trigger_type == "current_temperature_changed":
unit_of_measurement: str = hass.config.units.temperature_unit
else:
unit_of_measurement = PERCENTAGE
try:
entry = async_get_entity_registry_entry_or_raise(
hass, config[CONF_ENTITY_ID]
)
unit_of_measurement = get_capability(
hass, entry.entity_id, ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT
)
except HomeAssistantError:
unit_of_measurement = None
description = {"suffix": unit_of_measurement} if unit_of_measurement else None
return {
"extra_fields": probatio.Schema(
{
probatio.Optional(
CONF_ABOVE, description={"suffix": unit_of_measurement}
): probatio.Coerce(float),
probatio.Optional(
CONF_BELOW, description={"suffix": unit_of_measurement}
): probatio.Coerce(float),
probatio.Optional(CONF_ABOVE, description=description): probatio.Coerce(
float
),
probatio.Optional(CONF_BELOW, description=description): probatio.Coerce(
float
),
probatio.Optional(CONF_FOR): cv.positive_time_period_dict,
}
)
+108 -29
View File
@@ -438,42 +438,18 @@ async def test_get_trigger_capabilities_hvac_mode(hass: HomeAssistant) -> None:
]
@pytest.mark.parametrize(
("type", "suffix"),
[
("current_temperature_changed", UnitOfTemperature.CELSIUS),
("current_humidity_changed", "%"),
],
)
async def test_get_trigger_capabilities_temp_humid(
hass: HomeAssistant, type, suffix
) -> None:
"""Test we get the expected capabilities from a climate trigger."""
capabilities = await device_trigger.async_get_trigger_capabilities(
hass,
def _temp_humid_fields(description: dict[str, Any]) -> list[dict[str, Any]]:
"""Return the expected extra fields of a temperature or humidity trigger."""
return [
{
"platform": "device",
"domain": "climate",
"type": type,
"entity_id": "01234567890123456789012345678901",
"above": "23",
},
)
assert capabilities and "extra_fields" in capabilities
assert to_field_list(
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
) == [
{
"description": {"suffix": suffix},
**description,
"name": "above",
"optional": True,
"required": False,
"type": "float",
},
{
"description": {"suffix": suffix},
**description,
"name": "below",
"optional": True,
"required": False,
@@ -488,6 +464,109 @@ async def test_get_trigger_capabilities_temp_humid(
]
async def _async_get_extra_fields(
hass: HomeAssistant, trigger_type: str, entity_id: str
) -> list[dict[str, Any]]:
"""Return the serialized extra fields of a climate trigger."""
capabilities = await device_trigger.async_get_trigger_capabilities(
hass,
{
"platform": "device",
"domain": "climate",
"type": trigger_type,
"entity_id": entity_id,
"above": "23",
},
)
assert capabilities and "extra_fields" in capabilities
return to_field_list(
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
)
@pytest.mark.parametrize(
("trigger_type", "suffix"),
[
pytest.param(
"current_temperature_changed",
UnitOfTemperature.FAHRENHEIT,
id="temperature",
),
pytest.param("current_humidity_changed", "%", id="humidity"),
],
)
async def test_get_trigger_capabilities_temp_humid(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
trigger_type: str,
suffix: str,
) -> None:
"""Test the temperature unit comes from the entity state, not the system."""
entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
"5678",
capabilities={
const.ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: (
UnitOfTemperature.CELSIUS
)
},
)
hass.states.async_set(
entry.entity_id,
HVACMode.HEAT,
{
const.ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: (
UnitOfTemperature.FAHRENHEIT
)
},
)
assert await _async_get_extra_fields(
hass, trigger_type, entry.id
) == _temp_humid_fields({"description": {"suffix": suffix}})
async def test_get_trigger_capabilities_temp_unavailable(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the temperature unit of an entity without state comes from the registry."""
entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
"5678",
capabilities={
const.ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: (
UnitOfTemperature.FAHRENHEIT
)
},
)
assert await _async_get_extra_fields(
hass, "current_temperature_changed", entry.id
) == _temp_humid_fields({"description": {"suffix": UnitOfTemperature.FAHRENHEIT}})
async def test_get_trigger_capabilities_temp_no_capability(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test there is no temperature suffix when the entity has no unit capability."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")
assert await _async_get_extra_fields(
hass, "current_temperature_changed", entry.id
) == _temp_humid_fields({})
async def test_get_trigger_capabilities_temp_unknown_entity(
hass: HomeAssistant,
) -> None:
"""Test there is no temperature suffix for an unknown entity."""
assert await _async_get_extra_fields(
hass, "current_temperature_changed", "01234567890123456789012345678901"
) == _temp_humid_fields({})
def test_current_temperature_changed_with_bad_above_is_invalid() -> None:
"""Test the trigger type selects the schema that reports the error."""
with pytest.raises(Invalid, match="expected float at 'above'"):