Fix Gree infrared temperature restoration using the saved unit (#185528)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Abílio Costa
2026-10-10 17:46:06 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 76df4a66d5
commit e205fea2e0
2 changed files with 102 additions and 4 deletions
@@ -20,6 +20,7 @@ from homeassistant.components.climate import (
FAN_MEDIUM,
ClimateEntity,
ClimateEntityFeature,
ClimateEntityStateAttribute,
HVACMode,
)
from homeassistant.components.infrared import (
@@ -162,7 +163,10 @@ class GreeAcClimateEntity(
round(
TemperatureConverter.convert(
float(temperature),
self.hass.config.units.temperature_unit,
last_state.attributes.get(
ClimateEntityStateAttribute.TEMPERATURE_UNIT,
self.hass.config.units.temperature_unit,
),
self.native_temperature_unit,
)
)
+97 -3
View File
@@ -30,11 +30,16 @@ from homeassistant.const import (
ATTR_TEMPERATURE,
STATE_UNAVAILABLE,
Platform,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_registry as er
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from homeassistant.util.unit_system import (
METRIC_SYSTEM,
US_CUSTOMARY_SYSTEM,
UnitSystem,
)
from tests.common import (
MockConfigEntry,
@@ -806,7 +811,11 @@ async def test_supported_features_always_include_target_temperature(
[
pytest.param(
HVACMode.COOL,
{"fan_mode": FAN_HIGH, "temperature": 29.0},
{
"fan_mode": FAN_HIGH,
"temperature": 29.0,
"temperature_unit": UnitOfTemperature.CELSIUS,
},
(HVACMode.COOL, FAN_HIGH, 29.0),
id="full_state",
),
@@ -818,7 +827,11 @@ async def test_supported_features_always_include_target_temperature(
),
pytest.param(
HVACMode.HEAT,
{"fan_mode": FAN_HIGH, "temperature": 29.0},
{
"fan_mode": FAN_HIGH,
"temperature": 29.0,
"temperature_unit": UnitOfTemperature.CELSIUS,
},
(HVACMode.OFF, FAN_HIGH, 29.0),
id="mode_no_longer_configured_is_ignored",
),
@@ -851,6 +864,87 @@ async def test_state_restored_on_restart(
assert state.attributes["temperature"] == expected_temp
@pytest.mark.parametrize(
("restored_attributes", "unit_system", "expected_temperature"),
[
pytest.param(
{"temperature": 17, "temperature_unit": UnitOfTemperature.CELSIUS},
METRIC_SYSTEM,
17,
id="celsius_to_celsius",
),
pytest.param(
{"temperature": 17, "temperature_unit": UnitOfTemperature.CELSIUS},
US_CUSTOMARY_SYSTEM,
63,
id="celsius_to_fahrenheit",
),
pytest.param(
{"temperature": 63, "temperature_unit": UnitOfTemperature.FAHRENHEIT},
METRIC_SYSTEM,
17,
id="fahrenheit_to_celsius",
),
pytest.param(
{"temperature": 63, "temperature_unit": UnitOfTemperature.FAHRENHEIT},
US_CUSTOMARY_SYSTEM,
63,
id="fahrenheit_to_fahrenheit",
),
pytest.param({"temperature": 17}, METRIC_SYSTEM, 17, id="legacy_celsius"),
pytest.param(
{"temperature": 63}, US_CUSTOMARY_SYSTEM, 63, id="legacy_fahrenheit"
),
],
)
async def test_restore_temperature_unit(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_infrared_emitter_entity: MockInfraredEmitterEntity,
platforms: list[Platform],
restored_attributes: dict[str, int | str],
unit_system: UnitSystem,
expected_temperature: int,
) -> None:
"""Test restoration uses the saved unit, falling back to the configured unit."""
hass.config.units = unit_system
mock_restore_cache(
hass,
[
State(
_CLIMATE_ENTITY_ID,
HVACMode.COOL,
restored_attributes,
)
],
)
mock_config_entry.add_to_hass(hass)
with patch("homeassistant.components.gree_infrared.PLATFORMS", platforms):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(_CLIMATE_ENTITY_ID)
assert state is not None
assert state.attributes["temperature"] == expected_temperature
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: _CLIMATE_ENTITY_ID, "hvac_mode": HVACMode.COOL},
blocking=True,
)
assert len(mock_infrared_emitter_entity.send_command_calls) == 1
timings = mock_infrared_emitter_entity.send_command_calls[0].get_raw_timings()
assert (
timings
== GreeAcCommand(
mode=GreeAcMode.COOL, temperature=17, fan=GreeAcFanSpeed.AUTO
).get_raw_timings()
)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("hvac_mode", "expected_cmd"),