def test_light_profiles(self): """Test light profiles.""" platform = loader.get_component('light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) 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') self.assertTrue( light.setup(self.hass, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) dev1, dev2, dev3 = platform.DEVICES light.turn_on(self.hass, dev1.entity_id, profile='test') self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual( { light.ATTR_XY_COLOR: (.4, .6), light.ATTR_BRIGHTNESS: 100 }, data)
def test_activate_scene(self): """Test active scene.""" test_light = loader.get_component('light.test') test_light.init() self.assertTrue(light.setup(self.hass, { light.DOMAIN: {'platform': 'test'} })) light_1, light_2 = test_light.DEVICES[0:2] light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id]) self.hass.pool.block_till_done() self.assertTrue(scene.setup(self.hass, { 'scene': [{ 'name': 'test', 'entities': { light_1.entity_id: 'on', light_2.entity_id: { 'state': 'on', 'brightness': 100, } } }] })) scene.activate(self.hass, 'scene.test') self.hass.pool.block_till_done() self.assertTrue(light_1.is_on) self.assertTrue(light_2.is_on) self.assertEqual(100, light_2.last_call('turn_on')[1].get('brightness'))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() event_decorators.HASS = self.hass self.scanner = loader.get_component('device_tracker.test').get_scanner( None, None) self.scanner.reset() self.scanner.come_home('DEV1') loader.get_component('light.test').init() self.assertTrue( device_tracker.setup( self.hass, {device_tracker.DOMAIN: { CONF_PLATFORM: 'test' }})) self.assertTrue( light.setup(self.hass, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) self.assertTrue( sun.setup(self.hass, {sun.DOMAIN: { sun.CONF_ELEVATION: 0 }}))
def test_light_profiles(self): """Test light profiles.""" platform = loader.get_component('light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) 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') self.assertTrue(light.setup( self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}} )) dev1, dev2, dev3 = platform.DEVICES light.turn_on(self.hass, dev1.entity_id, profile='test') self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual( {light.ATTR_XY_COLOR: (.4, .6), light.ATTR_BRIGHTNESS: 100}, data)
def test_config_yaml_alias_anchor(self): """Test the usage of YAML aliases and anchors. The following test scene configuration is equivalent to: scene: - name: test entities: light_1: &light_1_state state: 'on' brightness: 100 light_2: *light_1_state When encountering a YAML alias/anchor, the PyYAML parser will use a reference to the original dictionary, instead of creating a copy, so care needs to be taken to not modify the original. """ test_light = loader.get_component('light.test') test_light.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: { 'platform': 'test' }})) light_1, light_2 = test_light.DEVICES[0:2] light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id]) self.hass.pool.block_till_done() entity_state = { 'state': 'on', 'brightness': 100, } self.assertTrue( scene.setup( self.hass, { 'scene': [{ 'name': 'test', 'entities': { light_1.entity_id: entity_state, light_2.entity_id: entity_state, } }] })) scene.activate(self.hass, 'scene.test') self.hass.pool.block_till_done() self.assertTrue(light_1.is_on) self.assertTrue(light_2.is_on) self.assertEqual(100, light_1.last_call('turn_on')[1].get('brightness')) self.assertEqual(100, light_2.last_call('turn_on')[1].get('brightness'))
def test_config_yaml_alias_anchor(self): """Test the usage of YAML aliases and anchors. The following test scene configuration is equivalent to: scene: - name: test entities: light_1: &light_1_state state: 'on' brightness: 100 light_2: *light_1_state When encountering a YAML alias/anchor, the PyYAML parser will use a reference to the original dictionary, instead of creating a copy, so care needs to be taken to not modify the original. """ test_light = loader.get_component('light.test') test_light.init() self.assertTrue(light.setup(self.hass, { light.DOMAIN: {'platform': 'test'} })) light_1, light_2 = test_light.DEVICES[0:2] light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id]) self.hass.pool.block_till_done() entity_state = { 'state': 'on', 'brightness': 100, } self.assertTrue(scene.setup(self.hass, { 'scene': [{ 'name': 'test', 'entities': { light_1.entity_id: entity_state, light_2.entity_id: entity_state, } }] })) scene.activate(self.hass, 'scene.test') self.hass.pool.block_till_done() self.assertTrue(light_1.is_on) self.assertTrue(light_2.is_on) self.assertEqual(100, light_1.last_call('turn_on')[1].get('brightness')) self.assertEqual(100, light_2.last_call('turn_on')[1].get('brightness'))
def test_broken_light_profiles(self): """Test light profiles.""" platform = loader.get_component('light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) # 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') self.assertFalse(light.setup( self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}} ))
def test_broken_light_profiles(self): """Test light profiles.""" platform = loader.get_component('light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) # 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') self.assertFalse( light.setup(self.hass, {light.DOMAIN: { CONF_PLATFORM: 'test' }}))
def test_flux_with_custom_colortemps(self): """Test the flux with custom start and stop colortemps.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1 = platform.DEVICES[0] # Verify initial state of light state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=17, minute=30, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch('blumate.util.dt.now', return_value=test_time): with patch('blumate.components.sun.next_rising', return_value=sunrise_time): with patch('blumate.components.sun.next_setting', return_value=sunset_time): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [dev1.entity_id], 'start_colortemp': '1000', 'stop_colortemp': '6000' } }) turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, 'switch.flux') self.hass.pool.block_till_done() fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 167) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.461, 0.389])
def test_activate_scene(self): """Test active scene.""" test_light = loader.get_component('light.test') test_light.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: { 'platform': 'test' }})) light_1, light_2 = test_light.DEVICES[0:2] light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id]) self.hass.pool.block_till_done() self.assertTrue( scene.setup( self.hass, { 'scene': [{ 'name': 'test', 'entities': { light_1.entity_id: 'on', light_2.entity_id: { 'state': 'on', 'brightness': 100, } } }] })) scene.activate(self.hass, 'scene.test') self.hass.pool.block_till_done() self.assertTrue(light_1.is_on) self.assertTrue(light_2.is_on) self.assertEqual(100, light_2.last_call('turn_on')[1].get('brightness'))
def test_flux_when_switch_is_off(self): """Test the flux switch when it is off.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1 = platform.DEVICES[0] # Verify initial state of light state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=10, minute=30, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch('blumate.util.dt.now', return_value=test_time): with patch('blumate.components.sun.next_rising', return_value=sunrise_time): with patch('blumate.components.sun.next_setting', return_value=sunset_time): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [dev1.entity_id] } }) turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() self.assertEqual(0, len(turn_on_calls))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() event_decorators.HASS = self.hass self.scanner = loader.get_component( 'device_tracker.test').get_scanner(None, None) self.scanner.reset() self.scanner.come_home('DEV1') loader.get_component('light.test').init() self.assertTrue(device_tracker.setup(self.hass, { device_tracker.DOMAIN: {CONF_PLATFORM: 'test'} })) self.assertTrue(light.setup(self.hass, { light.DOMAIN: {CONF_PLATFORM: 'test'} })) self.assertTrue(sun.setup( self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}))
def test_services(self): """Test the provided services.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) dev1, dev2, dev3 = platform.DEVICES # 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)) # Test basic turn_on, turn_off, toggle services light.turn_off(self.hass, entity_id=dev1.entity_id) light.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() self.assertFalse(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)) # toggle all lights light.toggle(self.hass) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() self.assertFalse(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)) # Ensure all attributes process correctly light.turn_on(self.hass, dev1.entity_id, transition=10, brightness=20) light.turn_on(self.hass, dev2.entity_id, rgb_color=(255, 255, 255)) light.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6)) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual({ light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20 }, data) method, data = dev2.last_call('turn_on') self.assertEquals(data[light.ATTR_RGB_COLOR], (255, 255, 255)) method, data = dev3.last_call('turn_on') self.assertEqual({light.ATTR_XY_COLOR: (.4, .6)}, data) # One of the light profiles prof_name, prof_x, prof_y, prof_bri = 'relax', 0.5119, 0.4147, 144 # Test light profiles light.turn_on(self.hass, dev1.entity_id, profile=prof_name) # Specify a profile and attributes to overwrite it light.turn_on(self.hass, dev2.entity_id, profile=prof_name, brightness=100, xy_color=(.4, .6)) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual( { light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_XY_COLOR: (prof_x, prof_y) }, data) method, data = dev2.last_call('turn_on') self.assertEqual( { light.ATTR_BRIGHTNESS: 100, light.ATTR_XY_COLOR: (.4, .6) }, data) # Test shitty data light.turn_on(self.hass) light.turn_on(self.hass, dev1.entity_id, profile="nonexisting") light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5]) light.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2]) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual({}, data) method, data = dev2.last_call('turn_on') self.assertEqual({}, data) method, data = dev3.last_call('turn_on') self.assertEqual({}, data) # faulty attributes will not trigger a service call light.turn_on(self.hass, dev1.entity_id, profile=prof_name, brightness='bright', rgb_color='yellowish') self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual({}, data)
def test_services(self): """Test the provided services.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1, dev2, dev3 = platform.DEVICES # 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)) # Test basic turn_on, turn_off, toggle services light.turn_off(self.hass, entity_id=dev1.entity_id) light.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() self.assertFalse(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)) # toggle all lights light.toggle(self.hass) self.hass.pool.block_till_done() 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) self.hass.pool.block_till_done() self.assertFalse(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)) # Ensure all attributes process correctly light.turn_on(self.hass, dev1.entity_id, transition=10, brightness=20) light.turn_on( self.hass, dev2.entity_id, rgb_color=(255, 255, 255)) light.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6)) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual( {light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20}, data) method, data = dev2.last_call('turn_on') self.assertEquals(data[light.ATTR_RGB_COLOR], (255, 255, 255)) method, data = dev3.last_call('turn_on') self.assertEqual({light.ATTR_XY_COLOR: (.4, .6)}, data) # One of the light profiles prof_name, prof_x, prof_y, prof_bri = 'relax', 0.5119, 0.4147, 144 # Test light profiles light.turn_on(self.hass, dev1.entity_id, profile=prof_name) # Specify a profile and attributes to overwrite it light.turn_on( self.hass, dev2.entity_id, profile=prof_name, brightness=100, xy_color=(.4, .6)) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual( {light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_XY_COLOR: (prof_x, prof_y)}, data) method, data = dev2.last_call('turn_on') self.assertEqual( {light.ATTR_BRIGHTNESS: 100, light.ATTR_XY_COLOR: (.4, .6)}, data) # Test shitty data light.turn_on(self.hass) light.turn_on(self.hass, dev1.entity_id, profile="nonexisting") light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5]) light.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2]) self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual({}, data) method, data = dev2.last_call('turn_on') self.assertEqual({}, data) method, data = dev3.last_call('turn_on') self.assertEqual({}, data) # faulty attributes will not trigger a service call light.turn_on( self.hass, dev1.entity_id, profile=prof_name, brightness='bright', rgb_color='yellowish') self.hass.pool.block_till_done() method, data = dev1.last_call('turn_on') self.assertEqual({}, data)
def test_flux_with_multiple_lights(self): """Test the flux switch with multiple light entities.""" platform = loader.get_component('light.test') platform.init() self.assertTrue( light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}})) dev1, dev2, dev3 = platform.DEVICES light.turn_on(self.hass, entity_id=dev2.entity_id) self.hass.pool.block_till_done() light.turn_on(self.hass, entity_id=dev3.entity_id) self.hass.pool.block_till_done() state = self.hass.states.get(dev1.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev2.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) state = self.hass.states.get(dev3.entity_id) self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('brightness')) test_time = dt_util.now().replace(hour=12, minute=0, second=0) sunset_time = test_time.replace(hour=17, minute=0, second=0) sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1) with patch('blumate.util.dt.now', return_value=test_time): with patch('blumate.components.sun.next_rising', return_value=sunrise_time): with patch('blumate.components.sun.next_setting', return_value=sunset_time): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'flux', 'name': 'flux', 'lights': [dev1.entity_id, dev2.entity_id, dev3.entity_id] } }) turn_on_calls = mock_service( self.hass, light.DOMAIN, SERVICE_TURN_ON) switch.turn_on(self.hass, 'switch.flux') self.hass.pool.block_till_done() fire_time_changed(self.hass, test_time) self.hass.pool.block_till_done() call = turn_on_calls[-1] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386]) call = turn_on_calls[-2] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386]) call = turn_on_calls[-3] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])