def test_on_command_last(self): """Test on command being sent after brightness.""" config = { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', } } with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'light.test', brightness=50) self.hass.block_till_done() # Should get the following MQTT messages. # test_light/bright: 50 # test_light/set: 'ON' self.mock_publish.async_publish.assert_has_calls([ mock.call('test_light/bright', 50, 0, False), mock.call('test_light/set', 'ON', 0, False), ], any_order=True) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False)
def test_transition(self): """Test for transition time being sent when included.""" with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_template', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'command_on_template': 'on,{{ transition }}', 'command_off_template': 'off,{{ transition|d }}' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) # transition on common.turn_on(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on,10', 0, False) self.mock_publish.async_publish.reset_mock() # transition off common.turn_off(self.hass, 'light.test', transition=4) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'off,4', 0, False)
def test_on_command_last(self): """Test on command being sent after brightness.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', }} with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'light.test', brightness=50) self.hass.block_till_done() # Should get the following MQTT messages. # test_light/bright: 50 # test_light/set: 'ON' self.mock_publish.async_publish.assert_has_calls([ mock.call('test_light/bright', 50, 0, False), mock.call('test_light/set', 'ON', 0, False), ], any_order=True) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False)
def test_on_command_brightness(self): """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', } } with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) # Turn on w/ no brightness - should set to max common.turn_on(self.hass, 'light.test') self.hass.block_till_done() # Should get the following MQTT messages. # test_light/bright: 255 self.mock_publish.async_publish.assert_called_once_with( 'test_light/bright', 255, 0, False) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False) self.mock_publish.async_publish.reset_mock() # Turn on w/ brightness common.turn_on(self.hass, 'light.test', brightness=50) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/bright', 50, 0, False) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() # Turn on w/ just a color to insure brightness gets # added and sent. common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0]) self.hass.block_till_done() self.mock_publish.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)
def test_on_command_brightness(self): """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', }} with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) # Turn on w/ no brightness - should set to max common.turn_on(self.hass, 'light.test') self.hass.block_till_done() # Should get the following MQTT messages. # test_light/bright: 255 self.mock_publish.async_publish.assert_called_once_with( 'test_light/bright', 255, 0, False) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/set', 'OFF', 0, False) self.mock_publish.async_publish.reset_mock() # Turn on w/ brightness common.turn_on(self.hass, 'light.test', brightness=50) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light/bright', 50, 0, False) self.mock_publish.async_publish.reset_mock() common.turn_off(self.hass, 'light.test') self.hass.block_till_done() # Turn on w/ just a color to insure brightness gets # added and sent. common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0]) self.hass.block_till_done() self.mock_publish.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)
def test_on_off(self): """Test turning the light on and off.""" assert self.light().state == 'off' assert self.other_light().state == 'off' assert not light.is_on(self.hass, ENTITY_LIGHT) common.turn_on(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER) common.turn_off(self.hass, ENTITY_LIGHT) self.hass.block_till_done() self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
def test_lights_turn_on_when_coming_home_after_sun_set(self): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): common_light.turn_off(self.hass) self.hass.block_till_done() assert setup_component(self.hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) self.hass.states.set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) self.hass.block_till_done() assert light.is_on(self.hass)
def test_lights_turn_on_when_coming_home_after_sun_set(self): """Test lights turn on when coming home after sun set.""" test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): common_light.turn_off(self.hass) self.hass.block_till_done() self.assertTrue(setup_component( self.hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}})) self.hass.states.set( device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass))
def test_transition(self): """Test for transition time being sent when included.""" assert setup_component( self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', 'qos': 0 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES)) common.turn_on(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(0, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[0][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("ON", message_json["state"]) # Transition back off common.turn_off(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[1][1][0]) self.assertEqual(0, self.mock_publish.async_publish.mock_calls[1][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[1][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[1][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("OFF", message_json["state"])
def test_lights_on_when_sun_sets(self): """Test lights go on when there is someone home and the sun sets.""" test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): assert setup_component(self.hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}) common_light.turn_off(self.hass) self.hass.block_till_done() test_time = test_time.replace(hour=3) with patch('homeassistant.util.dt.utcnow', return_value=test_time): fire_time_changed(self.hass, test_time) self.hass.block_till_done() assert light.is_on(self.hass)
def test_transition(self): """Test for transition time being sent when included.""" assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', 'qos': 0 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES)) common.turn_on(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(0, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[0][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("ON", message_json["state"]) # Transition back off common.turn_off(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[1][1][0]) self.assertEqual(0, self.mock_publish.async_publish.mock_calls[1][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[1][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[1][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("OFF", message_json["state"])
def test_lights_on_when_sun_sets(self): """Test lights go on when there is someone home and the sun sets.""" test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=test_time): self.assertTrue(setup_component( self.hass, device_sun_light_trigger.DOMAIN, { device_sun_light_trigger.DOMAIN: {}})) common_light.turn_off(self.hass) self.hass.block_till_done() test_time = test_time.replace(hour=3) with patch('homeassistant.util.dt.utcnow', return_value=test_time): fire_time_changed(self.hass, test_time) self.hass.block_till_done() self.assertTrue(light.is_on(self.hass))
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() test_light = loader.get_component(self.hass, 'light.test') test_light.init() self.assertTrue(setup_component(self.hass, light.DOMAIN, { light.DOMAIN: {'platform': 'test'} })) self.light_1, self.light_2 = test_light.DEVICES[0:2] common_light.turn_off( self.hass, [self.light_1.entity_id, self.light_2.entity_id]) self.hass.block_till_done() self.assertFalse(self.light_1.is_on) self.assertFalse(self.light_2.is_on)
def test_off_action(self): """Test off action.""" assert setup.setup_component( self.hass, "light", { "light": { "platform": "template", "lights": { "test_template_light": { "value_template": "{{states.light.test_state.state}}", "turn_on": { "service": "light.turn_on", "entity_id": "light.test_state", }, "turn_off": {"service": "test.automation"}, "set_level": { "service": "light.turn_on", "data_template": { "entity_id": "light.test_state", "brightness": "{{brightness}}", }, }, } }, } }, ) self.hass.block_till_done() self.hass.start() self.hass.block_till_done() self.hass.states.set("light.test_state", STATE_ON) self.hass.block_till_done() state = self.hass.states.get("light.test_template_light") assert state.state == STATE_ON common.turn_off(self.hass, "light.test_template_light") self.hass.block_till_done() assert len(self.calls) == 1
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() test_light = getattr(self.hass.components, "test.light") test_light.init() assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { "platform": "test" }}) self.light_1, self.light_2 = test_light.ENTITIES[0:2] common_light.turn_off(self.hass, [self.light_1.entity_id, self.light_2.entity_id]) self.hass.block_till_done() assert not self.light_1.is_on assert not self.light_2.is_on
def test_off_action(self): """Test off action.""" assert setup.setup_component(self.hass, 'light', { 'light': { 'platform': 'template', 'lights': { 'test_template_light': { 'value_template': "{{states.light.test_state.state}}", 'turn_on': { 'service': 'light.turn_on', 'entity_id': 'light.test_state' }, 'turn_off': { 'service': 'test.automation', }, 'set_level': { 'service': 'light.turn_on', 'data_template': { 'entity_id': 'light.test_state', 'brightness': '{{brightness}}' } } } } } }) self.hass.start() self.hass.block_till_done() self.hass.states.set('light.test_state', STATE_ON) self.hass.block_till_done() state = self.hass.states.get('light.test_template_light') assert state.state == STATE_ON common.turn_off(self.hass, 'light.test_template_light') self.hass.block_till_done() assert len(self.calls) == 1
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) self.hass.states.set('light.test', STATE_OFF) self.assertFalse(light.is_on(self.hass, 'light.test')) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) self.assertTrue(light.is_on(self.hass)) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) self.assertFalse(light.is_on(self.hass)) # Test turn_on turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) common.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', profile='profile_val', color_name='color_name_val', white_value='white_val') self.hass.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) 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)) self.assertEqual( 'color_name_val', call.data.get(light.ATTR_COLOR_NAME)) self.assertEqual('white_val', call.data.get(light.ATTR_WHITE_VALUE)) # Test turn_off turn_off_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_OFF) common.turn_off( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) # Test toggle toggle_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TOGGLE) common.toggle( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() 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])
def test_optimistic(self): """Test 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.components.light.mqtt_template' '.async_get_last_state', return_value=mock_coro(fake_state)): with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_template', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'command_on_template': 'on,' '{{ brightness|d }},' '{{ color_temp|d }},' '{{ white_value|d }},' '{{ red|d }}-' '{{ green|d }}-' '{{ blue|d }}', 'command_off_template': 'off', 'effect_list': ['colorloop', 'random'], 'effect_command_topic': 'test_light_rgb/effect/set', 'qos': 2 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(95, state.attributes.get('brightness')) self.assertEqual((100, 100), state.attributes.get('hs_color')) self.assertEqual('random', state.attributes.get('effect')) self.assertEqual(100, state.attributes.get('color_temp')) self.assertEqual(50, state.attributes.get('white_value')) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) # turn on the light common.turn_on(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on,,,,--', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) # turn the light off common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'off', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) # turn on the light with brightness, color common.turn_on(self.hass, 'light.test', brightness=50, rgb_color=[75, 75, 75]) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on,50,,,50-50-50', 2, False) self.mock_publish.async_publish.reset_mock() # turn on the light with color temp and white val common.turn_on(self.hass, 'light.test', color_temp=200, white_value=139) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on,,200,139,--', 2, False) # check the state state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual((255, 255, 255), state.attributes['rgb_color']) self.assertEqual(50, state.attributes['brightness']) self.assertEqual(200, state.attributes['color_temp']) self.assertEqual(139, state.attributes['white_value'])
def test_sending_mqtt_commands_and_optimistic(self): """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.components.light.mqtt_json' '.async_get_last_state', return_value=mock_coro(fake_state)): assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness': True, 'color_temp': True, 'effect': True, 'rgb': True, 'white_value': True, 'qos': 2 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(95, state.attributes.get('brightness')) self.assertEqual((100, 100), state.attributes.get('hs_color')) self.assertEqual('random', state.attributes.get('effect')) self.assertEqual(100, state.attributes.get('color_temp')) self.assertEqual(50, state.attributes.get('white_value')) self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', '{"state": "ON"}', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', '{"state": "OFF"}', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'light.test', brightness=50, color_temp=155, effect='colorloop', white_value=170) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(2, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[0][1][1]) self.assertEqual(50, message_json["brightness"]) self.assertEqual(155, message_json["color_temp"]) self.assertEqual('colorloop', message_json["effect"]) self.assertEqual(170, message_json["white_value"]) self.assertEqual("ON", message_json["state"]) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes['brightness']) self.assertEqual(155, state.attributes['color_temp']) self.assertEqual('colorloop', state.attributes['effect']) self.assertEqual(170, state.attributes['white_value']) # Test a color command common.turn_on(self.hass, 'light.test', brightness=50, hs_color=(125, 100)) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(2, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[1][1][1]) self.assertEqual(50, message_json["brightness"]) self.assertEqual({ 'r': 0, 'g': 255, 'b': 21, }, message_json["color"]) self.assertEqual("ON", message_json["state"]) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes['brightness']) self.assertEqual((125, 100), state.attributes['hs_color'])
def test_services(self): """Test the provided services.""" platform = loader.get_component(self.hass, 'light.test') platform.init() assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}) dev1, dev2, dev3 = platform.DEVICES # Test init assert light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # Test basic turn_on, turn_off, toggle services common.turn_off(self.hass, entity_id=dev1.entity_id) common.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) # turn on all lights common.turn_on(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) assert light.is_on(self.hass, dev3.entity_id) # turn off all lights common.turn_off(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) assert light.is_on(self.hass, dev3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # Ensure all attributes process correctly common.turn_on(self.hass, dev1.entity_id, transition=10, brightness=20, color_name='blue') common.turn_on( self.hass, dev2.entity_id, rgb_color=(255, 255, 255), white_value=255) common.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6)) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert { light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20, light.ATTR_HS_COLOR: (240, 100), } == data _, data = dev2.last_call('turn_on') assert { light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255, } == data _, data = dev3.last_call('turn_on') assert { light.ATTR_HS_COLOR: (71.059, 100), } == data # One of the light profiles prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144 # Test light profiles common.turn_on(self.hass, dev1.entity_id, profile=prof_name) # Specify a profile and a brightness attribute to overwrite it common.turn_on( self.hass, dev2.entity_id, profile=prof_name, brightness=100) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert { light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data _, data = dev2.last_call('turn_on') assert { light.ATTR_BRIGHTNESS: 100, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data # Test bad data common.turn_on(self.hass) common.turn_on(self.hass, dev1.entity_id, profile="nonexisting") common.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5]) common.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2]) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert {} == data _, data = dev2.last_call('turn_on') assert {} == data _, data = dev3.last_call('turn_on') assert {} == data # faulty attributes will not trigger a service call common.turn_on( self.hass, dev1.entity_id, profile=prof_name, brightness='bright') common.turn_on( self.hass, dev1.entity_id, rgb_color='yellowish') common.turn_on( self.hass, dev2.entity_id, white_value='high') self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert {} == data _, data = dev2.last_call('turn_on') assert {} == data
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set('light.test', STATE_ON) assert light.is_on(self.hass, 'light.test') self.hass.states.set('light.test', STATE_OFF) assert not light.is_on(self.hass, 'light.test') self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) assert light.is_on(self.hass) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) assert not light.is_on(self.hass) # Test turn_on turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) common.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', profile='profile_val', color_name='color_name_val', white_value='white_val') self.hass.block_till_done() assert 1 == len(turn_on_calls) call = turn_on_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert 'entity_id_val' == call.data.get(ATTR_ENTITY_ID) assert 'transition_val' == call.data.get(light.ATTR_TRANSITION) assert 'brightness_val' == call.data.get(light.ATTR_BRIGHTNESS) assert 'rgb_color_val' == call.data.get(light.ATTR_RGB_COLOR) assert 'xy_color_val' == call.data.get(light.ATTR_XY_COLOR) assert 'profile_val' == call.data.get(light.ATTR_PROFILE) assert 'color_name_val' == call.data.get(light.ATTR_COLOR_NAME) assert 'white_val' == call.data.get(light.ATTR_WHITE_VALUE) # Test turn_off turn_off_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_OFF) common.turn_off( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() assert 1 == len(turn_off_calls) call = turn_off_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert 'entity_id_val' == call.data[ATTR_ENTITY_ID] assert 'transition_val' == call.data[light.ATTR_TRANSITION] # Test toggle toggle_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TOGGLE) common.toggle( self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() assert 1 == len(toggle_calls) call = toggle_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TOGGLE == call.service assert 'entity_id_val' == call.data[ATTR_ENTITY_ID] assert 'transition_val' == call.data[light.ATTR_TRANSITION]
def test_sending_mqtt_commands_and_optimistic(self): """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.components.light.mqtt_json' '.async_get_last_state', return_value=mock_coro(fake_state)): assert setup_component( self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'brightness': True, 'color_temp': True, 'effect': True, 'rgb': True, 'white_value': True, 'qos': 2 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(95, state.attributes.get('brightness')) self.assertEqual((100, 100), state.attributes.get('hs_color')) self.assertEqual('random', state.attributes.get('effect')) self.assertEqual(100, state.attributes.get('color_temp')) self.assertEqual(50, state.attributes.get('white_value')) self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', '{"state": "ON"}', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', '{"state": "OFF"}', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'light.test', brightness=50, color_temp=155, effect='colorloop', white_value=170) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(2, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[0][1][1]) self.assertEqual(50, message_json["brightness"]) self.assertEqual(155, message_json["color_temp"]) self.assertEqual('colorloop', message_json["effect"]) self.assertEqual(170, message_json["white_value"]) self.assertEqual("ON", message_json["state"]) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes['brightness']) self.assertEqual(155, state.attributes['color_temp']) self.assertEqual('colorloop', state.attributes['effect']) self.assertEqual(170, state.attributes['white_value']) # Test a color command common.turn_on(self.hass, 'light.test', brightness=50, hs_color=(125, 100)) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.async_publish.mock_calls[0][1][0]) self.assertEqual(2, self.mock_publish.async_publish.mock_calls[0][1][2]) self.assertEqual(False, self.mock_publish.async_publish.mock_calls[0][1][3]) # Get the sent message message_json = json.loads( self.mock_publish.async_publish.mock_calls[1][1][1]) self.assertEqual(50, message_json["brightness"]) self.assertEqual({ 'r': 0, 'g': 255, 'b': 21, }, message_json["color"]) self.assertEqual("ON", message_json["state"]) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes['brightness']) self.assertEqual((125, 100), state.attributes['hs_color'])
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set('light.test', STATE_ON) self.assertTrue(light.is_on(self.hass, 'light.test')) self.hass.states.set('light.test', STATE_OFF) self.assertFalse(light.is_on(self.hass, 'light.test')) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) self.assertTrue(light.is_on(self.hass)) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) self.assertFalse(light.is_on(self.hass)) # Test turn_on turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) common.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', profile='profile_val', color_name='color_name_val', white_value='white_val') self.hass.block_till_done() self.assertEqual(1, len(turn_on_calls)) call = turn_on_calls[-1] self.assertEqual(light.DOMAIN, call.domain) 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)) self.assertEqual('color_name_val', call.data.get(light.ATTR_COLOR_NAME)) self.assertEqual('white_val', call.data.get(light.ATTR_WHITE_VALUE)) # Test turn_off turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF) common.turn_off(self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() self.assertEqual(1, len(turn_off_calls)) call = turn_off_calls[-1] self.assertEqual(light.DOMAIN, call.domain) self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) # Test toggle toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE) common.toggle(self.hass, entity_id='entity_id_val', transition='transition_val') self.hass.block_till_done() 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])
def test_sending_mqtt_commands_and_optimistic(self): """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', '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 setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(95, state.attributes.get('brightness')) self.assertEqual((100, 100), state.attributes.get('hs_color')) self.assertEqual('random', state.attributes.get('effect')) self.assertEqual(100, state.attributes.get('color_temp')) self.assertEqual(50, state.attributes.get('white_value')) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'off', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.mock_publish.reset_mock() common.turn_on(self.hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) self.hass.block_till_done() self.mock_publish.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/white_value/set', 80, 2, False), mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False), ], any_order=True) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual((255, 128, 0), state.attributes['rgb_color']) self.assertEqual(50, state.attributes['brightness']) self.assertEqual(80, state.attributes['white_value']) self.assertEqual((0.611, 0.375), state.attributes['xy_color'])
def test_services(self): """Test the provided services.""" platform = loader.get_component(self.hass, 'light.test') platform.init() assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}) dev1, dev2, dev3 = platform.DEVICES # Test init assert light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # Test basic turn_on, turn_off, toggle services common.turn_off(self.hass, entity_id=dev1.entity_id) common.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) # turn on all lights common.turn_on(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) assert light.is_on(self.hass, dev3.entity_id) # turn off all lights common.turn_off(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, dev1.entity_id) assert light.is_on(self.hass, dev2.entity_id) assert light.is_on(self.hass, dev3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, dev1.entity_id) assert not light.is_on(self.hass, dev2.entity_id) assert not light.is_on(self.hass, dev3.entity_id) # Ensure all attributes process correctly common.turn_on(self.hass, dev1.entity_id, transition=10, brightness=20, color_name='blue') common.turn_on( self.hass, dev2.entity_id, rgb_color=(255, 255, 255), white_value=255) common.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6)) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert { light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20, light.ATTR_HS_COLOR: (240, 100), } == data _, data = dev2.last_call('turn_on') assert { light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255, } == data _, data = dev3.last_call('turn_on') assert { light.ATTR_HS_COLOR: (71.059, 100), } == data # One of the light profiles prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144 # Test light profiles common.turn_on(self.hass, dev1.entity_id, profile=prof_name) # Specify a profile and a brightness attribute to overwrite it common.turn_on( self.hass, dev2.entity_id, profile=prof_name, brightness=100) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert { light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data _, data = dev2.last_call('turn_on') assert { light.ATTR_BRIGHTNESS: 100, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data # Test bad data common.turn_on(self.hass) common.turn_on(self.hass, dev1.entity_id, profile="nonexisting") common.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5]) common.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2]) self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert {} == data _, data = dev2.last_call('turn_on') assert {} == data _, data = dev3.last_call('turn_on') assert {} == data # faulty attributes will not trigger a service call common.turn_on( self.hass, dev1.entity_id, profile=prof_name, brightness='bright') common.turn_on( self.hass, dev1.entity_id, rgb_color='yellowish') common.turn_on( self.hass, dev2.entity_id, white_value='high') self.hass.block_till_done() _, data = dev1.last_call('turn_on') assert {} == data _, data = dev2.last_call('turn_on') assert {} == data
def test_services(self): """Test the provided services.""" platform = getattr(self.hass.components, "test.light") platform.init() assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: "test" }}) ent1, ent2, ent3 = platform.ENTITIES # Test init assert light.is_on(self.hass, ent1.entity_id) assert not light.is_on(self.hass, ent2.entity_id) assert not light.is_on(self.hass, ent3.entity_id) # Test basic turn_on, turn_off, toggle services common.turn_off(self.hass, entity_id=ent1.entity_id) common.turn_on(self.hass, entity_id=ent2.entity_id) self.hass.block_till_done() assert not light.is_on(self.hass, ent1.entity_id) assert light.is_on(self.hass, ent2.entity_id) # turn on all lights common.turn_on(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, ent1.entity_id) assert light.is_on(self.hass, ent2.entity_id) assert light.is_on(self.hass, ent3.entity_id) # turn off all lights common.turn_off(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, ent1.entity_id) assert not light.is_on(self.hass, ent2.entity_id) assert not light.is_on(self.hass, ent3.entity_id) # turn off all lights by setting brightness to 0 common.turn_on(self.hass) self.hass.block_till_done() common.turn_on(self.hass, brightness=0) self.hass.block_till_done() assert not light.is_on(self.hass, ent1.entity_id) assert not light.is_on(self.hass, ent2.entity_id) assert not light.is_on(self.hass, ent3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert light.is_on(self.hass, ent1.entity_id) assert light.is_on(self.hass, ent2.entity_id) assert light.is_on(self.hass, ent3.entity_id) # toggle all lights common.toggle(self.hass) self.hass.block_till_done() assert not light.is_on(self.hass, ent1.entity_id) assert not light.is_on(self.hass, ent2.entity_id) assert not light.is_on(self.hass, ent3.entity_id) # Ensure all attributes process correctly common.turn_on(self.hass, ent1.entity_id, transition=10, brightness=20, color_name="blue") common.turn_on(self.hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255) common.turn_on(self.hass, ent3.entity_id, xy_color=(0.4, 0.6)) self.hass.block_till_done() _, data = ent1.last_call("turn_on") assert { light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20, light.ATTR_HS_COLOR: (240, 100), } == data _, data = ent2.last_call("turn_on") assert { light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255 } == data _, data = ent3.last_call("turn_on") assert {light.ATTR_HS_COLOR: (71.059, 100)} == data # Ensure attributes are filtered when light is turned off common.turn_on(self.hass, ent1.entity_id, transition=10, brightness=0, color_name="blue") common.turn_on( self.hass, ent2.entity_id, brightness=0, rgb_color=(255, 255, 255), white_value=0, ) common.turn_on(self.hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6)) self.hass.block_till_done() assert not light.is_on(self.hass, ent1.entity_id) assert not light.is_on(self.hass, ent2.entity_id) assert not light.is_on(self.hass, ent3.entity_id) _, data = ent1.last_call("turn_off") assert {light.ATTR_TRANSITION: 10} == data _, data = ent2.last_call("turn_off") assert {} == data _, data = ent3.last_call("turn_off") assert {} == data # One of the light profiles prof_name, prof_h, prof_s, prof_bri = "relax", 35.932, 69.412, 144 # Test light profiles common.turn_on(self.hass, ent1.entity_id, profile=prof_name) # Specify a profile and a brightness attribute to overwrite it common.turn_on(self.hass, ent2.entity_id, profile=prof_name, brightness=100) self.hass.block_till_done() _, data = ent1.last_call("turn_on") assert { light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data _, data = ent2.last_call("turn_on") assert { light.ATTR_BRIGHTNESS: 100, light.ATTR_HS_COLOR: (prof_h, prof_s), } == data # Test bad data common.turn_on(self.hass) common.turn_on(self.hass, ent1.entity_id, profile="nonexisting") common.turn_on(self.hass, ent2.entity_id, xy_color=["bla-di-bla", 5]) common.turn_on(self.hass, ent3.entity_id, rgb_color=[255, None, 2]) self.hass.block_till_done() _, data = ent1.last_call("turn_on") assert {} == data _, data = ent2.last_call("turn_on") assert {} == data _, data = ent3.last_call("turn_on") assert {} == data # faulty attributes will not trigger a service call common.turn_on(self.hass, ent1.entity_id, profile=prof_name, brightness="bright") common.turn_on(self.hass, ent1.entity_id, rgb_color="yellowish") common.turn_on(self.hass, ent2.entity_id, white_value="high") self.hass.block_till_done() _, data = ent1.last_call("turn_on") assert {} == data _, data = ent2.last_call("turn_on") assert {} == data
def test_methods(self): """Test if methods call the services as expected.""" # Test is_on self.hass.states.set("light.test", STATE_ON) assert light.is_on(self.hass, "light.test") self.hass.states.set("light.test", STATE_OFF) assert not light.is_on(self.hass, "light.test") self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON) assert light.is_on(self.hass) self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF) assert not light.is_on(self.hass) # Test turn_on turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON) common.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", profile="profile_val", color_name="color_name_val", white_value="white_val", ) self.hass.block_till_done() assert 1 == len(turn_on_calls) call = turn_on_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert "entity_id_val" == call.data.get(ATTR_ENTITY_ID) assert "transition_val" == call.data.get(light.ATTR_TRANSITION) assert "brightness_val" == call.data.get(light.ATTR_BRIGHTNESS) assert "rgb_color_val" == call.data.get(light.ATTR_RGB_COLOR) assert "xy_color_val" == call.data.get(light.ATTR_XY_COLOR) assert "profile_val" == call.data.get(light.ATTR_PROFILE) assert "color_name_val" == call.data.get(light.ATTR_COLOR_NAME) assert "white_val" == call.data.get(light.ATTR_WHITE_VALUE) # Test turn_off turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF) common.turn_off(self.hass, entity_id="entity_id_val", transition="transition_val") self.hass.block_till_done() assert 1 == len(turn_off_calls) call = turn_off_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert "entity_id_val" == call.data[ATTR_ENTITY_ID] assert "transition_val" == call.data[light.ATTR_TRANSITION] # Test toggle toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE) common.toggle(self.hass, entity_id="entity_id_val", transition="transition_val") self.hass.block_till_done() assert 1 == len(toggle_calls) call = toggle_calls[-1] assert light.DOMAIN == call.domain assert SERVICE_TOGGLE == call.service assert "entity_id_val" == call.data[ATTR_ENTITY_ID] assert "transition_val" == call.data[light.ATTR_TRANSITION]
def test_sending_mqtt_commands_and_optimistic(self): """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', '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 setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(95, state.attributes.get('brightness')) self.assertEqual((100, 100), state.attributes.get('hs_color')) self.assertEqual('random', state.attributes.get('effect')) self.assertEqual(100, state.attributes.get('color_temp')) self.assertEqual(50, state.attributes.get('white_value')) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'on', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'light.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'test_light_rgb/set', 'off', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.mock_publish.reset_mock() common.turn_on(self.hass, 'light.test', brightness=50, xy_color=[0.123, 0.123]) common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0], white_value=80) self.hass.block_till_done() self.mock_publish.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/white_value/set', 80, 2, False), mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False), ], any_order=True) state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual((255, 128, 0), state.attributes['rgb_color']) self.assertEqual(50, state.attributes['brightness']) self.assertEqual(80, state.attributes['white_value']) self.assertEqual((0.611, 0.375), state.attributes['xy_color'])