def test_flash(self):
        """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
        common.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
        common.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)
    def test_light_profiles(self):
        """Test light profiles."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)

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

        assert setup_component(
            self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
        )

        dev1, _, _ = platform.DEVICES

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

        self.hass.block_till_done()

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

        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 100
        } == data
Exemple #3
0
    def test_light_profiles(self):
        """Test light profiles."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)

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

        assert setup_component(
            self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
        )

        dev1, _, _ = platform.DEVICES

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

        self.hass.block_till_done()

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

        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 100
        } == data
Exemple #4
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)

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

        common.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)
Exemple #5
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)

        common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 64])
        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', '#ff803f', 0, False),
        ],
                                                         any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((255, 128, 63), state.attributes['rgb_color'])
Exemple #6
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)

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

        common.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)
Exemple #7
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)

        common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 64])
        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', '#ff803f', 0, False),
        ], any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((255, 128, 63), state.attributes['rgb_color'])
    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
        common.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
        common.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)
Exemple #9
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)

        common.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)
Exemple #10
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)

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

        common.turn_off(self.hass, ENTITY_LIGHT)
        self.hass.block_till_done()
        self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
    def test_flash_short_and_long(self):
        """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))

        common.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()
        common.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"])
Exemple #12
0
    def test_lights_turn_off_when_everyone_leaves(self):
        """Test lights turn off when everyone leaves the house."""
        common_light.turn_on(self.hass)

        self.hass.block_till_done()

        assert 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()

        assert not light.is_on(self.hass)
Exemple #13
0
    def test_white_value_action_no_template(self):
        """Test setting white value with optimistic template."""
        assert setup.setup_component(
            self.hass,
            "light",
            {
                "light": {
                    "platform": "template",
                    "lights": {
                        "test_template_light": {
                            "value_template": "{{1 == 1}}",
                            "turn_on": {
                                "service": "light.turn_on",
                                "entity_id": "light.test_state",
                            },
                            "turn_off": {
                                "service": "light.turn_off",
                                "entity_id": "light.test_state",
                            },
                            "set_white_value": {
                                "service": "test.automation",
                                "data_template": {
                                    "entity_id": "test.test_state",
                                    "white_value": "{{white_value}}",
                                },
                            },
                        }
                    },
                }
            },
        )
        self.hass.block_till_done()
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert state.attributes.get("white_value") is None

        common.turn_on(
            self.hass, "light.test_template_light", **{ATTR_WHITE_VALUE: 124}
        )
        self.hass.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0].data["white_value"] == "124"

        state = self.hass.states.get("light.test_template_light")
        assert state is not None
        assert state.attributes.get("white_value") == 124
    def test_flash_short_and_long(self):
        """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))

        common.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()
        common.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."""
        common_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))
Exemple #16
0
    def test_temperature_action_no_template(self):
        """Test setting temperature with optimistic template."""
        assert setup.setup_component(
            self.hass,
            "light",
            {
                "light": {
                    "platform": "template",
                    "lights": {
                        "test_template_light": {
                            "value_template": "{{1 == 1}}",
                            "turn_on": {
                                "service": "light.turn_on",
                                "entity_id": "light.test_state",
                            },
                            "turn_off": {
                                "service": "light.turn_off",
                                "entity_id": "light.test_state",
                            },
                            "set_temperature": {
                                "service": "test.automation",
                                "data_template": {
                                    "entity_id": "test.test_state",
                                    "color_temp": "{{color_temp}}",
                                },
                            },
                        }
                    },
                }
            },
        )
        self.hass.block_till_done()
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert state.attributes.get("color_template") is None

        common.turn_on(self.hass, "light.test_template_light", **{ATTR_COLOR_TEMP: 345})
        self.hass.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0].data["color_temp"] == "345"

        state = self.hass.states.get("light.test_template_light")
        _LOGGER.info(str(state.attributes))
        assert state is not None
        assert state.attributes.get("color_temp") == 345
Exemple #17
0
    def test_level_action_no_template(self):
        """Test setting brightness with optimistic template."""
        assert setup.setup_component(
            self.hass,
            "light",
            {
                "light": {
                    "platform": "template",
                    "lights": {
                        "test_template_light": {
                            "value_template": "{{1 == 1}}",
                            "turn_on": {
                                "service": "light.turn_on",
                                "entity_id": "light.test_state",
                            },
                            "turn_off": {
                                "service": "light.turn_off",
                                "entity_id": "light.test_state",
                            },
                            "set_level": {
                                "service": "test.automation",
                                "data_template": {
                                    "entity_id": "test.test_state",
                                    "brightness": "{{brightness}}",
                                },
                            },
                        }
                    },
                }
            },
        )
        self.hass.block_till_done()
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert state.attributes.get("brightness") is None

        common.turn_on(self.hass, "light.test_template_light", **{ATTR_BRIGHTNESS: 124})
        self.hass.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0].data["brightness"] == "124"

        state = self.hass.states.get("light.test_template_light")
        _LOGGER.info(str(state.attributes))
        assert state is not None
        assert state.attributes.get("brightness") == 124
    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))

        common.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
        common.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_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))

        common.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
        common.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"])
