Example #1
0
    def test_activate_scene(self):
        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'))
Example #2
0
    def test_activate_scene(self):
        """Test active scene."""
        test_light = loader.get_component('light.test')
        test_light.init()

        self.assertTrue(setup_component(self.hass, light.DOMAIN, {
            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.block_till_done()

        self.assertTrue(setup_component(self.hass, scene.DOMAIN, {
            '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.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'))
Example #3
0
    def test_on_command_first(self):
        """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',
        }}

        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)

        light.turn_on(self.hass, 'light.test', brightness=50)
        self.hass.block_till_done()

        # Should get the following MQTT messages.
        #    test_light/set: 'ON'
        #    test_light/bright: 50
        self.assertEqual(('test_light/set', 'ON', 0, False),
                         self.mock_publish.mock_calls[-4][1])
        self.assertEqual(('test_light/bright', 50, 0, False),
                         self.mock_publish.mock_calls[-2][1])

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light/set', 'OFF', 0, False),
                         self.mock_publish.mock_calls[-2][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)

        light.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')

        self.hass.pool.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))

        # Test turn_off
        turn_off_calls = mock_service(
            self.hass, light.DOMAIN, SERVICE_TURN_OFF)

        light.turn_off(
            self.hass, entity_id='entity_id_val', transition='transition_val')

        self.hass.pool.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])
    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
        light.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
        light.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)
Example #6
0
    def test_sending_mqtt_commands_and_optimistic(self):
        self.assertTrue(light.setup(self.hass, {
            'light': {
                'platform': 'mqtt',
                'name': 'test',
                'command_topic': 'test_light_rgb/set',
                'brightness_state_topic': 'test_light_rgb/brightness/status',
                'brightness_command_topic': 'test_light_rgb/brightness/set',
                'rgb_state_topic': 'test_light_rgb/rgb/status',
                'rgb_command_topic': 'test_light_rgb/rgb/set',
                'qos': 2,
                'payload_on': 'on',
                'payload_off': 'off'
            }
        }))

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
Example #7
0
    def test_activate_scene(self):
        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"))
Example #8
0
    def test_sending_mqtt_commands_and_optimistic(self): \
            # pylint: disable=invalid-name
        """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',
            'qos': 2,
            'payload_on': 'on',
            'payload_off': 'off'
        }}

        with assert_setup_component(1):
            assert setup_component(self.hass, light.DOMAIN, config)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        self.mock_publish.reset_mock()
        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
                      brightness=50, white_value=80, xy_color=[0.123, 0.123])
        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', '75,75,75', 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.123,0.123', 2, False),
        ], any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
        self.assertEqual(80, state.attributes['white_value'])
        self.assertEqual((0.123, 0.123), state.attributes['xy_color'])
    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)

        light.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()

        light.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)
Example #10
0
    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)

        light.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()

        light.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
        light.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
        light.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)
Example #12
0
    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)

        light.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')

        self.hass.pool.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))

        # Test turn_off
        turn_off_calls = mock_service(self.hass, light.DOMAIN,
                                      SERVICE_TURN_OFF)

        light.turn_off(self.hass,
                       entity_id='entity_id_val',
                       transition='transition_val')

        self.hass.pool.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])
Example #13
0
    def test_sending_mqtt_commands_and_optimistic(self):
        self.assertTrue(
            light.setup(
                self.hass, {
                    'light': {
                        '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',
                        'qos': 2,
                        'payload_on': 'on',
                        'payload_off': 'off'
                    }
                }))

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass,
                      'light.test',
                      rgb_color=[75, 75, 75],
                      brightness=50)
        self.hass.pool.block_till_done()

        # Calls are threaded so we need to reorder them
        bright_call, rgb_call, state_call = \
            sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
                   key=lambda call: call[0])

        self.assertEqual(('test_light_rgb/set', 'on', 2, False), state_call)

        self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
                         rgb_call)

        self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
                         bright_call)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual([75, 75, 75], state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #14
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """Test the sending of command in optimistic mode."""
        self.hass.config.components = ['mqtt']
        assert _setup_component(self.hass, light.DOMAIN, {
            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',
                'qos': 2,
                'payload_on': 'on',
                'payload_off': 'off'
            }
        })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
                      brightness=50)
        self.hass.block_till_done()

        # Calls are threaded so we need to reorder them
        bright_call, rgb_call, state_call = \
            sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
                   key=lambda call: call[0])

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         state_call)

        self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
                         rgb_call)

        self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
                         bright_call)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #15
