예제 #1
0
    def test_on_action(self):
        """Test on action."""
        assert setup.setup_component(
            self.hass,
            "switch",
            {
                "switch": {
                    "platform": "template",
                    "switches": {
                        "test_template_switch": {
                            "value_template": "{{ states.switch.test_state.state }}",
                            "turn_on": {"service": "test.automation"},
                            "turn_off": {
                                "service": "switch.turn_off",
                                "entity_id": "switch.test_state",
                            },
                        }
                    },
                }
            },
        )

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

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

        state = self.hass.states.get("switch.test_template_switch")
        assert state.state == STATE_OFF

        common.turn_on(self.hass, "switch.test_template_switch")
        self.hass.block_till_done()

        assert len(self.calls) == 1
예제 #2
0
    def test_state_none(self):
        """Test with none state."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'command_on': 'echo 1 > {}'.format(path),
                'command_off': 'echo 0 > {}'.format(path),
            }
            self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

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

            common.turn_on(self.hass, 'switch.test')
            self.hass.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_ON, state.state)

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

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
예제 #3
0
    def test_state_none(self):
        """Test with none state."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, "switch_status")
            test_switch = {
                "command_on": "echo 1 > {}".format(path),
                "command_off": "echo 0 > {}".format(path),
            }
            assert setup_component(
                self.hass,
                switch.DOMAIN,
                {
                    "switch": {
                        "platform": "command_line",
                        "switches": {
                            "test": test_switch
                        },
                    }
                },
            )

            state = self.hass.states.get("switch.test")
            assert STATE_OFF == state.state

            common.turn_on(self.hass, "switch.test")
            self.hass.block_till_done()

            state = self.hass.states.get("switch.test")
            assert STATE_ON == state.state

            common.turn_off(self.hass, "switch.test")
            self.hass.block_till_done()

            state = self.hass.states.get("switch.test")
            assert STATE_OFF == state.state
예제 #4
0
    def test_state_none(self):
        """Test with none state."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'command_on': 'echo 1 > {}'.format(path),
                'command_off': 'echo 0 > {}'.format(path),
            }
            assert setup_component(
                self.hass, switch.DOMAIN, {
                    'switch': {
                        'platform': 'command_line',
                        'switches': {
                            'test': test_switch
                        }
                    }
                })

            state = self.hass.states.get('switch.test')
            assert STATE_OFF == state.state

            common.turn_on(self.hass, 'switch.test')
            self.hass.block_till_done()

            state = self.hass.states.get('switch.test')
            assert STATE_ON == state.state

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

            state = self.hass.states.get('switch.test')
            assert STATE_OFF == state.state
예제 #5
0
    def test_state_json_value(self):
        """Test with state JSON value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            oncmd = json.dumps({'status': 'ok'})
            offcmd = json.dumps({'status': 'nope'})
            test_switch = {
                'command_state': 'cat {}'.format(path),
                'command_on': 'echo \'{}\' > {}'.format(oncmd, path),
                'command_off': 'echo \'{}\' > {}'.format(offcmd, path),
                'value_template': '{{ value_json.status=="ok" }}'
            }
            self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

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

            common.turn_on(self.hass, 'switch.test')
            self.hass.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_ON, state.state)

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

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
예제 #6
0
    def test_state_value(self):
        """Test with state value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'command_state': 'cat {}'.format(path),
                'command_on': 'echo 1 > {}'.format(path),
                'command_off': 'echo 0 > {}'.format(path),
                'value_template': '{{ value=="1" }}'
            }
            self.assertTrue(
                setup_component(
                    self.hass, switch.DOMAIN, {
                        'switch': {
                            'platform': 'command_line',
                            'switches': {
                                'test': test_switch
                            }
                        }
                    }))

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

            common.turn_on(self.hass, 'switch.test')
            self.hass.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_ON, state.state)

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

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
예제 #7
0
    def test_valid_hostname(self):
        """Test with valid hostname."""
        global TEST_STATE
        TEST_STATE = False
        self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
            'switch': {
                'platform': 'wake_on_lan',
                'mac_address': '00-01-02-03-04-05',
                'host': 'validhostname',
            }
        }))

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_OFF, state.state)

        TEST_STATE = True

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_ON, state.state)

        common.turn_off(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_ON, state.state)
예제 #8
0
    def test_state_value(self):
        """Test with state value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'command_state': 'cat {}'.format(path),
                'command_on': 'echo 1 > {}'.format(path),
                'command_off': 'echo 0 > {}'.format(path),
                'value_template': '{{ value=="1" }}'
            }
            assert setup_component(self.hass, switch.DOMAIN, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            })

            state = self.hass.states.get('switch.test')
            assert STATE_OFF == state.state

            common.turn_on(self.hass, 'switch.test')
            self.hass.block_till_done()

            state = self.hass.states.get('switch.test')
            assert STATE_ON == state.state

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

            state = self.hass.states.get('switch.test')
            assert STATE_OFF == state.state