Exemple #20
0
    def test_on_action_optimistic(self):
        """Test on action with optimistic state."""
        assert setup.setup_component(
            self.hass,
            "light",
            {
                "light": {
                    "platform": "template",
                    "lights": {
                        "test_template_light": {
                            "turn_on": {"service": "test.automation"},
                            "turn_off": {
                                "service": "light.turn_off",
                                "entity_id": "light.test_state",
                            },
                            "set_level": {
                                "service": "light.turn_on",
                                "data_template": {
                                    "entity_id": "light.test_state",
                                    "brightness": "{{brightness}}",
                                },
                            },
                        }
                    },
                }
            },
        )

        self.hass.block_till_done()
        self.hass.start()
        self.hass.block_till_done()

        self.hass.states.set("light.test_state", STATE_OFF)
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert state.state == STATE_OFF

        common.turn_on(self.hass, "light.test_template_light")
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert len(self.calls) == 1
        assert state.state == STATE_ON
Exemple #21
0
    def test_level_action_no_template(self):
        """Test setting brightness with optimistic template."""
        assert setup.setup_component(self.hass, 'light', {
            'light': {
                'platform': 'template',
                'lights': {
                    'test_template_light': {
                        'value_template': '{{1 == 1}}',
                        'turn_on': {
                            'service': 'light.turn_on',
                            'entity_id': 'light.test_state'
                        },
                        'turn_off': {
                            'service': 'light.turn_off',
                            'entity_id': 'light.test_state'
                        },
                        'set_level': {
                            'service': 'test.automation',
                            'data_template': {
                                'entity_id': 'test.test_state',
                                'brightness': '{{brightness}}'
                            }
                        },
                    }
                }
            }
        })
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('light.test_template_light')
        assert state.attributes.get('brightness') is None

        common.turn_on(
            self.hass, 'light.test_template_light', **{ATTR_BRIGHTNESS: 124})
        self.hass.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0].data['brightness'] == '124'

        state = self.hass.states.get('light.test_template_light')
        _LOGGER.info(str(state.attributes))
        assert state is not None
        assert state.attributes.get('brightness') == 124