0
 def test_turn_off(self):
     """Test light turn off method."""
     light.turn_on(self.hass, ENTITY_LIGHT)
     self.hass.block_till_done()
     self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT))
     light.turn_off(self.hass, ENTITY_LIGHT)
     self.hass.block_till_done()
     self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
Example #16
0
 def test_turn_off_without_entity_id(self):
     """Test light turn off all lights."""
     light.turn_on(self.hass, ENTITY_LIGHT)
     self.hass.block_till_done()
     self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT))
     light.turn_off(self.hass)
     self.hass.block_till_done()
     self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
Example #17
0
 def test_turn_off(self):
     """Test light turn off method."""
     light.turn_on(self.hass, ENTITY_LIGHT)
     self.hass.block_till_done()
     self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT))
     light.turn_off(self.hass, ENTITY_LIGHT)
     self.hass.block_till_done()
     self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
Example #18
0
    def test_sending_mqtt_commands_and_optimistic(self):         \
            # pylint: disable=invalid-name
        """Test the sending of command in optimistic mode."""
        self.hass.config.components = set(['mqtt'])
        assert setup_component(
            self.hass, light.DOMAIN, {
                light.DOMAIN: {
                    'platform': 'mqtt_json',
                    'name': 'test',
                    'command_topic': 'test_light_rgb/set',
                    'brightness': True,
                    'rgb': True,
                    'qos': 2
                }
            })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "ON"}', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "OFF"}', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass,
                      'light.test',
                      rgb_color=[75, 75, 75],
                      brightness=50)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-2][1][0])
        self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
        self.assertEqual(50, message_json["brightness"])
        self.assertEqual(75, message_json["color"]["r"])
        self.assertEqual(75, message_json["color"]["g"])
        self.assertEqual(75, message_json["color"]["b"])
        self.assertEqual("ON", message_json["state"])

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #19
0
    def test_sending_mqtt_commands_and_optimistic(self):

    # pylint: disable=invalid-name
        """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",
                "qos": 2,
                "payload_on": "on",
                "payload_off": "off",
            }
        }

        self.hass.config.components = ["mqtt"]
        with assert_setup_component(1):
            assert setup_component(self.hass, light.DOMAIN, config)

        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, "light.test")
        self.hass.block_till_done()

        self.assertEqual(("test_light_rgb/set", "on", 2, False), self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, "light.test")
        self.hass.block_till_done()

        self.assertEqual(("test_light_rgb/set", "off", 2, False), self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, "light.test", rgb_color=[75, 75, 75], brightness=50)
        self.hass.block_till_done()

        # Calls are threaded so we need to reorder them
        bright_call, rgb_call, state_call = sorted(
            (call[1] for call in self.mock_publish.mock_calls[-3:]), key=lambda call: call[0]
        )

        self.assertEqual(("test_light_rgb/set", "on", 2, False), state_call)

        self.assertEqual(("test_light_rgb/rgb/set", "75,75,75", 2, False), rgb_call)

        self.assertEqual(("test_light_rgb/brightness/set", 50, 2, False), bright_call)

        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes["rgb_color"])
        self.assertEqual(50, state.attributes["brightness"])
    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
        light.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()

        light.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
        light.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()

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        # Turn on w/ just a color to insure brightness gets
        # added and sent.
        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75])
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_has_calls([
            mock.call('test_light/rgb', '50,50,50', 0, False),
            mock.call('test_light/bright', 50, 0, False)
        ],
                                                         any_order=True)
Example #21
0
    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(
            setup_component(self.hass, light.DOMAIN,
                            {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.block_till_done()

        entity_state = {
            'state': 'on',
            'brightness': 100,
        }
        self.assertTrue(
            setup_component(
                self.hass, scene.DOMAIN, {
                    'scene': [{
                        'name': 'test',
                        'entities': {
                            light_1.entity_id: entity_state,
                            light_2.entity_id: entity_state,
                        }
                    }]
                }))

        scene.activate(self.hass, 'scene.test')
        self.hass.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'))
Example #22
0
    def test_sending_mqtt_commands_and_optimistic(self):

    # pylint: disable=invalid-name
        """Test the sending of command in optimistic mode."""
        self.hass.config.components = ["mqtt"]
        assert _setup_component(
            self.hass,
            light.DOMAIN,
            {
                light.DOMAIN: {
                    "platform": "mqtt_json",
                    "name": "test",
                    "command_topic": "test_light_rgb/set",
                    "brightness": True,
                    "rgb": True,
                    "qos": 2,
                }
            },
        )

        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, "light.test")
        self.hass.block_till_done()

        self.assertEqual(("test_light_rgb/set", '{"state": "ON"}', 2, False), self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, "light.test")
        self.hass.block_till_done()

        self.assertEqual(("test_light_rgb/set", '{"state": "OFF"}', 2, False), self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, "light.test", rgb_color=[75, 75, 75], brightness=50)
        self.hass.block_till_done()

        self.assertEqual("test_light_rgb/set", self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(2, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
        self.assertEqual(50, message_json["brightness"])
        self.assertEqual(75, message_json["color"]["r"])
        self.assertEqual(75, message_json["color"]["g"])
        self.assertEqual(75, message_json["color"]["b"])
        self.assertEqual("ON", message_json["state"])

        state = self.hass.states.get("light.test")
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes["rgb_color"])
        self.assertEqual(50, state.attributes["brightness"])
Example #23
0
    def test_sending_mqtt_commands_and_optimistic(self): \
            # pylint: disable=invalid-name
        """Test the sending of command in optimistic mode."""
        self.hass.config.components = ['mqtt']
        assert _setup_component(self.hass, light.DOMAIN, {
            light.DOMAIN: {
                'platform': 'mqtt_json',
                'name': 'test',
                'command_topic': 'test_light_rgb/set',
                'brightness': True,
                'rgb': True,
                'qos': 2
            }
        })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "ON"}', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "OFF"}', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
                      brightness=50)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(2, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
        self.assertEqual(50, message_json["brightness"])
        self.assertEqual(75, message_json["color"]["r"])
        self.assertEqual(75, message_json["color"]["g"])
        self.assertEqual(75, message_json["color"]["b"])
        self.assertEqual("ON", message_json["state"])

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #24
0
    def test_sending_mqtt_commands_and_optimistic(self):
        self.assertTrue(light.setup(self.hass, {
            'light': {
                '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',
                'qos': 2,
                'payload_on': 'on',
                'payload_off': 'off'
            }
        }))

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
                      brightness=50)
        self.hass.pool.block_till_done()

        # Calls are threaded so we need to reorder them
        bright_call, rgb_call, state_call = \
            sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
                   key=lambda call: call[0])

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         state_call)

        self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
                         rgb_call)

        self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
                         bright_call)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual([75, 75, 75], state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #25
0
    def test_sending_mqtt_commands_and_optimistic(self): \
            # pylint: disable=invalid-name
        """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',
            'qos': 2,
            'payload_on': 'on',
            'payload_off': 'off'
        }}

        self.hass.config.components = set(['mqtt'])
        with assert_setup_component(1):
            assert setup_component(self.hass, light.DOMAIN, config)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        self.mock_publish.reset_mock()
        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
                      brightness=50)
        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', '75,75,75', 2, False),
                mock.call('test_light_rgb/brightness/set', 50, 2, False),
            ], any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