예제 #9
0
    def test_on_action(self):
        """Test on action."""
        assert setup.setup_component(self.hass, 'switch', {
            'switch': {
                'platform': 'template',
                'switches': {
                    'test_template_switch': {
                        'value_template':
                            "{{ states.switch.test_state.state }}",
                        'turn_on': {
                            'service': 'test.automation'
                        },
                        'turn_off': {
                            'service': 'switch.turn_off',
                            'entity_id': 'switch.test_state'
                        },
                    }
                }
            }
        })

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

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

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_OFF

        common.turn_on(self.hass, 'switch.test_template_switch')
        self.hass.block_till_done()

        assert len(self.calls) == 1
예제 #10
0
파일: test_switch.py 프로젝트: 1e1/core-1
async def test_flux_after_sunrise_before_sunset_stop_next_day(
        hass, legacy_patchable_time):
    """
    Test the flux switch after sunrise and before sunset.

    This test has the stop_time on the next day (after midnight).
    """
    platform = getattr(hass.components, "test.light")
    platform.init()
    assert await async_setup_component(hass, light.DOMAIN,
                                       {light.DOMAIN: {
                                           CONF_PLATFORM: "test"
                                       }})
    await hass.async_block_till_done()

    ent1 = platform.ENTITIES[0]

    # Verify initial state of light
    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

    test_time = dt_util.utcnow().replace(hour=8, minute=30, 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:
            return sunrise_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],
                    "stop_time": "01:00",
                }
            },
        )
        await hass.async_block_till_done()
        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] == 173
    assert call.data[light.ATTR_XY_COLOR] == [0.439, 0.37]
예제 #11
0
async def test_flux_with_custom_colortemps(hass):
    """Test the flux with custom start and stop colortemps."""
    platform = getattr(hass.components, "test.light")
    platform.init()
    assert await async_setup_component(hass, light.DOMAIN,
                                       {light.DOMAIN: {
                                           CONF_PLATFORM: "test"
                                       }})

    dev1 = platform.DEVICES[0]

    # Verify initial state of light
    state = hass.states.get(dev1.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=17, minute=30, 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:
            return sunrise_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": [dev1.entity_id],
                    "start_colortemp": "1000",
                    "stop_colortemp": "6000",
                    "stop_time": "22:00",
                }
            },
        )
        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] == 159
    assert call.data[light.ATTR_XY_COLOR] == [0.469, 0.378]
예제 #12
0
    def test_flux_after_sunrise_before_sunset_stop_next_day(self):
        """
        Test the flux switch after sunrise and before sunset.

        This test has the stop_time on the next day (after midnight).
        """
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        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'))

        test_time = dt_util.utcnow().replace(hour=8, minute=30, 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':
                return sunrise_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],
                        'stop_time': '01:00'
                    }
                })
            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], 173)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37])