Exemple #22
0
    def test_default_profiles_light(self):
        """Test default turn-on light profile for a specific light."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path)

        profile_data = "id,x,y,brightness\n" +\
                       "group.all_lights.default,.3,.5,200\n" +\
                       "light.ceiling_2.default,.6,.6,100\n"
        with mock.patch('os.path.isfile', side_effect=_mock_isfile):
            with mock.patch('builtins.open', side_effect=_mock_open):
                with mock_storage():
                    self.assertTrue(
                        setup_component(
                            self.hass, light.DOMAIN,
                            {light.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))

        dev = next(
            filter(lambda x: x.entity_id == 'light.ceiling_2',
                   platform.DEVICES))
        common.turn_on(self.hass, dev.entity_id)
        self.hass.block_till_done()
        _, data = dev.last_call('turn_on')
        self.assertEqual(
            {
                light.ATTR_HS_COLOR: (50.353, 100),
                light.ATTR_BRIGHTNESS: 100
            }, data)
Exemple #23
0
    def test_default_profiles_light(self):
        """Test default turn-on light profile for a specific light."""
        platform = getattr(self.hass.components, "test.light")
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path, *args, **kwargs):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path, *args, **kwargs)

        profile_data = ("id,x,y,brightness,transition\n" +
                        "group.all_lights.default,.3,.5,200,0\n" +
                        "light.ceiling_2.default,.6,.6,100,3\n")
        with mock.patch("os.path.isfile",
                        side_effect=_mock_isfile), mock.patch(
                            "builtins.open",
                            side_effect=_mock_open), mock_storage():
            assert setup_component(self.hass, light.DOMAIN,
                                   {light.DOMAIN: {
                                       CONF_PLATFORM: "test"
                                   }})
            self.hass.block_till_done()

        dev = next(
            filter(lambda x: x.entity_id == "light.ceiling_2",
                   platform.ENTITIES))
        common.turn_on(self.hass, dev.entity_id)
        self.hass.block_till_done()
        _, data = dev.last_call("turn_on")
        assert {
            light.ATTR_HS_COLOR: (50.353, 100),
            light.ATTR_BRIGHTNESS: 100,
            light.ATTR_TRANSITION: 3,
        } == data
Exemple #24
0
    def test_default_profiles_group(self):
        """Test default turn-on light profile for all lights."""
        platform = getattr(self.hass.components, "test.light")
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path, *args, **kwargs):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path, *args, **kwargs)

        profile_data = (
            "id,x,y,brightness,transition\ngroup.all_lights.default,.4,.6,99,2\n"
        )
        with mock.patch("os.path.isfile",
                        side_effect=_mock_isfile), mock.patch(
                            "builtins.open",
                            side_effect=_mock_open), mock_storage():
            assert setup_component(self.hass, light.DOMAIN,
                                   {light.DOMAIN: {
                                       CONF_PLATFORM: "test"
                                   }})
            self.hass.block_till_done()

        ent, _, _ = platform.ENTITIES
        common.turn_on(self.hass, ent.entity_id)
        self.hass.block_till_done()
        _, data = ent.last_call("turn_on")
        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 99,
            light.ATTR_TRANSITION: 2,
        } == data
Exemple #25
0
    def test_light_profiles_with_transition(self):
        """Test light profiles with transition."""
        platform = getattr(self.hass.components, "test.light")
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)

        with open(user_light_file, "w") as user_file:
            user_file.write("id,x,y,brightness,transition\n")
            user_file.write("test,.4,.6,100,2\n")
            user_file.write("test_off,0,0,0,0\n")

        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {
                                   CONF_PLATFORM: "test"
                               }})
        self.hass.block_till_done()

        ent1, _, _ = platform.ENTITIES

        common.turn_on(self.hass, ent1.entity_id, profile="test")

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_on")

        assert light.is_on(self.hass, ent1.entity_id)
        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 100,
            light.ATTR_TRANSITION: 2,
        } == data

        common.turn_on(self.hass, ent1.entity_id, profile="test_off")

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_off")

        assert not light.is_on(self.hass, ent1.entity_id)
        assert {light.ATTR_TRANSITION: 0} == data
Exemple #26
0
    def test_on_action_optimistic(self):
        """Test on action with optimistic state."""
        assert setup.setup_component(self.hass, 'light', {
            'light': {
                'platform': 'template',
                'lights': {
                    'test_template_light': {
                        'turn_on': {
                            'service': 'test.automation',
                        },
                        'turn_off': {
                            'service': 'light.turn_off',
                            'entity_id': 'light.test_state'
                        },
                        'set_level': {
                            'service': 'light.turn_on',
                            'data_template': {
                                'entity_id': 'light.test_state',
                                'brightness': '{{brightness}}'
                            }
                        }
                    }
                }
            }
        })

        self.hass.start()
        self.hass.block_till_done()

        self.hass.states.set('light.test_state', STATE_OFF)
        self.hass.block_till_done()

        state = self.hass.states.get('light.test_template_light')
        assert state.state == STATE_OFF

        common.turn_on(self.hass, 'light.test_template_light')
        self.hass.block_till_done()

        state = self.hass.states.get('light.test_template_light')
        assert len(self.calls) == 1
        assert state.state == STATE_ON
Exemple #27
0
    def test_sending_hs_color(self):
        """Test light.turn_on with hs color sends hs color parameters."""
        assert setup_component(self.hass, light.DOMAIN, {
            light.DOMAIN: {
                'platform': 'mqtt_json',
                'name': 'test',
                'command_topic': 'test_light_rgb/set',
                'hs': True,
            }
        })

        common.turn_on(self.hass, 'light.test', hs_color=(180.0, 50.0))
        self.hass.block_till_done()

        message_json = json.loads(
            self.mock_publish.async_publish.mock_calls[0][1][1])
        self.assertEqual("ON", message_json["state"])
        self.assertEqual({
            'h': 180.0,
            's': 50.0,
        }, message_json["color"])
Exemple #28
0
    def test_default_profiles_light(self):
        """Test default turn-on light profile for a specific light."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path)

        profile_data = "id,x,y,brightness\n" +\
                       "group.all_lights.default,.3,.5,200\n" +\
                       "light.ceiling_2.default,.6,.6,100\n"
        with mock.patch('os.path.isfile', side_effect=_mock_isfile):
            with mock.patch('builtins.open', side_effect=_mock_open):
                with mock_storage():
                    assert setup_component(
                        self.hass, light.DOMAIN,
                        {light.DOMAIN: {CONF_PLATFORM: 'test'}}
                    )

        dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2',
                          platform.DEVICES))
        common.turn_on(self.hass, dev.entity_id)
        self.hass.block_till_done()
        _, data = dev.last_call('turn_on')
        assert {
            light.ATTR_HS_COLOR: (50.353, 100),
            light.ATTR_BRIGHTNESS: 100
        } == data
Exemple #29
0
    def test_default_profiles_group(self):
        """Test default turn-on light profile for all lights."""
        platform = getattr(self.hass.components, 'test.light')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path, *args, **kwargs):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path, *args, **kwargs)

        profile_data = "id,x,y,brightness\n" +\
                       "group.all_lights.default,.4,.6,99\n"
        with mock.patch('os.path.isfile', side_effect=_mock_isfile):
            with mock.patch('builtins.open', side_effect=_mock_open):
                with mock_storage():
                    assert setup_component(
                        self.hass, light.DOMAIN,
                        {light.DOMAIN: {
                            CONF_PLATFORM: 'test'
                        }})

        dev, _, _ = platform.DEVICES
        common.turn_on(self.hass, dev.entity_id)
        self.hass.block_till_done()
        _, data = dev.last_call('turn_on')
        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 99
        } == data