Example #26
0
    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
        light.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()

        light.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
        light.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()

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        # Turn on w/ just a color to insure brightness gets
        # added and sent.
        light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75])
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_has_calls([
            mock.call('test_light/rgb', '50,50,50', 0, False),
            mock.call('test_light/bright', 50, 0, False)
        ], any_order=True)
Example #27
0
    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(setup_component(self.hass, light.DOMAIN, {
            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.block_till_done()

        entity_state = {
            'state': 'on',
            'brightness': 100,
        }
        self.assertTrue(setup_component(self.hass, scene.DOMAIN, {
            'scene': [{
                'name': 'test',
                'entities': {
                    light_1.entity_id: entity_state,
                    light_2.entity_id: entity_state,
                }
            }]
        }))

        scene.activate(self.hass, 'scene.test')
        self.hass.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_lights_on_when_sun_sets(self):
        """Test lights go on when there is someone home and the sun sets."""
        self.assertTrue(device_sun_light_trigger.setup(
            self.hass, {device_sun_light_trigger.DOMAIN: {}}))

        ensure_sun_risen(self.hass)
        light.turn_off(self.hass)

        self.hass.pool.block_till_done()

        ensure_sun_set(self.hass)
        self.hass.pool.block_till_done()

        self.assertTrue(light.is_on(self.hass))
Example #29
0
    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)

        light.turn_on(self.hass, ENTITY_LIGHT)
        self.hass.block_till_done()
        self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER)

        light.turn_off(self.hass, ENTITY_LIGHT)
        self.hass.block_till_done()
        self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
Example #30
0
    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)

        light.turn_on(self.hass, ENTITY_LIGHT)
        self.hass.block_till_done()
        self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER)

        light.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."""
        light.turn_off(self.hass)
        ensure_sun_set(self.hass)

        self.hass.pool.block_till_done()

        self.assertTrue(device_sun_light_trigger.setup(
            self.hass, {device_sun_light_trigger.DOMAIN: {}}))

        self.hass.states.set(
            device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME)

        self.hass.pool.block_till_done()
        self.assertTrue(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):
            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_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):
            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))
Example #34
0
    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))

        light.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
        light.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_turn_on_when_coming_home_after_sun_set(self):

    # pylint: disable=invalid-name
        """Test lights turn on when coming home after sun set."""
        light.turn_off(self.hass)
        ensure_sun_set(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_lights_turn_on_when_coming_home_after_sun_set(self):
        """ Test lights turn on when coming home after sun set. """
        light.turn_off(self.hass)

        ensure_sun_set(self.hass)

        self.hass.pool.block_till_done()

        device_sun_light_trigger.setup(
            self.hass, {device_sun_light_trigger.DOMAIN: {}})

        self.scanner.come_home('DEV2')
        trigger_device_tracker_scan(self.hass)

        self.hass.pool.block_till_done()

        self.assertTrue(light.is_on(self.hass))
Example #37
0
    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))

        light.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
        light.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"])
Example #38
0
    def test_lights_turn_on_when_coming_home_after_sun_set(self):
        """ Test lights turn on when coming home after sun set. """
        light.turn_off(self.hass)

        ensure_sun_set(self.hass)

        self.hass.pool.block_till_done()

        device_sun_light_trigger.setup(self.hass,
                                       {device_sun_light_trigger.DOMAIN: {}})

        self.scanner.come_home('DEV2')
        trigger_device_tracker_scan(self.hass)

        self.hass.pool.block_till_done()

        self.assertTrue(light.is_on(self.hass))
    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: {}}))

        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))
Example #40
0
    def set_lux(self, target):
        # Read the lux target from service arguments
        self.sensor_target = target
        # Target to feedforward
        self.light_target_brightness = self.lux_to_brigthness(
            self.sensor_target)
        # Reset PID integral
        self.reset_pid()
        #  if brightness is 0 turn_off light set brightness otherwise
        if self.light_target_brightness > 0:
            turn_on(self._hass,
                    self._light_id,
                    brightness_pct=self.light_target_brightness)
        else:
            turn_off(self._hass, self._light_id)

        _LOGGER.debug("SimpleLight.set_lux called with %d lux => %d bright",
                      self.sensor_target, self.light_target_brightness)
Example #41
0
    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: {}}))

        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 test_transition(self):
        """Test for transition time being sent when included."""
        self.hass.config.components = ["mqtt"]
        with assert_setup_component(1):
            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
        light.turn_on(self.hass, "light.test", transition=10)
        self.hass.block_till_done()

        self.assertEqual("test_light_rgb/set", self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual("on,10", payload)

        # transition off
        light.turn_off(self.hass, "light.test", transition=4)
        self.hass.block_till_done()

        self.assertEqual("test_light_rgb/set", self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual("off,4", payload)
Example #43
0
    def setUp(self):  # pylint: disable=invalid-name
        """Setup 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]

        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)
