def test_optimistic_state_change(self):
        """Test changing state optimistically."""
        self.assertTrue(setup_component(self.hass, cover.DOMAIN, {
            cover.DOMAIN: {
                'platform': 'mqtt',
                'name': 'test',
                'command_topic': 'command-topic',
                'qos': 0,
            }
        }))

        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)

        cover.open_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'OPEN', 0, False)
        self.mock_publish.async_publish.reset_mock()
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_OPEN, state.state)

        cover.close_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'CLOSE', 0, False)
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_CLOSED, state.state)
Beispiel #2
0
    def test_open_action(self):
        """Test the open_cover command."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ 0 }}",
                            'open_cover': {
                                'service': 'test.automation',
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

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

        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_CLOSED

        cover.open_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()

        assert len(self.calls) == 1
Beispiel #3
0
    def test_optimistic_state_change(self):
        """Test changing state optimistically."""
        self.assertTrue(
            setup_component(
                self.hass, cover.DOMAIN, {
                    cover.DOMAIN: {
                        'platform': 'mqtt',
                        'name': 'test',
                        'command_topic': 'command-topic',
                        'qos': 0,
                    }
                }))

        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)

        cover.open_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.assertEqual(('command-topic', 'OPEN', 0, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_OPEN, state.state)

        cover.close_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.assertEqual(('command-topic', 'CLOSE', 0, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_CLOSED, state.state)
    def test_stop_covers(self):
        """Test stop cover function."""
        with assert_setup_component(2, DOMAIN):
            assert setup.setup_component(self.hass, DOMAIN, CONFIG)

        cover.open_cover(self.hass, COVER_GROUP)
        self.hass.block_till_done()
        future = dt_util.utcnow() + timedelta(seconds=1)
        fire_time_changed(self.hass, future)
        self.hass.block_till_done()
        cover.stop_cover(self.hass, COVER_GROUP)
        self.hass.block_till_done()
        future = dt_util.utcnow() + timedelta(seconds=1)
        fire_time_changed(self.hass, future)
        self.hass.block_till_done()

        state = self.hass.states.get(COVER_GROUP)
        self.assertEqual(state.state, STATE_OPEN)
        self.assertEqual(state.attributes.get(ATTR_CURRENT_POSITION), 100)

        self.assertEqual(self.hass.states.get(DEMO_COVER).state, STATE_OPEN)
        self.assertEqual(self.hass.states.get(DEMO_COVER_POS)
                         .attributes.get(ATTR_CURRENT_POSITION), 20)
        self.assertEqual(self.hass.states.get(DEMO_COVER_TILT)
                         .attributes.get(ATTR_CURRENT_POSITION), 80)
Beispiel #5
0
    def test_optimistic_state_change(self):
        """Test changing state optimistically."""
        self.assertTrue(
            setup_component(
                self.hass, cover.DOMAIN, {
                    cover.DOMAIN: {
                        'platform': 'mqtt',
                        'name': 'test',
                        'command_topic': 'command-topic',
                        'qos': 0,
                    }
                }))

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

        cover.open_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'OPEN', 0, False)
        self.mock_publish.async_publish.reset_mock()
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_OPEN, state.state)

        cover.close_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'CLOSE', 0, False)
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_CLOSED, state.state)
Beispiel #6
0
    def test_stop_covers(self):
        """Test stop cover function."""
        with assert_setup_component(2, DOMAIN):
            assert setup.setup_component(self.hass, DOMAIN, CONFIG)

        cover.open_cover(self.hass, COVER_GROUP)
        self.hass.block_till_done()
        future = dt_util.utcnow() + timedelta(seconds=1)
        fire_time_changed(self.hass, future)
        self.hass.block_till_done()
        cover.stop_cover(self.hass, COVER_GROUP)
        self.hass.block_till_done()
        future = dt_util.utcnow() + timedelta(seconds=1)
        fire_time_changed(self.hass, future)
        self.hass.block_till_done()

        state = self.hass.states.get(COVER_GROUP)
        self.assertEqual(state.state, STATE_OPEN)
        self.assertEqual(state.attributes.get(ATTR_CURRENT_POSITION), 100)

        self.assertEqual(self.hass.states.get(DEMO_COVER).state, STATE_OPEN)
        self.assertEqual(
            self.hass.states.get(DEMO_COVER_POS).attributes.get(
                ATTR_CURRENT_POSITION), 20)
        self.assertEqual(
            self.hass.states.get(DEMO_COVER_TILT).attributes.get(
                ATTR_CURRENT_POSITION), 80)
Beispiel #7
0
    def test_send_open_cover_command(self):
        """Test the sending of open_cover."""
        self.hass.config.components = set(['mqtt'])
        self.assertTrue(
            setup_component(
                self.hass, cover.DOMAIN, {
                    cover.DOMAIN: {
                        'platform': 'mqtt',
                        'name': 'test',
                        'state_topic': 'state-topic',
                        'command_topic': 'command-topic',
                        'qos': 2
                    }
                }))

        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)

        cover.open_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.assertEqual(('command-topic', 'OPEN', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)
Beispiel #8
0
    def test_set_position(self):
        """Test the set_position command."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'input_number', {
               'input_number': {
                   'test': {
                       'min': '0',
                       'max': '100',
                       'initial': '42',
                   }
               }
            })
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ states.input_number.test.state | int }}",
                            'set_cover_position': {
                                'service': 'input_number.set_value',
                                'entity_id': 'input_number.test',
                                'data_template': {
                                    'value': '{{ position }}'
                                },
                            },
                        }
                    }
                }
            })

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

        state = self.hass.states.set('input_number.test', 42)
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_OPEN

        cover.open_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_position') == 100.0

        cover.close_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_position') == 0.0

        cover.set_cover_position(self.hass, 25,
                                 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_position') == 25.0
Beispiel #9
0
    def test_open_cover(self):
        """Test opening the cover."""
        state = self.hass.states.get(ENTITY_COVER)
        self.assertEqual(70, state.attributes.get('current_position'))
        cover.open_cover(self.hass, ENTITY_COVER)
        self.hass.block_till_done()
        for _ in range(7):
            future = dt_util.utcnow() + timedelta(seconds=1)
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()

        state = self.hass.states.get(ENTITY_COVER)
        self.assertEqual(100, state.attributes.get('current_position'))
Beispiel #10
0
    def test_open_cover(self):
        """Test opening the cover."""
        state = self.hass.states.get(ENTITY_COVER)
        self.assertEqual(70, state.attributes.get('current_position'))
        cover.open_cover(self.hass, ENTITY_COVER)
        self.hass.block_till_done()
        for _ in range(7):
            future = dt_util.utcnow() + timedelta(seconds=1)
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()

        state = self.hass.states.get(ENTITY_COVER)
        self.assertEqual(100, state.attributes.get('current_position'))
    def test_state_value(self):
        """Test with state value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'cover_status')
            test_cover = {
                'statecmd': 'cat {}'.format(path),
                'opencmd': 'echo 1 > {}'.format(path),
                'closecmd': 'echo 1 > {}'.format(path),
                'stopcmd': 'echo 0 > {}'.format(path),
                'value_template': '{{ value }}'
            }
            self.assertTrue(
                cover.setup(
                    self.hass, {
                        'cover': {
                            'platform': 'command_line',
                            'covers': {
                                'test': test_cover
                            }
                        }
                    }))

            state = self.hass.states.get('cover.test')
            self.assertEqual('unknown', state.state)

            cover.open_cover(self.hass, 'cover.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('open', state.state)

            cover.close_cover(self.hass, 'cover.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('open', state.state)

            cover.stop_cover(self.hass, 'cover.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('closed', state.state)
    def test_state_value(self):
        """Test with state value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'cover_status')
            test_cover = {
                'command_state': 'cat {}'.format(path),
                'command_open': 'echo 1 > {}'.format(path),
                'command_close': 'echo 1 > {}'.format(path),
                'command_stop': 'echo 0 > {}'.format(path),
                'value_template': '{{ value }}'
            }
            self.assertTrue(setup_component(self.hass, cover.DOMAIN, {
                'cover': {
                    'platform': 'command_line',
                    'covers': {
                        'test': test_cover
                    }
                }
            }))

            state = self.hass.states.get('cover.test')
            self.assertEqual('unknown', state.state)

            cover.open_cover(self.hass, 'cover.test')
            self.hass.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('open', state.state)

            cover.close_cover(self.hass, 'cover.test')
            self.hass.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('open', state.state)

            cover.stop_cover(self.hass, 'cover.test')
            self.hass.block_till_done()

            state = self.hass.states.get('cover.test')
            self.assertEqual('closed', state.state)