Exemple #30
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
        common.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()

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

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

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

        self.mock_publish.async_publish.assert_has_calls([
            mock.call('test_light/rgb', '255,128,0', 0, False),
            mock.call('test_light/bright', 50, 0, False)
        ],
                                                         any_order=True)
Exemple #31
0
    def test_default_profiles_group(self):
        """Test default turn-on light profile for all lights."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()

        user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
        real_isfile = os.path.isfile
        real_open = open

        def _mock_isfile(path):
            if path == user_light_file:
                return True
            return real_isfile(path)

        def _mock_open(path):
            if path == user_light_file:
                return StringIO(profile_data)
            return real_open(path)

        profile_data = "id,x,y,brightness\n" +\
                       "group.all_lights.default,.4,.6,99\n"
        with mock.patch('os.path.isfile', side_effect=_mock_isfile):
            with mock.patch('builtins.open', side_effect=_mock_open):
                with mock_storage():
                    self.assertTrue(setup_component(
                        self.hass, light.DOMAIN,
                        {light.DOMAIN: {CONF_PLATFORM: 'test'}}
                    ))

        dev, _, _ = platform.DEVICES
        common.turn_on(self.hass, dev.entity_id)
        self.hass.block_till_done()
        _, data = dev.last_call('turn_on')
        self.assertEqual({
            light.ATTR_HS_COLOR: (71.059, 100),
            light.ATTR_BRIGHTNESS: 99
        }, data)
Exemple #32
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
        common.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()

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

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

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

        self.mock_publish.async_publish.assert_has_calls([
            mock.call('test_light/rgb', '255,128,0', 0, False),
            mock.call('test_light/bright', 50, 0, False)
        ], any_order=True)
Exemple #33
0
async def test_flux_with_multiple_lights(hass):
    """Test the flux switch with multiple light entities."""
    platform = getattr(hass.components, "test.light")
    platform.init()
    assert await async_setup_component(
        hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
    )

    ent1, ent2, ent3 = platform.ENTITIES
    common_light.turn_on(hass, entity_id=ent2.entity_id)
    await hass.async_block_till_done()
    common_light.turn_on(hass, entity_id=ent3.entity_id)
    await hass.async_block_till_done()

    state = hass.states.get(ent1.entity_id)
    assert STATE_ON == state.state
    assert state.attributes.get("xy_color") is None
    assert state.attributes.get("brightness") is None

    state = hass.states.get(ent2.entity_id)
    assert STATE_ON == state.state
    assert state.attributes.get("xy_color") is None
    assert state.attributes.get("brightness") is None

    state = hass.states.get(ent3.entity_id)
    assert STATE_ON == state.state
    assert state.attributes.get("xy_color") is None
    assert state.attributes.get("brightness") is None

    test_time = dt_util.utcnow().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)

    def event_date(hass, event, now=None):
        if event == SUN_EVENT_SUNRISE:
            print("sunrise {}".format(sunrise_time))
            return sunrise_time
        print("sunset {}".format(sunset_time))
        return sunset_time

    with patch(
        "homeassistant.components.flux.switch.dt_utcnow", return_value=test_time
    ), patch(
        "homeassistant.components.flux.switch.get_astral_event_date",
        side_effect=event_date,
    ):
        assert await async_setup_component(
            hass,
            switch.DOMAIN,
            {
                switch.DOMAIN: {
                    "platform": "flux",
                    "name": "flux",
                    "lights": [ent1.entity_id, ent2.entity_id, ent3.entity_id],
                }
            },
        )
        turn_on_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_ON)
        common.turn_on(hass, "switch.flux")
        await hass.async_block_till_done()
        async_fire_time_changed(hass, test_time)
        await hass.async_block_till_done()
    call = turn_on_calls[-1]
    assert call.data[light.ATTR_BRIGHTNESS] == 163
    assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
    call = turn_on_calls[-2]
    assert call.data[light.ATTR_BRIGHTNESS] == 163
    assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
    call = turn_on_calls[-3]
    assert call.data[light.ATTR_BRIGHTNESS] == 163
    assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
Exemple #34
0
    def test_color_action_no_template(self):
        """Test setting color with optimistic template."""
        assert setup.setup_component(
            self.hass,
            "light",
            {
                "light": {
                    "platform": "template",
                    "lights": {
                        "test_template_light": {
                            "value_template": "{{1 == 1}}",
                            "turn_on": {
                                "service": "light.turn_on",
                                "entity_id": "light.test_state",
                            },
                            "turn_off": {
                                "service": "light.turn_off",
                                "entity_id": "light.test_state",
                            },
                            "set_color": [
                                {
                                    "service": "test.automation",
                                    "data_template": {
                                        "entity_id": "test.test_state",
                                        "h": "{{h}}",
                                        "s": "{{s}}",
                                    },
                                },
                                {
                                    "service": "test.automation",
                                    "data_template": {
                                        "entity_id": "test.test_state",
                                        "s": "{{s}}",
                                        "h": "{{h}}",
                                    },
                                },
                            ],
                        }
                    },
                }
            },
        )
        self.hass.block_till_done()
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get("light.test_template_light")
        assert state.attributes.get("hs_color") is None

        common.turn_on(
            self.hass, "light.test_template_light", **{ATTR_HS_COLOR: (40, 50)}
        )
        self.hass.block_till_done()
        assert len(self.calls) == 2
        assert self.calls[0].data["h"] == "40"
        assert self.calls[0].data["s"] == "50"
        assert self.calls[1].data["h"] == "40"
        assert self.calls[1].data["s"] == "50"

        state = self.hass.states.get("light.test_template_light")
        _LOGGER.info(str(state.attributes))
        assert state is not None
        assert self.calls[0].data["h"] == "40"
        assert self.calls[0].data["s"] == "50"
        assert self.calls[1].data["h"] == "40"
        assert self.calls[1].data["s"] == "50"
    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))

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

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

        common.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
        common.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'])
Exemple #36
0
    def test_flux_with_multiple_lights(self):
        """Test the flux switch with multiple light entities."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {CONF_PLATFORM: 'test'}}))

        dev1, dev2, dev3 = platform.DEVICES
        common_light.turn_on(self.hass, entity_id=dev2.entity_id)
        self.hass.block_till_done()
        common_light.turn_on(self.hass, entity_id=dev3.entity_id)
        self.hass.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)

        def event_date(hass, event, now=None):
            if event == 'sunrise':
                print('sunrise {}'.format(sunrise_time))
                return sunrise_time
            print('sunset {}'.format(sunset_time))
            return sunset_time

        with patch('homeassistant.util.dt.now', return_value=test_time):
            with patch('homeassistant.helpers.sun.get_astral_event_date',
                       side_effect=event_date):
                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)
                common.turn_on(self.hass, 'switch.flux')
                self.hass.block_till_done()
                fire_time_changed(self.hass, test_time)
                self.hass.block_till_done()
        call = turn_on_calls[-1]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
        call = turn_on_calls[-2]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
        call = turn_on_calls[-3]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
