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
def test_set_cover_position(self): """Test moving the cover to a specific position.""" state = self.hass.states.get(ENTITY_COVER) self.assertEqual(70, state.attributes.get('current_position')) cover.set_cover_position(self.hass, 10, ENTITY_COVER) self.hass.block_till_done() for _ in range(6): 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(10, state.attributes.get('current_position'))
def test_set_position_untemplated(self): """Test setting cover position via template.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP' } })) cover.set_cover_position(self.hass, 62, 'cover.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'position-topic', 62, 0, False)
def test_set_position(self): """Test the state text of a template.""" with assert_setup_component(1, 'cover'): assert setup.setup_component(self.hass, 'cover', { 'cover': { 'platform': 'template', 'covers': { 'test_template_cover': { 'position_template': "{{ 100 }}", 'open_cover': { 'service': 'cover.open_cover', 'entity_id': 'cover.test_state' }, 'close_cover': { 'service': 'cover.close_cover', 'entity_id': 'cover.test_state' }, 'stop_cover': { 'service': 'cover.stop_cover', 'entity_id': 'cover.test_state' }, '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.state == STATE_OPEN cover.set_cover_position(self.hass, 42, 'cover.test_template_cover') self.hass.block_till_done() assert len(self.calls) == 1
def test_set_cover_position(self): """Test set cover position function.""" with assert_setup_component(2, DOMAIN): assert setup.setup_component(self.hass, DOMAIN, CONFIG) cover.set_cover_position(self.hass, 50, COVER_GROUP) self.hass.block_till_done() for _ in range(4): 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), 50) self.assertEqual(self.hass.states.get(DEMO_COVER).state, STATE_CLOSED) self.assertEqual(self.hass.states.get(DEMO_COVER_POS) .attributes.get(ATTR_CURRENT_POSITION), 50) self.assertEqual(self.hass.states.get(DEMO_COVER_TILT) .attributes.get(ATTR_CURRENT_POSITION), 50)
def test_set_position_templated(self): """Test setting cover position via template.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'set_position_template': '{{100-62}}', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP' } })) cover.set_cover_position(self.hass, 100, 'cover.test') self.hass.block_till_done() self.assertEqual(('position-topic', '38', 0, False), self.mock_publish.mock_calls[-2][1])
def test_set_position_untemplated(self): """Test setting cover position via template.""" self.assertTrue( setup_component( self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP' } })) cover.set_cover_position(self.hass, 62, 'cover.test') self.hass.block_till_done() self.assertEqual(('position-topic', 62, 0, False), self.mock_publish.mock_calls[-2][1])
def test_set_position_templated(self): """Test setting cover position via template.""" self.assertTrue( setup_component( self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'set_position_template': '{{100-62}}', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP' } })) cover.set_cover_position(self.hass, 100, 'cover.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'position-topic', '38', 0, False)
def test_set_cover_position(self): """Test set cover position function.""" with assert_setup_component(2, DOMAIN): assert setup.setup_component(self.hass, DOMAIN, CONFIG) cover.set_cover_position(self.hass, 50, COVER_GROUP) self.hass.block_till_done() for _ in range(4): 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), 50) self.assertEqual(self.hass.states.get(DEMO_COVER).state, STATE_CLOSED) self.assertEqual( self.hass.states.get(DEMO_COVER_POS).attributes.get( ATTR_CURRENT_POSITION), 50) self.assertEqual( self.hass.states.get(DEMO_COVER_TILT).attributes.get( ATTR_CURRENT_POSITION), 50)
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