diff --git a/homeassistant/components/water_heater/condition.py b/homeassistant/components/water_heater/condition.py index d2087e97e9c5..76c458ce94ca 100644 --- a/homeassistant/components/water_heater/condition.py +++ b/homeassistant/components/water_heater/condition.py @@ -17,7 +17,7 @@ from homeassistant.helpers.condition import ( ) from homeassistant.util.unit_conversion import TemperatureConverter -from .const import DOMAIN, WaterHeaterStateAttribute +from .const import DOMAIN, WaterHeaterCapabilityAttribute, WaterHeaterStateAttribute ATTR_OPERATION_MODE = "operation_mode" @@ -84,8 +84,9 @@ class WaterHeaterTargetTemperatureCondition(EntityNumericalConditionWithUnitBase @override def _get_entity_unit(self, entity_state: State) -> str | None: """Get the temperature unit of a water heater entity from its state.""" - # Water heater entities convert temperatures to the system unit via show_temp - return self._hass.config.units.temperature_unit + return entity_state.attributes.get( + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT + ) CONDITIONS: dict[str, type[Condition]] = { diff --git a/homeassistant/components/water_heater/trigger.py b/homeassistant/components/water_heater/trigger.py index 8237d44ea90c..6f4e3ec2709a 100644 --- a/homeassistant/components/water_heater/trigger.py +++ b/homeassistant/components/water_heater/trigger.py @@ -20,7 +20,7 @@ from homeassistant.helpers.trigger import ( ) from homeassistant.util.unit_conversion import TemperatureConverter -from .const import DOMAIN, WaterHeaterStateAttribute +from .const import DOMAIN, WaterHeaterCapabilityAttribute, WaterHeaterStateAttribute CONF_OPERATION_MODE = "operation_mode" @@ -72,8 +72,7 @@ class _WaterHeaterTargetTemperatureTriggerMixin( @override def _get_entity_unit(self, state: State) -> str | None: """Get the temperature unit of a water heater entity from its state.""" - # Water heater entities convert temperatures to the system unit via show_temp - return self._hass.config.units.temperature_unit + return state.attributes.get(WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT) class WaterHeaterTargetTemperatureChangedTrigger( diff --git a/tests/components/water_heater/test_condition.py b/tests/components/water_heater/test_condition.py index 2558c5a9cf81..05ab0a70ed96 100644 --- a/tests/components/water_heater/test_condition.py +++ b/tests/components/water_heater/test_condition.py @@ -26,6 +26,7 @@ from homeassistant.components.water_heater import ( STATE_HEAT_PUMP, STATE_HIGH_DEMAND, STATE_PERFORMANCE, + WaterHeaterCapabilityAttribute, ) from homeassistant.components.water_heater.condition import CONDITIONS from homeassistant.const import ( @@ -57,6 +58,9 @@ async def target_water_heaters(hass: HomeAssistant) -> dict[str, list[str]]: return await target_entities(hass, "water_heater") +_WATER_HEATER_UNIT_ATTRIBUTES = { + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS +} _TEMPERATURE_THRESHOLD = { "threshold": { "type": "above", @@ -237,6 +241,7 @@ async def test_water_heater_state_condition_behavior_all( "eco", ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -276,6 +281,7 @@ async def test_water_heater_numerical_condition_behavior_any( "eco", ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -315,12 +321,31 @@ async def test_water_heater_numerical_condition_unit_conversion( hass, condition="water_heater.is_target_temperature", entity_id="water_heater.test", - pass_states=[{"state": "eco", "attributes": {ATTR_TEMPERATURE: 55}}], + pass_states=[ + { + "state": "eco", + "attributes": {ATTR_TEMPERATURE: 55, **_WATER_HEATER_UNIT_ATTRIBUTES}, + }, + { + "state": "eco", + "attributes": { + ATTR_TEMPERATURE: 130, + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + }, + ], fail_states=[ { "state": "eco", - "attributes": {ATTR_TEMPERATURE: 40}, - } + "attributes": {ATTR_TEMPERATURE: 40, **_WATER_HEATER_UNIT_ATTRIBUTES}, + }, + { + "state": "eco", + "attributes": { + ATTR_TEMPERATURE: 104, + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + }, ], numerical_condition_options=[ { diff --git a/tests/components/water_heater/test_trigger.py b/tests/components/water_heater/test_trigger.py index c225c13a4ad1..faa39b30d130 100644 --- a/tests/components/water_heater/test_trigger.py +++ b/tests/components/water_heater/test_trigger.py @@ -6,6 +6,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, @@ -25,9 +26,16 @@ from homeassistant.components.water_heater import ( STATE_HEAT_PUMP, STATE_HIGH_DEMAND, STATE_PERFORMANCE, + WaterHeaterCapabilityAttribute, ) from homeassistant.components.water_heater.trigger import TRIGGERS -from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF, STATE_ON, UnitOfTemperature +from homeassistant.const import ( + ATTR_TEMPERATURE, + CONF_ENTITY_ID, + STATE_OFF, + STATE_ON, + UnitOfTemperature, +) from homeassistant.core import HomeAssistant ALL_ON_STATES = [ @@ -49,6 +57,9 @@ async def target_water_heaters(hass: HomeAssistant) -> list[str]: return await target_entities(hass, "water_heater") +_WATER_HEATER_UNIT_ATTRIBUTES = { + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.CELSIUS +} _CHANGED_THRESHOLD = {"threshold": {"type": "any"}} _CROSSED_THRESHOLD = { "threshold": { @@ -186,6 +197,7 @@ async def test_water_heater_state_trigger_behavior_each( STATE_ECO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), *parametrize_numerical_attribute_crossed_threshold_trigger_states( @@ -193,6 +205,7 @@ async def test_water_heater_state_trigger_behavior_each( STATE_ECO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -292,6 +305,7 @@ async def test_water_heater_state_trigger_behavior_first( STATE_ECO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -391,6 +405,7 @@ async def test_water_heater_state_trigger_behavior_all( STATE_ECO, ATTR_TEMPERATURE, threshold_unit=UnitOfTemperature.CELSIUS, + unit_attributes=_WATER_HEATER_UNIT_ATTRIBUTES, attribute_required=True, ), ], @@ -416,3 +431,59 @@ async def test_water_heater_state_attribute_trigger_behavior_all( trigger_options=trigger_options, states=states, ) + + +async def test_water_heater_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 = "water_heater.test" + + # The system unit is °C, the entity reports in °F + hass.states.async_set( + entity_id, + STATE_ECO, + { + ATTR_TEMPERATURE: 70, + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + + await arm_trigger( + hass, + "water_heater.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, + STATE_ECO, + { + ATTR_TEMPERATURE: 72, + WaterHeaterCapabilityAttribute.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, + STATE_ECO, + { + ATTR_TEMPERATURE: 80, + WaterHeaterCapabilityAttribute.TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + assert len(calls) == 1