Exemple #37
0
    def test_flux_with_multiple_lights(self):
        """Test the flux switch with multiple light entities."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))

        dev1, dev2, dev3 = platform.DEVICES
        common_light.turn_on(self.hass, entity_id=dev2.entity_id)
        self.hass.block_till_done()
        common_light.turn_on(self.hass, entity_id=dev3.entity_id)
        self.hass.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.utcnow().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)

        def event_date(hass, event, now=None):
            if event == 'sunrise':
                print('sunrise {}'.format(sunrise_time))
                return sunrise_time
            print('sunset {}'.format(sunset_time))
            return sunset_time

        with patch('homeassistant.components.switch.flux.dt_utcnow',
                   return_value=test_time), \
            patch('homeassistant.helpers.sun.get_astral_event_date',
                  side_effect=event_date):
            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)
            common.turn_on(self.hass, 'switch.flux')
            self.hass.block_till_done()
            fire_time_changed(self.hass, test_time)
            self.hass.block_till_done()
        call = turn_on_calls[-1]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
        call = turn_on_calls[-2]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
        call = turn_on_calls[-3]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 163)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.46, 0.376])
Exemple #38
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)

        common.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',
            color_name='color_name_val',
            white_value='white_val')

        self.hass.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))
        self.assertEqual(
            'color_name_val', call.data.get(light.ATTR_COLOR_NAME))
        self.assertEqual('white_val', call.data.get(light.ATTR_WHITE_VALUE))

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

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

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

        # Test toggle
        toggle_calls = mock_service(
            self.hass, light.DOMAIN, SERVICE_TOGGLE)

        common.toggle(
            self.hass, entity_id='entity_id_val', transition='transition_val')

        self.hass.block_till_done()

        self.assertEqual(1, len(toggle_calls))
        call = toggle_calls[-1]

        self.assertEqual(light.DOMAIN, call.domain)
        self.assertEqual(SERVICE_TOGGLE, call.service)
        self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
        self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
Exemple #39
0
    def test_services(self):
        """Test the provided services."""
        platform = getattr(self.hass.components, "test.light")

        platform.init()
        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {
                                   CONF_PLATFORM: "test"
                               }})

        ent1, ent2, ent3 = platform.ENTITIES

        # Test init
        assert light.is_on(self.hass, ent1.entity_id)
        assert not light.is_on(self.hass, ent2.entity_id)
        assert not light.is_on(self.hass, ent3.entity_id)

        # Test basic turn_on, turn_off, toggle services
        common.turn_off(self.hass, entity_id=ent1.entity_id)
        common.turn_on(self.hass, entity_id=ent2.entity_id)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, ent1.entity_id)
        assert light.is_on(self.hass, ent2.entity_id)

        # turn on all lights
        common.turn_on(self.hass)

        self.hass.block_till_done()

        assert light.is_on(self.hass, ent1.entity_id)
        assert light.is_on(self.hass, ent2.entity_id)
        assert light.is_on(self.hass, ent3.entity_id)

        # turn off all lights
        common.turn_off(self.hass)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, ent1.entity_id)
        assert not light.is_on(self.hass, ent2.entity_id)
        assert not light.is_on(self.hass, ent3.entity_id)

        # turn off all lights by setting brightness to 0
        common.turn_on(self.hass)

        self.hass.block_till_done()

        common.turn_on(self.hass, brightness=0)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, ent1.entity_id)
        assert not light.is_on(self.hass, ent2.entity_id)
        assert not light.is_on(self.hass, ent3.entity_id)

        # toggle all lights
        common.toggle(self.hass)

        self.hass.block_till_done()

        assert light.is_on(self.hass, ent1.entity_id)
        assert light.is_on(self.hass, ent2.entity_id)
        assert light.is_on(self.hass, ent3.entity_id)

        # toggle all lights
        common.toggle(self.hass)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, ent1.entity_id)
        assert not light.is_on(self.hass, ent2.entity_id)
        assert not light.is_on(self.hass, ent3.entity_id)

        # Ensure all attributes process correctly
        common.turn_on(self.hass,
                       ent1.entity_id,
                       transition=10,
                       brightness=20,
                       color_name="blue")
        common.turn_on(self.hass,
                       ent2.entity_id,
                       rgb_color=(255, 255, 255),
                       white_value=255)
        common.turn_on(self.hass, ent3.entity_id, xy_color=(0.4, 0.6))

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_on")
        assert {
            light.ATTR_TRANSITION: 10,
            light.ATTR_BRIGHTNESS: 20,
            light.ATTR_HS_COLOR: (240, 100),
        } == data

        _, data = ent2.last_call("turn_on")
        assert {
            light.ATTR_HS_COLOR: (0, 0),
            light.ATTR_WHITE_VALUE: 255
        } == data

        _, data = ent3.last_call("turn_on")
        assert {light.ATTR_HS_COLOR: (71.059, 100)} == data

        # Ensure attributes are filtered when light is turned off
        common.turn_on(self.hass,
                       ent1.entity_id,
                       transition=10,
                       brightness=0,
                       color_name="blue")
        common.turn_on(
            self.hass,
            ent2.entity_id,
            brightness=0,
            rgb_color=(255, 255, 255),
            white_value=0,
        )
        common.turn_on(self.hass,
                       ent3.entity_id,
                       brightness=0,
                       xy_color=(0.4, 0.6))

        self.hass.block_till_done()

        assert not light.is_on(self.hass, ent1.entity_id)
        assert not light.is_on(self.hass, ent2.entity_id)
        assert not light.is_on(self.hass, ent3.entity_id)

        _, data = ent1.last_call("turn_off")
        assert {light.ATTR_TRANSITION: 10} == data

        _, data = ent2.last_call("turn_off")
        assert {} == data

        _, data = ent3.last_call("turn_off")
        assert {} == data

        # One of the light profiles
        prof_name, prof_h, prof_s, prof_bri = "relax", 35.932, 69.412, 144

        # Test light profiles
        common.turn_on(self.hass, ent1.entity_id, profile=prof_name)
        # Specify a profile and a brightness attribute to overwrite it
        common.turn_on(self.hass,
                       ent2.entity_id,
                       profile=prof_name,
                       brightness=100)

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_on")
        assert {
            light.ATTR_BRIGHTNESS: prof_bri,
            light.ATTR_HS_COLOR: (prof_h, prof_s),
        } == data

        _, data = ent2.last_call("turn_on")
        assert {
            light.ATTR_BRIGHTNESS: 100,
            light.ATTR_HS_COLOR: (prof_h, prof_s),
        } == data

        # Test bad data
        common.turn_on(self.hass)
        common.turn_on(self.hass, ent1.entity_id, profile="nonexisting")
        common.turn_on(self.hass, ent2.entity_id, xy_color=["bla-di-bla", 5])
        common.turn_on(self.hass, ent3.entity_id, rgb_color=[255, None, 2])

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_on")
        assert {} == data

        _, data = ent2.last_call("turn_on")
        assert {} == data

        _, data = ent3.last_call("turn_on")
        assert {} == data

        # faulty attributes will not trigger a service call
        common.turn_on(self.hass,
                       ent1.entity_id,
                       profile=prof_name,
                       brightness="bright")
        common.turn_on(self.hass, ent1.entity_id, rgb_color="yellowish")
        common.turn_on(self.hass, ent2.entity_id, white_value="high")

        self.hass.block_till_done()

        _, data = ent1.last_call("turn_on")
        assert {} == data

        _, data = ent2.last_call("turn_on")
        assert {} == data
Exemple #40
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """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',
                'effect_list': ['colorloop', 'random'],
                'qos': 2,
                'payload_on': 'on',
                'payload_off': 'off'
            }
        }
        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.async_get_last_state',
                   return_value=mock_coro(fake_state)):
            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_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))

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

        common.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()
        common.turn_on(self.hass,
                       'light.test',
                       brightness=50,
                       xy_color=[0.123, 0.123])
        common.turn_on(self.hass,
                       'light.test',
                       rgb_color=[255, 128, 0],
                       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', '255,128,0', 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.14,0.131', 2, False),
        ],
                                                         any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((255, 128, 0), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
        self.assertEqual(80, state.attributes['white_value'])
        self.assertEqual((0.611, 0.375), state.attributes['xy_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
        common.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
        common.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
        common.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
        common.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'])
Exemple #42
0
    def test_methods(self):
        """Test if methods call the services as expected."""
        # Test is_on
        self.hass.states.set('light.test', STATE_ON)
        assert light.is_on(self.hass, 'light.test')

        self.hass.states.set('light.test', STATE_OFF)
        assert not light.is_on(self.hass, 'light.test')

        self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
        assert light.is_on(self.hass)

        self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF)
        assert not light.is_on(self.hass)

        # Test turn_on
        turn_on_calls = mock_service(
            self.hass, light.DOMAIN, SERVICE_TURN_ON)

        common.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',
            color_name='color_name_val',
            white_value='white_val')

        self.hass.block_till_done()

        assert 1 == len(turn_on_calls)
        call = turn_on_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TURN_ON == call.service
        assert 'entity_id_val' == call.data.get(ATTR_ENTITY_ID)
        assert 'transition_val' == call.data.get(light.ATTR_TRANSITION)
        assert 'brightness_val' == call.data.get(light.ATTR_BRIGHTNESS)
        assert 'rgb_color_val' == call.data.get(light.ATTR_RGB_COLOR)
        assert 'xy_color_val' == call.data.get(light.ATTR_XY_COLOR)
        assert 'profile_val' == call.data.get(light.ATTR_PROFILE)
        assert 'color_name_val' == call.data.get(light.ATTR_COLOR_NAME)
        assert 'white_val' == call.data.get(light.ATTR_WHITE_VALUE)

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

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

        self.hass.block_till_done()

        assert 1 == len(turn_off_calls)
        call = turn_off_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TURN_OFF == call.service
        assert 'entity_id_val' == call.data[ATTR_ENTITY_ID]
        assert 'transition_val' == call.data[light.ATTR_TRANSITION]

        # Test toggle
        toggle_calls = mock_service(
            self.hass, light.DOMAIN, SERVICE_TOGGLE)

        common.toggle(
            self.hass, entity_id='entity_id_val', transition='transition_val')

        self.hass.block_till_done()

        assert 1 == len(toggle_calls)
        call = toggle_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TOGGLE == call.service
        assert 'entity_id_val' == call.data[ATTR_ENTITY_ID]
        assert 'transition_val' == call.data[light.ATTR_TRANSITION]
Exemple #43
0
    def test_services(self):
        """Test the provided services."""
        platform = loader.get_component(self.hass, 'light.test')

        platform.init()
        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {CONF_PLATFORM: 'test'}})

        dev1, dev2, dev3 = platform.DEVICES

        # Test init
        assert light.is_on(self.hass, dev1.entity_id)
        assert not light.is_on(self.hass, dev2.entity_id)
        assert not light.is_on(self.hass, dev3.entity_id)

        # Test basic turn_on, turn_off, toggle services
        common.turn_off(self.hass, entity_id=dev1.entity_id)
        common.turn_on(self.hass, entity_id=dev2.entity_id)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, dev1.entity_id)
        assert light.is_on(self.hass, dev2.entity_id)

        # turn on all lights
        common.turn_on(self.hass)

        self.hass.block_till_done()

        assert light.is_on(self.hass, dev1.entity_id)
        assert light.is_on(self.hass, dev2.entity_id)
        assert light.is_on(self.hass, dev3.entity_id)

        # turn off all lights
        common.turn_off(self.hass)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, dev1.entity_id)
        assert not light.is_on(self.hass, dev2.entity_id)
        assert not light.is_on(self.hass, dev3.entity_id)

        # toggle all lights
        common.toggle(self.hass)

        self.hass.block_till_done()

        assert light.is_on(self.hass, dev1.entity_id)
        assert light.is_on(self.hass, dev2.entity_id)
        assert light.is_on(self.hass, dev3.entity_id)

        # toggle all lights
        common.toggle(self.hass)

        self.hass.block_till_done()

        assert not light.is_on(self.hass, dev1.entity_id)
        assert not light.is_on(self.hass, dev2.entity_id)
        assert not light.is_on(self.hass, dev3.entity_id)

        # Ensure all attributes process correctly
        common.turn_on(self.hass, dev1.entity_id,
                       transition=10, brightness=20, color_name='blue')
        common.turn_on(
            self.hass, dev2.entity_id, rgb_color=(255, 255, 255),
            white_value=255)
        common.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6))

        self.hass.block_till_done()

        _, data = dev1.last_call('turn_on')
        assert {
            light.ATTR_TRANSITION: 10,
            light.ATTR_BRIGHTNESS: 20,
            light.ATTR_HS_COLOR: (240, 100),
        } == data

        _, data = dev2.last_call('turn_on')
        assert {
            light.ATTR_HS_COLOR: (0, 0),
            light.ATTR_WHITE_VALUE: 255,
        } == data

        _, data = dev3.last_call('turn_on')
        assert {
            light.ATTR_HS_COLOR: (71.059, 100),
        } == data

        # One of the light profiles
        prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144

        # Test light profiles
        common.turn_on(self.hass, dev1.entity_id, profile=prof_name)
        # Specify a profile and a brightness attribute to overwrite it
        common.turn_on(
            self.hass, dev2.entity_id,
            profile=prof_name, brightness=100)

        self.hass.block_till_done()

        _, data = dev1.last_call('turn_on')
        assert {
            light.ATTR_BRIGHTNESS: prof_bri,
            light.ATTR_HS_COLOR: (prof_h, prof_s),
        } == data

        _, data = dev2.last_call('turn_on')
        assert {
            light.ATTR_BRIGHTNESS: 100,
            light.ATTR_HS_COLOR: (prof_h, prof_s),
        } == data

        # Test bad data
        common.turn_on(self.hass)
        common.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
        common.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
        common.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2])

        self.hass.block_till_done()

        _, data = dev1.last_call('turn_on')
        assert {} == data

        _, data = dev2.last_call('turn_on')
        assert {} == data

        _, data = dev3.last_call('turn_on')
        assert {} == data

        # faulty attributes will not trigger a service call
        common.turn_on(
            self.hass, dev1.entity_id,
            profile=prof_name, brightness='bright')
        common.turn_on(
            self.hass, dev1.entity_id,
            rgb_color='yellowish')
        common.turn_on(
            self.hass, dev2.entity_id,
            white_value='high')

        self.hass.block_till_done()

        _, data = dev1.last_call('turn_on')
        assert {} == data

        _, data = dev2.last_call('turn_on')
        assert {} == data