예제 #13
0
파일: test_switch.py 프로젝트: 1e1/core-1
async def test_flux_with_mired(hass, legacy_patchable_time):
    """Test the flux switch´s mode mired."""
    platform = getattr(hass.components, "test.light")
    platform.init()
    assert await async_setup_component(hass, light.DOMAIN,
                                       {light.DOMAIN: {
                                           CONF_PLATFORM: "test"
                                       }})
    await hass.async_block_till_done()

    ent1 = platform.ENTITIES[0]

    # Verify initial state of light
    state = hass.states.get(ent1.entity_id)
    assert STATE_ON == state.state
    assert state.attributes.get("color_temp") is None

    test_time = dt_util.utcnow().replace(hour=8, minute=30, 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:
            return sunrise_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],
                    "mode": "mired",
                }
            },
        )
        await hass.async_block_till_done()
        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_COLOR_TEMP] == 269
예제 #14
0
async def test_flux_after_sunrise_before_sunset_stop_next_day(hass):
    """
    Test the flux switch after sunrise and before sunset.

    This test has the stop_time on the next day (after midnight).
    """
    platform = getattr(hass.components, 'test.light')
    platform.init()
    assert await async_setup_component(hass, light.DOMAIN,
                                       {light.DOMAIN: {
                                           CONF_PLATFORM: 'test'
                                       }})

    dev1 = platform.DEVICES[0]

    # Verify initial state of light
    state = hass.states.get(dev1.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=8, minute=30, 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:
            return sunrise_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': [dev1.entity_id],
                    'stop_time': '01:00'
                }
            })
        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] == 173
    assert call.data[light.ATTR_XY_COLOR] == [0.439, 0.37]
예제 #15
0
    def test_flux_with_custom_colortemps(self):
        """Test the flux with custom start and stop colortemps."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        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'))

        test_time = dt_util.now().replace(hour=17, minute=30, 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':
                return sunrise_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],
                            'start_colortemp': '1000',
                            'stop_colortemp': '6000',
                            'stop_time': '22:00'
                        }
                    })
                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], 159)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.469, 0.378])
예제 #16
0
    def test_on_off(self):
        """Test turning the switch on and off."""
        assert self.switch().state == 'off'
        assert self.other_switch().state == 'off'

        assert not switch.is_on(self.hass, ENTITY_SWITCH)

        common.turn_on(self.hass, ENTITY_SWITCH)
        self.hass.block_till_done()
        self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)

        common.turn_off(self.hass, ENTITY_SWITCH)
        self.hass.block_till_done()
        self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
예제 #17
0
    def test_on_off(self):
        """Test turning the switch on and off."""
        assert self.switch().state == "off"
        assert self.other_switch().state == "off"

        assert not switch.is_on(self.hass, ENTITY_SWITCH)

        common.turn_on(self.hass, ENTITY_SWITCH)
        self.hass.block_till_done()
        self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)

        common.turn_off(self.hass, ENTITY_SWITCH)
        self.hass.block_till_done()
        self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
예제 #18
0
    def test_flux_with_rgb(self):
        """Test the flux switch´s mode rgb."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        state = self.hass.states.get(dev1.entity_id)
        self.assertEqual(STATE_ON, state.state)
        self.assertIsNone(state.attributes.get('color_temp'))

        test_time = dt_util.utcnow().replace(hour=8, minute=30, 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':
                return sunrise_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],
                        'mode': 'rgb'
                    }
                })
            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]
        rgb = (255, 198, 152)
        rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR]))
        self.assertEqual(rounded_call, rgb)
