Skip to content

Commit

Permalink
Allow setting hvac mode through set_temperature climate method in Gre…
Browse files Browse the repository at this point in the history
…e integration (#101196)

* Allow setting hvac mode through set_temperature climate method

* Suggested code simplification when reading hvac mode

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Remove unnecessary temperature unit handling from set temperature with hvac mode tests

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
  • Loading branch information
MislavMandaric and gjohansson-ST committed Oct 30, 2023
1 parent d97a030 commit 84b71c9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions homeassistant/components/gree/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

from homeassistant.components.climate import (
ATTR_HVAC_MODE,
FAN_AUTO,
FAN_HIGH,
FAN_LOW,
Expand Down Expand Up @@ -158,6 +159,9 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
if ATTR_TEMPERATURE not in kwargs:
raise ValueError(f"Missing parameter {ATTR_TEMPERATURE}")

if hvac_mode := kwargs.get(ATTR_HVAC_MODE):
await self.async_set_hvac_mode(hvac_mode)

temperature = kwargs[ATTR_TEMPERATURE]
_LOGGER.debug(
"Setting temperature to %d for %s",
Expand Down
44 changes: 43 additions & 1 deletion tests/components/gree/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
SWING_VERTICAL,
HVACMode,
)
from homeassistant.components.gree.climate import FAN_MODES_REVERSE, HVAC_MODES_REVERSE
from homeassistant.components.gree.climate import (
FAN_MODES_REVERSE,
HVAC_MODES,
HVAC_MODES_REVERSE,
)
from homeassistant.components.gree.const import FAN_MEDIUM_HIGH, FAN_MEDIUM_LOW
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -384,6 +388,9 @@ async def test_send_target_temperature(
"""Test for sending target temperature command to the device."""
hass.config.units.temperature_unit = units

device().power = True
device().mode = HVAC_MODES_REVERSE.get(HVACMode.AUTO)

fake_device = device()
if units == UnitOfTemperature.FAHRENHEIT:
fake_device.temperature_units = 1
Expand All @@ -407,12 +414,47 @@ async def test_send_target_temperature(
state.attributes.get(ATTR_CURRENT_TEMPERATURE)
== fake_device.current_temperature
)
assert state.state == HVAC_MODES.get(fake_device.mode)

# Reset config temperature_unit back to CELSIUS, required for
# additional tests outside this component.
hass.config.units.temperature_unit = UnitOfTemperature.CELSIUS


@pytest.mark.parametrize(
("temperature", "hvac_mode"),
[
(26, HVACMode.OFF),
(26, HVACMode.HEAT),
(26, HVACMode.COOL),
(26, HVACMode.AUTO),
(26, HVACMode.DRY),
(26, HVACMode.FAN_ONLY),
],
)
async def test_send_target_temperature_with_hvac_mode(
hass: HomeAssistant, discovery, device, temperature, hvac_mode
) -> None:
"""Test for sending target temperature command to the device alongside hvac mode."""
await async_setup_gree(hass)

await hass.services.async_call(
DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: ENTITY_ID,
ATTR_TEMPERATURE: temperature,
ATTR_HVAC_MODE: hvac_mode,
},
blocking=True,
)

state = hass.states.get(ENTITY_ID)
assert state is not None
assert state.attributes.get(ATTR_TEMPERATURE) == temperature
assert state.state == hvac_mode


@pytest.mark.parametrize(
("units", "temperature"),
[(UnitOfTemperature.CELSIUS, 25), (UnitOfTemperature.FAHRENHEIT, 74)],
Expand Down

0 comments on commit 84b71c9

Please sign in to comment.