Example #44
0
    def state_changed_listener(self, entity_id, old_state, new_state):
        if self._sensor_id == entity_id:
            try:
                self.sensor_last_state = float(new_state.state)
            except ValueError:
                return

            # Update sensor data only if light is ON
            if self._state:
                _LOGGER.debug(
                    "SmartLight.state_changed_listener lux:%f target:%f",
                    self.sensor_last_state, self.sensor_target)
                self.apply_pid()
                if self.light_target_brightness > 0:
                    turn_on(self._hass,
                            self._light_id,
                            brightness_pct=self.light_target_brightness)
                else:
                    turn_off(self._hass, self._light_id)
Example #45
0
    def setUp(self):  # pylint: disable=invalid-name
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        test_light = loader.get_component('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]

        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)
Example #46
0
    def test_transition(self):
        """Test for transition time being sent when included."""
        self.hass.config.components = ['mqtt']
        with assert_setup_component(1):
            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
        light.turn_on(self.hass, 'light.test', transition=10)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual('on,10', payload)

        # transition off
        light.turn_off(self.hass, 'light.test', transition=4)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual('off,4', payload)
Example #47
0
    def test_transition(self):
        """Test for transition time being sent when included."""
        self.hass.config.components = ["mqtt"]
        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)

        light.turn_on(self.hass, "light.test", transition=10)
        self.hass.block_till_done()

        self.assertEqual("test_light_rgb/set", self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
        self.assertEqual(10, message_json["transition"])
        self.assertEqual("ON", message_json["state"])

        # Transition back off
        light.turn_off(self.hass, "light.test", transition=10)
        self.hass.block_till_done()

        self.assertEqual("test_light_rgb/set", self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
        self.assertEqual(10, message_json["transition"])
        self.assertEqual("OFF", message_json["state"])
    def test_transition(self):
        """Test for transition time being sent when included."""
        self.hass.config.components = ['mqtt']
        with assert_setup_component(1):
            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
        light.turn_on(self.hass, 'light.test', transition=10)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual('on,10', payload)

        # transition off
        light.turn_off(self.hass, 'light.test', transition=4)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-1][1][0])
        self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-1][1][1]
        self.assertEqual('off,4', payload)