예제 #19
0
    def test_methods(self):
        """Test is_on, turn_on, turn_off methods."""
        self.assertTrue(
            setup_component(self.hass, switch.DOMAIN,
                            {switch.DOMAIN: {
                                CONF_PLATFORM: 'test'
                            }}))
        self.assertTrue(switch.is_on(self.hass))
        self.assertEqual(
            STATE_ON,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))

        common.turn_off(self.hass, self.switch_1.entity_id)
        common.turn_on(self.hass, self.switch_2.entity_id)

        self.hass.block_till_done()

        self.assertTrue(switch.is_on(self.hass))
        self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))

        # Turn all off
        common.turn_off(self.hass)

        self.hass.block_till_done()

        self.assertFalse(switch.is_on(self.hass))
        self.assertEqual(
            STATE_OFF,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))

        # Turn all on
        common.turn_on(self.hass)

        self.hass.block_till_done()

        self.assertTrue(switch.is_on(self.hass))
        self.assertEqual(
            STATE_ON,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))
예제 #20
0
    def test_flux_after_sunrise_before_sunset_stop_next_day(self):
        """
        Test the flux switch after sunrise and before sunset.

        This test has the stop_time on the next day (after midnight).
        """
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {CONF_PLATFORM: 'test'}}))

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        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'))

        test_time = dt_util.now().replace(hour=8, minute=30, 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':
                return sunrise_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],
                        'stop_time': '01:00'
                    }
                })
                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], 173)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.439, 0.37])
예제 #21
0
    def test_broadcast_config(self):
        """Test with broadcast address config."""
        self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
            'switch': {
                'platform': 'wake_on_lan',
                'mac_address': '00-01-02-03-04-05',
                'broadcast_address': '255.255.255.255',
            }
        }))

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_OFF, state.state)

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()
예제 #22
0
    def test_flux_before_sunrise_stop_next_day(self):
        """Test the flux switch before sunrise.

        This test has the stop_time on the next day (after midnight).
        """
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {CONF_PLATFORM: 'test'}})

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        state = self.hass.states.get(dev1.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=2, minute=30, 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:
                return sunrise_time
            return sunset_time

        with patch('homeassistant.components.flux.switch.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],
                    'stop_time': '01:00'
                }
            })
            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]
        assert call.data[light.ATTR_BRIGHTNESS] == 112
        assert call.data[light.ATTR_XY_COLOR] == [0.606, 0.379]
예제 #23
0
    def test_methods(self):
        """Test is_on, turn_on, turn_off methods."""
        self.assertTrue(setup_component(
            self.hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: 'test'}}
        ))
        self.assertTrue(switch.is_on(self.hass))
        self.assertEqual(
            STATE_ON,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))

        common.turn_off(self.hass, self.switch_1.entity_id)
        common.turn_on(self.hass, self.switch_2.entity_id)

        self.hass.block_till_done()

        self.assertTrue(switch.is_on(self.hass))
        self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))

        # Turn all off
        common.turn_off(self.hass)

        self.hass.block_till_done()

        self.assertFalse(switch.is_on(self.hass))
        self.assertEqual(
            STATE_OFF,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))

        # Turn all on
        common.turn_on(self.hass)

        self.hass.block_till_done()

        self.assertTrue(switch.is_on(self.hass))
        self.assertEqual(
            STATE_ON,
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
        self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
        self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))
예제 #24
0
    def test_flux_with_custom_brightness(self):
        """Test the flux with custom start and stop colortemps."""
        platform = getattr(self.hass.components, 'test.light')
        platform.init()
        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {CONF_PLATFORM: 'test'}})

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        state = self.hass.states.get(dev1.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=17, minute=30, 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:
                return sunrise_time
            return sunset_time

        with patch('homeassistant.components.flux.switch.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],
                    'brightness': 255,
                    'stop_time': '22:00'
                }
            })
            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]
        assert call.data[light.ATTR_BRIGHTNESS] == 255
        assert call.data[light.ATTR_XY_COLOR] == [0.506, 0.385]
