diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 30a221cbaeb..b7e6a4fd348 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -141,6 +141,35 @@ def setup(hass, config): if not util.validate_config(config, {DOMAIN: [ha.CONF_TYPE]}, _LOGGER): return False + # Load built-in profiles and custom profiles + profile_paths = [os.path.join(os.path.dirname(__file__), + LIGHT_PROFILES_FILE), + hass.get_config_path(LIGHT_PROFILES_FILE)] + profiles = {} + + for profile_path in profile_paths: + + if os.path.isfile(profile_path): + with open(profile_path) as inp: + reader = csv.reader(inp) + + # Skip the header + next(reader, None) + + try: + for profile_id, color_x, color_y, brightness in reader: + profiles[profile_id] = (float(color_x), float(color_y), + int(brightness)) + + except ValueError: + # ValueError if not 4 values per row + # ValueError if convert to float/int failed + _LOGGER.error( + "Error parsing light profiles from %s", profile_path) + + return False + + # Load platform light_type = config[DOMAIN][ha.CONF_TYPE] light_init = get_component('light.{}'.format(light_type)) @@ -174,34 +203,6 @@ def setup(hass, config): light.entity_id = entity_id ent_to_light[entity_id] = light - # Load built-in profiles and custom profiles - profile_paths = [os.path.join(os.path.dirname(__file__), - LIGHT_PROFILES_FILE), - hass.get_config_path(LIGHT_PROFILES_FILE)] - profiles = {} - - for profile_path in profile_paths: - - if os.path.isfile(profile_path): - with open(profile_path) as inp: - reader = csv.reader(inp) - - # Skip the header - next(reader, None) - - try: - for profile_id, color_x, color_y, brightness in reader: - profiles[profile_id] = (float(color_x), float(color_y), - int(brightness)) - - except ValueError: - # ValueError if not 4 values per row - # ValueError if convert to float/int failed - _LOGGER.error( - "Error parsing light profiles from %s", profile_path) - - return False - # pylint: disable=unused-argument def update_lights_state(now): """ Update the states of all the lights. """ diff --git a/test/test_component_light.py b/test/test_component_light.py index 6bc29498742..04db6d9ec13 100644 --- a/test/test_component_light.py +++ b/test/test_component_light.py @@ -6,6 +6,7 @@ Tests switch component. """ # pylint: disable=too-many-public-methods,protected-access import unittest +import os import homeassistant as ha import homeassistant.loader as loader @@ -15,14 +16,14 @@ import homeassistant.components.light as light import mock_toggledevice_platform -from helper import mock_service +from helper import mock_service, get_test_home_assistant class TestLight(unittest.TestCase): """ Test the switch module. """ def setUp(self): # pylint: disable=invalid-name - self.hass = ha.HomeAssistant() + self.hass = get_test_home_assistant() loader.prepare(self.hass) loader.set_component('light.test', mock_toggledevice_platform) @@ -30,6 +31,11 @@ class TestLight(unittest.TestCase): """ Stop down stuff we started. """ self.hass._pool.stop() + user_light_file = self.hass.get_config_path(light.LIGHT_PROFILES_FILE) + + if os.path.isfile(user_light_file): + os.remove(user_light_file) + def test_methods(self): """ Test if methods call the services as expected. """ # Test is_on @@ -226,3 +232,41 @@ class TestLight(unittest.TestCase): self.assertFalse(light.setup( self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}} )) + + def test_light_profiles(self): + """ Test light profiles. """ + mock_toggledevice_platform.init() + + user_light_file = self.hass.get_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: {ha.CONF_TYPE: 'test'}} + )) + + # Clean up broken file + os.remove(user_light_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: {ha.CONF_TYPE: 'test'}} + )) + + dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None) + + 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)