Intent: Set light color (#12633)

* Make color_name_to_rgb raise

* Add Light Set Color intent

* Move some methods around

* Cleanup

* Prevent 1 more func call

* Make a generic Set intent for light

* Lint

* lint
This commit is contained in:
Paulus Schoutsen 2018-02-27 18:02:21 -08:00 committed by GitHub
parent 14d052d242
commit efd155dd3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 231 additions and 47 deletions

View file

@ -7,10 +7,12 @@ from homeassistant.setup import setup_component
import homeassistant.loader as loader
from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM,
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE)
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, ATTR_SUPPORTED_FEATURES)
import homeassistant.components.light as light
from homeassistant.helpers.intent import IntentHandleError
from tests.common import mock_service, get_test_home_assistant
from tests.common import (
async_mock_service, mock_service, get_test_home_assistant)
class TestLight(unittest.TestCase):
@ -302,3 +304,93 @@ class TestLight(unittest.TestCase):
self.assertEqual(
{light.ATTR_XY_COLOR: (.4, .6), light.ATTR_BRIGHTNESS: 100},
data)
async def test_intent_set_color(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello_2', 'off', {
ATTR_SUPPORTED_FEATURES: light.SUPPORT_RGB_COLOR
})
hass.states.async_set('switch.hello', 'off')
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
result = await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
}
})
await hass.async_block_till_done()
assert result.speech['plain']['speech'] == \
'Changed hello 2 to the color blue'
assert len(calls) == 1
call = calls[0]
assert call.domain == light.DOMAIN
assert call.service == SERVICE_TURN_ON
assert call.data.get(ATTR_ENTITY_ID) == 'light.hello_2'
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
async def test_intent_set_color_tests_feature(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello', 'off')
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
try:
await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
}
})
assert False, 'handling intent should have raised'
except IntentHandleError as err:
assert str(err) == 'Entity hello does not support changing colors'
assert len(calls) == 0
async def test_intent_set_color_and_brightness(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello_2', 'off', {
ATTR_SUPPORTED_FEATURES: (
light.SUPPORT_RGB_COLOR | light.SUPPORT_BRIGHTNESS)
})
hass.states.async_set('switch.hello', 'off')
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
result = await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
},
'brightness': {
'value': '20'
}
})
await hass.async_block_till_done()
assert result.speech['plain']['speech'] == \
'Changed hello 2 to the color blue and 20% brightness'
assert len(calls) == 1
call = calls[0]
assert call.domain == light.DOMAIN
assert call.service == SERVICE_TURN_ON
assert call.data.get(ATTR_ENTITY_ID) == 'light.hello_2'
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
assert call.data.get(light.ATTR_BRIGHTNESS_PCT) == 20