예제 #25
0
async def test_flux_with_mired(hass):
    """Test the flux switch´s mode mired."""
    platform = getattr(hass.components, 'test.light')
    platform.init()
    assert await async_setup_component(hass, light.DOMAIN,
                                       {light.DOMAIN: {
                                           CONF_PLATFORM: 'test'
                                       }})

    dev1 = platform.DEVICES[0]

    # Verify initial state of light
    state = hass.states.get(dev1.entity_id)
    assert STATE_ON == state.state
    assert state.attributes.get('color_temp') is None

    test_time = dt_util.utcnow().replace(hour=8, minute=30, 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:
            return sunrise_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': [dev1.entity_id],
                    'mode': 'mired'
                }
            })
        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_COLOR_TEMP] == 269
예제 #26
0
    def test_flux_with_rgb(self):
        """Test the flux switch´s mode rgb."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        self.assertTrue(
            setup_component(self.hass, light.DOMAIN,
                            {light.DOMAIN: {CONF_PLATFORM: 'test'}}))

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        state = self.hass.states.get(dev1.entity_id)
        self.assertEqual(STATE_ON, state.state)
        self.assertIsNone(state.attributes.get('color_temp'))

        test_time = dt_util.now().replace(hour=8, minute=30, 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':
                return sunrise_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],
                        'mode': 'rgb'
                    }
                })
                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]
        rgb = (255, 198, 152)
        rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR]))
        self.assertEqual(rounded_call, rgb)
예제 #27
0
    def test_methods(self):
        """Test is_on, turn_on, turn_off methods."""
        assert setup_component(
            self.hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: 'test'}}
        )
        assert switch.is_on(self.hass)
        assert STATE_ON == \
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state
        assert switch.is_on(self.hass, self.switch_1.entity_id)
        assert not switch.is_on(self.hass, self.switch_2.entity_id)
        assert not switch.is_on(self.hass, self.switch_3.entity_id)

        common.turn_off(self.hass, self.switch_1.entity_id)
        common.turn_on(self.hass, self.switch_2.entity_id)

        self.hass.block_till_done()

        assert switch.is_on(self.hass)
        assert not switch.is_on(self.hass, self.switch_1.entity_id)
        assert switch.is_on(self.hass, self.switch_2.entity_id)

        # Turn all off
        common.turn_off(self.hass)

        self.hass.block_till_done()

        assert not switch.is_on(self.hass)
        assert STATE_OFF == \
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state
        assert not switch.is_on(self.hass, self.switch_1.entity_id)
        assert not switch.is_on(self.hass, self.switch_2.entity_id)
        assert not switch.is_on(self.hass, self.switch_3.entity_id)

        # Turn all on
        common.turn_on(self.hass)

        self.hass.block_till_done()

        assert switch.is_on(self.hass)
        assert STATE_ON == \
            self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state
        assert switch.is_on(self.hass, self.switch_1.entity_id)
        assert switch.is_on(self.hass, self.switch_2.entity_id)
        assert switch.is_on(self.hass, self.switch_3.entity_id)
예제 #28
0
    def test_flux_with_mired(self):
        """Test the flux switch´s mode mired."""
        platform = loader.get_component(self.hass, 'light.test')
        platform.init()
        assert setup_component(self.hass, light.DOMAIN,
                               {light.DOMAIN: {CONF_PLATFORM: 'test'}})

        dev1 = platform.DEVICES[0]

        # Verify initial state of light
        state = self.hass.states.get(dev1.entity_id)
        assert STATE_ON == state.state
        assert state.attributes.get('color_temp') is None

        test_time = dt_util.utcnow().replace(hour=8, minute=30, 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:
                return sunrise_time
            return sunset_time

        with patch('homeassistant.components.flux.switch.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],
                    'mode': 'mired'
                }
            })
            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]
        assert call.data[light.ATTR_COLOR_TEMP] == 269
예제 #29
0
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
    """Test the sending MQTT commands in optimistic mode."""
    fake_state = ha.State('switch.test', 'on')

    with patch(
            'homeassistant.helpers.restore_state.RestoreEntity'
            '.async_get_last_state',
            return_value=mock_coro(fake_state)):
        assert await async_setup_component(
            hass, switch.DOMAIN, {
                switch.DOMAIN: {
                    'platform': 'mqtt',
                    'name': 'test',
                    'command_topic': 'command-topic',
                    'payload_on': 'beer on',
                    'payload_off': 'beer off',
                    'qos': '2'
                }
            })

    state = hass.states.get('switch.test')
    assert STATE_ON == state.state
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.turn_on(hass, 'switch.test')
    await hass.async_block_till_done()

    mock_publish.async_publish.assert_called_once_with('command-topic',
                                                       'beer on', 2, False)
    mock_publish.async_publish.reset_mock()
    state = hass.states.get('switch.test')
    assert STATE_ON == state.state

    common.turn_off(hass, 'switch.test')
    await hass.async_block_till_done()
    await hass.async_block_till_done()

    mock_publish.async_publish.assert_called_once_with('command-topic',
                                                       'beer off', 2, False)
    state = hass.states.get('switch.test')
    assert STATE_OFF == state.state
예제 #30
0
파일: test_switch.py 프로젝트: 1e1/core-1
    def test_state_json_value(self):
        """Test with state JSON value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, "switch_status")
            oncmd = json.dumps({"status": "ok"})
            offcmd = json.dumps({"status": "nope"})
            test_switch = {
                "command_state": f"cat {path}",
                "command_on": f"echo '{oncmd}' > {path}",
                "command_off": f"echo '{offcmd}' > {path}",
                "value_template": '{{ value_json.status=="ok" }}',
            }
            assert setup_component(
                self.hass,
                switch.DOMAIN,
                {
                    "switch": {
                        "platform": "command_line",
                        "switches": {
                            "test": test_switch
                        },
                    }
                },
            )
            self.hass.block_till_done()

            state = self.hass.states.get("switch.test")
            assert STATE_OFF == state.state

            common.turn_on(self.hass, "switch.test")
            self.hass.block_till_done()

            state = self.hass.states.get("switch.test")
            assert STATE_ON == state.state

            common.turn_off(self.hass, "switch.test")
            self.hass.block_till_done()

            state = self.hass.states.get("switch.test")
            assert STATE_OFF == state.state