Exemple #44
0
    def test_methods(self):
        """Test if methods call the services as expected."""
        # Test is_on
        self.hass.states.set("light.test", STATE_ON)
        assert light.is_on(self.hass, "light.test")

        self.hass.states.set("light.test", STATE_OFF)
        assert not light.is_on(self.hass, "light.test")

        self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
        assert light.is_on(self.hass)

        self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF)
        assert not light.is_on(self.hass)

        # Test turn_on
        turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON)

        common.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",
            color_name="color_name_val",
            white_value="white_val",
        )

        self.hass.block_till_done()

        assert 1 == len(turn_on_calls)
        call = turn_on_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TURN_ON == call.service
        assert "entity_id_val" == call.data.get(ATTR_ENTITY_ID)
        assert "transition_val" == call.data.get(light.ATTR_TRANSITION)
        assert "brightness_val" == call.data.get(light.ATTR_BRIGHTNESS)
        assert "rgb_color_val" == call.data.get(light.ATTR_RGB_COLOR)
        assert "xy_color_val" == call.data.get(light.ATTR_XY_COLOR)
        assert "profile_val" == call.data.get(light.ATTR_PROFILE)
        assert "color_name_val" == call.data.get(light.ATTR_COLOR_NAME)
        assert "white_val" == call.data.get(light.ATTR_WHITE_VALUE)

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

        common.turn_off(self.hass,
                        entity_id="entity_id_val",
                        transition="transition_val")

        self.hass.block_till_done()

        assert 1 == len(turn_off_calls)
        call = turn_off_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TURN_OFF == call.service
        assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
        assert "transition_val" == call.data[light.ATTR_TRANSITION]

        # Test toggle
        toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE)

        common.toggle(self.hass,
                      entity_id="entity_id_val",
                      transition="transition_val")

        self.hass.block_till_done()

        assert 1 == len(toggle_calls)
        call = toggle_calls[-1]

        assert light.DOMAIN == call.domain
        assert SERVICE_TOGGLE == call.service
        assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
        assert "transition_val" == call.data[light.ATTR_TRANSITION]
Exemple #45
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """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',
            'effect_list': ['colorloop', 'random'],
            'qos': 2,
            'payload_on': 'on',
            'payload_off': 'off'
        }}
        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.async_get_last_state',
                   return_value=mock_coro(fake_state)):
            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_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))

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

        common.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()
        common.turn_on(self.hass, 'light.test',
                       brightness=50, xy_color=[0.123, 0.123])
        common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0],
                       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', '255,128,0', 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.14,0.131', 2, False),
        ], any_order=True)

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((255, 128, 0), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
        self.assertEqual(80, state.attributes['white_value'])
        self.assertEqual((0.611, 0.375), state.attributes['xy_color'])