From 37031ac01ecf97afa9b675781d81f18afbeda7bb Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 9 Nov 2026 16:27:48 +0100 Subject: [PATCH] Use the temperature_unit attribute in climate triggers and conditions (#191310) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/climate/condition.py | 13 +++- homeassistant/components/climate/trigger.py | 11 +++- tests/components/climate/test_condition.py | 31 ++++++++- tests/components/climate/test_trigger.py | 65 +++++++++++++++++++ 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/climate/condition.py b/homeassistant/components/climate/condition.py index 49f7a2a03226..1d043dbff803 100644 --- a/homeassistant/components/climate/condition.py +++ b/homeassistant/components/climate/condition.py @@ -18,7 +18,13 @@ from homeassistant.helpers.condition import ( ) from homeassistant.util.unit_conversion import TemperatureConverter -from .const import DOMAIN, ClimateEntityStateAttribute, HVACAction, HVACMode +from .const import ( + DOMAIN, + ClimateEntityCapabilityAttribute, + ClimateEntityStateAttribute, + HVACAction, + HVACMode, +) CONF_HVAC_MODE = "hvac_mode" @@ -75,8 +81,9 @@ class ClimateTargetTemperatureCondition(EntityNumericalConditionWithUnitBase): @override def _get_entity_unit(self, entity_state: State) -> str | None: """Get the temperature unit of a climate entity from its state.""" - # Climate entities convert temperatures to the system unit via show_temp - return self._hass.config.units.temperature_unit + return entity_state.attributes.get( + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT + ) class ClimateTargetHumidityCondition(EntityNumericalConditionBase): diff --git a/homeassistant/components/climate/trigger.py b/homeassistant/components/climate/trigger.py index 5559ef0f1c07..c2688bd452b1 100644 --- a/homeassistant/components/climate/trigger.py +++ b/homeassistant/components/climate/trigger.py @@ -23,7 +23,13 @@ from homeassistant.helpers.trigger import ( ) from homeassistant.util.unit_conversion import TemperatureConverter -from .const import DOMAIN, ClimateEntityStateAttribute, HVACAction, HVACMode +from .const import ( + DOMAIN, + ClimateEntityCapabilityAttribute, + ClimateEntityStateAttribute, + HVACAction, + HVACMode, +) CONF_HVAC_MODE = "hvac_mode" @@ -73,8 +79,7 @@ class _ClimateTargetTemperatureTriggerMixin(EntityNumericalStateTriggerWithUnitB @override def _get_entity_unit(self, state: State) -> str | None: """Get the temperature unit of a climate entity from its state.""" - # Climate entities convert temperatures to the system unit via show_temp - return self._hass.config.units.temperature_unit + return state.attributes.get(ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT) class ClimateTargetTemperatureChangedTrigger( diff --git a/tests/components/climate/test_condition.py b/tests/components/climate/test_condition.py index 94d4384846f7..0a6406ba2823 100644 --- a/tests/components/climate/test_condition.py +++ b/tests/components/climate/test_condition.py @@ -24,6 +24,7 @@ from homeassistant.components.climate.condition import CONDITIONS from homeassistant.components.climate.const import ( ATTR_HUMIDITY, ATTR_HVAC_ACTION, + ClimateEntityCapabilityAttribute, HVACAction, HVACMode, ) @@ -41,6 +42,9 @@ async def target_climates(hass: HomeAssistant) -> dict[str, list[str]]: return await target_entities(hass, "climate") +_CLIMATE_UNIT_ATTRIBUTES = { + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS +} _HUMIDITY_THRESHOLD = {"threshold": {"type": "above", "value": {"number": 50}}} _TEMPERATURE_THRESHOLD = { "threshold": { @@ -343,6 +347,7 @@ async def test_climate_attribute_condition_behavior_all( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -388,6 +393,7 @@ async def test_climate_numerical_condition_behavior_any( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -425,12 +431,31 @@ async def test_climate_numerical_condition_unit_conversion(hass: HomeAssistant) hass, condition="climate.is_target_temperature", entity_id="climate.test", - pass_states=[{"state": HVACMode.AUTO, "attributes": {ATTR_TEMPERATURE: 25}}], + pass_states=[ + { + "state": HVACMode.AUTO, + "attributes": {ATTR_TEMPERATURE: 25, **_CLIMATE_UNIT_ATTRIBUTES}, + }, + { + "state": HVACMode.AUTO, + "attributes": { + ATTR_TEMPERATURE: 80, + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + }, + ], fail_states=[ { "state": HVACMode.AUTO, - "attributes": {ATTR_TEMPERATURE: 20}, - } + "attributes": {ATTR_TEMPERATURE: 20, **_CLIMATE_UNIT_ATTRIBUTES}, + }, + { + "state": HVACMode.AUTO, + "attributes": { + ATTR_TEMPERATURE: 68, + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + }, ], numerical_condition_options=[ { diff --git a/tests/components/climate/test_trigger.py b/tests/components/climate/test_trigger.py index dc451afcd6bf..b8321a6531ae 100644 --- a/tests/components/climate/test_trigger.py +++ b/tests/components/climate/test_trigger.py @@ -8,6 +8,7 @@ import pytest from pytest_homeassistant.components.common import ( TargetSupport, TriggerStateDescription, + arm_trigger, assert_trigger_behavior_all, assert_trigger_behavior_each, assert_trigger_behavior_first, @@ -24,6 +25,7 @@ from pytest_homeassistant.components.common import ( from homeassistant.components.climate.const import ( ATTR_HUMIDITY, ATTR_HVAC_ACTION, + ClimateEntityCapabilityAttribute, HVACAction, HVACMode, ) @@ -45,6 +47,9 @@ async def target_climates(hass: HomeAssistant) -> dict[str, list[str]]: return await target_entities(hass, "climate") +_CLIMATE_UNIT_ATTRIBUTES = { + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS +} _CHANGED_THRESHOLD = {"threshold": {"type": "any"}} _HUMIDITY_CROSSED_THRESHOLD = {"threshold": {"type": "above", "value": {"number": 50}}} _TEMPERATURE_CROSSED_THRESHOLD = { @@ -246,6 +251,7 @@ async def test_climate_state_trigger_behavior_each( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), *parametrize_numerical_attribute_crossed_threshold_trigger_states( @@ -259,6 +265,7 @@ async def test_climate_state_trigger_behavior_each( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), *parametrize_trigger_states( @@ -376,6 +383,7 @@ async def test_climate_state_trigger_behavior_first( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), *parametrize_trigger_states( @@ -493,6 +501,7 @@ async def test_climate_state_trigger_behavior_all( HVACMode.AUTO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_CLIMATE_UNIT_ATTRIBUTES, attribute_required=True, ), *parametrize_trigger_states( @@ -533,3 +542,59 @@ async def test_climate_state_attribute_trigger_behavior_all( trigger_options=trigger_options, states=states, ) + + +async def test_climate_target_temperature_trigger_temperature_unit( + hass: HomeAssistant, +) -> None: + """Test the trigger uses the temperature_unit attribute, not the system unit.""" + calls: list[str] = [] + entity_id = "climate.test" + + # The system unit is °C, the entity reports in °F + hass.states.async_set( + entity_id, + HVACMode.AUTO, + { + ATTR_TEMPERATURE: 70, + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + + await arm_trigger( + hass, + "climate.target_temperature_crossed_threshold", + { + "threshold": { + "type": "above", + "value": {"number": 25, "unit_of_measurement": "°C"}, + } + }, + {CONF_ENTITY_ID: [entity_id]}, + calls, + ) + + # 72°F = 22.2°C, below 25°C - should NOT trigger + hass.states.async_set( + entity_id, + HVACMode.AUTO, + { + ATTR_TEMPERATURE: 72, + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + assert len(calls) == 0 + + # 80°F = 26.7°C, above 25°C - should trigger + hass.states.async_set( + entity_id, + HVACMode.AUTO, + { + ATTR_TEMPERATURE: 80, + ClimateEntityCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + assert len(calls) == 1