Skip to content

Commit

Permalink
Sync Climate min/max temp with Google Assistant (#94143)
Browse files Browse the repository at this point in the history
* Sync climate min/max temp to Google Assistant

* Improving coverage on TemperatureSettingTrait
  • Loading branch information
hookedonunix committed Jun 8, 2023
1 parent a6a2b8d commit 85a12c3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
23 changes: 21 additions & 2 deletions homeassistant/components/google_assistant/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,28 @@ def climate_google_modes(self):
def sync_attributes(self):
"""Return temperature point and modes attributes for a sync request."""
response = {}
response["thermostatTemperatureUnit"] = _google_temp_unit(
self.hass.config.units.temperature_unit
attrs = self.state.attributes
unit = self.hass.config.units.temperature_unit
response["thermostatTemperatureUnit"] = _google_temp_unit(unit)

min_temp = round(
TemperatureConverter.convert(
float(attrs[climate.ATTR_MIN_TEMP]),
unit,
UnitOfTemperature.CELSIUS,
)
)
max_temp = round(
TemperatureConverter.convert(
float(attrs[climate.ATTR_MAX_TEMP]),
unit,
UnitOfTemperature.CELSIUS,
)
)
response["thermostatTemperatureRange"] = {
"minThresholdCelsius": min_temp,
"maxThresholdCelsius": max_temp,
}

modes = self.climate_google_modes

Expand Down
91 changes: 83 additions & 8 deletions tests/components/google_assistant/test_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,18 @@ async def test_temperature_setting_climate_onoff(hass: HomeAssistant) -> None:
climate.HVACMode.HEAT,
climate.HVACMode.HEAT_COOL,
],
climate.ATTR_MIN_TEMP: None,
climate.ATTR_MAX_TEMP: None,
climate.ATTR_MIN_TEMP: 45,
climate.ATTR_MAX_TEMP: 95,
},
),
BASIC_CONFIG,
)
assert trt.sync_attributes() == {
"availableThermostatModes": ["off", "cool", "heat", "heatcool", "on"],
"thermostatTemperatureRange": {
"minThresholdCelsius": 7,
"maxThresholdCelsius": 35,
},
"thermostatTemperatureUnit": "F",
}
assert trt.can_execute(trait.COMMAND_THERMOSTAT_SET_MODE, {})
Expand Down Expand Up @@ -893,14 +897,18 @@ async def test_temperature_setting_climate_no_modes(hass: HomeAssistant) -> None
climate.HVACMode.AUTO,
{
climate.ATTR_HVAC_MODES: [],
climate.ATTR_MIN_TEMP: None,
climate.ATTR_MAX_TEMP: None,
climate.ATTR_MIN_TEMP: climate.DEFAULT_MIN_TEMP,
climate.ATTR_MAX_TEMP: climate.DEFAULT_MAX_TEMP,
},
),
BASIC_CONFIG,
)
assert trt.sync_attributes() == {
"availableThermostatModes": ["heat"],
"thermostatTemperatureRange": {
"minThresholdCelsius": climate.DEFAULT_MIN_TEMP,
"maxThresholdCelsius": climate.DEFAULT_MAX_TEMP,
},
"thermostatTemperatureUnit": "C",
}