Beispiel #13
0
    def test_send_open_cover_command(self):
        """Test the sending of open_cover."""
        self.assertTrue(setup_component(self.hass, cover.DOMAIN, {
            cover.DOMAIN: {
                'platform': 'mqtt',
                'name': 'test',
                'state_topic': 'state-topic',
                'command_topic': 'command-topic',
                'qos': 2
            }
        }))

        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)

        cover.open_cover(self.hass, 'cover.test')
        self.hass.block_till_done()

        self.mock_publish.async_publish.assert_called_once_with(
            'command-topic', 'OPEN', 2, False)
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)
Beispiel #14
0
    def test_send_open_cover_command(self):
        """Test the sending of open_cover."""
        self.hass.config.components = ['mqtt']
        assert _setup_component(self.hass, cover.DOMAIN, {
            cover.DOMAIN: {
                'platform': 'mqtt',
                'name': 'test',
                'state_topic': 'state-topic',
                'command_topic': 'command-topic',
                'qos': 2
            }
        })

        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)

        cover.open_cover(self.hass, 'cover.test')
        self.hass.pool.block_till_done()

        self.assertEqual(('command-topic', 'OPEN', 2, False),
                         self.mock_publish.mock_calls[-1][1])
        state = self.hass.states.get('cover.test')
        self.assertEqual(STATE_UNKNOWN, state.state)
Beispiel #15
0
    def test_set_position_optimistic(self):
        """Test optimistic position mode."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'set_cover_position': {
                                'service': 'test.automation',
                            },
                        }
                    }
                }
            })
        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_position') is None

        cover.set_cover_position(self.hass, 42,
                                 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_position') == 42.0

        cover.close_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_CLOSED

        cover.open_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()
        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_OPEN