def test_change_direction(self): """Test changing direction while travelling.""" travelcalculator = TravelCalculator(50, 25) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(60) travelcalculator.start_travel(80) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_DOWN) # change direction after two seconds mock_time.return_value = 1580000002.0 self.assertEqual(travelcalculator.current_position(), 64) travelcalculator.start_travel(48) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) self.assertEqual(travelcalculator.current_position(), 64) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000004.0 self.assertEqual(travelcalculator.current_position(), 56) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000006.0 self.assertEqual(travelcalculator.current_position(), 48) self.assertTrue(travelcalculator.position_reached())
def test_stop(self): """Test stopping.""" travelcalculator = TravelCalculator(25, 50) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(80) travelcalculator.start_travel(60) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) # stop aftert two seconds mock_time.return_value = 1580000002.0 travelcalculator.stop() mock_time.return_value = 1580000004.0 self.assertEqual(travelcalculator.current_position(), 76) self.assertTrue(travelcalculator.position_reached()) # restart after 1 additional second (3 seconds) mock_time.return_value = 1580000005.0 travelcalculator.start_travel(68) # running up for 6 seconds mock_time.return_value = 1580000006.0 self.assertEqual(travelcalculator.current_position(), 74) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000009.0 self.assertEqual(travelcalculator.current_position(), 68) self.assertTrue(travelcalculator.position_reached())
def test_travel_down_with_updates(self): """Test travel down with position updates from bus.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(40) travelcalculator.start_travel(100) # 15 seconds to reach 100 # time not changed, still at beginning assert travelcalculator.current_position() == 40 assert not travelcalculator.position_reached() assert travelcalculator.travel_direction == TravelStatus.DIRECTION_DOWN mock_time.return_value = 1580000002.0 assert travelcalculator.current_position() == 48 assert not travelcalculator.position_reached() # update from bus matching calculation travelcalculator.update_position(48) assert travelcalculator.current_position() == 48 assert not travelcalculator.position_reached() mock_time.return_value = 1580000010.0 assert travelcalculator.current_position() == 80 assert not travelcalculator.position_reached() # update from bus not matching calculation takes precedence (1 second slower) travelcalculator.update_position(76) assert travelcalculator.current_position() == 76 assert not travelcalculator.position_reached() # travel time extended by 1 second due to update from bus mock_time.return_value = 1580000015.0 assert travelcalculator.current_position() == 96 assert not travelcalculator.position_reached() mock_time.return_value = 1580000015.0 + 1 assert travelcalculator.current_position() == 100 assert travelcalculator.position_reached()
def test_travel_down(self): """Test travel up.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(40) travelcalculator.start_travel(60) # time not changed, still at beginning self.assertEqual(travelcalculator.current_position(), 40) self.assertFalse(travelcalculator.position_reached()) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_DOWN) mock_time.return_value = 1580000001.0 self.assertEqual(travelcalculator.current_position(), 44) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000002.0 self.assertEqual(travelcalculator.current_position(), 48) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000004.0 self.assertEqual(travelcalculator.current_position(), 56) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000005.0 # position reached self.assertEqual(travelcalculator.current_position(), 60) self.assertTrue(travelcalculator.position_reached()) mock_time.return_value = 1580000010.0 self.assertEqual(travelcalculator.current_position(), 60) self.assertTrue(travelcalculator.position_reached())
def test_is_opening_closing(self): """Test reports is_opening and is_closing.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 assert not travelcalculator.is_opening() assert not travelcalculator.is_closing() travelcalculator.set_position(80) assert not travelcalculator.is_opening() assert not travelcalculator.is_closing() mock_time.return_value = 1580000000.0 travelcalculator.start_travel_down() assert not travelcalculator.is_opening() assert travelcalculator.is_closing() mock_time.return_value = 1580000004.0 assert not travelcalculator.is_opening() assert travelcalculator.is_closing() mock_time.return_value = 1580000005.0 assert not travelcalculator.is_opening() assert not travelcalculator.is_closing() # up direction travelcalculator.start_travel(50) assert travelcalculator.is_opening() assert not travelcalculator.is_closing() mock_time.return_value = 1580000030.0 assert not travelcalculator.is_opening() assert not travelcalculator.is_closing()
def test_travel_up(self): """Test travel up.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(70) travelcalculator.start_travel(50) # time not changed, still at beginning assert travelcalculator.current_position() == 70 assert not travelcalculator.position_reached() assert travelcalculator.travel_direction == TravelStatus.DIRECTION_UP mock_time.return_value = 1580000002.0 assert travelcalculator.current_position() == 66 assert not travelcalculator.position_reached() mock_time.return_value = 1580000004.0 assert travelcalculator.current_position() == 62 assert not travelcalculator.position_reached() mock_time.return_value = 1580000008.0 assert travelcalculator.current_position() == 54 assert not travelcalculator.position_reached() mock_time.return_value = 1580000010.0 # position reached assert travelcalculator.current_position() == 50 assert travelcalculator.position_reached() mock_time.return_value = 1580000020.0 assert travelcalculator.current_position() == 50 assert travelcalculator.position_reached()
def test_set_position_after_travel(self): """Set explicit position after start_travel should stop traveling.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.start_travel(30) travelcalculator.set_position(80) self.assertTrue(travelcalculator.position_reached()) self.assertEqual(travelcalculator.current_position(), 80)
def test_stop(self): """Test stopping.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.set_position(60) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel(80) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) # stop aftert two seconds travelcalculator.time_set_from_outside = 1000 + 2 travelcalculator.stop() travelcalculator.time_set_from_outside = 1000 + 4 self.assertEqual(travelcalculator.current_position(), 64) self.assertTrue(travelcalculator.position_reached()) # restart after 1 additional second (3 seconds) travelcalculator.time_set_from_outside = 1000 + 5 travelcalculator.start_travel(68) # running up for 6 seconds travelcalculator.time_set_from_outside = 1000 + 6 self.assertEqual(travelcalculator.current_position(), 66) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 7 self.assertEqual(travelcalculator.current_position(), 68) self.assertTrue(travelcalculator.position_reached())
def test_change_direction(self): """Test changing direction while travelling.""" travelcalculator = TravelCalculator(50, 25) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(60) travelcalculator.start_travel(80) assert travelcalculator.travel_direction == TravelStatus.DIRECTION_DOWN # change direction after two seconds mock_time.return_value = 1580000002.0 assert travelcalculator.current_position() == 64 travelcalculator.start_travel(48) assert travelcalculator.travel_direction == TravelStatus.DIRECTION_UP assert travelcalculator.current_position() == 64 assert not travelcalculator.position_reached() mock_time.return_value = 1580000004.0 assert travelcalculator.current_position() == 56 assert not travelcalculator.position_reached() mock_time.return_value = 1580000006.0 assert travelcalculator.current_position() == 48 assert travelcalculator.position_reached()
def test_change_direction(self): """Test changing direction while travelling.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.set_position(60) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel(80) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) # change direction after two seconds travelcalculator.time_set_from_outside = 1000 + 2 self.assertEqual(travelcalculator.current_position(), 64) travelcalculator.start_travel(48) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_DOWN) self.assertEqual(travelcalculator.current_position(), 64) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 4 self.assertEqual(travelcalculator.current_position(), 56) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 6 self.assertEqual(travelcalculator.current_position(), 48) self.assertTrue(travelcalculator.position_reached())
def test_travel_up(self): """Test travel down.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.set_position(50) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel(70) # time not changed, still at beginning self.assertEqual(travelcalculator.current_position(), 50) self.assertFalse(travelcalculator.position_reached()) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) travelcalculator.time_set_from_outside = 1000 + 2 self.assertEqual(travelcalculator.current_position(), 54) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 4 self.assertEqual(travelcalculator.current_position(), 58) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 8 self.assertEqual(travelcalculator.current_position(), 66) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 10 # position reached self.assertEqual(travelcalculator.current_position(), 70) self.assertTrue(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 20 self.assertEqual(travelcalculator.current_position(), 70) self.assertTrue(travelcalculator.position_reached())
def test_stop(self): """Test stopping.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(80) travelcalculator.start_travel(60) assert travelcalculator.travel_direction == TravelStatus.DIRECTION_UP # stop aftert two seconds mock_time.return_value = 1580000002.0 travelcalculator.stop() mock_time.return_value = 1580000004.0 assert travelcalculator.current_position() == 76 assert travelcalculator.position_reached() # restart after 1 additional second (3 seconds) mock_time.return_value = 1580000005.0 travelcalculator.start_travel(68) # running up for 6 seconds mock_time.return_value = 1580000006.0 assert travelcalculator.current_position() == 74 assert not travelcalculator.position_reached() mock_time.return_value = 1580000009.0 assert travelcalculator.current_position() == 68 assert travelcalculator.position_reached()
def test_travel_up(self): """Test travel down.""" travelcalculator = TravelCalculator(25, 50) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(70) travelcalculator.start_travel(50) # time not changed, still at beginning self.assertEqual(travelcalculator.current_position(), 70) self.assertFalse(travelcalculator.position_reached()) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_UP) mock_time.return_value = 1580000002.0 self.assertEqual(travelcalculator.current_position(), 66) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000004.0 self.assertEqual(travelcalculator.current_position(), 62) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000008.0 self.assertEqual(travelcalculator.current_position(), 54) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000010.0 # position reached self.assertEqual(travelcalculator.current_position(), 50) self.assertTrue(travelcalculator.position_reached()) mock_time.return_value = 1580000020.0 self.assertEqual(travelcalculator.current_position(), 50) self.assertTrue(travelcalculator.position_reached())
def test_is_opening_closing(self): """Test reports is_opening and is_closing.""" travelcalculator = TravelCalculator(25, 50) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 self.assertFalse(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_closing()) travelcalculator.set_position(80) self.assertFalse(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_closing()) mock_time.return_value = 1580000000.0 travelcalculator.start_travel_down() self.assertFalse(travelcalculator.is_opening()) self.assertTrue(travelcalculator.is_closing()) mock_time.return_value = 1580000004.0 self.assertFalse(travelcalculator.is_opening()) self.assertTrue(travelcalculator.is_closing()) mock_time.return_value = 1580000005.0 self.assertFalse(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_closing()) # up direction travelcalculator.start_travel(50) self.assertTrue(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_closing()) mock_time.return_value = 1580000030.0 self.assertFalse(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_closing())
def __init__(self, device_id, name, travel_time_down, travel_time_up, move_group_address, stop_group_address, watch_move_group_addresses, watch_stop_group_addresses, send_stop_at_ends): """Initialize the cover.""" from xknx.devices import TravelCalculator self._travel_time_down = travel_time_down self._travel_time_up = travel_time_up self._move_group_address = move_group_address self._stop_group_address = stop_group_address self._watch_move_group_addresses = watch_move_group_addresses self._watch_stop_group_addresses = watch_stop_group_addresses self._send_stop_at_ends = send_stop_at_ends self._assume_uncertain_position = True self._target_position = 0 self._processing_known_position = False if name: self._name = name else: self._name = device_id self._unsubscribe_auto_updater = None self.tc = TravelCalculator(self._travel_time_down, self._travel_time_up)
def test_travel_down(self): """Test travel down.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(40) travelcalculator.start_travel(60) # time not changed, still at beginning assert travelcalculator.current_position() == 40 assert not travelcalculator.position_reached() assert travelcalculator.travel_direction == TravelStatus.DIRECTION_DOWN mock_time.return_value = 1580000001.0 assert travelcalculator.current_position() == 44 assert not travelcalculator.position_reached() mock_time.return_value = 1580000002.0 assert travelcalculator.current_position() == 48 assert not travelcalculator.position_reached() mock_time.return_value = 1580000004.0 assert travelcalculator.current_position() == 56 assert not travelcalculator.position_reached() mock_time.return_value = 1580000005.0 # position reached assert travelcalculator.current_position() == 60 assert travelcalculator.position_reached() mock_time.return_value = 1580000010.0 assert travelcalculator.current_position() == 60 assert travelcalculator.position_reached()
def test_travel_down(self): """Test travel up.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.set_position(60) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel(40) # time not changed, still at beginning self.assertEqual(travelcalculator.current_position(), 60) self.assertFalse(travelcalculator.position_reached()) self.assertEqual(travelcalculator.travel_direction, TravelStatus.DIRECTION_DOWN) travelcalculator.time_set_from_outside = 1000 + 1 self.assertEqual(travelcalculator.current_position(), 56) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 2 self.assertEqual(travelcalculator.current_position(), 52) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 4 self.assertEqual(travelcalculator.current_position(), 44) self.assertFalse(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 5 # position reached self.assertEqual(travelcalculator.current_position(), 40) self.assertTrue(travelcalculator.position_reached()) travelcalculator.time_set_from_outside = 1000 + 10 self.assertEqual(travelcalculator.current_position(), 40) self.assertTrue(travelcalculator.position_reached())
def test_init(self): """Test initial status.""" travelcalculator = TravelCalculator(25, 50) assert not travelcalculator.is_closed() assert not travelcalculator.is_closing() assert not travelcalculator.is_opening() assert not travelcalculator.is_traveling() assert travelcalculator.position_reached() assert travelcalculator.current_position() is None
def test_init(self): """Test initial status.""" travelcalculator = TravelCalculator(25, 50) self.assertFalse(travelcalculator.is_closed()) self.assertFalse(travelcalculator.is_closing()) self.assertFalse(travelcalculator.is_opening()) self.assertFalse(travelcalculator.is_traveling()) self.assertTrue(travelcalculator.position_reached()) self.assertIsNone(travelcalculator.current_position())
def __init__(self, device_id, rts_my_position, travel_time_down, travel_time_up, **device_config): """Initialize the cover.""" self._rts_my_position = rts_my_position self._travel_time_down = travel_time_down self._travel_time_up = travel_time_up # self.async_register_callbacks() self._unsubscribe_auto_updater = None super().__init__(device_id, None, **device_config) self.travel_calc = TravelCalculator(self._travel_time_down, self._travel_time_up)
def test_is_traveling(self): """Test if cover is traveling.""" travelcalculator = TravelCalculator(25, 50) self.assertFalse(travelcalculator.is_traveling()) travelcalculator.set_position(20) self.assertFalse(travelcalculator.is_traveling()) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel_down() travelcalculator.time_set_from_outside = 1004 self.assertTrue(travelcalculator.is_traveling()) travelcalculator.time_set_from_outside = 1005 self.assertFalse(travelcalculator.is_traveling())
def test_travel_full_down(self): """Test travelling to the full down position.""" travelcalculator = TravelCalculator(25, 50) travelcalculator.set_position(80) travelcalculator.time_set_from_outside = 1000 travelcalculator.start_travel_down() travelcalculator.time_set_from_outside = 1019 self.assertFalse(travelcalculator.position_reached()) self.assertFalse(travelcalculator.is_closed()) self.assertFalse(travelcalculator.is_open()) travelcalculator.time_set_from_outside = 1020 self.assertTrue(travelcalculator.position_reached()) self.assertTrue(travelcalculator.is_closed()) self.assertFalse(travelcalculator.is_open())
def test_travel_full_up(self): """Test travelling to the full up position.""" travelcalculator = TravelCalculator(25, 50) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(30) travelcalculator.start_travel_up() mock_time.return_value = 1580000014.0 self.assertFalse(travelcalculator.position_reached()) self.assertFalse(travelcalculator.is_closed()) self.assertFalse(travelcalculator.is_open()) mock_time.return_value = 1580000015.0 self.assertTrue(travelcalculator.position_reached()) self.assertTrue(travelcalculator.is_open()) self.assertFalse(travelcalculator.is_closed())
def test_travel_full_down(self): """Test travelling to the full down position.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(20) travelcalculator.start_travel_down() mock_time.return_value = 1580000019.0 assert not travelcalculator.position_reached() assert not travelcalculator.is_closed() assert not travelcalculator.is_open() mock_time.return_value = 1580000020.0 assert travelcalculator.position_reached() assert travelcalculator.is_closed() assert not travelcalculator.is_open()
def __init__(self, entity_id, rts_my_position, travel_time_down, travel_time_up, **device_config): """Initialize the cover.""" from xknx.devices import TravelCalculator self._rts_my_position = rts_my_position self._travel_time_down = travel_time_down self._travel_time_up = travel_time_up self._require_stop_cover = False # self.async_register_callbacks() self._unsubscribe_auto_updater = None super().__init__(entity_id, None, **device_config) self.tc = TravelCalculator(self._travel_time_down, self._travel_time_up)
def __init__(self, device_id, name, travel_time_down, travel_time_up, open_switch_entity_id, close_switch_entity_id): """Initialize the cover.""" from xknx.devices import TravelCalculator self._travel_time_down = travel_time_down self._travel_time_up = travel_time_up self._open_switch_entity_id = open_switch_entity_id self._close_switch_entity_id = close_switch_entity_id if name: self._name = name else: self._name = device_id self._unsubscribe_auto_updater = None self.tc = TravelCalculator(self._travel_time_down, self._travel_time_up)
def test_travel_up_with_updates(self): """Test travel up with position updates from bus.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 travelcalculator.set_position(70) travelcalculator.start_travel(50) # 10 seconds to reach 50 mock_time.return_value = 1580000005.0 assert travelcalculator.current_position() == 60 assert not travelcalculator.position_reached() # update from bus not matching calculation takes precedence (1 second faster) travelcalculator.update_position(58) assert travelcalculator.current_position() == 58 assert not travelcalculator.position_reached() # position reached 1 second earlier than predicted mock_time.return_value = 1580000010.0 - 1 assert travelcalculator.current_position() == 50 assert travelcalculator.position_reached()
def test_is_traveling(self): """Test if cover is traveling and position reached.""" travelcalculator = TravelCalculator(25, 50) with patch('time.time') as mock_time: mock_time.return_value = 1580000000.0 self.assertFalse(travelcalculator.is_traveling()) self.assertTrue(travelcalculator.position_reached()) travelcalculator.set_position(80) self.assertFalse(travelcalculator.is_traveling()) self.assertTrue(travelcalculator.position_reached()) mock_time.return_value = 1580000000.0 travelcalculator.start_travel_down() mock_time.return_value = 1580000004.0 self.assertTrue(travelcalculator.is_traveling()) self.assertFalse(travelcalculator.position_reached()) mock_time.return_value = 1580000005.0 self.assertFalse(travelcalculator.is_traveling()) self.assertTrue(travelcalculator.position_reached())
def test_is_traveling(self): """Test if cover is traveling and position reached.""" travelcalculator = TravelCalculator(25, 50) with patch("time.time") as mock_time: mock_time.return_value = 1580000000.0 assert not travelcalculator.is_traveling() assert travelcalculator.position_reached() travelcalculator.set_position(80) assert not travelcalculator.is_traveling() assert travelcalculator.position_reached() mock_time.return_value = 1580000000.0 travelcalculator.start_travel_down() mock_time.return_value = 1580000004.0 assert travelcalculator.is_traveling() assert not travelcalculator.position_reached() mock_time.return_value = 1580000005.0 assert not travelcalculator.is_traveling() assert travelcalculator.position_reached()
def __init__(self, device_id, name, travel_time_down, travel_time_up, open_script_entity_id, close_script_entity_id, stop_script_entity_id, send_stop_at_ends): """Initialize the cover.""" from xknx.devices import TravelCalculator self._travel_time_down = travel_time_down self._travel_time_up = travel_time_up self._open_script_entity_id = open_script_entity_id self._close_script_entity_id = close_script_entity_id self._stop_script_entity_id = stop_script_entity_id self._send_stop_at_ends = send_stop_at_ends self._assume_uncertain_position = True self._target_position = 0 self._processing_known_position = False if name: self._name = name else: self._name = device_id self._unsubscribe_auto_updater = None self.tc = TravelCalculator(self._travel_time_down, self._travel_time_up)