2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Light component."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2014-11-26 05:28:43 +00:00
|
|
|
import unittest
|
2014-11-26 07:16:07 +00:00
|
|
|
import os
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2014-11-26 05:28:43 +00:00
|
|
|
import homeassistant.loader as loader
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2015-03-06 08:04:32 +00:00
|
|
|
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM,
|
2018-02-28 02:02:21 +00:00
|
|
|
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, ATTR_SUPPORTED_FEATURES)
|
2014-11-26 05:28:43 +00:00
|
|
|
import homeassistant.components.light as light
|
2018-02-28 02:02:21 +00:00
|
|
|
from homeassistant.helpers.intent import IntentHandleError
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2018-02-28 02:02:21 +00:00
|
|
|
from tests.common import (
|
|
|
|
async_mock_service, mock_service, get_test_home_assistant)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLight(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the light module."""
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2014-11-26 07:16:07 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2014-11-29 04:22:08 +00:00
|
|
|
self.hass.stop()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2015-03-19 19:27:56 +00:00
|
|
|
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-26 07:16:07 +00:00
|
|
|
|
|
|
|
if os.path.isfile(user_light_file):
|
|
|
|
os.remove(user_light_file)
|
|
|
|
|
2014-11-26 05:28:43 +00:00
|
|
|
def test_methods(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if methods call the services as expected."""
|
2014-11-26 05:28:43 +00:00
|
|
|
# Test is_on
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass.states.set('light.test', STATE_ON)
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertTrue(light.is_on(self.hass, 'light.test'))
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass.states.set('light.test', STATE_OFF)
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertFalse(light.is_on(self.hass, 'light.test'))
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertTrue(light.is_on(self.hass))
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF)
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertFalse(light.is_on(self.hass))
|
|
|
|
|
|
|
|
# Test turn_on
|
|
|
|
turn_on_calls = mock_service(
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
light.turn_on(
|
|
|
|
self.hass,
|
|
|
|
entity_id='entity_id_val',
|
|
|
|
transition='transition_val',
|
|
|
|
brightness='brightness_val',
|
|
|
|
rgb_color='rgb_color_val',
|
|
|
|
xy_color='xy_color_val',
|
2016-09-21 04:26:40 +00:00
|
|
|
profile='profile_val',
|
|
|
|
color_name='color_name_val',
|
|
|
|
white_value='white_val')
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(turn_on_calls))
|
|
|
|
call = turn_on_calls[-1]
|
|
|
|
|
|
|
|
self.assertEqual(light.DOMAIN, call.domain)
|
2014-12-01 07:14:08 +00:00
|
|
|
self.assertEqual(SERVICE_TURN_ON, call.service)
|
|
|
|
self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID))
|
|
|
|
self.assertEqual(
|
|
|
|
'transition_val', call.data.get(light.ATTR_TRANSITION))
|
|
|
|
self.assertEqual(
|
|
|
|
'brightness_val', call.data.get(light.ATTR_BRIGHTNESS))
|
|
|
|
self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR))
|
|
|
|
self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR))
|
|
|
|
self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE))
|
2016-09-21 04:26:40 +00:00
|
|
|
self.assertEqual(
|
|
|
|
'color_name_val', call.data.get(light.ATTR_COLOR_NAME))
|
|
|
|
self.assertEqual('white_val', call.data.get(light.ATTR_WHITE_VALUE))
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
# Test turn_off
|
|
|
|
turn_off_calls = mock_service(
|
2014-12-01 07:14:08 +00:00
|
|
|
self.hass, light.DOMAIN, SERVICE_TURN_OFF)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
light.turn_off(
|
|
|
|
self.hass, entity_id='entity_id_val', transition='transition_val')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(turn_off_calls))
|
|
|
|
call = turn_off_calls[-1]
|
|
|
|
|
|
|
|
self.assertEqual(light.DOMAIN, call.domain)
|
2014-12-01 07:14:08 +00:00
|
|
|
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
|
|
|
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
|
|
|
|
|
2016-01-17 21:42:18 +00:00
|
|
|
# Test toggle
|
|
|
|
toggle_calls = mock_service(
|
|
|
|
self.hass, light.DOMAIN, SERVICE_TOGGLE)
|
|
|
|
|
|
|
|
light.toggle(
|
|
|
|
self.hass, entity_id='entity_id_val', transition='transition_val')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-17 21:42:18 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(toggle_calls))
|
|
|
|
call = toggle_calls[-1]
|
|
|
|
|
|
|
|
self.assertEqual(light.DOMAIN, call.domain)
|
|
|
|
self.assertEqual(SERVICE_TOGGLE, call.service)
|
|
|
|
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
|
|
|
|
self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
|
|
|
|
|
2014-11-26 05:28:43 +00:00
|
|
|
def test_services(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the provided services."""
|
2014-12-07 07:57:02 +00:00
|
|
|
platform = loader.get_component('light.test')
|
2014-12-01 07:14:08 +00:00
|
|
|
|
|
|
|
platform.init()
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertTrue(
|
2016-09-17 17:29:58 +00:00
|
|
|
setup_component(self.hass, light.DOMAIN,
|
|
|
|
{light.DOMAIN: {CONF_PLATFORM: 'test'}}))
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2015-03-01 09:35:58 +00:00
|
|
|
dev1, dev2, dev3 = platform.DEVICES
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
# Test init
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev3.entity_id))
|
|
|
|
|
2016-01-17 21:42:18 +00:00
|
|
|
# Test basic turn_on, turn_off, toggle services
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_off(self.hass, entity_id=dev1.entity_id)
|
|
|
|
light.turn_on(self.hass, entity_id=dev2.entity_id)
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev1.entity_id))
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
|
|
|
|
# turn on all lights
|
|
|
|
light.turn_on(self.hass)
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev3.entity_id))
|
|
|
|
|
|
|
|
# turn off all lights
|
|
|
|
light.turn_off(self.hass)
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev1.entity_id))
|
2016-01-17 21:42:18 +00:00
|
|
|
self.assertFalse(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev3.entity_id))
|
|
|
|
|
|
|
|
# toggle all lights
|
|
|
|
light.toggle(self.hass)
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-17 21:42:18 +00:00
|
|
|
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
self.assertTrue(light.is_on(self.hass, dev3.entity_id))
|
|
|
|
|
|
|
|
# toggle all lights
|
|
|
|
light.toggle(self.hass)
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-17 21:42:18 +00:00
|
|
|
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev1.entity_id))
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertFalse(light.is_on(self.hass, dev2.entity_id))
|
|
|
|
self.assertFalse(light.is_on(self.hass, dev3.entity_id))
|
|
|
|
|
|
|
|
# Ensure all attributes process correctly
|
|
|
|
light.turn_on(self.hass, dev1.entity_id,
|
2016-09-21 04:26:40 +00:00
|
|
|
transition=10, brightness=20, color_name='blue')
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_on(
|
2016-09-21 04:26:40 +00:00
|
|
|
self.hass, dev2.entity_id, rgb_color=(255, 255, 255),
|
|
|
|
white_value=255)
|
2016-04-01 02:59:18 +00:00
|
|
|
light.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6))
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev1.last_call('turn_on')
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_TRANSITION: 10,
|
|
|
|
light.ATTR_BRIGHTNESS: 20,
|
|
|
|
light.ATTR_HS_COLOR: (240, 100),
|
|
|
|
}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev2.last_call('turn_on')
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_HS_COLOR: (0, 0),
|
|
|
|
light.ATTR_WHITE_VALUE: 255,
|
|
|
|
}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev3.last_call('turn_on')
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_HS_COLOR: (71.059, 100),
|
|
|
|
}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
# One of the light profiles
|
2018-03-18 22:00:29 +00:00
|
|
|
prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144
|
2014-11-26 05:28:43 +00:00
|
|
|
|
|
|
|
# Test light profiles
|
|
|
|
light.turn_on(self.hass, dev1.entity_id, profile=prof_name)
|
2017-06-02 06:05:05 +00:00
|
|
|
# Specify a profile and a brightness attribute to overwrite it
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_on(
|
|
|
|
self.hass, dev2.entity_id,
|
2017-06-02 06:05:05 +00:00
|
|
|
profile=prof_name, brightness=100)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev1.last_call('turn_on')
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_BRIGHTNESS: prof_bri,
|
|
|
|
light.ATTR_HS_COLOR: (prof_h, prof_s),
|
|
|
|
}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev2.last_call('turn_on')
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_BRIGHTNESS: 100,
|
|
|
|
light.ATTR_HS_COLOR: (prof_h, prof_s),
|
|
|
|
}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2017-06-02 06:05:05 +00:00
|
|
|
# Test bad data
|
2016-03-31 22:24:06 +00:00
|
|
|
light.turn_on(self.hass)
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
|
2014-11-26 05:42:33 +00:00
|
|
|
light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2])
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev1.last_call('turn_on')
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertEqual({}, data)
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev2.last_call('turn_on')
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertEqual({}, data)
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev3.last_call('turn_on')
|
2014-11-26 05:28:43 +00:00
|
|
|
self.assertEqual({}, data)
|
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
# faulty attributes will not trigger a service call
|
2014-11-26 05:28:43 +00:00
|
|
|
light.turn_on(
|
|
|
|
self.hass, dev1.entity_id,
|
2017-06-02 06:05:05 +00:00
|
|
|
profile=prof_name, brightness='bright')
|
|
|
|
light.turn_on(
|
|
|
|
self.hass, dev1.entity_id,
|
|
|
|
rgb_color='yellowish')
|
2016-09-21 04:26:40 +00:00
|
|
|
light.turn_on(
|
|
|
|
self.hass, dev2.entity_id,
|
|
|
|
white_value='high')
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev1.last_call('turn_on')
|
|
|
|
self.assertEqual({}, data)
|
|
|
|
|
|
|
|
_, data = dev2.last_call('turn_on')
|
2016-03-31 22:24:06 +00:00
|
|
|
self.assertEqual({}, data)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2015-01-20 05:39:24 +00:00
|
|
|
def test_broken_light_profiles(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test light profiles."""
|
2014-12-07 07:57:02 +00:00
|
|
|
platform = loader.get_component('light.test')
|
2014-12-01 07:14:08 +00:00
|
|
|
platform.init()
|
2014-11-26 07:16:07 +00:00
|
|
|
|
2015-03-19 19:27:56 +00:00
|
|
|
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-26 07:16:07 +00:00
|
|
|
|
|
|
|
# Setup a wrong light file
|
|
|
|
with open(user_light_file, 'w') as user_file:
|
|
|
|
user_file.write('id,x,y,brightness\n')
|
|
|
|
user_file.write('I,WILL,NOT,WORK\n')
|
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertFalse(setup_component(
|
2016-11-30 21:33:38 +00:00
|
|
|
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}))
|
2014-11-26 07:16:07 +00:00
|
|
|
|
2015-01-20 05:39:24 +00:00
|
|
|
def test_light_profiles(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test light profiles."""
|
2015-01-20 05:39:24 +00:00
|
|
|
platform = loader.get_component('light.test')
|
|
|
|
platform.init()
|
|
|
|
|
2015-03-19 19:27:56 +00:00
|
|
|
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-26 07:16:07 +00:00
|
|
|
|
|
|
|
with open(user_light_file, 'w') as user_file:
|
|
|
|
user_file.write('id,x,y,brightness\n')
|
|
|
|
user_file.write('test,.4,.6,100\n')
|
|
|
|
|
2016-09-17 17:29:58 +00:00
|
|
|
self.assertTrue(setup_component(
|
|
|
|
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
|
2014-11-26 07:16:07 +00:00
|
|
|
))
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
dev1, _, _ = platform.DEVICES
|
2014-11-26 07:16:07 +00:00
|
|
|
|
|
|
|
light.turn_on(self.hass, dev1.entity_id, profile='test')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2014-11-26 07:16:07 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
_, data = dev1.last_call('turn_on')
|
2014-11-26 07:16:07 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
self.assertEqual({
|
|
|
|
light.ATTR_HS_COLOR: (71.059, 100),
|
|
|
|
light.ATTR_BRIGHTNESS: 100
|
|
|
|
}, data)
|
2018-02-28 02:02:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_intent_set_color(hass):
|
|
|
|
"""Test the set color intent."""
|
|
|
|
hass.states.async_set('light.hello_2', 'off', {
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_SUPPORTED_FEATURES: light.SUPPORT_COLOR
|
2018-02-28 02:02:21 +00:00
|
|
|
})
|
|
|
|
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: (
|
2018-03-18 22:00:29 +00:00
|
|
|
light.SUPPORT_COLOR | light.SUPPORT_BRIGHTNESS)
|
2018-02-28 02:02:21 +00:00
|
|
|
})
|
|
|
|
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
|