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)
示例#2
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_flash(self): \
            # pylint: disable=invalid-name
        """Test flash."""
        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,{{ flash }}',
                    'command_off_template': 'off',
                    'qos': 0
                }
            })

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

        # short flash
        light.turn_on(self.hass, 'light.test', flash='short')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'test_light_rgb/set', 'on,short', 0, False)
        self.mock_publish.async_publish.reset_mock()

        # long flash
        light.turn_on(self.hass, 'light.test', flash='long')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'test_light_rgb/set', 'on,long', 0, False)
示例#4
0
def set_lights_rgb(hass, lights, rgb, transition):
    """Set color of array of lights."""
    for light in lights:
        if is_on(hass, light):
            turn_on(hass, light,
                    rgb_color=rgb,
                    transition=transition)
示例#5
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)
    def test_light_profiles(self):
        """ Test light profiles. """
        platform = loader.get_component('light.test')
        platform.init()

        user_light_file = self.hass.get_config_path(light.LIGHT_PROFILES_FILE)

        with open(user_light_file, 'w') as user_file:
            user_file.write('id,x,y,brightness\n')
            user_file.write('test,.4,.6,100\n')

        self.assertTrue(light.setup(
            self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
        ))

        dev1, dev2, dev3 = platform.DEVICES

        light.turn_on(self.hass, dev1.entity_id, profile='test')

        self.hass.pool.block_till_done()

        method, data = dev1.last_call('turn_on')

        self.assertEqual(
            {light.ATTR_XY_COLOR: [.4, .6], light.ATTR_BRIGHTNESS: 100},
            data)
    def test_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])
示例#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'])
示例#9
0
    def test_sending_mqtt_rgb_command_with_template(self):
        """Test the sending of RGB command with template."""
        config = {light.DOMAIN: {
            'platform': 'mqtt',
            'name': 'test',
            'command_topic': 'test_light_rgb/set',
            'rgb_command_topic': 'test_light_rgb/rgb/set',
            'rgb_command_template': '{{ "#%02x%02x%02x" | '
                                    'format(red, green, blue)}}',
            'payload_on': 'on',
            'payload_off': 'off',
            'qos': 0
        }}

        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', rgb_color=[255, 255, 255])
        self.hass.block_till_done()

        self.mock_publish().async_publish.assert_has_calls([
            mock.call('test_light_rgb/set', 'on', 0, False),
            mock.call('test_light_rgb/rgb/set', '#ffffff', 0, 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'])
示例#10
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'])
示例#11
0
    def test_flux_with_multiple_lights(self):
        """Test the flux switch with multiple light entities."""
        platform = loader.get_component("light.test")
        platform.init()
        self.assertTrue(light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: "test"}}))

        dev1, dev2, dev3 = platform.DEVICES
        light.turn_on(self.hass, entity_id=dev2.entity_id)
        self.hass.pool.block_till_done()
        light.turn_on(self.hass, entity_id=dev3.entity_id)
        self.hass.pool.block_till_done()

        state = self.hass.states.get(dev1.entity_id)
        self.assertEqual(STATE_ON, state.state)
        self.assertIsNone(state.attributes.get("xy_color"))
        self.assertIsNone(state.attributes.get("brightness"))

        state = self.hass.states.get(dev2.entity_id)
        self.assertEqual(STATE_ON, state.state)
        self.assertIsNone(state.attributes.get("xy_color"))
        self.assertIsNone(state.attributes.get("brightness"))

        state = self.hass.states.get(dev3.entity_id)
        self.assertEqual(STATE_ON, state.state)
        self.assertIsNone(state.attributes.get("xy_color"))
        self.assertIsNone(state.attributes.get("brightness"))

        test_time = dt_util.now().replace(hour=12, minute=0, second=0)
        sunset_time = test_time.replace(hour=17, minute=0, second=0)
        sunrise_time = test_time.replace(hour=5, minute=0, second=0) + timedelta(days=1)

        with patch("homeassistant.util.dt.now", return_value=test_time):
            with patch("homeassistant.components.sun.next_rising", return_value=sunrise_time):
                with patch("homeassistant.components.sun.next_setting", return_value=sunset_time):
                    assert setup_component(
                        self.hass,
                        switch.DOMAIN,
                        {
                            switch.DOMAIN: {
                                "platform": "flux",
                                "name": "flux",
                                "lights": [dev1.entity_id, dev2.entity_id, dev3.entity_id],
                            }
                        },
                    )
                    turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON)
                    switch.turn_on(self.hass, "switch.flux")
                    self.hass.pool.block_till_done()
                    fire_time_changed(self.hass, test_time)
                    self.hass.pool.block_till_done()
        call = turn_on_calls[-1]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
        call = turn_on_calls[-2]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
        call = turn_on_calls[-3]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
示例#12
0
def set_lights_xy(hass, lights, x_val, y_val, brightness):
    """Set color of array of lights."""
    for light in lights:
        if is_on(hass, light):
            turn_on(hass, light,
                    xy_color=[x_val, y_val],
                    brightness=brightness,
                    transition=30)
示例#13
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))
示例#14
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))
示例#15
0
def set_lights_temp(hass, lights, mired, brightness):
    """Set color of array of lights."""
    for light in lights:
        if is_on(hass, light):
            turn_on(hass, light,
                    color_temp=int(mired),
                    brightness=brightness,
                    transition=30)