예제 #31
0
    def test_valid_hostname_windows(self):
        """Test with valid hostname on windows."""
        global TEST_STATE
        TEST_STATE = False
        assert setup_component(self.hass, switch.DOMAIN, {
            'switch': {
                'platform': 'wake_on_lan',
                'mac_address': '00-01-02-03-04-05',
                'host': 'validhostname',
            }
        })

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_OFF == state.state

        TEST_STATE = True

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_ON == state.state
예제 #32
0
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
    """Test the sending MQTT commands in optimistic mode."""
    fake_state = ha.State('switch.test', 'on')

    with patch('homeassistant.helpers.restore_state.RestoreEntity'
               '.async_get_last_state',
               return_value=mock_coro(fake_state)):
        assert await async_setup_component(hass, switch.DOMAIN, {
            switch.DOMAIN: {
                'platform': 'mqtt',
                'name': 'test',
                'command_topic': 'command-topic',
                'payload_on': 'beer on',
                'payload_off': 'beer off',
                'qos': '2'
            }
        })

    state = hass.states.get('switch.test')
    assert STATE_ON == state.state
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.turn_on(hass, 'switch.test')
    await hass.async_block_till_done()

    mock_publish.async_publish.assert_called_once_with(
        'command-topic', 'beer on', 2, False)
    mock_publish.async_publish.reset_mock()
    state = hass.states.get('switch.test')
    assert STATE_ON == state.state

    common.turn_off(hass, 'switch.test')
    await hass.async_block_till_done()
    await hass.async_block_till_done()

    mock_publish.async_publish.assert_called_once_with(
        'command-topic', 'beer off', 2, False)
    state = hass.states.get('switch.test')
    assert STATE_OFF == state.state
