async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock): """Test the sending of Color Temp command with template.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_color_temp/set', 'color_temp_command_topic': 'test_light_color_temp/color_temp/set', 'color_temp_command_template': '{{ (1000 / value) | round(0) }}', 'payload_on': 'on', 'payload_off': 'off', 'qos': 0 } } assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', color_temp=100) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_color_temp/set', 'on', 0, False), mock.call('test_light_color_temp/color_temp/set', '10', 0, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert 100 == state.attributes['color_temp']
async def test_on_command_rgb(hass, mqtt_mock): """Test on command in RGB brightness mode.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'rgb_command_topic': "test_light/rgb", }} assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', brightness=127) await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/rgb: '127,127,127' # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/rgb', '127,127,127', 0, False), mock.call('test_light/set', 'ON', 0, False), ], any_order=True) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False)
async def test_light_service_calls(hass): """Test service calls to light.""" await async_setup_component(hass, 'switch', {'switch': [ {'platform': 'demo'} ]}) await async_setup_component(hass, 'light', {'light': [ {'platform': 'switch', 'entity_id': 'switch.decorative_lights'} ]}) await hass.async_block_till_done() assert hass.states.get('light.light_switch').state == 'on' common.async_toggle(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'off' assert hass.states.get('light.light_switch').state == 'off' common.async_turn_on(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'on' assert hass.states.get('light.light_switch').state == 'on' common.async_turn_off(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'off' assert hass.states.get('light.light_switch').state == 'off'
async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock): """Test the sending of RGB command with template.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'rgb_command_topic': 'test_light_rgb/rgb/set', 'rgb_command_template': '{{ "#%02x%02x%02x" | ' 'format(red, green, blue)}}', 'payload_on': 'on', 'payload_off': 'off', 'qos': 0 } } assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 64]) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_rgb/set', 'on', 0, False), mock.call('test_light_rgb/rgb/set', '#ff803f', 0, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert (255, 128, 63) == state.attributes['rgb_color']
async def test_light_service_calls(hass): """Test service calls to light.""" await async_setup_component(hass, 'switch', {'switch': [{ 'platform': 'demo' }]}) await async_setup_component(hass, 'light', { 'light': [{ 'platform': 'switch', 'entity_id': 'switch.decorative_lights' }] }) await hass.async_block_till_done() assert hass.states.get('light.light_switch').state == 'on' common.async_toggle(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'off' assert hass.states.get('light.light_switch').state == 'off' common.async_turn_on(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'on' assert hass.states.get('light.light_switch').state == 'on' common.async_turn_off(hass, 'light.light_switch') await hass.async_block_till_done() assert hass.states.get('switch.decorative_lights').state == 'off' assert hass.states.get('light.light_switch').state == 'off'
async def test_sending_mqtt_color_temp_command_with_template(hass, mqtt_mock): """Test the sending of Color Temp command with template.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_color_temp/set', 'color_temp_command_topic': 'test_light_color_temp/color_temp/set', 'color_temp_command_template': '{{ (1000 / value) | round(0) }}', 'payload_on': 'on', 'payload_off': 'off', 'qos': 0 }} assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', color_temp=100) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_color_temp/set', 'on', 0, False), mock.call('test_light_color_temp/color_temp/set', '10', 0, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert 100 == state.attributes['color_temp']
async def test_on_command_first(hass, mqtt_mock): """Test on command being sent before brightness.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', 'on_command_type': 'first', } } assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', brightness=50) await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/set: 'ON' # test_light/bright: 50 mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/set', 'ON', 0, False), mock.call('test_light/bright', 50, 0, False), ], any_order=True) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0, False)
async def test_on_command_rgb(hass, mqtt_mock): """Test on command in RGB brightness mode.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'rgb_command_topic': "test_light/rgb", } } assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', brightness=127) await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/rgb: '127,127,127' # test_light/set: 'ON' mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/rgb', '127,127,127', 0, False), mock.call('test_light/set', 'ON', 0, False), ], any_order=True) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0, False)
async def test_sending_mqtt_rgb_command_with_template(hass, mqtt_mock): """Test the sending of RGB command with template.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'rgb_command_topic': 'test_light_rgb/rgb/set', 'rgb_command_template': '{{ "#%02x%02x%02x" | ' 'format(red, green, blue)}}', 'payload_on': 'on', 'payload_off': 'off', 'qos': 0 }} assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 64]) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_rgb/set', 'on', 0, False), mock.call('test_light_rgb/rgb/set', '#ff803f', 0, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert (255, 128, 63) == state.attributes['rgb_color']
async def test_on_command_first(hass, mqtt_mock): """Test on command being sent before brightness.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', 'on_command_type': 'first', }} assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', brightness=50) await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/set: 'ON' # test_light/bright: 50 mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/set', 'ON', 0, False), mock.call('test_light/bright', 50, 0, False), ], any_order=True) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False)
async def test_service_calls(hass): """Test service calls.""" await async_setup_component(hass, 'light', {'light': [ {'platform': 'demo'}, {'platform': 'group', 'entities': ['light.bed_light', 'light.ceiling_lights', 'light.kitchen_lights']} ]}) await hass.async_block_till_done() assert hass.states.get('light.light_group').state == 'on' common.async_toggle(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off' common.async_turn_on(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'on' assert hass.states.get('light.ceiling_lights').state == 'on' assert hass.states.get('light.kitchen_lights').state == 'on' common.async_turn_off(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off' common.async_turn_on(hass, 'light.light_group', brightness=128, effect='Random', rgb_color=(42, 255, 255)) await hass.async_block_till_done() state = hass.states.get('light.bed_light') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255) state = hass.states.get('light.ceiling_lights') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255) state = hass.states.get('light.kitchen_lights') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255)
async def test_lights_turn_off_when_everyone_leaves(hass, scanner): """Test lights turn off when everyone leaves the house.""" common_light.async_turn_on(hass) await hass.async_block_till_done() assert await async_setup_component(hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) hass.states.async_set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) await hass.async_block_till_done() assert not light.is_on(hass)
async def test_state_attributes(hass): """Test light state attributes.""" common.async_turn_on(hass, ENTITY_LIGHT, xy_color=(.4, .4), brightness=25) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert light.is_on(hass, ENTITY_LIGHT) assert (0.4, 0.4) == state.attributes.get(light.ATTR_XY_COLOR) assert 25 == state.attributes.get(light.ATTR_BRIGHTNESS) assert (255, 234, 164) == state.attributes.get(light.ATTR_RGB_COLOR) assert 'rainbow' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on(hass, ENTITY_LIGHT, rgb_color=(251, 253, 255), white_value=254) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 254 == state.attributes.get(light.ATTR_WHITE_VALUE) assert (250, 252, 255) == state.attributes.get(light.ATTR_RGB_COLOR) assert (0.319, 0.326) == state.attributes.get(light.ATTR_XY_COLOR) common.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect='none') await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 400 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 153 == state.attributes.get(light.ATTR_MIN_MIREDS) assert 500 == state.attributes.get(light.ATTR_MAX_MIREDS) assert 'none' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 333 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 127 == state.attributes.get(light.ATTR_BRIGHTNESS)
async def test_state_attributes(hass): """Test light state attributes.""" common.async_turn_on( hass, ENTITY_LIGHT, xy_color=(.4, .4), brightness=25) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert light.is_on(hass, ENTITY_LIGHT) assert (0.4, 0.4) == state.attributes.get(light.ATTR_XY_COLOR) assert 25 == state.attributes.get(light.ATTR_BRIGHTNESS) assert (255, 234, 164) == state.attributes.get(light.ATTR_RGB_COLOR) assert 'rainbow' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on( hass, ENTITY_LIGHT, rgb_color=(251, 253, 255), white_value=254) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 254 == state.attributes.get(light.ATTR_WHITE_VALUE) assert (250, 252, 255) == state.attributes.get(light.ATTR_RGB_COLOR) assert (0.319, 0.326) == state.attributes.get(light.ATTR_XY_COLOR) common.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect='none') await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 400 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 153 == state.attributes.get(light.ATTR_MIN_MIREDS) assert 500 == state.attributes.get(light.ATTR_MAX_MIREDS) assert 'none' == state.attributes.get(light.ATTR_EFFECT) common.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50) await hass.async_block_till_done() state = hass.states.get(ENTITY_LIGHT) assert 333 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 127 == state.attributes.get(light.ATTR_BRIGHTNESS)
async def test_lights_turn_off_when_everyone_leaves(hass, scanner): """Test lights turn off when everyone leaves the house.""" common_light.async_turn_on(hass) await hass.async_block_till_done() assert await async_setup_component( hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}}) hass.states.async_set(device_tracker.ENTITY_ID_ALL_DEVICES, STATE_NOT_HOME) await hass.async_block_till_done() assert not light.is_on(hass)
async def test_flash_short_and_long(hass, mqtt_mock): """Test for flash length being sent when included.""" assert await async_setup_component( hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'schema': 'json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'flash_time_short': 5, 'flash_time_long': 15, 'qos': 0 } }) state = hass.states.get('light.test') assert STATE_OFF == state.state assert 40 == state.attributes.get(ATTR_SUPPORTED_FEATURES) common.async_turn_on(hass, 'light.test', flash='short') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', JsonValidator('{"state": "ON", "flash": 5}'), 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state common.async_turn_on(hass, 'light.test', flash='long') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', JsonValidator('{"state": "ON", "flash": 15}'), 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state
async def test_on_command_brightness(hass, mqtt_mock): """Test on command being sent as only brightness.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', 'rgb_command_topic': "test_light/rgb", 'on_command_type': 'brightness', } } assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state # Turn on w/ no brightness - should set to max common.async_turn_on(hass, 'light.test') await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/bright: 255 mqtt_mock.async_publish.assert_called_once_with('test_light/bright', 255, 0, False) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0, False) mqtt_mock.async_publish.reset_mock() # Turn on w/ brightness common.async_turn_on(hass, 'light.test', brightness=50) await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light/bright', 50, 0, False) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() # Turn on w/ just a color to insure brightness gets # added and sent. common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0]) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/rgb', '255,128,0', 0, False), mock.call('test_light/bright', 50, 0, False) ], any_order=True)
async def test_transition(hass, mqtt_mock): """Test for transition time being sent when included.""" assert await async_setup_component( hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'schema': 'json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'qos': 0 } }) state = hass.states.get('light.test') assert STATE_OFF == state.state assert 40 == state.attributes.get(ATTR_SUPPORTED_FEATURES) common.async_turn_on(hass, 'light.test', transition=15) await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', JsonValidator('{"state": "ON", "transition": 15}'), 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state common.async_turn_off(hass, 'light.test', transition=30) await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', JsonValidator('{"state": "OFF", "transition": 30}'), 0, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_OFF == state.state
async def test_on_command_brightness(hass, mqtt_mock): """Test on command being sent as only brightness.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', 'rgb_command_topic': "test_light/rgb", 'on_command_type': 'brightness', }} assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_OFF == state.state # Turn on w/ no brightness - should set to max common.async_turn_on(hass, 'light.test') await hass.async_block_till_done() # Should get the following MQTT messages. # test_light/bright: 255 mqtt_mock.async_publish.assert_called_once_with( 'test_light/bright', 255, 0, False) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False) mqtt_mock.async_publish.reset_mock() # Turn on w/ brightness common.async_turn_on(hass, 'light.test', brightness=50) await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light/bright', 50, 0, False) mqtt_mock.async_publish.reset_mock() common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() # Turn on w/ just a color to insure brightness gets # added and sent. common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0]) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light/rgb', '255,128,0', 0, False), mock.call('test_light/bright', 50, 0, False) ], any_order=True)
async def test_sending_rgb_color_with_brightness(hass, mqtt_mock): """Test light.turn_on with hs color sends rgb color parameters.""" assert await async_setup_component( hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'schema': 'json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness': True, 'rgb': True, } }) state = hass.states.get('light.test') assert STATE_OFF == state.state common.async_turn_on(hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78]) common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 0, "g": 123, "b": 255},' ' "brightness": 50}'), 0, False), mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 56, "b": 59},' ' "brightness": 50}'), 0, False), mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 128, "b": 0},' ' "white_value": 80}'), 0, False), ], any_order=True)
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): """Test the sending of command in optimistic mode.""" fake_state = ha.State( 'light.test', 'on', { 'brightness': 95, 'hs_color': [100, 100], 'effect': 'random', 'color_temp': 100, 'white_value': 50 }) with patch( 'homeassistant.helpers.restore_state.RestoreEntity' '.async_get_last_state', return_value=mock_coro(fake_state)): assert await async_setup_component( hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'schema': 'json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness': True, 'color_temp': True, 'effect': True, 'hs': True, 'rgb': True, 'xy': True, 'white_value': True, 'qos': 2 } }) state = hass.states.get('light.test') assert STATE_ON == state.state assert 95 == state.attributes.get('brightness') assert (100, 100) == state.attributes.get('hs_color') assert 'random' == state.attributes.get('effect') assert 100 == state.attributes.get('color_temp') assert 50 == state.attributes.get('white_value') assert 191 == state.attributes.get(ATTR_SUPPORTED_FEATURES) assert state.attributes.get(ATTR_ASSUMED_STATE) common.async_turn_on(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set', '{"state": "ON"}', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set', '{"state": "OFF"}', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_OFF == state.state mqtt_mock.reset_mock() common.async_turn_on(hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78]) common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,' ' "x": 0.14, "y": 0.131, "h": 210.824, "s": 100.0},' ' "brightness": 50}'), 2, False), mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,' ' "x": 0.654, "y": 0.301, "h": 359.0, "s": 78.0},' ' "brightness": 50}'), 2, False), mock.call( 'test_light_rgb/set', JsonValidator( '{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,' ' "x": 0.611, "y": 0.375, "h": 30.118, "s": 100.0},' ' "white_value": 80}'), 2, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert (255, 128, 0) == state.attributes['rgb_color'] assert 50 == state.attributes['brightness'] assert (30.118, 100) == state.attributes['hs_color'] assert 80 == state.attributes['white_value'] assert (0.611, 0.375) == state.attributes['xy_color']
async def test_service_calls(hass): """Test service calls.""" await async_setup_component( hass, 'light', { 'light': [{ 'platform': 'demo' }, { 'platform': 'group', 'entities': [ 'light.bed_light', 'light.ceiling_lights', 'light.kitchen_lights' ] }] }) await hass.async_block_till_done() assert hass.states.get('light.light_group').state == 'on' common.async_toggle(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off' common.async_turn_on(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'on' assert hass.states.get('light.ceiling_lights').state == 'on' assert hass.states.get('light.kitchen_lights').state == 'on' common.async_turn_off(hass, 'light.light_group') await hass.async_block_till_done() assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off' common.async_turn_on(hass, 'light.light_group', brightness=128, effect='Random', rgb_color=(42, 255, 255)) await hass.async_block_till_done() state = hass.states.get('light.bed_light') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255) state = hass.states.get('light.ceiling_lights') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255) state = hass.states.get('light.kitchen_lights') assert state.state == 'on' assert state.attributes['brightness'] == 128 assert state.attributes['effect'] == 'Random' assert state.attributes['rgb_color'] == (42, 255, 255)
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): """Test the sending of command in optimistic mode.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness_command_topic': 'test_light_rgb/brightness/set', 'rgb_command_topic': 'test_light_rgb/rgb/set', 'color_temp_command_topic': 'test_light_rgb/color_temp/set', 'effect_command_topic': 'test_light_rgb/effect/set', 'hs_command_topic': 'test_light_rgb/hs/set', 'white_value_command_topic': 'test_light_rgb/white_value/set', 'xy_command_topic': 'test_light_rgb/xy/set', 'effect_list': ['colorloop', 'random'], 'qos': 2, 'payload_on': 'on', 'payload_off': 'off' } } fake_state = ha.State( 'light.test', 'on', { 'brightness': 95, 'hs_color': [100, 100], 'effect': 'random', 'color_temp': 100, 'white_value': 50 }) with patch( 'homeassistant.helpers.restore_state.RestoreEntity' '.async_get_last_state', return_value=mock_coro(fake_state)): with assert_setup_component(1, light.DOMAIN): assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_ON == state.state assert 95 == state.attributes.get('brightness') assert (100, 100) == state.attributes.get('hs_color') assert 'random' == state.attributes.get('effect') assert 100 == state.attributes.get('color_temp') assert 50 == state.attributes.get('white_value') assert state.attributes.get(ATTR_ASSUMED_STATE) common.async_turn_on(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set', 'on', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set', 'off', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_OFF == state.state mqtt_mock.reset_mock() common.async_turn_on(hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78]) common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_rgb/set', 'on', 2, False), mock.call('test_light_rgb/rgb/set', '255,128,0', 2, False), mock.call('test_light_rgb/brightness/set', 50, 2, False), mock.call('test_light_rgb/hs/set', '359.0,78.0', 2, False), mock.call('test_light_rgb/white_value/set', 80, 2, False), mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert (255, 128, 0) == state.attributes['rgb_color'] assert 50 == state.attributes['brightness'] assert (30.118, 100) == state.attributes['hs_color'] assert 80 == state.attributes['white_value'] assert (0.611, 0.375) == state.attributes['xy_color']
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): """Test the sending of command in optimistic mode.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness_command_topic': 'test_light_rgb/brightness/set', 'rgb_command_topic': 'test_light_rgb/rgb/set', 'color_temp_command_topic': 'test_light_rgb/color_temp/set', 'effect_command_topic': 'test_light_rgb/effect/set', 'hs_command_topic': 'test_light_rgb/hs/set', 'white_value_command_topic': 'test_light_rgb/white_value/set', 'xy_command_topic': 'test_light_rgb/xy/set', 'effect_list': ['colorloop', 'random'], 'qos': 2, 'payload_on': 'on', 'payload_off': 'off' }} fake_state = ha.State('light.test', 'on', {'brightness': 95, 'hs_color': [100, 100], 'effect': 'random', 'color_temp': 100, 'white_value': 50}) with patch('homeassistant.components.light.mqtt.async_get_last_state', return_value=mock_coro(fake_state)): with assert_setup_component(1, light.DOMAIN): assert await async_setup_component(hass, light.DOMAIN, config) state = hass.states.get('light.test') assert STATE_ON == state.state assert 95 == state.attributes.get('brightness') assert (100, 100) == state.attributes.get('hs_color') assert 'random' == state.attributes.get('effect') assert 100 == state.attributes.get('color_temp') assert 50 == state.attributes.get('white_value') assert state.attributes.get(ATTR_ASSUMED_STATE) common.async_turn_on(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_ON == state.state common.async_turn_off(hass, 'light.test') await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with( 'test_light_rgb/set', 'off', 2, False) mqtt_mock.async_publish.reset_mock() state = hass.states.get('light.test') assert STATE_OFF == state.state mqtt_mock.reset_mock() common.async_turn_on(hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78]) common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) await hass.async_block_till_done() mqtt_mock.async_publish.assert_has_calls([ mock.call('test_light_rgb/set', 'on', 2, False), mock.call('test_light_rgb/rgb/set', '255,128,0', 2, False), mock.call('test_light_rgb/brightness/set', 50, 2, False), mock.call('test_light_rgb/hs/set', '359.0,78.0', 2, False), mock.call('test_light_rgb/white_value/set', 80, 2, False), mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False), ], any_order=True) state = hass.states.get('light.test') assert STATE_ON == state.state assert (255, 128, 0) == state.attributes['rgb_color'] assert 50 == state.attributes['brightness'] assert (30.118, 100) == state.attributes['hs_color'] assert 80 == state.attributes['white_value'] assert (0.611, 0.375) == state.attributes['xy_color']