Expand Down Expand Up @@ -937,6 +945,10 @@ async def test_temperature_setting_climate_range(hass: HomeAssistant) -> None:
)
assert trt.sync_attributes() == {
"availableThermostatModes": ["off", "cool", "heat", "auto", "on"],
"thermostatTemperatureRange": {
"minThresholdCelsius": 10,
"maxThresholdCelsius": 27,
},
"thermostatTemperatureUnit": "F",
}
assert trt.query_attributes() == {
Expand Down Expand Up @@ -978,12 +990,40 @@ async def test_temperature_setting_climate_range(hass: HomeAssistant) -> None:

with pytest.raises(helpers.SmartHomeError) as err:
await trt.execute(
trait.COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT,
trait.COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE,
BASIC_DATA,
{"thermostatTemperatureSetpoint": -100},
{
"thermostatTemperatureSetpointHigh": 26,
"thermostatTemperatureSetpointLow": -100,
},
{},
)
assert err.value.code == const.ERR_VALUE_OUT_OF_RANGE

with pytest.raises(helpers.SmartHomeError) as err:
await trt.execute(
trait.COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE,
BASIC_DATA,
{
"thermostatTemperatureSetpointHigh": 100,
"thermostatTemperatureSetpointLow": 18,
},
{},
)
assert err.value.code == const.ERR_VALUE_OUT_OF_RANGE

calls = async_mock_service(hass, climate.DOMAIN, climate.SERVICE_SET_TEMPERATURE)
await trt.execute(
trait.COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT,
BASIC_DATA,
{"thermostatTemperatureSetpoint": 23.9},
{},
)
assert len(calls) == 1
assert calls[0].data == {
ATTR_ENTITY_ID: "climate.bla",
climate.ATTR_TEMPERATURE: 75,
}
hass.config.units.temperature_unit = UnitOfTemperature.CELSIUS


Expand All @@ -1000,9 +1040,11 @@ async def test_temperature_setting_climate_setpoint(hass: HomeAssistant) -> None
"climate.bla",
climate.HVACMode.COOL,
{
ATTR_SUPPORTED_FEATURES: climate.SUPPORT_TARGET_TEMPERATURE,
climate.ATTR_HVAC_MODES: [STATE_OFF, climate.HVACMode.COOL],
climate.ATTR_MIN_TEMP: 10,
climate.ATTR_MAX_TEMP: 30,
climate.ATTR_PRESET_MODE: climate.PRESET_ECO,
ATTR_TEMPERATURE: 18,
climate.ATTR_CURRENT_TEMPERATURE: 20,
},
Expand All @@ -1011,18 +1053,21 @@ async def test_temperature_setting_climate_setpoint(hass: HomeAssistant) -> None
)
assert trt.sync_attributes() == {
"availableThermostatModes": ["off", "cool", "on"],
"thermostatTemperatureRange": {
"minThresholdCelsius": 10,
"maxThresholdCelsius": 30,
},
"thermostatTemperatureUnit": "C",
}
assert trt.query_attributes() == {
"thermostatMode": "cool",
"thermostatMode": "eco",
"thermostatTemperatureAmbient": 20,
"thermostatTemperatureSetpoint": 18,
}
assert trt.can_execute(trait.COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT, {})
assert trt.can_execute(trait.COMMAND_THERMOSTAT_SET_MODE, {})

calls = async_mock_service(hass, climate.DOMAIN, climate.SERVICE_SET_TEMPERATURE)

with pytest.raises(helpers.SmartHomeError):
await trt.execute(
trait.COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT,
Expand All @@ -1040,6 +1085,32 @@ async def test_temperature_setting_climate_setpoint(hass: HomeAssistant) -> None
assert len(calls) == 1
assert calls[0].data == {ATTR_ENTITY_ID: "climate.bla", ATTR_TEMPERATURE: 19}

calls = async_mock_service(hass, climate.DOMAIN, climate.SERVICE_SET_PRESET_MODE)
await trt.execute(
trait.COMMAND_THERMOSTAT_SET_MODE,
BASIC_DATA,
{"thermostatMode": "eco"},
{},
)
assert len(calls) == 1
assert calls[0].data == {
ATTR_ENTITY_ID: "climate.bla",
climate.ATTR_PRESET_MODE: "eco",
}

calls = async_mock_service(hass, climate.DOMAIN, climate.SERVICE_SET_TEMPERATURE)
await trt.execute(
trait.COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE,
BASIC_DATA,
{
"thermostatTemperatureSetpointHigh": 15,
"thermostatTemperatureSetpointLow": 22,
},
{},
)
assert len(calls) == 1
assert calls[0].data == {ATTR_ENTITY_ID: "climate.bla", ATTR_TEMPERATURE: 18.5}


async def test_temperature_setting_climate_setpoint_auto(hass: HomeAssistant) -> None:
"""Test TemperatureSetting trait support for climate domain.
Expand Down Expand Up @@ -1068,6 +1139,10 @@ async def test_temperature_setting_climate_setpoint_auto(hass: HomeAssistant) ->
)
assert trt.sync_attributes() == {
"availableThermostatModes": ["off", "heatcool", "on"],
"thermostatTemperatureRange": {
"minThresholdCelsius": 10,
"maxThresholdCelsius": 30,
},
"thermostatTemperatureUnit": "C",
}
assert trt.query_attributes() == {
Expand Down

0 comments on commit 85a12c3

Please sign in to comment.