Skip to content

Commit

Permalink
Allow setting climate devices to AUTO mode via Google Assistant (#11923)
Browse files Browse the repository at this point in the history
* Allow setting climate devices to AUTO mode via Google Assistant

* Remove cast to lower

* Clarify const name
  • Loading branch information
balloob authored and pvizeli committed Jan 26, 2018
1 parent deb10a1 commit 390b727
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
17 changes: 8 additions & 9 deletions homeassistant/components/climate/nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from homeassistant.components.nest import DATA_NEST
from homeassistant.components.climate import (
STATE_AUTO, STATE_COOL, STATE_HEAT, ClimateDevice,
STATE_AUTO, STATE_COOL, STATE_HEAT, STATE_ECO, ClimateDevice,
PLATFORM_SCHEMA, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW,
ATTR_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_HIGH, SUPPORT_TARGET_TEMPERATURE_LOW,
Expand All @@ -27,8 +27,7 @@
vol.All(vol.Coerce(int), vol.Range(min=1)),
})

STATE_ECO = 'eco'
STATE_HEAT_COOL = 'heat-cool'
NEST_MODE_HEAT_COOL = 'heat-cool'

SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | SUPPORT_TARGET_TEMPERATURE_HIGH |
SUPPORT_TARGET_TEMPERATURE_LOW | SUPPORT_OPERATION_MODE |
Expand Down Expand Up @@ -118,14 +117,14 @@ def current_operation(self):
"""Return current operation ie. heat, cool, idle."""
if self._mode in [STATE_HEAT, STATE_COOL, STATE_OFF, STATE_ECO]:
return self._mode
elif self._mode == STATE_HEAT_COOL:
elif self._mode == NEST_MODE_HEAT_COOL:
return STATE_AUTO
return STATE_UNKNOWN

@property
def target_temperature(self):
"""Return the temperature we try to reach."""
if self._mode != STATE_HEAT_COOL and not self.is_away_mode_on:
if self._mode != NEST_MODE_HEAT_COOL and not self.is_away_mode_on:
return self._target_temperature
return None

Expand All @@ -136,7 +135,7 @@ def target_temperature_low(self):
self._eco_temperature[0]:
# eco_temperature is always a low, high tuple
return self._eco_temperature[0]
if self._mode == STATE_HEAT_COOL:
if self._mode == NEST_MODE_HEAT_COOL:
return self._target_temperature[0]
return None

Expand All @@ -147,7 +146,7 @@ def target_temperature_high(self):
self._eco_temperature[1]:
# eco_temperature is always a low, high tuple
return self._eco_temperature[1]
if self._mode == STATE_HEAT_COOL:
if self._mode == NEST_MODE_HEAT_COOL:
return self._target_temperature[1]
return None

Expand All @@ -160,7 +159,7 @@ def set_temperature(self, **kwargs):
"""Set new target temperature."""
target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW)
target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
if self._mode == STATE_HEAT_COOL:
if self._mode == NEST_MODE_HEAT_COOL:
if target_temp_low is not None and target_temp_high is not None:
temp = (target_temp_low, target_temp_high)
else:
Expand All @@ -173,7 +172,7 @@ def set_operation_mode(self, operation_mode):
if operation_mode in [STATE_HEAT, STATE_COOL, STATE_OFF, STATE_ECO]:
device_mode = operation_mode
elif operation_mode == STATE_AUTO:
device_mode = STATE_HEAT_COOL
device_mode = NEST_MODE_HEAT_COOL
self.device.mode = device_mode

@property
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/google_assistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
DEFAULT_EXPOSED_DOMAINS = [
'switch', 'light', 'group', 'media_player', 'fan', 'cover', 'climate'
]
CLIMATE_SUPPORTED_MODES = {'heat', 'cool', 'off', 'on', 'heatcool'}
CLIMATE_MODE_HEATCOOL = 'heatcool'
CLIMATE_SUPPORTED_MODES = {'heat', 'cool', 'off', 'on', CLIMATE_MODE_HEATCOOL}

PREFIX_TRAITS = 'action.devices.traits.'
TRAIT_ONOFF = PREFIX_TRAITS + 'OnOff'
Expand Down
29 changes: 18 additions & 11 deletions homeassistant/components/google_assistant/smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
TRAIT_ONOFF, TRAIT_BRIGHTNESS, TRAIT_COLOR_TEMP,
TRAIT_RGB_COLOR, TRAIT_SCENE, TRAIT_TEMPERATURE_SETTING,
TYPE_LIGHT, TYPE_SCENE, TYPE_SWITCH, TYPE_THERMOSTAT,
CONF_ALIASES, CLIMATE_SUPPORTED_MODES
CONF_ALIASES, CLIMATE_SUPPORTED_MODES, CLIMATE_MODE_HEATCOOL
)

HANDLERS = Registry()
Expand Down Expand Up @@ -147,12 +147,15 @@ def entity_to_device(entity: Entity, config: Config, units: UnitSystem):
entity.attributes.get(light.ATTR_MIN_MIREDS))))

if entity.domain == climate.DOMAIN:
modes = ','.join(
m.lower() for m in entity.attributes.get(
climate.ATTR_OPERATION_LIST, [])
if m.lower() in CLIMATE_SUPPORTED_MODES)
modes = []
for mode in entity.attributes.get(climate.ATTR_OPERATION_LIST, []):
if mode in CLIMATE_SUPPORTED_MODES:
modes.append(mode)
elif mode == climate.STATE_AUTO:
modes.append(CLIMATE_MODE_HEATCOOL)

device['attributes'] = {
'availableThermostatModes': modes,
'availableThermostatModes': ','.join(modes),
'thermostatTemperatureUnit':
'F' if units.temperature_unit == TEMP_FAHRENHEIT else 'C',
}
Expand Down Expand Up @@ -323,9 +326,9 @@ def determine_service(
# special climate handling
if domain == climate.DOMAIN:
if command == COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT:
service_data['temperature'] = units.temperature(
params.get('thermostatTemperatureSetpoint', 25),
TEMP_CELSIUS)
service_data['temperature'] = \
units.temperature(
params['thermostatTemperatureSetpoint'], TEMP_CELSIUS)
return (climate.SERVICE_SET_TEMPERATURE, service_data)
if command == COMMAND_THERMOSTAT_TEMPERATURE_SET_RANGE:
service_data['target_temp_high'] = units.temperature(
Expand All @@ -336,8 +339,12 @@ def determine_service(
TEMP_CELSIUS)
return (climate.SERVICE_SET_TEMPERATURE, service_data)
if command == COMMAND_THERMOSTAT_SET_MODE:
service_data['operation_mode'] = params.get(
'thermostatMode', 'off')
mode = params['thermostatMode']

if mode == CLIMATE_MODE_HEATCOOL:
mode = climate.STATE_AUTO

service_data['operation_mode'] = mode
return (climate.SERVICE_SET_OPERATION_MODE, service_data)

if command == COMMAND_BRIGHTNESS:
Expand Down
2 changes: 1 addition & 1 deletion tests/components/google_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
'type': 'action.devices.types.THERMOSTAT',
'willReportState': False,
'attributes': {
'availableThermostatModes': 'heat,cool,off',
'availableThermostatModes': 'heat,cool,heatcool,off',
'thermostatTemperatureUnit': 'C',
},
}, {
Expand Down

0 comments on commit 390b727

Please sign in to comment.