示例#16
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"])
示例#17
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"])
示例#18
0
def set_lights_temp(hass, lights, kelvin, mode):
    """Set color of array of lights."""
    temp = kelvin
    if mode == MODE_MIRED:
        temp = 1000000 / kelvin
    for light in lights:
        if is_on(hass, light):
            turn_on(hass, light,
                    color_temp=int(temp),
                    transition=30)
示例#19
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'])
示例#20
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'])
示例#21
0
    def test_on_brightness(self):
        """Test turning the light on with brightness."""
        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, brightness=102)
        self.hass.block_till_done()
        self.mock_lj.activate_load_at.assert_called_with(
            ENTITY_LIGHT_NUMBER, 39, 0)
示例#22
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)
示例#23
0
 def test_state_attributes(self):
     """Test light state attributes."""
     light.turn_on(
         self.hass, ENTITY_LIGHT, xy_color=(.4, .6), brightness=25)
     self.hass.block_till_done()
     state = self.hass.states.get(ENTITY_LIGHT)
     self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT))
     self.assertEqual((.4, .6), state.attributes.get(light.ATTR_XY_COLOR))
     self.assertEqual(25, state.attributes.get(light.ATTR_BRIGHTNESS))
     self.assertEqual(
         (76, 95, 0), state.attributes.get(light.ATTR_RGB_COLOR))
     self.assertEqual('rainbow', state.attributes.get(light.ATTR_EFFECT))
     light.turn_on(
         self.hass, ENTITY_LIGHT, rgb_color=(251, 252, 253),
         white_value=254)
     self.hass.block_till_done()
     state = self.hass.states.get(ENTITY_LIGHT)
     self.assertEqual(254, state.attributes.get(light.ATTR_WHITE_VALUE))
     self.assertEqual(
         (251, 252, 253), state.attributes.get(light.ATTR_RGB_COLOR))
     light.turn_on(self.hass, ENTITY_LIGHT, color_temp=400, effect='none')
     self.hass.block_till_done()
     state = self.hass.states.get(ENTITY_LIGHT)
     self.assertEqual(400, state.attributes.get(light.ATTR_COLOR_TEMP))
     self.assertEqual(154, state.attributes.get(light.ATTR_MIN_MIREDS))
     self.assertEqual(500, state.attributes.get(light.ATTR_MAX_MIREDS))
     self.assertEqual('none', state.attributes.get(light.ATTR_EFFECT))
     light.turn_on(self.hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50)
     self.hass.block_till_done()
     state = self.hass.states.get(ENTITY_LIGHT)
     self.assertEqual(333, state.attributes.get(light.ATTR_COLOR_TEMP))
     self.assertEqual(127, state.attributes.get(light.ATTR_BRIGHTNESS))
示例#24
0
    def test_flash_short_and_long(self): \
            # pylint: disable=invalid-name
        """Test for flash length 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',
                'flash_time_short': 5,
                'flash_time_long': 15,
                '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', flash="short")
        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(5, message_json["flash"])
        self.assertEqual("ON", message_json["state"])

        self.mock_publish.async_publish.reset_mock()
        light.turn_on(self.hass, 'light.test', flash="long")
        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(15, message_json["flash"])
        self.assertEqual("ON", message_json["state"])
    def test_lights_turn_off_when_everyone_leaves(self):
        """ Test lights turn off when everyone leaves the house. """
        light.turn_on(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_ALL_DEVICES,
                             STATE_NOT_HOME)

        self.hass.pool.block_till_done()

        self.assertFalse(light.is_on(self.hass))
    def test_lights_turn_off_when_everyone_leaves(self):
        """ Test lights turn off when everyone leaves the house. """
        light.turn_on(self.hass)

        self.hass.pool.block_till_done()

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

        self.scanner.leave_home('DEV1')

        trigger_device_tracker_scan(self.hass)

        self.hass.pool.block_till_done()

        self.assertFalse(light.is_on(self.hass))
    def test_flash(self):

    # pylint: disable=invalid-name
        """Test flash."""
        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,{{ flash }}",
                        "command_off_template": "off",
                        "qos": 0,
                    }
                },
            )

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

        # short flash
        light.turn_on(self.hass, "light.test", flash="short")
        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,short", payload)

        # long flash
        light.turn_on(self.hass, "light.test", flash="long")
        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,long", payload)
    def test_lights_turn_off_when_everyone_leaves(self): \
            # pylint: disable=invalid-name
        """Test lights turn off when everyone leaves the house."""
        light.turn_on(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_ALL_DEVICES,
                             STATE_NOT_HOME)

        self.hass.block_till_done()

        self.assertFalse(light.is_on(self.hass))
示例#29
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"])
示例#30
0
    def test_flash_short_and_long(self):

    # pylint: disable=invalid-name
        """Test for flash length 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",
                    "flash_time_short": 5,
                    "flash_time_long": 15,
                    "qos": 0,
                }
            },
        )

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

        light.turn_on(self.hass, "light.test", flash="short")
        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(5, message_json["flash"])
        self.assertEqual("ON", message_json["state"])

        light.turn_on(self.hass, "light.test", flash="long")
        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(15, message_json["flash"])
        self.assertEqual("ON", message_json["state"])