Use the temperature_unit attribute in climate triggers and conditions (#191310)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-11-09 15:27:48 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent d07704ce12
commit 37031ac01e
4 changed files with 111 additions and 9 deletions
+10 -3
View File
@@ -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):
+8 -3
View File
@@ -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(
+28 -3
View File
@@ -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=[
{
+65
View File
@@ -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