Skip to content

Commit

Permalink
Check color temp range for google assistant (#12994)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Mar 9, 2018
1 parent c4a4802 commit 7f065e3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 12 additions & 1 deletion homeassistant/components/google_assistant/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,20 @@ def can_execute(self, command, params):

async def execute(self, hass, command, params):
"""Execute a color temperature command."""
temp = color_util.color_temperature_kelvin_to_mired(
params['color']['temperature'])
min_temp = self.state.attributes[light.ATTR_MIN_MIREDS]
max_temp = self.state.attributes[light.ATTR_MAX_MIREDS]

if temp < min_temp or temp > max_temp:
raise SmartHomeError(
ERR_VALUE_OUT_OF_RANGE,
"Temperature should be between {} and {}".format(min_temp,
max_temp))

await hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: self.state.entity_id,
light.ATTR_KELVIN: params['color']['temperature'],
light.ATTR_COLOR_TEMP: temp,
}, blocking=True)


Expand Down
17 changes: 14 additions & 3 deletions tests/components/google_assistant/test_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
script,
switch,
)
from homeassistant.components.google_assistant import trait, helpers
from homeassistant.components.google_assistant import trait, helpers, const
from homeassistant.util import color

from tests.common import async_mock_service

Expand Down Expand Up @@ -399,6 +400,15 @@ async def test_color_temperature_light(hass):
})

calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_ON)

with pytest.raises(helpers.SmartHomeError) as err:
await trt.execute(hass, trait.COMMAND_COLOR_ABSOLUTE, {
'color': {
'temperature': 5555
}
})
assert err.value.code == const.ERR_VALUE_OUT_OF_RANGE

await trt.execute(hass, trait.COMMAND_COLOR_ABSOLUTE, {
'color': {
'temperature': 2857
Expand All @@ -407,7 +417,7 @@ async def test_color_temperature_light(hass):
assert len(calls) == 1
assert calls[0].data == {
ATTR_ENTITY_ID: 'light.bla',
light.ATTR_KELVIN: 2857
light.ATTR_COLOR_TEMP: color.color_temperature_kelvin_to_mired(2857)
}


Expand Down Expand Up @@ -511,11 +521,12 @@ async def test_temperature_setting_climate_range(hass):
climate.ATTR_OPERATION_MODE: climate.STATE_AUTO,
}

with pytest.raises(helpers.SmartHomeError):
with pytest.raises(helpers.SmartHomeError) as err:
await trt.execute(
hass, trait.COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT, {
'thermostatTemperatureSetpoint': -100,
})
assert err.value.code == const.ERR_VALUE_OUT_OF_RANGE


async def test_temperature_setting_climate_setpoint(hass):
Expand Down

0 comments on commit 7f065e3

Please sign in to comment.