Support setting hvac_mode and temp in same homekit_controller set_temperature service call (#52195)

* Support setting hvac_mode and temp in same set_temperature service call

* Update homeassistant/components/homekit_controller/climate.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/homekit_controller/climate.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/52299/head
Jc2k 2021-06-29 10:14:28 +01:00 committed by GitHub
parent 040c88f982
commit 7de3e7d1dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 7 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.components.climate import (
ClimateEntity,
)
from homeassistant.components.climate.const import (
ATTR_HVAC_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_COOL,
@ -342,16 +343,27 @@ class HomeKitClimateEntity(HomeKitEntity, ClimateEntity):
async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
chars = {}
value = self.service.value(CharacteristicsTypes.HEATING_COOLING_TARGET)
mode = MODE_HOMEKIT_TO_HASS.get(value)
if kwargs.get(ATTR_HVAC_MODE, mode) != mode:
mode = kwargs[ATTR_HVAC_MODE]
chars[CharacteristicsTypes.HEATING_COOLING_TARGET] = MODE_HASS_TO_HOMEKIT[
mode
]
temp = kwargs.get(ATTR_TEMPERATURE)
heat_temp = kwargs.get(ATTR_TARGET_TEMP_LOW)
cool_temp = kwargs.get(ATTR_TARGET_TEMP_HIGH)
value = self.service.value(CharacteristicsTypes.HEATING_COOLING_TARGET)
if (MODE_HOMEKIT_TO_HASS.get(value) in {HVAC_MODE_HEAT_COOL}) and (
if (mode == HVAC_MODE_HEAT_COOL) and (
SUPPORT_TARGET_TEMPERATURE_RANGE & self.supported_features
):
if temp is None:
temp = (cool_temp + heat_temp) / 2
await self.async_put_characteristics(
chars.update(
{
CharacteristicsTypes.TEMPERATURE_HEATING_THRESHOLD: heat_temp,
CharacteristicsTypes.TEMPERATURE_COOLING_THRESHOLD: cool_temp,
@ -359,9 +371,9 @@ class HomeKitClimateEntity(HomeKitEntity, ClimateEntity):
}
)
else:
await self.async_put_characteristics(
{CharacteristicsTypes.TEMPERATURE_TARGET: temp}
)
chars[CharacteristicsTypes.TEMPERATURE_TARGET] = temp
await self.async_put_characteristics(chars)
async def async_set_humidity(self, humidity):
"""Set new target humidity."""

View File

@ -271,7 +271,6 @@ async def test_climate_cannot_set_thermostat_temp_range_in_wrong_mode(hass, utcn
SERVICE_SET_TEMPERATURE,
{
"entity_id": "climate.testdevice",
"hvac_mode": HVAC_MODE_HEAT_COOL,
"temperature": 22,
"target_temp_low": 20,
"target_temp_high": 24,
@ -370,6 +369,35 @@ async def test_climate_set_thermostat_temp_on_sspa_device(hass, utcnow):
)
assert helper.characteristics[TEMPERATURE_TARGET].value == 21
await hass.services.async_call(
DOMAIN,
SERVICE_SET_TEMPERATURE,
{
"entity_id": "climate.testdevice",
"temperature": 22,
},
blocking=True,
)
assert helper.characteristics[TEMPERATURE_TARGET].value == 22
async def test_climate_set_mode_via_temp(hass, utcnow):
"""Test setting temperature and mode at same tims."""
helper = await setup_test_component(hass, create_thermostat_single_set_point_auto)
await hass.services.async_call(
DOMAIN,
SERVICE_SET_TEMPERATURE,
{
"entity_id": "climate.testdevice",
"temperature": 21,
"hvac_mode": HVAC_MODE_HEAT,
},
blocking=True,
)
assert helper.characteristics[TEMPERATURE_TARGET].value == 21
assert helper.characteristics[HEATING_COOLING_TARGET].value == 1
await hass.services.async_call(
DOMAIN,
SERVICE_SET_TEMPERATURE,
@ -381,6 +409,7 @@ async def test_climate_set_thermostat_temp_on_sspa_device(hass, utcnow):
blocking=True,
)
assert helper.characteristics[TEMPERATURE_TARGET].value == 22
assert helper.characteristics[HEATING_COOLING_TARGET].value == 3
async def test_climate_change_thermostat_humidity(hass, utcnow):