Example #49
0
    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))

        light.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)

        light.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)

        light.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
        light.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('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 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)
    def test_optimistic(self):         \
            # pylint: disable=invalid-name
        """Test optimistic mode."""
        with assert_setup_component(1):
            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',
                        'qos':
                        2
                    }
                })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        # turn on the light
        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on,,,,--', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        # turn the light off
        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        # turn on the light with brightness, color, color temp and white val
        light.turn_on(self.hass,
                      'light.test',
                      brightness=50,
                      rgb_color=[75, 75, 75],
                      color_temp=200,
                      white_value=139)
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-2][1][0])
        self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])

        # check the payload
        payload = self.mock_publish.mock_calls[-2][1][1]
        self.assertEqual('on,50,200,139,75-75-75', payload)

        # check the state
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
        self.assertEqual(200, state.attributes['color_temp'])
        self.assertEqual(139, state.attributes['white_value'])
Example #52
0
    def test_sending_mqtt_commands_and_optimistic(self):         \
            # pylint: disable=invalid-name
        """Test the sending of command in optimistic mode."""
        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_OFF, state.state)
        self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES))
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "ON"}', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', '{"state": "OFF"}', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        light.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.mock_calls[-2][1][0])
        self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
        # Get the sent message
        message_json = json.loads(self.mock_publish.mock_calls[-2][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'])
Example #53
0
    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))

        light.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)

        light.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)

        light.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
        light.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_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
        light.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
        light.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
        light.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
        light.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'])
Example #55
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 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: 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)
    def test_sending_mqtt_commands_and_optimistic(self):         \
            # pylint: disable=invalid-name
        """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',
                'qos': 2,
                'payload_on': 'on',
                'payload_off': 'off'
            }
        }

        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)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        light.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)

        light.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()
        light.turn_on(self.hass,
                      'light.test',
                      brightness=50,
                      xy_color=[0.123, 0.123])
        light.turn_on(self.hass,
                      'light.test',
                      rgb_color=[75, 75, 75],
                      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', '50,50,50', 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.32,0.336', 2, False),
        ],
                                                         any_order=True)

        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(80, state.attributes['white_value'])
        self.assertEqual((0.32, 0.336), state.attributes['xy_color'])