Beispiel #1
0
    def test_state_none(self):
        """Test with none state."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'oncmd': 'echo 1 > {}'.format(path),
                'offcmd': 'echo 0 > {}'.format(path),
            }
            self.assertTrue(switch.setup(self.hass, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

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

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

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

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

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
Beispiel #2
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 = {
                'statecmd': 'cat {}'.format(path),
                'oncmd': 'echo \'{}\' > {}'.format(oncmd, path),
                'offcmd': 'echo \'{}\' > {}'.format(offcmd, path),
                'value_template': '{{ value_json.status=="ok" }}'
            }
            self.assertTrue(switch.setup(self.hass, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

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

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

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

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

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
Beispiel #3
0
    def test_sending_mqtt_commands_and_optimistic(self):
        """Test the sending MQTT commands in optimistic mode."""
        self.hass.config.components = ['mqtt']
        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_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

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

        self.assertEqual(('command-topic', 'beer on', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_ON, state.state)

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

        self.assertEqual(('command-topic', 'beer off', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('switch.test')
        self.assertEqual(STATE_OFF, state.state)
Beispiel #4
0
    def test_methods(self):
        """Test is_on, turn_on, turn_off methods."""
        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))

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

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

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

        self.hass.pool.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))
Beispiel #5
0
    def test_flux_with_custom_colortemps(self):
        """Test the flux with custom start and stop colortemps."""
        platform = loader.get_component('light.test')
        platform.init()
        self.assertTrue(
            light.setup(self.hass, {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) + timedelta(days=1)

        with patch('blumate.util.dt.now', return_value=test_time):
            with patch('blumate.components.sun.next_rising',
                       return_value=sunrise_time):
                with patch('blumate.components.sun.next_setting',
                           return_value=sunset_time):
                    assert setup_component(self.hass, switch.DOMAIN, {
                        switch.DOMAIN: {
                            'platform': 'flux',
                            'name': 'flux',
                            'lights': [dev1.entity_id],
                            'start_colortemp': '1000',
                            'stop_colortemp': '6000'
                        }
                    })
                    turn_on_calls = mock_service(
                        self.hass, light.DOMAIN, SERVICE_TURN_ON)
                    switch.turn_on(self.hass, 'switch.flux')
                    self.hass.pool.block_till_done()
                    fire_time_changed(self.hass, test_time)
                    self.hass.pool.block_till_done()
        call = turn_on_calls[-1]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 167)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.461, 0.389])
Beispiel #6
0
    def _control_heating(self):
        """Check if we need to turn heating on or off."""
        if not self._active and None not in (self._cur_temp,
                                             self._target_temp):
            self._active = True
            _LOGGER.info('Obtained current and target temperature. '
                         'Heat control active.')

        if not self._active:
            return

        too_cold = self._target_temp - self._cur_temp > TOL_TEMP
        is_heating = self._is_heating

        if too_cold and not is_heating:
            _LOGGER.info('Turning on heater %s', self.heater_entity_id)
            switch.turn_on(self.hass, self.heater_entity_id)
        elif not too_cold and is_heating:
            _LOGGER.info('Turning off heater %s', self.heater_entity_id)
            switch.turn_off(self.hass, self.heater_entity_id)
Beispiel #7
0
    def test_flux_with_multiple_lights(self):
        """Test the flux switch with multiple light entities."""
        platform = loader.get_component('light.test')
        platform.init()
        self.assertTrue(
            light.setup(self.hass, {light.DOMAIN: {CONF_PLATFORM: 'test'}}))

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

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

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

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

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

        with patch('blumate.util.dt.now', return_value=test_time):
            with patch('blumate.components.sun.next_rising',
                       return_value=sunrise_time):
                with patch('blumate.components.sun.next_setting',
                           return_value=sunset_time):
                    assert setup_component(self.hass, switch.DOMAIN, {
                        switch.DOMAIN: {
                            'platform': 'flux',
                            'name': 'flux',
                            'lights': [dev1.entity_id,
                                       dev2.entity_id,
                                       dev3.entity_id]
                        }
                    })
                    turn_on_calls = mock_service(
                        self.hass, light.DOMAIN, SERVICE_TURN_ON)
                    switch.turn_on(self.hass, 'switch.flux')
                    self.hass.pool.block_till_done()
                    fire_time_changed(self.hass, test_time)
                    self.hass.pool.block_till_done()
        call = turn_on_calls[-1]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
        call = turn_on_calls[-2]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])
        call = turn_on_calls[-3]
        self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171)
        self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386])