def setUp(self):  # pylint: disable=invalid-name
        self.hass = ha.HomeAssistant()
        loader.prepare(self.hass)
        loader.set_component('switch.test', mock_toggledevice_platform)

        mock_toggledevice_platform.init()
        self.assertTrue(switch.setup(
            self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}}
        ))

        # Switch 1 is ON, switch 2 is OFF
        self.switch_1, self.switch_2, self.switch_3 = \
            mock_toggledevice_platform.get_switches(None, None)
示例#2
0
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = ha.HomeAssistant()
        loader.prepare(self.hass)
        loader.set_component('switch.test', mock_toggledevice_platform)

        mock_toggledevice_platform.init()
        self.assertTrue(
            switch.setup(self.hass, {switch.DOMAIN: {
                ha.CONF_TYPE: 'test'
            }}))

        # Switch 1 is ON, switch 2 is OFF
        self.switch_1, self.switch_2, self.switch_3 = \
            mock_toggledevice_platform.get_switches(None, None)
    def test_setup(self):
        # Bogus config
        self.assertFalse(switch.setup(self.hass, {}))

        self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}}))

        # Test with non-existing component
        self.assertFalse(switch.setup(
            self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'nonexisting'}}
        ))

        # Test if switch component returns 0 switches
        mock_toggledevice_platform.init(True)

        self.assertEqual(
            [], mock_toggledevice_platform.get_switches(None, None))

        self.assertFalse(switch.setup(
            self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}}
        ))
    def test_setup(self):
        """ Test the setup method. """
        # Bogus config
        self.assertFalse(light.setup(self.hass, {}))

        self.assertFalse(light.setup(self.hass, {light.DOMAIN: {}}))

        # Test with non-existing component
        self.assertFalse(light.setup(
            self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'nonexisting'}}
        ))

        # Test if light component returns 0 lightes
        mock_toggledevice_platform.init(True)

        self.assertEqual(
            [], mock_toggledevice_platform.get_lights(None, None))

        self.assertFalse(light.setup(
            self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}}
        ))
示例#5
0
    def test_setup(self):
        # Bogus config
        self.assertFalse(switch.setup(self.hass, {}))

        self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}}))

        # Test with non-existing component
        self.assertFalse(
            switch.setup(self.hass,
                         {switch.DOMAIN: {
                             ha.CONF_TYPE: 'nonexisting'
                         }}))

        # Test if switch component returns 0 switches
        mock_toggledevice_platform.init(True)

        self.assertEqual([],
                         mock_toggledevice_platform.get_switches(None, None))

        self.assertFalse(
            switch.setup(self.hass, {switch.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)
    def test_services(self):
        """ Test the provided services. """
        mock_toggledevice_platform.init()
        self.assertTrue(
            light.setup(self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}}))

        dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None)

        # 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 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))

        # 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.assertEqual(
            {light.ATTR_XY_COLOR: util.color_RGB_to_xy(255, 255, 255)},
            data)

        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, 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 should not overwrite profile data
        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(
            {light.ATTR_BRIGHTNESS: prof_bri,
             light.ATTR_XY_COLOR: [prof_x, prof_y]},
            data)