File tree 2 files changed +64
-1
lines changed
2 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -643,8 +643,17 @@ def query(self, query):
643
643
entries = response .json ()
644
644
_LOGGER .debug (f"Client | Query response: { entries } " )
645
645
items = {}
646
+
647
+ try :
648
+ # Determine the last_id by finding the maximum Id in the list
649
+ last_id = max (entry ["Id" ] for entry in entries )
650
+ except (TypeError , ValueError ) as err :
651
+ _LOGGER .error ("Client | Could not determine max Id from entries, defaulting to 0." )
652
+ _LOGGER .debug (f"Client | Error: { err } | Entries: { entries } " )
653
+ last_id = 0
654
+
646
655
result = {
647
- "last_id" : entries [ - 1 ][ "Id" ] ,
656
+ "last_id" : last_id ,
648
657
key_group : items ,
649
658
}
650
659
try :
Original file line number Diff line number Diff line change @@ -2562,3 +2562,57 @@ def test_client_get_alerts_missing_data(server):
2562
2562
with pytest .raises (ParseError ):
2563
2563
client .query (query .ALERTS )
2564
2564
assert len (server .calls ) == 1
2565
+
2566
+
2567
+ def test_client_query_last_id_unordered (server , mocker ):
2568
+ """Should determine the last_id correctly even if entries are unordered.
2569
+ Regression test for: https://github.com/palazzem/econnect-python/issues/154
2570
+ """
2571
+ html = """[
2572
+ {
2573
+ "Alarm": true,
2574
+ "MemoryAlarm": false,
2575
+ "Excluded": false,
2576
+ "InUse": true,
2577
+ "IsVideo": false,
2578
+ "Id": 3,
2579
+ "Index": 0,
2580
+ "Element": 1,
2581
+ "CommandId": 0,
2582
+ "InProgress": false
2583
+ },
2584
+ {
2585
+ "Alarm": false,
2586
+ "MemoryAlarm": false,
2587
+ "Excluded": true,
2588
+ "InUse": true,
2589
+ "IsVideo": false,
2590
+ "Id": 5,
2591
+ "Index": 2,
2592
+ "Element": 3,
2593
+ "CommandId": 0,
2594
+ "InProgress": false
2595
+ },
2596
+ {
2597
+ "Alarm": true,
2598
+ "MemoryAlarm": false,
2599
+ "Excluded": false,
2600
+ "InUse": true,
2601
+ "IsVideo": false,
2602
+ "Id": 2,
2603
+ "Index": 1,
2604
+ "Element": 2,
2605
+ "CommandId": 0,
2606
+ "InProgress": false
2607
+ }
2608
+ ]"""
2609
+ server .add (responses .POST , "https://example.com/api/inputs" , body = html , status = 200 )
2610
+ client = ElmoClient (base_url = "https://example.com" , domain = "domain" )
2611
+ client ._session_id = "test"
2612
+ mocker .patch .object (client , "_get_descriptions" )
2613
+ client ._get_descriptions .return_value = {
2614
+ 10 : {0 : "Input 1" , 1 : "Input 2" , 2 : "Input 3" },
2615
+ }
2616
+ # Test
2617
+ inputs = client .query (query .INPUTS )
2618
+ assert inputs ["last_id" ] == 5
You can’t perform that action at this time.
0 commit comments