예제 #33
0
    def test_valid_hostname_windows(self):
        """Test with valid hostname on windows."""
        global TEST_STATE
        TEST_STATE = False
        assert setup_component(self.hass, switch.DOMAIN, {
            'switch': {
                'platform': 'wake_on_lan',
                'mac_address': '00-01-02-03-04-05',
                'host': 'validhostname',
            }
        })

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_OFF == state.state

        TEST_STATE = True

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_ON == state.state
예제 #34
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """Test the sending MQTT commands in optimistic mode."""
        fake_state = ha.State('switch.test', 'on')

        with patch('homeassistant.components.switch.mqtt.async_get_last_state',
                   return_value=mock_coro(fake_state)):
            assert setup_component(
                self.hass, switch.DOMAIN, {
                    switch.DOMAIN: {
                        'platform': 'mqtt',
                        'name': 'test',
                        'command_topic': 'command-topic',
                        'payload_on': 'beer on',
                        'payload_off': 'beer off',
                        'qos': '2'
                    }
                })

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

        common.turn_on(self.hass, 'switch.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'beer on', 2, False)
        self.mock_publish.async_publish.reset_mock()
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_ON, state.state)

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

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'beer off', 2, False)
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_OFF, state.state)
예제 #35
0
    def test_off_script(self):
        """Test with turn off script."""
        global TEST_STATE
        TEST_STATE = False
        assert setup_component(
            self.hass, switch.DOMAIN, {
                'switch': {
                    'platform': 'wake_on_lan',
                    'mac_address': '00-01-02-03-04-05',
                    'host': 'validhostname',
                    'turn_off': {
                        'service': 'shell_command.turn_off_target',
                    },
                }
            })
        calls = mock_service(self.hass, 'shell_command', 'turn_off_target')

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_OFF == state.state

        TEST_STATE = True

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_ON == state.state
        assert len(calls) == 0

        TEST_STATE = False

        common.turn_off(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        assert STATE_OFF == state.state
        assert len(calls) == 1
예제 #36
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """Test the sending MQTT commands in optimistic mode."""
        fake_state = ha.State('switch.test', 'on')

        with patch('homeassistant.components.switch.mqtt.async_get_last_state',
                   return_value=mock_coro(fake_state)):
            assert setup_component(self.hass, switch.DOMAIN, {
                switch.DOMAIN: {
                    'platform': 'mqtt',
                    'name': 'test',
                    'command_topic': 'command-topic',
                    'payload_on': 'beer on',
                    'payload_off': 'beer off',
                    'qos': '2'
                }
            })

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

        common.turn_on(self.hass, 'switch.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'beer on', 2, False)
        self.mock_publish.async_publish.reset_mock()
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_ON, state.state)

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

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'beer off', 2, False)
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_OFF, state.state)
예제 #37
0
    def test_off_script(self):
        """Test with turn off script."""
        global TEST_STATE
        TEST_STATE = False
        self.assertTrue(setup_component(self.hass, switch.DOMAIN, {
            'switch': {
                'platform': 'wake_on_lan',
                'mac_address': '00-01-02-03-04-05',
                'host': 'validhostname',
                'turn_off': {
                    'service': 'shell_command.turn_off_TARGET',
                },
            }
        }))
        calls = mock_service(self.hass, 'shell_command', 'turn_off_TARGET')

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_OFF, state.state)

        TEST_STATE = True

        common.turn_on(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_ON, state.state)
        assert len(calls) == 0

        TEST_STATE = False

        common.turn_off(self.hass, 'switch.wake_on_lan')
        self.hass.block_till_done()

        state = self.hass.states.get('switch.wake_on_lan')
        self.assertEqual(STATE_OFF, state.state)
        assert len(calls) == 1
예제 #38
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])