Skip to content

Client

The protocol client: discovery, polling, mTLS transport and retry.

py20305.client.csip_client

High-level CSIP client with lifecycle, polling, and 404 recovery.

CsipClient

Async IEEE 2030.5 CSIP client with discovery, polling, and shutdown.

Several processes can act as one client; see the guide "Running one client in several processes". device_filter limits the EndDevices this process acts for, own_end_device says whether it does the client's own-EndDevice work, and request_limiter paces its requests against a shared budget. event_state_store shares the event state of each EndDevice, so another process can take a device over (see :meth:take_over_devices).

Source code in src/py20305/client/csip_client.py
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
class CsipClient:
    """Async IEEE 2030.5 CSIP client with discovery, polling, and shutdown.

    Several processes can act as one client; see the guide "Running one client
    in several processes". ``device_filter`` limits the EndDevices this process
    acts for, ``own_end_device`` says whether it does the client's own-EndDevice
    work, and ``request_limiter`` paces its requests against a shared budget.
    ``event_state_store`` shares the event state of each EndDevice, so another
    process can take a device over (see :meth:`take_over_devices`).
    """

    def __init__(
        self,
        base_url: str,
        *,
        tls: TlsConfig | None = None,
        retry: RetryPolicy | None = None,
        dispatcher: ControlDispatcher | None = None,
        subscription_manager: SubscriptionManager | None = None,
        notification_server: NotificationServer | None = None,
        on_structural_change: Callable[[], Awaitable[None]] | None = None,
        on_device_removed: Callable[[str], Awaitable[None]] | None = None,
        profile: ServerProfile | None = None,
        always_send_alarm_status: bool = False,
        request_headers: dict[str, str] | None = None,
        dcap_path: str = "/dcap",
        heartbeat_enabled: bool = True,
        heartbeat_seconds: float | None = None,
        on_reconnect: Callable[[], None] | None = None,
        connection_heartbeat_seconds: int = 120,
        reconcile_enabled: bool = True,
        reconcile_interval_seconds: int = 0,
        renewal_interval_seconds: int = 86400,
        group_lookup: Callable[[str], list[str] | None] | None = None,
        registration_pins: dict[str, int] | None = None,
        comms_loss_seconds: int = 0,
        comms_loss_eval_seconds: int = 30,
        use_server_time: bool = True,
        time_drift_warn_seconds: int = 30,
        fsa_stale_seconds: float | None = None,
        pricing_enabled: bool = False,
        server_2018_compat: bool | None = None,
        device_filter: DeviceFilter | None = None,
        own_end_device: bool = True,
        request_limiter: RequestLimiter | None = None,
        event_state_store: EventStateStore | None = None,
    ) -> None:
        #: Application-level server timebase :
        #: time-of-day-sensitive operations follow the head-end's Time resource;
        #: the OS clock is never touched. Shared via Sep2Client (discovery,
        #: telemetry) and EventProcessor.
        self._timebase = ServerTimebase(
            enabled=use_server_time,
            drift_warn_seconds=time_drift_warn_seconds,
            fsa_stale_seconds=fsa_stale_seconds,
        )
        self._http = Sep2Client(
            base_url,
            tls=tls,
            retry=retry,
            profile=profile_for_constructor(profile, server_2018_compat),
            always_send_alarm_status=always_send_alarm_status,
            request_headers=request_headers,
            timebase=self._timebase,
            request_limiter=request_limiter,
        )
        self._state = DiscoveredState()
        #: The EndDevices this process acts for; None is every one.
        self._device_filter = device_filter
        #: Whether this process does the client's own-EndDevice work. See
        #: the ``own_end_device`` property.
        self._own_end_device = own_end_device
        #: Pricing function set opt-in (config-driven). Preserved across
        #: rediscovery (state.clear() keeps it) and gates tariff discovery/poll.
        self._state.pricing_enabled = pricing_enabled
        self._heartbeat_enabled = heartbeat_enabled
        self._heartbeat_seconds = heartbeat_seconds
        #: Called when the server is reachable again after a sustained loss of
        #: contact, before the rediscovery that follows, so the host can treat
        #: what it sent before the outage as possibly lost.
        self._on_reconnect = on_reconnect
        #: Library components told the same, ahead of ``on_reconnect``; see
        #: ``add_reconnect_listener``.
        self._reconnect_listeners: list[Callable[[], None]] = []
        self._connection_heartbeat_seconds = connection_heartbeat_seconds
        self._reconcile_enabled = reconcile_enabled
        self._reconcile_interval_seconds = reconcile_interval_seconds
        self._renewal_interval_seconds = renewal_interval_seconds
        self._scheduler = self._new_scheduler()
        # Monotonic time each aggregation EndDevice was last read by the "adev" poll.
        self._aggregation_reads: dict[str, float] = {}
        self._shutdown_event = asyncio.Event()
        self._rediscovery_lock = asyncio.Lock()
        self._state_ready = asyncio.Event()
        self._state_ready.set()  # state is valid initially
        self._dispatcher: ControlDispatcher = dispatcher or NullDispatcher()
        #: Loss-of-communications mode. Shared with the EventProcessor so the
        #: time-based detector here can flip it and the processor gates control
        #: application on it. Zero seconds disables detection entirely.
        if comms_loss_seconds > 0 and comms_loss_eval_seconds <= 0:
            # A non-positive cadence would make the scheduler busy-loop
            # (wait_for timeout of 0). The configuration model enforces ge=1; this
            # guards direct constructions.
            raise ValueError(
                "comms_loss_eval_seconds must be positive when comms-loss "
                "detection is enabled (comms_loss_seconds > 0)"
            )
        self._comms_loss_seconds = comms_loss_seconds
        self._comms_loss_eval_seconds = comms_loss_eval_seconds
        self._comms_loss = CommsLossState()
        #: Expiry timer for an operator-triggered outage simulation. A window is
        #: never open-ended: a forgotten one would strand a live site at the
        #: planning limit.
        self._comm_loss_simulation_expiry: asyncio.Task[None] | None = None
        #: Pending follow-up re-polls for suppressed duplicate notifications,
        #: keyed by subscribed resource. See _schedule_notification_followup().
        self._notification_followups: dict[str, asyncio.Task[None]] = {}
        #: Every follow-up task until it finishes, including one already routing
        #: its re-poll, so shutdown can cancel and await it.
        self._live_followups: set[asyncio.Task[None]] = set()
        #: Serializes simulate/clear/expiry. Without it a timer that has already
        #: begun clearing can finish against a window someone else has since
        #: opened, marking a live simulation recovered.
        self._comm_loss_lifecycle = asyncio.Lock()
        #: Incremented per window, so an older timer can tell it woke too late.
        self._comm_loss_generation = 0
        #: The client's own certificate LFDI, captured at connect() for in-band
        #: self-reregistration on comms-loss recovery. Cert-derived and stable.
        self._own_lfdi: str | None = None
        #: Audit trail for event lifecycle and loss-of-communications records.
        #: Disabled until the host attaches a history or an audit observer.
        self._audit = AuditEmitter()
        self._history: History | None = None
        #: Whether the current comms-loss episode was a simulation, so the
        #: clear record attributes it the same way the entry record did.
        self._comms_loss_simulated = False
        #: Monotonic time the current comms-loss episode began, for how long it
        #: lasted. Monotonic because the server timebase can move under it.
        self._comms_loss_entered_monotonic: float | None = None
        self._event_processor = EventProcessor(
            self._http,
            self._state,
            self._dispatcher,
            self._shutdown_event,
            state_ready=self._state_ready,
            group_lookup=group_lookup,
            comms_loss=self._comms_loss,
            timebase=self._timebase,
            audit=self._audit,
            state_store=event_state_store,
        )
        #: Pricing function set: relays active-interval prices to connectors.
        #: Constructed unconditionally but only driven by the tariff poll when
        #: pricing is enabled (see _start_polls).
        self._tariff_processor = TariffProcessor(
            self._http, self._state, self._dispatcher, timebase=self._timebase
        )
        #: Serializes tariff refreshes and processing. Re-polls from the
        #: scheduler, from notifications and from the boundary timer would
        #: otherwise interleave, and an older read could be relayed after a
        #: newer one.
        self._tariff_lock = asyncio.Lock()
        #: Runs the tariff processor at the next interval boundary. See
        #: _arm_tariff_timer().
        self._tariff_timer: asyncio.Task[None] | None = None
        self._subscription_manager = subscription_manager
        self._notification_server = notification_server
        self._on_structural_change = on_structural_change
        self._on_rediscovered: Callable[[], Awaitable[None]] | None = None
        self._on_device_removed = on_device_removed
        #: The exception that ended the last failed rediscovery pass, or None.
        self._rediscovery_error: Exception | None = None
        #: Rediscovery passes started, and the number of the last one that
        #: completed. take_over_devices checks a pass started after it did.
        self._rediscovery_passes = 0
        self._last_completed_pass = 0
        #: The hrefs this process acted for when the running rediscovery began.
        #: A lifecycle notification during the rediscovery is matched against it.
        self._scope_before_rediscovery: set[str] = set()
        self._dcap_path = dcap_path
        #: LFDI hex -> expected registration PIN. Verified once during the
        #: initial connect() discovery (not on rediscovery). Empty/None
        #: disables the check.
        self._registration_pins = registration_pins or None
        self._renewal_task: asyncio.Task[None] | None = None
        #: Gap 4: Track consecutive poll failures for connectivity recovery
        self._poll_failure_count = 0
        #: Set when trigger_rediscovery() is called while rediscovery is already running.
        #: The running rediscovery will loop once more after completing.
        self._rediscovery_pending = False
        #: One-shot guard for the "no SubscriptionListLink anywhere" warning.
        #: ``_auto_subscribe`` is called on every poll (default 900s) and after every
        #: rediscovery; without this guard a server that never exposes the link would
        #: emit a WARNING ~96 times/day. Reset to False once a link is discovered so a
        #: subsequent server-side fix is also logged (and a regression re-warns).
        self._no_sub_link_warned = False

    @property
    def device_filter(self) -> DeviceFilter | None:
        """The filter choosing the EndDevices this process acts for; None is every one.

        Read at each discovery, so a filter whose answers change (a
        :class:`~py20305.client.scope.DeviceSet` the host edits) takes effect at
        the next rediscovery.
        """
        return self._device_filter

    @property
    def own_end_device(self) -> bool:
        """Whether this process does the work of the client's own EndDevice.

        That work is: renewing the subscriptions, reconciling them with the
        server's SubscriptionList, checking restored subscriptions at connect,
        renewing adopted subscriptions and removing duplicate ones, cancelling
        subscriptions at rediscovery and at shutdown, registering EndDevices,
        re-registering the client's own EndDevice after a loss of
        communications, and the telemetry a
        :class:`~py20305.telemetry.coordinator.TelemetryCoordinator` posts for
        the client's own LFDI. True, the default, does all of it.
        When several processes act as one client, give it to one of them.
        A process without it still creates subscriptions for the resources it
        needs that nobody has subscribed to yet.
        """
        return self._own_end_device

    @property
    def dispatcher(self) -> ControlDispatcher:
        """The dispatcher this client applies controls through."""
        return self._dispatcher

    @property
    def audit(self) -> AuditEmitter:
        """The audit trail emitter. Attach a history or an audit observer to enable it."""
        return self._audit

    @property
    def observers(self) -> Observers:
        """The exchange, connection and anomaly observers attached to this client."""
        return self._http.observers

    def attach_observers(self, observers: Observers | None) -> None:
        """Report this client's exchanges, connections and anomalies to ``observers``.

        Covers the requests the client sends and, once subscriptions are
        attached, the notifications its listener receives. None detaches them.
        """
        self._http.observers = observers
        if self._notification_server is not None:
            self._notification_server.observers = self._http.observers

    @property
    def history(self) -> History | None:
        """The operations history attached to this client, if any."""
        return self._history

    def attach_history(self, history: History | None) -> None:
        """Keep this client's event lifecycle and posted responses in ``history``.

        Only the event part is fed from here. The dispatcher feeds the write
        part and the telemetry manager the interval part; hand those parts to
        them. Device writes are put on this client's server timebase.
        """
        self._history = history
        events = history.events if history is not None else None
        self._audit.attach_history(events)
        self._event_processor.attach_event_history(events)
        if history is not None:
            history.use_timebase(self._timebase)

    @property
    def subscription_manager(self) -> SubscriptionManager | None:
        """The subscription manager, when subscriptions are wired."""
        return self._subscription_manager

    def attach_subscriptions(
        self,
        manager: SubscriptionManager,
        notification_server: NotificationServer | None = None,
    ) -> None:
        """Wire subscribe/notify into this client after construction.

        The manager needs this client's transport, which exists only once the
        client does, so a caller building both cannot pass them to
        ``__init__`` -- construction order forces attachment to come second.
        Attach before ``connect()``: auto-subscription and poll suppression
        consult the manager during connection.

        In a client without own-EndDevice work (see :attr:`own_end_device`)
        this sets the manager's ``maintains_set`` to False, so the manager
        never renews, cancels or removes the shared subscriptions.
        """
        if not self._own_end_device:
            manager.maintains_set = False
        self._subscription_manager = manager
        if notification_server is not None:
            self._notification_server = notification_server
            notification_server.on_notification = self._handle_notification
            notification_server.observers = self._http.observers

    def set_on_structural_change(self, callback: Callable[[], Awaitable[None]] | None) -> None:
        """Set the structural-change callback after construction.

        The same construction-order problem as ``attach_subscriptions``, one
        step later: telemetry is started once discovery has produced the hrefs
        it posts to, so what wants to hear about a later structural change does
        not exist when this client is built.

        Note what this callback replaces. Where a structural notification would
        otherwise call ``trigger_rediscovery()``, it calls this instead, so a
        callback that does not rediscover leaves the state it was called about
        unrefreshed. To act on rebuilt state, use ``set_on_rediscovered``.
        """
        self._on_structural_change = callback

    def set_on_rediscovered(self, callback: Callable[[], Awaitable[None]] | None) -> None:
        """Set a callback to run after any rediscovery pass that completed.

        Every path that rebuilds discovered state ends here -- a structural
        notification, 404 recovery, comms-loss recovery, a subscription lost,
        an operator's request through the management API -- so anything holding
        hrefs from the previous state refreshes once, in one place, rather than
        each trigger remembering to say so.

        Not called when discovery raised, because the state was not rebuilt,
        nor when the call coalesced into a running rediscovery, because that
        one calls it on completion.
        """
        self._on_rediscovered = callback

    def add_reconnect_listener(self, callback: Callable[[], None]) -> None:
        """Also call ``callback`` when ``on_reconnect`` would be called.

        For a component built after this client, such as the telemetry
        coordinator, that has to treat what it sent before an outage as lost.
        Listeners run in the order added, before ``on_reconnect``, and one that
        raises is logged without stopping the others or the rediscovery.
        """
        self._reconnect_listeners.append(callback)

    @property
    def http(self) -> Sep2Client:
        return self._http

    @property
    def state(self) -> DiscoveredState:
        return self._state

    @property
    def timebase(self) -> ServerTimebase:
        return self._timebase

    def aggregation(self, lfdi: str) -> AggregationInfo | None:
        """Return the aggregation the discovered EndDevice ``lfdi`` represents, or None.

        None when no discovered EndDevice has that LFDI or the EndDevice
        describes no aggregation (IEEE 2030.5-2023 §8.7). Read from the
        discovered state: the EndDevice's ``distribution``, its
        AggregatedDeviceList and, for a Priority distribution, its
        AggregationPriority (Annex C.3), as of the last discovery or EndDevice
        list refresh.
        """
        wanted = bytes.fromhex(lfdi)
        for edev_state in self._state.end_devices.values():
            if edev_state.lfdi == wanted:
                return edev_state.aggregation
        return None

    async def register_aggregated_device(
        self,
        *,
        aggregated_device_list_href: str,
        lfdi: str,
        device_category: int | None = None,
        check_duplicate: bool = True,
    ) -> str | None:
        """POST an AggregatedDevice to an AggregatedDeviceList and return its ``Location``.

        IEEE 2030.5-2023 §8.7.1 lets a client create an AggregatedDevice by a POST
        to the list, where the server allows it (Annex A.2.5.2: POST optional).
        ``lfdi`` is a 40-hex string; the ``sFDI`` is derived from it. With
        ``check_duplicate``, the list is first read with the ``sFDI`` query
        string parameter (§8.7.3.2), and an LFDI the list already holds raises
        ``ValueError``: clients SHALL NOT POST an AggregatedDevice the list
        already contains (§8.7.3.1). Raises ``Sep2ProtocolError`` on a server
        rejection status and ``RuntimeError`` in a process without
        own-EndDevice duties (see :attr:`own_end_device`).
        """
        if not self._own_end_device:
            raise RuntimeError(
                "this client process does not do own-EndDevice work; register devices "
                "from the process that does"
            )
        from py20305.models.sep.sep import (
            AggregatedDevice,
            AggregatedDeviceList,
            DeviceCategoryType,
            Sfditype,
            TimeType,
        )
        from py20305.security.identity import compute_sfdi

        # An LFDI is 160 bits: 40 hex characters. A shorter or longer string
        # would register a different identity.
        try:
            lfdi_bytes = bytes.fromhex(lfdi)
        except ValueError:
            raise ValueError(f"lfdi must be 40 hexadecimal characters, got {lfdi!r}") from None
        if len(lfdi_bytes) != 20 or len(lfdi) != 40:
            raise ValueError(f"lfdi must be 40 hexadecimal characters, got {lfdi!r}")
        sfdi = compute_sfdi(lfdi)
        if device_category is not None and not 0 <= device_category < 2**32:
            raise ValueError("device_category must be between 0 and 4294967295 (32-bit bitmap)")
        if check_duplicate:
            separator = "&" if "?" in aggregated_device_list_href else "?"
            query = f"{aggregated_device_list_href}{separator}sFDI={sfdi}"
            for page in await self._http.get_list(query, AggregatedDeviceList):
                if any(device.l_fdi == lfdi_bytes for device in page.aggregated_device):
                    raise ValueError(f"An AggregatedDevice with LFDI {lfdi} is already listed")
        device = AggregatedDevice(
            l_fdi=lfdi_bytes,
            s_fdi=Sfditype(value=sfdi),
            changed_time=TimeType(value=int(self._timebase.now())),
        )
        if device_category is not None:
            device.device_category = DeviceCategoryType(value=device_category.to_bytes(4, "big"))
        return await self._http.post(aggregated_device_list_href, device)

    async def run_redirect_probe(self) -> dict[str, Any]:
        """Run ERR-001 HTTP-to-HTTPS redirect probe using this client's session."""
        from py20305.client.redirect_probe import run_redirect_probe

        return await run_redirect_probe(self._http.host, self._http)

    async def register_end_device(
        self,
        *,
        lfdi: str,
        device_category: int | None = None,
        edev_list_href: str | None = None,
        check_duplicate: bool = True,
    ) -> str | None:
        """In-band registration: POST a managed EndDevice to the server's
        EndDeviceList and return the ``Location`` (the new ``/edev/{id}`` href).

        ``lfdi`` is a 40-hex string; ``sFDI`` is derived from it (never
        free-entered). ``device_category`` is an optional DeviceCategoryType
        bitmap. The target list defaults to the discovered
        ``dcap.end_device_list_link`` unless ``edev_list_href`` overrides it.

        Raises ``ValueError`` for a missing EndDeviceList href or, when
        ``check_duplicate``, an LFDI the server already lists (IEEE 2030.5 §8.5.3:
        clients SHALL NOT POST a duplicate EndDevice). Raises ``Sep2ProtocolError``
        on a server rejection status. Raises ``RuntimeError`` in a process
        without own-EndDevice duties (see :attr:`own_end_device`).
        """
        if not self._own_end_device:
            raise RuntimeError(
                "this client process does not do own-EndDevice work; register EndDevices "
                "from the process that does"
            )
        from py20305.models.sep.sep import (
            DeviceCategoryType,
            EndDevice,
            Sfditype,
            TimeType,
        )
        from py20305.security.identity import compute_sfdi

        lfdi_bytes = bytes.fromhex(lfdi)
        href = edev_list_href or _extract_href(
            self._state.dcap.end_device_list_link if self._state.dcap else None
        )
        if not href:
            raise ValueError(
                "No EndDeviceList href -- the server's DeviceCapability has not been discovered"
            )

        if check_duplicate and await self._server_lists_end_device(lfdi, edev_list_href=href):
            raise ValueError(f"An EndDevice with LFDI {lfdi} is already registered")

        end_device = EndDevice(
            l_fdi=lfdi_bytes,
            s_fdi=Sfditype(value=compute_sfdi(lfdi)),
            changed_time=TimeType(value=int(self._timebase.now())),
        )
        if device_category is not None:
            if not 0 <= device_category < 2**32:
                raise ValueError("device_category must be between 0 and 4294967295 (32-bit bitmap)")
            end_device.device_category = DeviceCategoryType(
                value=device_category.to_bytes(4, "big")
            )
        return await self._http.post(href, end_device)

    async def _server_lists_end_device(self, lfdi: str, edev_list_href: str | None = None) -> bool:
        """Whether the server's EndDeviceList currently contains ``lfdi``.

        Live paged GET against the discovered EndDeviceList (or
        ``edev_list_href``). Raises ``ValueError`` when no EndDeviceList href is
        known. Used for the §8.5.3 duplicate check in ``register_end_device``
        and to make comms-loss recovery reregistration conditional: some servers
        (e.g. certain utility head-ends) drop EndDevices during an outage and
        expect the client to re-register, while others retain them and must not
        receive a duplicate POST.
        """
        from py20305.models.sep.sep import EndDeviceList

        href = edev_list_href or _extract_href(
            self._state.dcap.end_device_list_link if self._state.dcap else None
        )
        if not href:
            raise ValueError(
                "No EndDeviceList href -- the server's DeviceCapability has not been discovered"
            )
        lfdi_bytes = bytes.fromhex(lfdi)
        for page in await self._http.get_list(href, EndDeviceList):
            for existing in page.end_device or []:
                if existing.l_fdi == lfdi_bytes:
                    return True
        return False

    async def connect(self) -> None:
        """Run initial resource discovery and auto-subscribe if configured."""
        if self._notification_server and not self._notification_server.running:
            await self._notification_server.start()
        await discover(
            self._http,
            self._state,
            registration_pins=self._registration_pins,
            dcap_path=self._dcap_path,
            device_filter=self._device_filter,
        )
        # Capture the client's own LFDI for comms-loss self-reregistration.
        # Cert-derived and stable across reset_session, so a single capture holds.
        if self._own_lfdi is None:
            self._own_lfdi = self._http.client_lfdi
        if self._subscription_manager:
            if self._own_end_device and self._subscription_manager.active_subscriptions:
                valid, removed = await self._subscription_manager.validate_restored_subscriptions()
                logger.info("Subscription validation: %d valid, %d removed", valid, removed)
            if self._subscription_manager.adopt_on_connect:
                await self.adopt_subscriptions()
            await self._auto_subscribe()
            self._update_poll_suppression()
            self._start_renewal_task()

    def _start_renewal_task(self) -> None:
        """Start the subscription renewal task, in a process with own-EndDevice duties."""
        mgr = self._subscription_manager
        if mgr is None or not self._own_end_device or not mgr.active_subscriptions:
            return
        self._renewal_task = asyncio.create_task(
            mgr.start_renewal_task(
                self._shutdown_event, interval_seconds=self._renewal_interval_seconds
            )
        )

    async def adopt_subscriptions(
        self, *, remove_duplicates: bool | None = None
    ) -> AdoptionResult | None:
        """Take over the subscriptions the server already holds for this client.

        Calls :meth:`SubscriptionManager.adopt_existing` with this client's
        SubscriptionList and the resources it subscribes to. ``connect()`` calls
        this before creating subscriptions when the manager was built with
        ``adopt_on_connect``. ``remove_duplicates`` defaults to the manager's
        setting.

        Returns None when no subscription manager is attached or discovery has
        found no SubscriptionListLink.
        """
        mgr = self._subscription_manager
        if mgr is None:
            return None
        sub_list_href = self._find_agg_sub_href()
        if sub_list_href is None:
            return None
        if remove_duplicates is None:
            remove_duplicates = mgr.remove_duplicates
        result = await mgr.adopt_existing(
            sub_list_href,
            self._compute_desired_subscriptions(),
            remove_duplicates=remove_duplicates,
        )
        self._update_poll_suppression()
        return result

    async def run(self) -> None:
        """Start polling and block until shutdown is signaled."""
        logger.info("Starting poll loop")
        # Process controls from initial discovery immediately
        for href in list(self._state.der_programs):
            await self._event_processor.process_controls(href)
        self._start_polls()
        await self._shutdown_event.wait()
        logger.info("Poll loop exiting (shutdown signaled)")

    def _start_polls(self) -> None:
        rates = self._state.poll_rates
        logger.debug("Scheduling polls: %s", {k: v for k, v in rates.items() if v is not None})

        dcap_rate = rates.get("dcap")
        if dcap_rate is not None:
            self._scheduler.schedule("dcap", dcap_rate, self._poll_dcap)

        edev_rate = rates.get("edev")
        if edev_rate is not None:
            self._scheduler.schedule("edev", edev_rate, self._poll_edev)

        fsa_rate = rates.get("fsa")
        if fsa_rate is not None:
            self._scheduler.schedule("fsa", fsa_rate, self._poll_fsa)

        self._schedule_aggregation_poll()

        derp_rate = rates.get("derp")
        if derp_rate is not None:
            self._scheduler.schedule("derp", derp_rate, self._poll_derp)

        # Pricing function set (opt-in). Uses the tariff pollRate captured in
        # discovery, or the default when a tariff tree hasn't been seen yet (so
        # tariffs that appear later are still picked up).
        if self._state.pricing_enabled:
            tariff_rate = rates.get("tariff") or DEFAULT_POLL_RATE
            self._scheduler.schedule("tariff", tariff_rate, self._poll_tariff)

        time_rate = rates.get("time")
        if time_rate is not None:
            # The staleness threshold has to know this: a per-FSA observation
            # is at its oldest just before the next successful refresh, so a
            # threshold shorter than the poll interval retires healthy scopes.
            self._timebase.note_time_poll_rate(time_rate)
            self._scheduler.schedule("time", time_rate, self._poll_time)

        # Connectivity liveness heartbeat (issue: stale server_alive). Runs on a
        # short, fixed cadence independent of resource poll rates so server_alive
        # / last_contact_epoch can't go stale-true while only telemetry PUT/POSTs
        # are in flight. Never subscription-suppressed; idle-gated in the probe.
        if self._connection_heartbeat_seconds > 0:
            self._scheduler.schedule(
                "connectivity", self._connection_heartbeat_seconds, self._connectivity_probe
            )

        # Loss-of-communications detector.
        # Time-based: enters when last_contact_epoch is older than the window,
        # recovers on the first fresh contact. The connectivity heartbeat above
        # keeps issuing GETs during the outage so contact refreshes on restore.
        # Re-scheduled on every rediscovery (_start_polls re-runs), so it
        # self-rearms.
        if self._comms_loss_seconds > 0:
            self._scheduler.schedule(
                "comms_loss", self._comms_loss_eval_seconds, self._comms_loss_probe
            )

        # SubscriptionList reconcile (issue: server drops a subscription after a
        # notification-delivery failure, and we never notice). Polls the server's
        # SubscriptionList and re-establishes any locally-active subscription it's
        # missing. Runs only when subscriptions exist; never subscription-
        # suppressed (the SubscriptionList itself isn't subscribed -- rule (r)).
        if (
            self._reconcile_enabled
            and self._own_end_device
            and self._subscription_manager is not None
        ):
            sub_rate = self._sub_poll_rate()
            if sub_rate is not None:
                self._scheduler.schedule("sub", sub_rate, self._poll_sub_reconcile)

    def _new_scheduler(self) -> PollScheduler:
        """A poll scheduler with this client's backstop settings."""
        return PollScheduler(
            heartbeat_enabled=self._heartbeat_enabled,
            heartbeat_seconds=self._heartbeat_seconds,
        )

    def _sub_poll_rate(self) -> int | None:
        """Cadence for the SubscriptionList reconcile poll, in seconds.

        Follows the server-advertised SubscriptionList ``pollRate`` (captured into
        ``poll_rates["sub"]`` at discovery, default 900s) unless
        ``reconcile_interval_seconds`` is set (>0), which forces a fixed override.
        The value is clamped to the same safe lower bound as other poll rates.
        """
        override = self._reconcile_interval_seconds
        raw_rate = override if override > 0 else self._state.poll_rates.get("sub")
        return normalize_poll_rate(raw_rate, resource_key="sub", default=DEFAULT_POLL_RATE)

    async def _poll_sub_reconcile(self) -> None:
        """Reconcile local subscriptions against the server's SubscriptionList."""
        if self._subscription_manager is None:
            return
        agg_sub_href = self._find_agg_sub_href()
        if agg_sub_href is None:
            return  # no SubscriptionList discovered yet; nothing to reconcile
        if not self._subscription_manager.active_subscriptions:
            return  # nothing to reconcile
        await self._subscription_manager.reconcile_with_server(agg_sub_href)

    async def _connectivity_probe(self) -> None:
        """Cheap liveness GET, but only when the connection isn't confirmed alive.

        The IEEE chain audit now runs at the TLS handshake, so *any* successful
        request -- a GET or a telemetry PUT/POST -- both sets ``server_alive`` True
        and refreshes ``last_validated_epoch``. So the gate is: skip only when the
        connection is currently alive *and* a request confirmed it within the
        heartbeat window. The probe still fires while ``server_alive`` is False, or
        when nothing has kept the connection warm within the window (e.g. a
        poll-suppressing subscription with only sparse traffic) -- issuing a small
        GET (Time, falling back to DeviceCapability) whose outcome refreshes
        ``server_alive`` via the normal request path. Telemetry reviving
        ``server_alive`` directly is what removes the old stuck-false-after-
        reconnect failure mode. Errors are expected when the server is down and are
        left to the poll loop's handler.
        """
        from py20305.models.sep.sep import DeviceCapability, Time

        if self._http.server_alive:
            # Every established connection is chain-validated, so the last contact
            # is also the last validated request.
            since_contact = self._http.seconds_since_contact()
            if since_contact is not None and since_contact < self._connection_heartbeat_seconds:
                return  # alive and a validated request confirmed it within the window

        if self._state.time_href:
            # Free timebase refresh: the probe already GETs Time, so feed the
            # response into the shared timebase instead of discarding it.
            t = await self._http.get(self._state.time_href, Time)
            observe_time_resource(self._timebase, t, href=self._state.time_href)
        else:
            await self._http.get(self._dcap_path, DeviceCapability)

    async def _comms_loss_probe(self) -> None:
        """Detect and recover from loss of communications (time-based).

        Enters comms-loss mode when the last reachable contact is older than
        ``comms_loss_seconds``; recovers on the first contact fresher than that.
        Never fires before the first contact (``last_contact_epoch is None``),
        avoiding cold-start false positives. Owns both the enter and exit edges,
        independent of the count-based server-restart detection in
        ``_note_successful_contact``.
        """
        # Boundary epochs are server-derived (event ends), so compare against
        # the server timebase; the silence detection below stays on the local
        # clock (elapsed, drift-immune). Integer comparison (like the recovery
        # clear) so a fractional now() can't drop the gate up to ~1s early.
        boundary = self._comms_loss.resume_after_epoch
        if (
            not self._comms_loss.active
            and boundary is not None
            and int(self._timebase.now()) > boundary
        ):
            # The opted-out window has fully elapsed: no event starting at/before
            # the boundary can still be pending, so the resume-after gate is done.
            self._comms_loss.resume_after_epoch = None
            logger.info("Comms-loss resume boundary passed (epoch %d); cleared", boundary)
        since_contact = self._http.seconds_since_contact()
        if since_contact is None:
            return
        elapsed = int(since_contact)
        if not self._comms_loss.active:
            if elapsed >= self._comms_loss_seconds:
                await self._enter_comms_loss(elapsed)
        elif elapsed < self._comms_loss_seconds:
            await self._recover_from_comms_loss()

    async def _enter_comms_loss(self, elapsed: int) -> None:
        """Flag comms-loss mode and opt out of active events (idempotent)."""
        if self._comms_loss.active:
            return
        self._comms_loss.active = True
        from py20305.diagnostics import report

        # Attribution is causal, not "was a window open at the time": a link
        # already down when the operator armed a simulation stays reported as
        # the genuine outage it is, and a real fault during a window is never
        # relabelled a test artifact and hidden.
        simulated = self._http.silence_is_simulated is True
        details: dict[str, Any] = {
            "elapsed_seconds": elapsed,
            "threshold": self._comms_loss_seconds,
        }
        if simulated:
            details["simulated"] = True
        report(
            "warnings",
            f"Loss of communications: no upstream contact for {elapsed}s "
            f"(>= {self._comms_loss_seconds}s). Opting out of active events and "
            "managing the DER at the planning limit (DefaultDERControl)."
            + (" Cause: operator-triggered simulation." if simulated else ""),
            source="client",
            # Separate dedup identity per mode. The diagnostics store keeps the
            # first entry's message and details for a repeated key, so sharing
            # one key would let a simulated outage's `simulated: true` stick to
            # every genuine outage that followed it, and the reverse.
            dedup_key="comms_loss:entered:simulated" if simulated else "comms_loss:entered",
            details=details,
        )
        logger.warning("Entering loss-of-communications mode (silent for %ds)", elapsed)
        self._comms_loss_simulated = simulated
        self._comms_loss_entered_monotonic = time.monotonic()
        # Recorded before any event is opted out, so the record that explains
        # the opt-outs and the fallback writes comes ahead of them in the trail,
        # and is not lost if reverting the fleet fails part way.
        self._audit.comms_loss(
            transition="entered",
            elapsed_seconds=elapsed,
            threshold=self._comms_loss_seconds,
            simulated=simulated,
            at=self._timebase.now(),
        )
        await self._event_processor.enter_comms_loss()

    def _comms_loss_duration(self) -> int:
        """Whole seconds in loss-of-communications mode so far, or 0 if unknown."""
        started = self._comms_loss_entered_monotonic
        return int(time.monotonic() - started) if started is not None else 0

    async def _recover_from_comms_loss(self) -> None:
        """Recover once communications are restored.

        Reregisters the client's own EndDevice in-band, re-polls the latest
        schedules (superseding prior ones), resumes events after the last
        opted-out event via the resume-after boundary, then clears the mode.
        """
        logger.info("Communications restored -- recovering from loss-of-communications mode")
        # Before the re-poll, whose records it explains. Repeats if this
        # recovery fails and the next probe tick tries again.
        self._audit.comms_loss(
            transition="recovering",
            duration_seconds=self._comms_loss_duration(),
            threshold=self._comms_loss_seconds,
            simulated=self._comms_loss_simulated,
            at=self._timebase.now(),
        )
        # 1. In-band self-reregistration, only when the server no longer
        #    lists our EndDevice: some head-ends remove EndDevices during an
        #    outage and expect re-registration, while others retain them and
        #    must not receive a duplicate POST (IEEE 2030.5 §8.5.3).
        #    Best-effort: a failure is logged and recovery continues (the
        #    re-poll below is what actually resumes control). The client method
        #    (unlike the web layer) does not refuse the own LFDI;
        #    check_duplicate=False avoids re-fetching the list we just checked.
        if self._own_lfdi is not None and self._own_end_device:
            try:
                if await self._server_lists_end_device(self._own_lfdi):
                    logger.info(
                        "Comms-loss recovery: own EndDevice still listed by the "
                        "server; skipping in-band reregistration"
                    )
                else:
                    await self.register_end_device(lfdi=self._own_lfdi, check_duplicate=False)
            except (Sep2Error, ValueError) as exc:
                logger.warning("Comms-loss recovery: self-reregistration failed: %s", exc)
        # 2. Re-poll schedules, supersede, rebuild timers, manage idle devices per
        #    DDERC. The resume-after boundary makes process_controls skip the
        #    events opted out during the outage.
        if not await self.trigger_rediscovery():
            # Rediscovery raised (or coalesced): the schedule wasn't rebuilt. Stay
            # in comms-loss mode so scheduled events remain opted out, and let the
            # next probe tick retry the whole recovery (reregister + re-poll). The
            # boundary must stay set for that retry.
            logger.warning(
                "Comms-loss recovery: rediscovery did not complete; staying in "
                "loss-of-communications mode, will retry on the next probe tick"
            )
            return
        # 3. Clear the mode now the fresh schedule has been processed. The
        #    resume-after boundary is NOT cleared while it is still in the
        #    future: recovery's re-poll skips (does not store) opted-out-window
        #    events, so a later routine poll would re-classify them as new and
        #    late-apply them if the gate dropped early. The probe clears the
        #    boundary once it has passed. Opted-out event records likewise keep
        #    their opted_out flag until they prune (their end is at/before the
        #    boundary): they must not resume and must stay excluded from
        #    "other active" DDERC checks.
        self._comms_loss.active = False
        boundary = self._comms_loss.resume_after_epoch
        if boundary is not None and int(self._timebase.now()) > boundary:
            self._comms_loss.resume_after_epoch = None
        self._audit.comms_loss(
            transition="cleared",
            duration_seconds=self._comms_loss_duration(),
            threshold=self._comms_loss_seconds,
            simulated=self._comms_loss_simulated,
            at=self._timebase.now(),
            resume_after=self._comms_loss.resume_after_epoch,
        )
        self._comms_loss_entered_monotonic = None
        logger.info(
            "Loss-of-communications mode cleared%s",
            (
                f" (resume-after boundary {boundary} retained until it passes)"
                if self._comms_loss.resume_after_epoch is not None
                else ""
            ),
        )

    # ------------------------------------------------------------------
    # Operator-triggered comms-loss simulation
    # ------------------------------------------------------------------

    async def simulate_comm_loss(
        self, duration_seconds: float, *, drain_timeout: float = 5.0
    ) -> dict[str, Any]:
        """Cut this client off from the head-end for ``duration_seconds``.

        Mechanism, not policy: whether a simulation is permitted at all, and how
        long a window may reasonably run, are the caller's decisions. This
        method does what it is told and guarantees only that the window closes.

        Notifications are suspended before the transport gate closes, so a
        control cannot slip in through the inbound path while the outbound one
        is being shut. Both then drain: a request or handler already running
        would otherwise finish after the window opened, refreshing
        ``last_contact_epoch`` or applying a setpoint to a DER the operator
        believes is isolated.

        The returned status reports whether each drain completed. A drain that
        timed out does not abort the simulation -- the window is open either way
        -- but it tells the caller the isolation was not clean.
        """
        if not math.isfinite(duration_seconds) or duration_seconds <= 0:
            # An infinite or nonsensical duration would defeat the one guarantee
            # this method makes. Rejected before anything is suspended so a bad
            # call cannot half-isolate the client.
            raise ValueError(
                f"duration_seconds must be finite and greater than zero, got {duration_seconds!r}"
            )
        if not math.isfinite(drain_timeout) or drain_timeout < 0:
            # An unbounded drain would stall between arming the gates and
            # scheduling the timer that closes them -- isolated, with nothing
            # left to restore it.
            raise ValueError(
                f"drain_timeout must be finite and not negative, got {drain_timeout!r}"
            )

        async with self._comm_loss_lifecycle:
            # A generation per window. Everything that ends a window checks it,
            # so a timer or clear belonging to a previous one cannot reach in
            # and disarm the window that replaced it.
            self._comm_loss_generation += 1
            generation = self._comm_loss_generation
            # Cancelled, not awaited: an older timer may itself be waiting on
            # this lock, and awaiting it here would deadlock. The generation
            # check is what makes cancelling sufficient.
            self._cancel_comm_loss_simulation_expiry()

            expires_at = time.time() + duration_seconds
            # The sleep runs on the monotonic clock, like the rest of the
            # scheduler, so a wall-clock step cannot cut a window short or
            # stretch it. `expires_at` stays wall-clock because that is what an
            # operator reads.
            monotonic_deadline = time.monotonic() + duration_seconds

            notifications_drained = True
            try:
                if self._notification_server is not None:
                    notifications_drained = await self._notification_server.suspend(
                        drain_timeout=drain_timeout
                    )
                requests_drained = await self._http.arm_comm_loss_simulation(
                    expires_at=expires_at, drain_timeout=drain_timeout
                )
            except BaseException:
                # Both gates close before their drains await, so a cancellation
                # or failure part-way through would otherwise leave the client
                # isolated with no timer to restore it -- the one outcome this
                # must never produce. Roll back to a client that was never
                # isolated, and let the caller see the failure.
                self._http.comm_loss_simulation.abort()
                if self._notification_server is not None:
                    self._notification_server.resume()
                raise

            self._comm_loss_simulation_expiry = asyncio.create_task(
                self._expire_comm_loss_simulation(monotonic_deadline, generation),
                name="comm-loss-simulation-expiry",
            )

            logger.warning(
                "Comms-loss simulation started for %.0fs (operator-triggered); "
                "upstream traffic suppressed",
                duration_seconds,
            )
            status = self.comm_loss_simulation_status()
            status["requests_drained"] = requests_drained
            status["notifications_drained"] = notifications_drained
            return status

    async def clear_comm_loss_simulation(self) -> dict[str, Any]:
        """End the simulation and drive recovery, without waiting for a probe.

        Clearing the gate is not the same event as leaving comms-loss mode: the
        detector only exits once a request actually reaches the server, which
        waits on the connectivity heartbeat and then a probe tick -- some two
        and a half minutes of apparent inactivity on default settings. So
        recovery is driven here rather than left to the schedule.
        """
        async with self._comm_loss_lifecycle:
            return await self._clear_comm_loss_simulation_locked()

    async def _clear_comm_loss_simulation_locked(self) -> dict[str, Any]:
        """Restore the link and recover. Caller holds the lifecycle lock."""
        self._cancel_comm_loss_simulation_expiry()
        self._http.disarm_comm_loss_simulation()
        if self._notification_server is not None:
            self._notification_server.resume()

        simulation = self._http.comm_loss_simulation
        try:
            await self._recover_after_simulation()
        except Exception as exc:  # noqa: BLE001 - reported, not swallowed
            # Recovery re-polls schedules against a live head-end and can fail
            # for reasons that have nothing to do with the simulation. The
            # window is closed either way; the operator needs to see that
            # recovery did not complete rather than a bare "recovered".
            logger.warning("Comms-loss simulation cleared, but recovery failed: %s", exc)
            simulation.note_recovery_failed(str(exc))
            return self.comm_loss_simulation_status()

        if self._comms_loss.active:
            simulation.note_recovery_failed(
                "still in loss-of-communications mode; rediscovery did not complete"
            )
        else:
            simulation.note_recovered()
        return self.comm_loss_simulation_status()

    async def _recover_after_simulation(self) -> None:
        """Bring the client back up to date once the gate reopens.

        Full comms-loss recovery when the detector actually entered the mode.
        Otherwise a plain rediscovery, which still has to happen: notifications
        were accepted and dropped for the whole window, so a schedule or control
        change delivered only by notification is missing even though the
        detector never fired -- a short window, or a client with comms-loss
        detection disabled entirely.
        """
        if self._comms_loss.active:
            await self.recover_from_comms_loss_now()
            return
        await self._scheduler.run_exclusive("comms_loss", self._rediscover_after_simulation)

    async def _rediscover_after_simulation(self) -> None:
        if not await self.trigger_rediscovery():
            logger.warning(
                "Comms-loss simulation cleared, but rediscovery did not complete; "
                "resources delivered by notification during the window may be stale"
            )

    async def recover_from_comms_loss_now(self) -> None:
        """Run comms-loss recovery immediately, serialized with the scheduler.

        Takes the same per-key lock as the scheduled comms-loss probe. Calling
        ``_recover_from_comms_loss`` directly would bypass it, letting a
        scheduled tick overlap this one -- and recovery checks for and possibly
        re-POSTs the client's own EndDevice before it reaches the rediscovery
        lock, so two overlapping runs can register twice against a live
        head-end.

        The active check happens inside the lock, so a tick that recovered while
        this call was waiting is not undone by a second, redundant recovery.
        """

        async def _run() -> None:
            if not self._comms_loss.active:
                return
            await self._recover_from_comms_loss()

        await self._scheduler.run_exclusive("comms_loss", _run)

    def comm_loss_simulation_status(self) -> dict[str, Any]:
        """Report the simulation window and its recovery phase."""
        return self._http.comm_loss_simulation.status()

    def _cancel_comm_loss_simulation_expiry(self) -> None:
        """Stop the expiry timer, unless the caller *is* that timer.

        A timer clearing the window it owns must not cancel itself part-way
        through recovery, and the handle has to stay set so shutdown still has
        something to wait on while that recovery is in flight.
        """
        task = self._comm_loss_simulation_expiry
        if task is None or task is asyncio.current_task():
            return
        task.cancel()
        self._comm_loss_simulation_expiry = None

    async def _stop_comm_loss_simulation_expiry(self) -> None:
        """Cancel the expiry timer and wait for it to actually stop.

        Shutdown uses this rather than the bare cancel: a task merely asked to
        cancel has not run yet, and this one must be finished before the
        resources its clear path would touch are torn down.
        """
        task = self._comm_loss_simulation_expiry
        self._comm_loss_simulation_expiry = None
        if task is not None:
            task.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                await task

    async def _expire_comm_loss_simulation(self, deadline: float, generation: int) -> None:
        """Close the window on time, with no operator action.

        Sleeps to an absolute monotonic deadline rather than for the requested
        duration: the deadline is fixed when the caller asks, but this task only
        starts once both drains have finished, so sleeping the full duration
        would hold the window open past the ``expires`` already reported.
        """
        try:
            await asyncio.sleep(max(0.0, deadline - time.monotonic()))
        except asyncio.CancelledError:
            return
        async with self._comm_loss_lifecycle:
            if generation != self._comm_loss_generation:
                # A newer window owns the client now. This timer woke too late
                # to matter, and clearing here would disarm somebody else's
                # simulation.
                return
            if not self._http.comm_loss_simulation.active:
                return
            logger.info("Comms-loss simulation reached its deadline; restoring")
            # Cleared through the same path an operator would use, so expiry and
            # a manual clear cannot diverge. The handle is deliberately left
            # set: shutdown waits on it, and this recovery talks to the head-end.
            await self._clear_comm_loss_simulation_locked()

    async def _poll_dcap(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_dcap)

    async def _poll_edev(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_edev)

    async def _poll_fsa(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_fsa)

    async def _poll_derp(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_derp)

    async def _poll_tariff(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_tariff)

    async def _poll_time(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_time)

    async def _do_poll_dcap(self) -> None:
        from py20305.models.sep.sep import DeviceCapability

        logger.debug("Polling dcap")
        self._state.dcap = await self._http.get(self._dcap_path, DeviceCapability)

        # Late MirrorUsagePoint adoption: if the link was absent at discovery but
        # this DeviceCapability now advertises one (the server brought the
        # MirrorUsagePoint function set online after connect), trigger rediscovery
        # so telemetry is wired up without a restart. Rediscovery re-extracts
        # mup_list_href and re-attempts telemetry setup via the structural-change
        # path (_restart_device_telemetry).
        if (
            self._state.mup_list_href is None
            and _extract_href(self._state.dcap.mirror_usage_point_list_link)
            and self._on_structural_change
        ):
            logger.info("MirrorUsagePointListLink now advertised, triggering rediscovery")
            await self._on_structural_change()

    async def _do_poll_edev(self) -> None:
        logger.debug(
            "Polling edev: refreshing end device lists for %d devices", len(self._state.end_devices)
        )
        await refresh_end_device_lists(self._http, self._state)
        # A refresh can add or remove an aggregation or change its list's pollRate.
        self._schedule_aggregation_poll()

    def _schedule_aggregation_poll(self) -> None:
        """Schedule the "adev" poll at the shortest aggregation poll interval.

        The AggregatedDeviceList and AggregationPriority are never subscribed,
        so this poll is never suppressed: an aggregation stays current while a
        subscription suppresses the EndDeviceList poll. Each run reads only the
        aggregations whose own interval has elapsed.
        """
        interval = aggregations_poll_interval(self._state)
        if interval is None:
            return
        if "adev" in self._scheduler.active_keys:
            self._scheduler.set_interval("adev", interval)
        else:
            self._scheduler.schedule("adev", interval, self._poll_aggregations)

    async def _poll_aggregations(self) -> None:
        await self._poll_with_404_recovery(self._do_poll_aggregations)

    async def _do_poll_aggregations(self) -> None:
        await refresh_aggregations(
            self._http, self._state, self._aggregation_reads, time.monotonic()
        )
        # A read can change a list's pollRate, and so the poll's interval.
        self._schedule_aggregation_poll()

    def _subscribed_fsa_hrefs(self) -> set[str]:
        """FSA list hrefs with an active subscription -- rule (r): don't poll them."""
        mgr = self._subscription_manager
        if mgr is None:
            return set()
        return {
            s.subscribed_resource for s in mgr.active_subscriptions if s.resource_type == "FSAList"
        }

    async def _do_poll_fsa(self) -> None:
        logger.debug(
            "Polling fsa: refreshing FSA lists for %d end devices", len(self._state.end_devices)
        )
        # Skip FSA lists we're subscribed to (rule r); a cancelled FSA is still
        # polled while its still-subscribed siblings are not. The poll is
        # suppressed only when every FSA list is subscribed, so a run of it then
        # is the backstop poll, which refreshes them all: skipping them would
        # fetch nothing and miss what a dropped notification carried.
        backstop = "fsa" in self._scheduler.suppressed_keys
        removed_programs = await refresh_function_set_assignments(
            self._http,
            self._state,
            skip_hrefs=None if backstop else self._subscribed_fsa_hrefs(),
        )

        # Gap 3: cancel events from removed FSA programs (IEEE 8.8.3)
        if removed_programs:
            logger.info(
                "Cancelling events from %d program(s) due to FSA removal",
                len(removed_programs),
            )
            for program_href in removed_programs:
                self._event_processor.cancel_program(program_href)
                # Clean up state
                self._state.der_programs.pop(program_href, None)
                await self._event_processor.release_cancelled_program(program_href)

    async def _do_poll_derp(self) -> None:
        logger.debug(
            "Polling derp: refreshing DER programs for %d end devices", len(self._state.end_devices)
        )
        withdrawn: list[tuple[str, str]] = []
        removed_programs = (
            await refresh_der_programs(self._http, self._state, withdrawn=withdrawn) or []
        )
        for program_href in removed_programs:
            self._event_processor.cancel_program(program_href)
            await self._event_processor.release_cancelled_program(program_href)
        await self._event_processor.release_withdrawn_devices(withdrawn)
        n_progs = len(self._state.der_programs)
        logger.debug("Polling derp: refreshing controls for %d programs", n_progs)
        await refresh_der_controls(self._http, self._state)
        for href in list(self._state.der_programs):
            await self._event_processor.process_controls(href)

    async def _do_poll_tariff(self) -> None:
        """Pricing poll: re-walk the tariff tree, then relay any active-interval
        price change to connectors. No-op unless pricing is enabled.

        Waits for a running tariff re-poll to finish rather than skipping, so
        this run reads the newest server state. Then arms the boundary timer.
        """
        async with self._tariff_lock:
            await refresh_tariffs(self._http, self._state)
            await self._tariff_processor.process_tariffs()
        self._arm_tariff_timer()

    def _arm_tariff_timer(self) -> None:
        """Start the timer that re-runs the tariff processor when it is next due.

        Replaces any pending timer. Starts none when the processor has nothing
        due (no later interval boundary and no price fetch to retry) or after
        shutdown started.
        """
        pending = self._tariff_timer
        self._tariff_timer = None
        if pending is not None and pending is not asyncio.current_task():
            pending.cancel()
        if self._shutdown_event.is_set() or self._tariff_processor.next_run_delay() is None:
            return
        self._tariff_timer = asyncio.create_task(
            self._run_tariff_timer(), name="tariff-boundary-timer"
        )

    async def _run_tariff_timer(self) -> None:
        """Run the tariff processor at its next interval boundary, then re-arm.

        Processing uses the schedule already fetched; it does not refresh the
        tariff tree. It waits for a running rediscovery and a running tariff
        re-poll to finish first.
        """
        while True:
            delay = self._tariff_processor.next_run_delay()
            if delay is None:
                return
            if delay <= 0:
                break
            await asyncio.sleep(min(delay, _TARIFF_TIMER_TICK_S))
        while self._rediscovery_lock.locked():
            await asyncio.sleep(_REDISCOVERY_WAIT_INTERVAL_S)
        try:
            async with self._tariff_lock:
                await self._tariff_processor.process_tariffs()
        except Exception:
            logger.warning("Tariff processing at an interval boundary failed", exc_info=True)
        self._arm_tariff_timer()

    async def _cancel_tariff_timer(self) -> None:
        """Cancel the tariff boundary timer and wait for it to stop."""
        task = self._tariff_timer
        self._tariff_timer = None
        if task is None:
            return
        task.cancel()
        with contextlib.suppress(asyncio.CancelledError):
            await task

    async def _do_poll_derp_targeted(
        self, subscribed_resource: str, *, strict: bool = False
    ) -> bool:
        """Fetch controls for the single program that owns *subscribed_resource*.

        Returns True if a targeted refresh was performed, False if the
        resource could not be resolved to a known program (caller should
        fall back to full re-poll). ``strict`` raises a failed fetch instead
        of skipping it.
        """
        program_href = self._state.find_program_for_resource(subscribed_resource)
        if program_href is None:
            logger.warning(
                "Cannot resolve %s to a known program, falling back to full re-poll",
                subscribed_resource,
            )
            return False

        logger.debug(
            "Targeted DERP refresh for program %s (from %s)",
            program_href,
            subscribed_resource,
        )
        found = await refresh_der_controls_for_program(
            self._http, self._state, program_href, strict=strict
        )
        if found:
            await self._event_processor.process_controls(program_href)
        return found

    async def _do_poll_time(self) -> None:
        """Refresh every Time observation the timebase serves, global and per-FSA.

        Both scopes have to be renewed here. Event classification and timer
        firing read the FSA-scoped offset (IEEE 2030.5 §9.2.3), which
        :meth:`ServerTimebase.offset` prefers over the global one, so a per-FSA
        observation left at its discovery-time value is the offset the client
        actually runs on. Refreshing only the global scope produced a client
        whose reported drift was correct and whose scheduling was not: on a host
        with no NTP, events drifted with the local clock while ``/status`` looked
        healthy the whole time.

        Each distinct href is fetched once and observed for every scope that
        names it. A server commonly points DeviceCapability and every FSA at the
        same ``/tm``, and polling it once per FSA would multiply this poll by the
        FSA count for no new information.

        Isolation is deliberately partial. An error that ``_poll_with_404_recovery``
        exists to act on -- a redirect, or anything the global href answers other
        than 200 -- is re-raised even when other scopes refreshed fine, because
        swallowing it is the same silent failure this method fixes, one level up:
        the global Time href moves, the refresh quietly stops, and the recovery
        that would repair it never runs. The FSA hrefs keep the tolerant path
        that discovery already gives them, where an absent Time resource
        (404/204) means this FSA has none rather than that the server is broken.
        """
        from py20305.models.sep.sep import Time

        # href -> the scopes reading it; None is the global scope. The global
        # href goes in first, so when several fail it is its error that reaches
        # the recovery path -- the one that says the client's own view of the
        # server moved, rather than one FSA's endpoint being down.
        scopes: dict[str, list[str | None]] = {}
        if self._state.time_href:
            scopes.setdefault(self._state.time_href, []).append(None)
        for fsa_href, time_href in self._state.fsa_time_hrefs.items():
            scopes.setdefault(time_href, []).append(fsa_href)

        refreshed = 0
        recoverable: Exception | None = None  # must reach _poll_with_404_recovery
        last_error: Exception | None = None  # any real failure, for a total loss
        for time_href, hrefs in scopes.items():
            is_global = None in hrefs
            try:
                resource = await self._http.get(time_href, Time)
            except Exception as exc:
                if _is_absent_fsa_time(exc, is_global=is_global):
                    # Same reading discovery gives it: this FSA publishes no
                    # Time resource. Not a failure, and not a reason to
                    # rediscover on every poll for the life of the deployment.
                    logger.debug("FSA Time at %s absent; keeping prior observation", time_href)
                    continue
                # Otherwise one unreachable Time resource must not cost the
                # others their refresh: dropping the global observation because
                # an FSA's endpoint is down is how a local failure becomes a
                # fleet-wide clock problem. The stale observation stays until it
                # succeeds.
                last_error = exc
                if recoverable is None and _needs_poll_recovery(exc, is_global=is_global):
                    recoverable = exc
                logger.warning("Time refresh failed at %s; keeping prior observation", time_href)
                continue
            refreshed += 1
            for scope in hrefs:
                if scope is None:
                    self._state.time = resource
                else:
                    self._state.fsa_time[scope] = (time_href, resource)
                observe_time_resource(self._timebase, resource, fsa_href=scope, href=time_href)

        if recoverable is not None:
            # Raised even though other scopes refreshed. Selecting it up front
            # rather than re-raising whichever error came last is what keeps
            # this independent of href iteration order: a global 404 behind an
            # FSA connection error still reaches the rediscovery branch.
            raise recoverable

        if refreshed == 0 and last_error is not None:
            # Isolating each href must not turn a total failure into a success.
            # The caller treats a clean return as proof of contact and clears the
            # failure streak on it, so swallowing the last error here would keep
            # a server that answers nothing looking reachable -- and suppress the
            # rediscovery that recovers from it. Re-raise the original so the
            # 404 and 204 handling upstream still sees the status it needs.
            raise last_error

    async def _note_successful_contact(self) -> None:
        """Clear the poll-failure streak after the server is reachable again.

        Runs after a successful poll *or* a benign 204 (both prove the server
        responded). Gap 4 connectivity-recovery, two regimes:
          * Short failure (count < threshold) -- likely a network blip or brief
            5xx; re-check subscription state but keep current discovery state.
          * Sustained failure (count >= threshold) -- the server probably
            restarted; its in-memory subscription store, telemetry
            MUPs, and resource paths may all differ now, so trigger full
            rediscovery rather than leaving the client half-wired with dead
            subscriptions.
        """
        if self._poll_failure_count == 0:
            return
        recovered_after = self._poll_failure_count
        self._poll_failure_count = 0
        if recovered_after >= _SERVER_RESTART_DETECTION_THRESHOLD:
            logger.info(
                "Server reachable again after %d consecutive poll failures -- "
                "treating as upstream restart and triggering full rediscovery",
                recovered_after,
            )
            # A genuine reconnect: the server's subscription store is presumed gone,
            # so drop any honoured status=1 suppression and re-attempt every desired
            # resource from a clean session. This is the only place the
            # suppression is cleared -- not on every rediscovery.
            if self._subscription_manager is not None:
                self._subscription_manager.clear_resubscribe_suppression()
            callbacks = [*self._reconnect_listeners]
            if self._on_reconnect is not None:
                callbacks.append(self._on_reconnect)
            for callback in callbacks:
                # The failure streak is already cleared, so letting one raise
                # past here would skip the rediscovery for good.
                try:
                    callback()
                except Exception:
                    logger.warning(
                        "reconnect callback raised; continuing with rediscovery",
                        exc_info=True,
                    )
            await self.trigger_rediscovery()
        else:
            logger.info(
                "Connectivity recovered after %d failure(s), re-checking subscriptions",
                recovered_after,
            )
            if self._subscription_manager:
                await self._auto_subscribe()

    async def _poll_with_404_recovery(
        self,
        poll_fn: Callable[[], Awaitable[None]],
    ) -> None:
        contacts = self._http.contacts
        try:
            await poll_fn()
            # A poll that sent no request, such as a backstop poll whose
            # resources are all subscribed, says nothing about the server. Taken
            # as contact, it cleared the failure streak on every cycle, so an
            # outage was never treated as a restart.
            if self._http.contacts != contacts:
                await self._note_successful_contact()

        except Sep2NoContentError:
            # 204 No Content on a polled resource is benign (the resource is
            # present but empty) -- nothing to act on. Must be caught before the
            # generic Sep2ProtocolError below (it's a subclass) so an unexpected
            # 204 can never crash the poll loop (CSIP [GEN.037]). It is still a
            # *successful* contact, though, so it clears the failure streak and
            # runs the same recovery path as a normal poll.
            logger.debug("Poll got 204 No Content; treating as empty (benign)")
            await self._note_successful_contact()
        except Sep2RedirectError as exc:
            # Persistent redirect (the retry loop already retried transient ones):
            # resources moved, so re-discover from dcap (IEEE 2030.5 §5.5.2.7)
            # rather than propagating an error.
            logger.info("Poll got redirect -> %s; triggering rediscovery", exc.location or "(none)")
            self._poll_failure_count = 0
            await self.trigger_rediscovery()
        except Sep2ProtocolError as exc:
            self._poll_failure_count += 1
            if exc.status_code == 404:
                logger.warning("Got 404 during poll, triggering rediscovery")
                self._poll_failure_count = 0
                await self.trigger_rediscovery()
            else:
                raise
        except Sep2PayloadError as exc:
            # Server replied 200 but body was malformed/empty. This is an
            # operational issue with the upstream, not a client bug. Log a
            # clean one-line warning -- the message already includes the
            # path, length, and snippet -- and continue polling.
            self._poll_failure_count += 1
            logger.warning("Poll skipped due to unparseable response: %s", exc)
        except Exception:
            self._poll_failure_count += 1
            raise

    async def poll_now(self) -> int:
        """Trigger an immediate DER program control poll.

        Unlike trigger_rediscovery(), this does NOT reset the event processor
        or re-run discovery. It simply refreshes DERControl lists for all
        known programs and processes any new/changed controls through the
        existing event processor.

        Returns the number of programs polled.
        """
        logger.info("Immediate derp poll requested")
        await self._do_poll_derp()
        n = len(self._state.der_programs)
        logger.info("Immediate derp poll complete: %d program(s)", n)
        return n

    def _update_poll_suppression(self) -> None:
        """Suppress or unsuppress polls based on active subscriptions."""
        mgr = self._subscription_manager
        if mgr is None:
            return

        active_poll_keys = {
            _RESOURCE_TYPE_TO_POLL_KEY[rt]
            for rt in mgr.subscribed_resource_types()
            if rt in _RESOURCE_TYPE_TO_POLL_KEY
        }

        # FSA granularity: the "fsa" poll refreshes every
        # end device's FSA list, but suppression is per-key -- so a single
        # subscribed FSA would suppress polling of a *different*, cancelled FSA on
        # another EndDevice, losing its structural changes. Keep the fsa poll
        # running whenever a discovered FSA list is NOT actively subscribed (it
        # needs poll-only observation); _do_poll_fsa skips the subscribed ones
        # (rule r), so the still-subscribed siblings are never redundantly polled.
        subscribed_fsa = self._subscribed_fsa_hrefs()
        all_fsa = {
            _extract_href(ed.device.function_set_assignments_list_link)
            for ed in self._state.end_devices.values()
        }
        all_fsa.discard(None)
        if all_fsa - subscribed_fsa:
            active_poll_keys.discard("fsa")

        # Keep the tariff poll running even when subscribed. The boundary timer
        # relays interval starts and ends from the schedule already fetched; the
        # poll re-reads the schedule and re-arms that timer, and the
        # subscription only speeds up pickup of schedule edits between polls.
        active_poll_keys.discard("tariff")

        # Suppress keys covered by subscriptions
        for key in active_poll_keys:
            self._scheduler.suppress(key)

        # Unsuppress keys no longer covered
        for key in _RESOURCE_TYPE_TO_POLL_KEY.values():
            if key not in active_poll_keys:
                self._scheduler.unsuppress(key)

    def _find_agg_sub_href(self) -> str | None:
        """Find the client device's SubscriptionListLink href.

        Falls back to the one discovery recorded from an EndDevice the device
        filter left out, so a process acting for none of the EndDevices that
        carry the link can still subscribe.
        """
        for edev_state in self._state.end_devices.values():
            if edev_state.subscription_list_href:
                return edev_state.subscription_list_href
        return self._state.subscription_list_href

    def _compute_desired_subscriptions(self) -> set[tuple[str, str]]:
        """Return the set of (subscribed_resource, resource_type) tuples to subscribe to.

        Pure function over DiscoveredState -- no I/O. Used by both
        ``_auto_subscribe`` and ``trigger_rediscovery`` reconciliation.
        """
        desired: set[tuple[str, str]] = set()

        # EndDeviceList
        edev_list_href = _extract_href(
            self._state.dcap.end_device_list_link if self._state.dcap else None
        )
        if edev_list_href:
            desired.add((edev_list_href, "EndDeviceList"))

        for edev_state in self._state.end_devices.values():
            # FSAList (only if server advertises subscribable)
            fsa_href = _extract_href(edev_state.device.function_set_assignments_list_link)
            if fsa_href and edev_state.fsa_list_subscribable:
                desired.add((fsa_href, "FSAList"))

            # DERProgramList per FSA (only if server advertises subscribable)
            if edev_state.derp_list_subscribable:
                for fsa in edev_state.fsa_list:
                    derp_href = _extract_href(fsa.derprogram_list_link)
                    if derp_href:
                        desired.add((derp_href, "DERProgramList"))

        # DERControlList and DefaultDERControl per program
        for derp_state in self._state.der_programs.values():
            derc_href = _extract_href(derp_state.program.dercontrol_list_link)
            if derc_href and derp_state.derc_list_subscribable:
                desired.add((derc_href, "DERControlList"))
            dderc_href = _extract_href(derp_state.program.default_dercontrol_link)
            if dderc_href and derp_state.dderc_subscribable:
                desired.add((dderc_href, "DefaultDERControl"))

        # TimeTariffIntervalList per rate component (Pricing; only if subscribable).
        # tariff_profiles is only populated when pricing is enabled, but gate
        # explicitly so intent is clear.
        if self._state.pricing_enabled:
            for tp_state in self._state.tariff_profiles.values():
                for rc_state in tp_state.rate_components:
                    if rc_state.tti_list_subscribable and rc_state.tti_list_href:
                        desired.add((rc_state.tti_list_href, "TimeTariffIntervalList"))

        return desired

    async def _auto_subscribe(self) -> None:
        """Subscribe to key resources using the client's SubscriptionListLink."""
        mgr = self._subscription_manager
        if mgr is None:
            return

        agg_sub_href = self._find_agg_sub_href()
        if not agg_sub_href:
            # The server didn't expose a SubscriptionListLink on any discovered
            # EndDevice, so there's nowhere to POST a Subscription resource and the
            # client silently falls back to polling. CSIP CORE-018 Setup #1
            # requires servers that support subscriptions to include this link on
            # the client's EndDevice; without it the ``subscribable`` attribute is
            # an empty advertisement. Warn once so this isn't invisible.
            if not self._no_sub_link_warned:
                logger.warning(
                    "Subscriptions disabled: server did not expose a "
                    "SubscriptionListLink on any discovered EndDevice. Falling "
                    "back to polling. (CSIP CORE-018 Setup #1 requires the "
                    "EndDevice for this client's SFDI/LFDI to include a "
                    "SubscriptionListLink when the server supports subscriptions.)"
                )
                self._no_sub_link_warned = True
            return

        if self._no_sub_link_warned:
            logger.info(
                "Subscriptions now available: SubscriptionListLink discovered at %s",
                agg_sub_href,
            )
            self._no_sub_link_warned = False

        for resource, rtype in self._compute_desired_subscriptions():
            if mgr.resubscribe_suppressed(resource):
                # Server cancelled this resource's subscription (status=1); honour
                # it (poll-only) instead of re-subscribing. Cleared on rediscovery.
                continue
            await mgr.subscribe(agg_sub_href, resource, rtype)

    async def process_notification(self, notification: bytes | Notification) -> bool:
        """Process one notification received by any process acting as this client.

        For a host that receives notifications in one process and processes them
        in another: the listener's ``forward`` hands the raw body to the host,
        the host queues it, and the process acting for the affected EndDevice
        calls this with it. ``notification`` is the raw XML body as the server
        sent it, or a parsed Notification.

        Returns True when this process acted on the notification, False when it
        did not: it concerns no EndDevice this process acts for (see
        :meth:`acts_for_resource`), or a loss-of-communications simulation is
        running, during which notifications are dropped as the listener drops
        them. A host can therefore deliver every notification to every process.

        Raises ``ValueError`` when the body is not a valid Notification. An
        error while acting on it (a failed GET, or a failed rediscovery the
        notification started) is raised to the caller, so the host can keep the
        notification and retry it; a retry is not skipped as a duplicate. A
        notification that arrives while a rediscovery is running is left to
        that rediscovery and counts as acted on.
        """
        from py20305.models.sep.sep import Notification
        from py20305.subscription.notification_server import (
            parse_notification,
            validate_notification,
        )

        if isinstance(notification, Notification):
            parsed = notification
        else:
            try:
                parsed = parse_notification(notification)
            except Exception as exc:
                raise ValueError(f"not a valid IEEE 2030.5 Notification: {exc}") from exc
            if not validate_notification(parsed, notification):
                raise ValueError("not a valid IEEE 2030.5 Notification: xsi:type mismatch")
        if self._http.comm_loss_simulation.active:
            logger.info(
                "Notification for %s dropped: comms-loss simulation active",
                parsed.subscribed_resource or "<unknown>",
            )
            return False
        return await self._handle_notification(parsed, raise_failures=True)

    def acts_for_resource(self, href: str) -> bool:
        """Return whether a notification about ``href`` concerns this process.

        Always True without a device filter. With one, True for the
        EndDeviceList, for the EndDevices this process acts for and their
        FunctionSetAssignmentsLists, for the DERProgramLists of their
        FunctionSetAssignments, for their DERPrograms with their DERControlLists
        and DefaultDERControls, and for the Pricing resources it reads. Also
        True while a rediscovery is running, because the discovered state is
        being rebuilt then and the rediscovery deals with what arrives. A
        notification that reports a resource cancelled, moved, changed or
        deleted is not left to a running rediscovery: the client matches it
        against the EndDevices this process acted for when the rediscovery
        began, and those it has found since. After a rediscovery that failed,
        the hrefs this process acted for before it still count, so a
        notification retried after the failure is not dropped.
        """
        if self._device_filter is None or self._rediscovery_lock.locked():
            return True
        return href in self._scope()

    def _scope(self) -> set[str]:
        """Return the hrefs this process acts for, with the earlier scope when it applies.

        The earlier scope applies while a rediscovery runs and after one that
        failed, because the discovered state is then incomplete.
        """
        scope = self._scoped_resource_hrefs()
        if self._rediscovery_lock.locked() or self._rediscovery_error is not None:
            scope |= self._scope_before_rediscovery
        return scope

    def _acts_for_notification(self, href: str, status: int) -> bool:
        """Return whether a notification with ``status`` about ``href`` concerns this process."""
        if self._device_filter is None or status == STATUS_DEFAULT:
            return self.acts_for_resource(href)
        return href in self._scope()

    def _scoped_resource_hrefs(self) -> set[str]:
        """Return the hrefs a notification may name that concern this process."""
        state = self._state
        hrefs: set[str | None] = set()
        if state.dcap is not None:
            hrefs.add(_extract_href(state.dcap.end_device_list_link))
        for edev_href, edev_state in state.end_devices.items():
            hrefs.add(edev_href)
            hrefs.add(_extract_href(edev_state.device.function_set_assignments_list_link))
            for fsa in edev_state.fsa_list:
                hrefs.add(fsa.href)
                hrefs.add(_extract_href(fsa.derprogram_list_link))
                hrefs.add(_extract_href(fsa.tariff_profile_list_link))
        for program_href, derp_state in state.der_programs.items():
            hrefs.add(program_href)
            hrefs.add(_extract_href(derp_state.program.dercontrol_list_link))
            hrefs.add(_extract_href(derp_state.program.default_dercontrol_link))
        for tp_href, tp_state in state.tariff_profiles.items():
            hrefs.add(tp_href)
            for rc_state in tp_state.rate_components:
                hrefs.add(rc_state.tti_list_href)
        return {href for href in hrefs if href}

    async def _handle_notification(
        self, notification: object, *, raise_failures: bool = False
    ) -> bool:
        """Process a received IEEE 2030.5 notification.

        Returns False when the notification was not acted on.
        ``raise_failures`` raises the error of a rediscovery the notification
        started and that failed, instead of only reporting it.

        Routes STATUS_DEFAULT notifications based on subscribed_resource path:
        - DERControlList changes (/derc): re-poll DERPs (fast path)
        - EndDevice/FSA/DERProgram changes: trigger full rediscovery (structural)

        STATUS_RESOURCE_DELETED with an EndDevice path triggers device removal.
        """
        from py20305.models.sep.sep import Notification

        if not isinstance(notification, Notification):
            logger.warning("Unexpected notification type: %s", type(notification))
            return False

        mgr = self._subscription_manager
        if mgr is None:
            return False

        if not self._acts_for_notification(notification.subscribed_resource, notification.status):
            logger.debug(
                "Ignoring notification for %s: no EndDevice this process acts for",
                notification.subscribed_resource,
            )
            return False

        # Dedup: a notification for a resource processed in the last 5 s is not
        # processed now. It may report a newer change than the one processed, so
        # it schedules one follow-up re-poll for when the window ends; further
        # suppressed notifications join that follow-up. The latest state is
        # therefore always read, at most one window late.
        # Only STATUS_DEFAULT (data-change) notifications are deduped. Lifecycle
        # statuses (cancel/moved/definition-changed/deleted) are distinct one-shot
        # events, never duplicates of a preceding change -- deduping them swallows
        # a cancellation that arrives right after a change for the same resource
        # (conformance test ERR-002: add_fsa_err status=0 then cancel_subscriptions status=1
        # on /edev/2/fsa), so the cancellation is never honoured. Re-processing a
        # lifecycle notification is idempotent, so exempting them is safe.
        # Exception 1: a structural notification arriving while rediscovery is running must
        # not be completely lost — set the pending flag so the running rediscovery loops
        # once more and picks up the change.
        # Exception 2: DERControlList / DefaultDERControl notifications skip dedup entirely.
        # The targeted-fetch handler is a single GET against one URL, so it's cheap to repeat,
        # and burst notifications (e.g. conformance test BASIC-015's 23-call
        # cancel_derc) must each be
        # processed — deduping them means we only see snapshots from the earliest notifications
        # and miss the trailing state where the burst's later cancellations apply.
        # Exception 3: TariffProfileList / TimeTariffIntervalList notifications skip dedup.
        # A second price change within the window would otherwise wait for the next tariff
        # poll (hourly by default). The re-poll relays a price only when the active
        # interval changed, so repeating it sends no duplicate price.
        # Classify by the subscription's topology-derived resource_type (naming-
        # independent); the path heuristics inside _is_control/_is_structural are
        # only the fallback when this subscription isn't tracked.
        resource_type = mgr.resource_type_for(notification.subscription_uri)

        # List/non-list filter: non-list subscriptions should not fire on list
        # changes. Checked before dedup, so an ignored notification neither
        # starts a dedup window nor schedules a follow-up re-poll.
        if not mgr.should_process_notification(
            notification.subscription_uri, notification.subscribed_resource
        ):
            logger.debug(
                "Ignoring notification (list/non-list filter) for %s",
                notification.subscribed_resource,
            )
            return False

        if (
            notification.status == STATUS_DEFAULT
            and not self._is_control(notification.subscribed_resource, resource_type)
            and not self._is_tariff(notification.subscribed_resource, resource_type)
            and mgr.is_duplicate_notification(notification.subscribed_resource)
        ):
            if self._is_structural(notification.subscribed_resource, resource_type) and (
                self._rediscovery_lock.locked()
            ):
                # The running rediscovery loops once more and reads the change.
                self._rediscovery_pending = True
            else:
                self._schedule_notification_followup(
                    notification.subscribed_resource, resource_type
                )
            logger.debug(
                "Deferring duplicate notification for %s", notification.subscribed_resource
            )
            return False

        import time

        if notification.status == STATUS_DEFAULT:
            mgr.record_notification(
                StoredNotification(
                    subscribed_resource=notification.subscribed_resource,
                    status=notification.status,
                    subscription_uri=notification.subscription_uri,
                    created_at=time.time(),
                )
            )
            try:
                await self._route_default_notification(
                    notification.subscribed_resource,
                    resource_type,
                    raise_failures=raise_failures,
                )
            except Exception:
                # Let a redelivery of this notification through the dedup window.
                mgr.forget_notification(notification.subscribed_resource)
                raise
        elif notification.status == STATUS_CANCELLED:
            # Server cancelled with no further info (rule n, status=1). Honour it:
            # stop treating the sub as active and DON'T auto-re-subscribe -- fall
            # back to polling the resource (permitted once the sub is inactive per
            # rule r). The suppression persists across rediscovery and is cleared
            # only on a genuine reconnect, so an unrelated rediscovery
            # can't undo the honoured cancellation.
            mgr.mark_cancelled(notification.subscription_uri)
            mgr.remove_notifications_for(notification.subscribed_resource)
            mgr.suppress_resubscribe(notification.subscribed_resource)
            self._update_poll_suppression()
        elif notification.status == STATUS_RESOURCE_MOVED:
            # Resource URI changed -> the server terminated the sub (rule h,
            # status=2). Record the new location for visibility, then rediscover
            # so we re-subscribe at the resource's new URI (discovery walks the
            # tree; new_resource_uri alone lacks the resource_type/context).
            mgr.mark_cancelled(notification.subscription_uri)
            self._update_poll_suppression()
            if notification.new_resource_uri:
                mgr.record_notification(
                    StoredNotification(
                        subscribed_resource=notification.subscribed_resource,
                        status=notification.status,
                        subscription_uri=notification.subscription_uri,
                        created_at=time.time(),
                        new_resource_uri=notification.new_resource_uri,
                    )
                )
            await self._rediscover_for_notification(raise_failures)
        elif notification.status == STATUS_DEFINITION_CHANGED:
            mgr.mark_cancelled(notification.subscription_uri)
            self._update_poll_suppression()
        elif notification.status == STATUS_RESOURCE_DELETED:
            mgr.mark_cancelled(notification.subscription_uri)
            self._update_poll_suppression()
            await self._handle_resource_deleted(
                notification.subscribed_resource, raise_failures=raise_failures
            )
        return True

    async def _rediscover_for_notification(self, raise_failures: bool) -> None:
        """Rediscover after a structural notification.

        Calls the structural-change callback instead when one is set. With
        ``raise_failures``, raises the error of a rediscovery pass that failed.
        A call that joins a rediscovery already running returns at once: that
        rediscovery runs again and handles the change.
        """
        if self._on_structural_change:
            await self._on_structural_change()
            return
        joined = self._rediscovery_lock.locked()
        if await self.trigger_rediscovery() or joined or not raise_failures:
            return
        if self._rediscovery_error is not None:
            raise self._rediscovery_error
        raise RuntimeError("rediscovery failed")

    def _schedule_notification_followup(
        self,
        subscribed_resource: str,
        resource_type: str | None,
        *,
        after_dedup_window: bool = True,
    ) -> None:
        """Schedule one re-poll of ``subscribed_resource``.

        With ``after_dedup_window`` the re-poll waits for the resource's dedup
        window to end; without it, it runs as soon as no rediscovery is running.
        Every re-poll waits for a running rediscovery to finish first.

        Does nothing when a follow-up for the resource is already pending, when
        the subscription manager says none is needed, or after shutdown started.
        """
        mgr = self._subscription_manager
        if (
            mgr is None
            or self._shutdown_event.is_set()
            or subscribed_resource in self._notification_followups
        ):
            return
        delay: float | None = None
        if after_dedup_window:
            delay = mgr.followup_delay(subscribed_resource)
            if delay is None:
                return
        task = asyncio.create_task(
            self._run_notification_followup(subscribed_resource, resource_type, delay),
            name=f"notification-followup:{subscribed_resource}",
        )
        self._notification_followups[subscribed_resource] = task
        self._live_followups.add(task)
        task.add_done_callback(self._live_followups.discard)

    async def _run_notification_followup(
        self, subscribed_resource: str, resource_type: str | None, delay: float | None
    ) -> None:
        """Route a data-change notification for the resource once it is due.

        With a ``delay``, wait that many seconds; if a notification for the
        resource was processed meanwhile, the window restarted, so wait for the
        new window to end. Then wait for any running rediscovery to finish.
        """
        mgr = self._subscription_manager
        try:
            while delay is not None:
                await asyncio.sleep(delay)
                if mgr is None or not mgr.is_duplicate_notification(subscribed_resource):
                    break
                delay = mgr.followup_delay(subscribed_resource) or 0.0
            while self._rediscovery_lock.locked():
                await asyncio.sleep(_REDISCOVERY_WAIT_INTERVAL_S)
        finally:
            # Removed before routing, so a notification suppressed during the
            # re-poll schedules a new follow-up rather than being dropped.
            if self._notification_followups.get(subscribed_resource) is asyncio.current_task():
                del self._notification_followups[subscribed_resource]
        logger.info("Re-polling %s after a deferred notification", subscribed_resource)
        try:
            await self._route_default_notification(subscribed_resource, resource_type)
        except Exception:
            if mgr is not None:
                mgr.forget_notification(subscribed_resource)
            logger.warning(
                "Follow-up re-poll for %s failed", subscribed_resource, exc_info=True
            )

    async def _cancel_notification_followups(self) -> None:
        """Cancel the pending follow-up re-polls and wait for them to stop."""
        tasks = list(self._live_followups)
        self._notification_followups.clear()
        self._live_followups.clear()
        for task in tasks:
            task.cancel()
        for task in tasks:
            with contextlib.suppress(asyncio.CancelledError):
                await task

    def _is_structural(self, resource_path: str, resource_type: str | None = None) -> bool:
        """Classify a notification as structural (EndDevice/FSA -> rediscovery).

        Prefers the subscription's topology-derived ``resource_type`` (naming-
        independent); falls back to the path heuristic only when the type is
        unknown (the notification's subscription isn't tracked locally).
        """
        if resource_type is not None:
            return resource_type in _STRUCTURAL_RESOURCE_TYPES
        return self._is_structural_resource(resource_path)

    def _is_control(self, resource_path: str, resource_type: str | None = None) -> bool:
        """Classify a notification as a control change (DERControl -> targeted fetch).

        Prefers ``resource_type``; falls back to the path heuristic when unknown.
        """
        if resource_type is not None:
            return resource_type in _CONTROL_RESOURCE_TYPES
        return self._is_control_resource(resource_path)

    def _is_structural_resource(self, resource_path: str) -> bool:
        """Return True if the resource path represents a structural change.

        Path-heuristic fallback used when the notification's subscription type
        isn't known. Only EndDevice and FSA changes require full rediscovery (they
        alter the device mapping); DERProgram and DERControl changes are handled
        by a lighter re-poll via ``_do_poll_derp``.
        """
        path = resource_path.rstrip("/")
        parts = path.strip("/").split("/")
        if not parts:
            return True

        has_control = "derc" in parts or "dderc" in parts
        has_program = "derp" in parts

        # EndDeviceList (/edev) or a single EndDevice (/edev/<id>). Match ``edev``
        # as a path segment near the end rather than at position 0, so a server
        # base-path prefix (e.g. /api/v2/edev) still resolves. Deeper paths
        # (.../edev/<id>/fsa, .../der/...) fall through to the fsa branch.
        if "edev" in parts:
            edev_idx = parts.index("edev")
            if len(parts) - edev_idx <= 2:
                return True

        # FSA changes (contains /fsa but not /derp or /derc)
        return "fsa" in parts and not has_program and not has_control

    @staticmethod
    def _is_control_resource(resource_path: str) -> bool:
        """Return True if the path is a DERControlList or DefaultDERControl.

        These are candidates for targeted fetch (single-program refresh)
        rather than a full re-poll of all programs.
        """
        path = resource_path.rstrip("/")
        return path.endswith("/derc") or path.endswith("/dderc")

    def _is_tariff(self, resource_path: str, resource_type: str | None = None) -> bool:
        """Classify a notification as a Pricing change (tariff -> tariff re-poll).

        Prefers ``resource_type``; falls back to the path heuristic when unknown.
        """
        if resource_type is not None:
            return resource_type in _TARIFF_RESOURCE_TYPES
        return self._is_tariff_resource(resource_path)

    @staticmethod
    def _is_tariff_resource(resource_path: str) -> bool:
        """Return True if the path is a TariffProfileList or TimeTariffIntervalList.

        Path-heuristic fallback used when the subscription's type isn't tracked.
        The tariff tree convention is ``/tp/<n>/rc/<n>/tti`` (see the Pricing plan).
        """
        path = resource_path.rstrip("/")
        return path.endswith("/tti") or path.endswith("/tp")

    async def _route_default_notification(
        self,
        subscribed_resource: str,
        resource_type: str | None = None,
        *,
        raise_failures: bool = False,
    ) -> None:
        """Route a STATUS_DEFAULT notification based on resource type.

        ``resource_type`` (the subscription's topology-derived type) drives the
        classification when known; otherwise the path heuristic is the fallback.
        ``raise_failures`` raises the error of a rediscovery that failed.
        """
        if self._is_structural(subscribed_resource, resource_type):
            logger.info(
                "Structural change notification for %s, triggering rediscovery",
                subscribed_resource,
            )
            await self._rediscover_for_notification(raise_failures)
        else:
            # Defer the re-poll while rediscovery is in progress: the device
            # mapping is in flux and process_controls would produce incorrect
            # results. The rediscovery may already have read this resource before
            # the change, so re-poll once it finishes.
            if self._rediscovery_lock.locked():
                logger.debug(
                    "Deferring re-poll for %s until rediscovery finishes",
                    subscribed_resource,
                )
                self._schedule_notification_followup(
                    subscribed_resource, resource_type, after_dedup_window=False
                )
                return

            # Pricing change: refresh the tariff tree and relay any active-interval
            # price change (parallel to the DER path; not a control or structural).
            if self._is_tariff(subscribed_resource, resource_type):
                logger.info(
                    "Tariff change notification for %s, re-polling tariffs",
                    subscribed_resource,
                )
                await self._do_poll_tariff()
                return

            # Targeted fetch for DERControlList / DefaultDERControl notifications
            if self._is_control(subscribed_resource, resource_type):
                logger.info(
                    "Control change notification for %s, targeted fetch",
                    subscribed_resource,
                )
                # Record ancestor paths in the dedup cache BEFORE the targeted
                # fetch's awaits. The server sends a sibling DERProgramList
                # notification alongside the DERControlList one (its
                # DERControlListLink.all count changed); recording ancestry up
                # front suppresses that sibling instead of letting it race ahead
                # into a redundant full re-poll during our await window. Recorded
                # unconditionally: on the rare targeted-fetch failure we fall
                # through to a full re-poll below, which covers the sibling too.
                mgr = self._subscription_manager
                if mgr:
                    mgr.record_notification_ancestry(subscribed_resource)
                targeted = (
                    self._do_poll_derp_targeted(subscribed_resource, strict=True)
                    if raise_failures
                    else self._do_poll_derp_targeted(subscribed_resource)
                )
                if await targeted:
                    return
                logger.info(
                    "Targeted fetch failed for %s, falling back to full re-poll",
                    subscribed_resource,
                )

            logger.info(
                "Control change notification for %s, re-polling DERPs",
                subscribed_resource,
            )
            await self._do_poll_derp()

    async def _handle_resource_deleted(
        self, subscribed_resource: str, *, raise_failures: bool = False
    ) -> None:
        """Handle STATUS_RESOURCE_DELETED notification.

        If the deleted resource is an EndDevice, call the device-removal callback.
        Otherwise trigger rediscovery; ``raise_failures`` raises its error if it fails.
        """
        path = subscribed_resource.rstrip("/")
        parts = path.strip("/").split("/")

        # EndDevice deletion: /edev/X (exactly 2 segments)
        if len(parts) == 2 and parts[0] == "edev":
            logger.info("EndDevice deleted: %s", subscribed_resource)
            if self._on_device_removed:
                await self._on_device_removed(subscribed_resource)
            return

        # Other resource deletions trigger rediscovery
        logger.info("Resource deleted: %s, triggering rediscovery", subscribed_resource)
        await self._rediscover_for_notification(raise_failures)

    async def trigger_rediscovery(self) -> bool:
        """Re-run discovery and restart polling. Safe to call concurrently.

        If called while rediscovery is already running, sets a pending flag so
        the running rediscovery will loop once more after completing. This ensures
        structural change notifications received mid-run are not silently dropped.

        Returns True when a rediscovery pass completed (state rebuilt, polls
        re-armed); False when discovery raised (polls are still re-armed so the
        heartbeat can recover) or when the call coalesced into an already-running
        rediscovery. Most callers ignore the result; the comms-loss recovery path
        uses it to avoid clearing the outage gate on a failed re-poll.
        """
        if self._rediscovery_lock.locked():
            self._rediscovery_pending = True
            logger.debug("Rediscovery already in progress, will re-run after completion")
            return False

        async with self._rediscovery_lock:
            passes = 0
            # A status=1 resubscribe-suppression is deliberately NOT cleared here.
            # Rediscovery is frequent (structural-change notifications trigger it),
            # so clearing on every pass would undo an honoured server cancellation
            # -- a status=1 that lands just before an unrelated rediscovery would be
            # wiped and the resource re-subscribed. The suppression is
            # cleared only on a genuine reconnect (upstream-restart detection in
            # _note_successful_contact), where the server's subscription store is
            # presumed gone. The cancelled resource is still observed via polling
            # (mark_cancelled unsuppresses its poll key), so honouring the
            # cancellation does not lose changes.
            while True:
                self._rediscovery_pending = False
                passes += 1
                self._rediscovery_passes += 1
                this_pass = self._rediscovery_passes
                logger.info("Rediscovery: resetting polls (preserving event processor)")
                await self._scheduler.cancel_all()
                self._scheduler = self._new_scheduler()
                # Cancel renewal task (reconciliation handles subscriptions)
                if self._renewal_task and not self._renewal_task.done():
                    self._renewal_task.cancel()
                    with contextlib.suppress(asyncio.CancelledError):
                        await self._renewal_task
                    self._renewal_task = None
                # Remember active programs before clearing state
                previous_programs = set(self._state.der_programs.keys())
                # Taken only from state a pass rebuilt: after a failed pass the
                # state is incomplete, and the earlier scope still applies.
                if self._device_filter is not None and self._rediscovery_error is None:
                    self._scope_before_rediscovery = self._scoped_resource_hrefs()
                # Signal that state is being rebuilt — event timers that fire
                # during this window will wait until discover() repopulates it.
                self._state_ready.clear()
                # A rediscovery is commonly triggered by a *reconnect* (upstream
                # restart detection). If the server flaps -- recovers just long
                # enough to trigger this, then drops again -- a GET here (discover
                # or a refresh) raises. The scheduler was already rebuilt empty
                # above, so we MUST still re-arm it in that case: otherwise no
                # poll and no connectivity heartbeat fire, and server_alive is
                # stuck false forever while telemetry keeps last_contact fresh but
                # never chain-validates. _start_polls() therefore runs on both the
                # success and failure paths.
                try:
                    try:
                        # Clear discovered state but preserve subscriptions for reconciliation.
                        # Registration PIN verification is a one-shot startup check
                        # (done in connect()), so it's intentionally not repeated on
                        # rediscovery -- no registration_pins passed here.
                        # discover() reads the tariff tree with no previous state, so
                        # a read that fails there would drop the schedule and the
                        # boundary timer would relay a false end. Re-read the tree
                        # from the previous one, which keeps what a failed read
                        # misses. This runs before subscriptions are reconciled, so
                        # they use the current interval-list links.
                        previous_tariffs = dict(self._state.tariff_profiles)
                        self._state.clear()
                        await discover(
                            self._http,
                            self._state,
                            dcap_path=self._dcap_path,
                            device_filter=self._device_filter,
                        )
                        if previous_tariffs:
                            self._state.tariff_profiles = previous_tariffs
                            previous_tariffs = {}
                            async with self._tariff_lock:
                                await refresh_tariffs(self._http, self._state)
                    finally:
                        if previous_tariffs:
                            # discover() raised: keep the previous tree.
                            self._state.tariff_profiles = previous_tariffs
                        # Always unblock timer callbacks — if discover() failed they will
                        # see empty state (harmless no-ops) rather than hanging forever.
                        self._state_ready.set()
                    if self._subscription_manager:
                        desired = self._compute_desired_subscriptions()
                        agg_sub_href = self._find_agg_sub_href()
                        if agg_sub_href and desired:
                            # renew_kept=False: rediscovery must not re-POST kept
                            # subscriptions. Re-POSTing makes some servers echo a
                            # current-state notification, which we classify as a
                            # structural change and rediscover again -- an infinite
                            # loop. The periodic renewal task owns subscription
                            # lifetime; rediscovery only reconciles membership.
                            # A process acting for some of the EndDevices sees only
                            # the resources it needs, so a subscription it does not
                            # want may be another process's. Only the process with
                            # own-EndDevice work, acting for every EndDevice,
                            # cancels what it does not want.
                            result = await self._subscription_manager.reconcile(
                                agg_sub_href,
                                desired,
                                renew_kept=False,
                                cancel_unwanted=self._maintains_whole_set(),
                            )
                            logger.info(
                                "Subscription reconciliation: kept=%d cancelled=%d"
                                " created=%d renewed=%d",
                                result.kept,
                                result.cancelled,
                                result.created,
                                result.renewed,
                            )
                            # renew_kept=False keeps subs without verifying them, so a
                            # subscription whose server-side URI has drifted would still
                            # be reported under its stale local URI. Adopt the server's
                            # current URIs so post-rediscovery state matches the server
                            # (a stale URI otherwise 404s on a later cancel/renew).
                            # reestablish_missing=False: adopt-only, no re-POST -- a
                            # re-POST here risks the rediscovery<->echo loop.
                            # Only records for resources this process wants are
                            # dropped: it cannot subscribe to the others again.
                            await self._subscription_manager.reconcile_with_server(
                                agg_sub_href,
                                reestablish_missing=False,
                                resources={resource for resource, _ in desired},
                            )
                        elif self._maintains_whole_set():
                            # No subscription list or no desired subs; cancel remaining
                            await self._subscription_manager.cancel_all()
                        self._update_poll_suppression()
                        self._start_renewal_task()
                    # Re-fetch FSA and program hierarchy to catch resources added between
                    # discover() and subscription creation (same race as DERControl refresh).
                    await refresh_function_set_assignments(self._http, self._state)
                    withdrawn_pairs: list[tuple[str, str]] = []
                    pruned_programs = (
                        await refresh_der_programs(
                            self._http, self._state, withdrawn=withdrawn_pairs
                        )
                        or []
                    )
                    if self._subscription_manager:
                        await self._auto_subscribe()
                        self._update_poll_suppression()
                    # Cancel events from programs that no longer exist after rediscovery
                    current_programs = set(self._state.der_programs.keys())
                    all_removed = (previous_programs - current_programs) | set(pruned_programs)
                    for removed in all_removed:
                        self._event_processor.cancel_program(removed)
                        await self._event_processor.release_cancelled_program(removed)
                    await self._event_processor.release_withdrawn_devices(withdrawn_pairs)
                    # Re-fetch DER controls before processing: data from discover() may be
                    # stale if controls were written during subscription reconciliation.
                    await refresh_der_controls(self._http, self._state)
                    # Process controls discovered during rediscovery immediately
                    for href in list(self._state.der_programs):
                        await self._event_processor.process_controls(href)
                    self._start_polls()
                    self._rediscovery_error = None
                    self._last_completed_pass = this_pass
                    logger.info("Rediscovery complete")
                except Exception as exc:
                    # Re-arm the scheduler so the connectivity heartbeat (and polls)
                    # keep running: the heartbeat's validating GET recovers
                    # server_alive, and a later successful poll re-triggers
                    # rediscovery to rebuild state. Return rather than re-raise --
                    # the caller is often a poll loop or the reconnect path, and the
                    # empty-scheduler condition is now healed.
                    from py20305.diagnostics import report

                    report(
                        "warnings",
                        f"Rediscovery failed ({exc}); re-armed polls so the "
                        "connectivity heartbeat can recover server_alive",
                        source="client",
                        dedup_key="rediscovery:failed",
                        details={"error": str(exc)},
                        exc_info=True,
                    )
                    self._rediscovery_error = exc
                    self._start_polls()
                    return False
                if not self._rediscovery_pending:
                    break
                if passes >= _MAX_REDISCOVERY_PASSES:
                    self._rediscovery_pending = False
                    from py20305.diagnostics import report

                    report(
                        "warnings",
                        f"Rediscovery still pending after {passes} passes; stopping to "
                        "avoid an unbounded rediscovery/notification loop. A server that "
                        "echoes a structural notification on every subscription write can "
                        "cause this.",
                        source="client",
                        dedup_key="rediscovery:max_passes",
                        details={"passes": passes},
                    )
                    logger.warning(
                        "Rediscovery still pending after %d passes; stopping to avoid an "
                        "unbounded rediscovery<->notification loop",
                        passes,
                    )
                    break
                logger.info(
                    "Re-running rediscovery (structural change received during previous run)"
                )
        if self._on_rediscovered:
            # Outside the pass loop: a re-run means the state is still moving,
            # and a listener that rescheduled work per pass would do it twice.
            await self._on_rediscovered()
        return True

    def _maintains_whole_set(self) -> bool:
        """Return whether this process may cancel subscriptions it does not want.

        True only with own-EndDevice work and no device filter: a process that
        acts for some EndDevices does not know what the others need.
        """
        return self._own_end_device and self._device_filter is None

    async def take_over_devices(self, device_hrefs: Iterable[str]) -> bool:
        """Start acting for EndDevices another process stopped acting for.

        The host first makes this client's device filter accept the devices,
        and the other process releases them (:meth:`release_devices`). This
        loads their event state from the shared event state store, rediscovers
        so the devices' programs and controls are read from the server again,
        and then applies any running event a device never received. Events the
        previous process handled are carried on, not started again: no
        Response already posted is posted again, and no control already applied
        is applied again. A change the server made while no process acted for
        the devices is found by the rediscovery, as is one a notification
        reports while it runs.

        Returns False when no rediscovery pass that started after this call
        completed, when the last pass failed, or when the rediscovery did not
        find every device; nothing is
        resumed then, and the call can be made again to complete the take-over.
        Without an event state store the devices are rediscovered and treated
        as new, as a fresh client would treat them.
        """
        hrefs = list(device_hrefs)
        started_before = self._rediscovery_passes
        self._event_processor.load_devices(hrefs)
        await self.trigger_rediscovery()
        # A rediscovery already running coalesces this one into a further pass;
        # wait for that pass, so the devices are mapped before they are resumed.
        async with self._rediscovery_lock:
            pass
        if self._last_completed_pass <= started_before or self._rediscovery_error is not None:
            logger.warning(
                "Take-over incomplete: the rediscovery failed; running events are not "
                "applied to the EndDevices taken over yet"
            )
            return False
        if not all(href in self._state.end_devices for href in hrefs):
            logger.warning(
                "Take-over incomplete: not every EndDevice taken over was discovered; "
                "running events are not applied to them yet"
            )
            return False
        await self._event_processor.resume_devices(hrefs)
        return True

    async def release_devices(self, device_hrefs: Iterable[str]) -> bool:
        """Stop acting for EndDevices another process will take over.

        The host first makes this client's device filter reject the devices.
        This stops new event work for them, waits for work already running for
        them to finish, saves their event state to the shared event state
        store, forgets the events that concern only them without a Response or
        a write to a device, and rediscovers without them. Subscriptions stay
        as they are. Returns what :meth:`trigger_rediscovery` returns.

        Raises the store's error when their event state cannot be saved. The
        devices then stay with this process; the host puts them back in its
        device filter, or calls this again.
        """
        await self._event_processor.release_devices(device_hrefs)
        return await self.trigger_rediscovery()

    async def shutdown(
        self,
        timeout: float = 10.0,
        *,
        subscription_cancel_timeout: float | None = 2.0,
        unsubscribe: bool | None = None,
    ) -> None:
        """Gracefully stop polling, event timers, and close the HTTP session.

        ``timeout`` bounds the wait for the poll tasks. ``subscription_cancel_timeout``
        bounds the DELETEs that cancel this client's subscriptions; ``None`` waits
        for every one. See :meth:`SubscriptionManager.cancel_all`.

        ``unsubscribe`` decides whether those DELETEs are sent; ``None`` uses the
        subscription manager's ``unsubscribe_on_close``. Pass False when another
        instance with the same identity takes over and adopts the subscriptions.
        A process without own-EndDevice work sends none even when asked: other
        processes share its subscriptions.
        """
        self._shutdown_event.set()
        # Before anything is torn down. An expiry firing later would call
        # clear_comm_loss_simulation() against a stopped notification server and
        # scheduler, and its recovery would reopen the HTTP session this method
        # is about to close.
        await self._stop_comm_loss_simulation_expiry()
        await self._cancel_notification_followups()
        await self._cancel_tariff_timer()
        if self._renewal_task and not self._renewal_task.done():
            self._renewal_task.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                await self._renewal_task
        if self._subscription_manager:
            if unsubscribe is None:
                unsubscribe = (
                    self._own_end_device and self._subscription_manager.unsubscribe_on_close
                )
            if unsubscribe:
                await self._subscription_manager.cancel_all(timeout=subscription_cancel_timeout)
            else:
                logger.info(
                    "Leaving %d subscription(s) on the server at shutdown",
                    len(self._subscription_manager.active_subscriptions),
                )
        if self._notification_server:
            await self._notification_server.stop()
        await self._event_processor.shutdown()
        await self._tariff_processor.shutdown()
        await self._scheduler.cancel_all(timeout=timeout)
        await self._http.close()

    async def __aenter__(self) -> CsipClient:
        return self

    async def __aexit__(
        self,
        _exc_type: type[BaseException] | None,
        _exc_val: BaseException | None,
        _exc_tb: TracebackType | None,
    ) -> None:
        await self.shutdown()
device_filter property
device_filter: DeviceFilter | None

The filter choosing the EndDevices this process acts for; None is every one.

Read at each discovery, so a filter whose answers change (a :class:~py20305.client.scope.DeviceSet the host edits) takes effect at the next rediscovery.

own_end_device property
own_end_device: bool

Whether this process does the work of the client's own EndDevice.

That work is: renewing the subscriptions, reconciling them with the server's SubscriptionList, checking restored subscriptions at connect, renewing adopted subscriptions and removing duplicate ones, cancelling subscriptions at rediscovery and at shutdown, registering EndDevices, re-registering the client's own EndDevice after a loss of communications, and the telemetry a :class:~py20305.telemetry.coordinator.TelemetryCoordinator posts for the client's own LFDI. True, the default, does all of it. When several processes act as one client, give it to one of them. A process without it still creates subscriptions for the resources it needs that nobody has subscribed to yet.

dispatcher property
dispatcher: ControlDispatcher

The dispatcher this client applies controls through.

audit property
audit: AuditEmitter

The audit trail emitter. Attach a history or an audit observer to enable it.

observers property
observers: Observers

The exchange, connection and anomaly observers attached to this client.

history property
history: History | None

The operations history attached to this client, if any.

subscription_manager property
subscription_manager: SubscriptionManager | None

The subscription manager, when subscriptions are wired.

attach_observers
attach_observers(observers: Observers | None) -> None

Report this client's exchanges, connections and anomalies to observers.

Covers the requests the client sends and, once subscriptions are attached, the notifications its listener receives. None detaches them.

Source code in src/py20305/client/csip_client.py
def attach_observers(self, observers: Observers | None) -> None:
    """Report this client's exchanges, connections and anomalies to ``observers``.

    Covers the requests the client sends and, once subscriptions are
    attached, the notifications its listener receives. None detaches them.
    """
    self._http.observers = observers
    if self._notification_server is not None:
        self._notification_server.observers = self._http.observers
attach_history
attach_history(history: History | None) -> None

Keep this client's event lifecycle and posted responses in history.

Only the event part is fed from here. The dispatcher feeds the write part and the telemetry manager the interval part; hand those parts to them. Device writes are put on this client's server timebase.

Source code in src/py20305/client/csip_client.py
def attach_history(self, history: History | None) -> None:
    """Keep this client's event lifecycle and posted responses in ``history``.

    Only the event part is fed from here. The dispatcher feeds the write
    part and the telemetry manager the interval part; hand those parts to
    them. Device writes are put on this client's server timebase.
    """
    self._history = history
    events = history.events if history is not None else None
    self._audit.attach_history(events)
    self._event_processor.attach_event_history(events)
    if history is not None:
        history.use_timebase(self._timebase)
attach_subscriptions
attach_subscriptions(manager: SubscriptionManager, notification_server: NotificationServer | None = None) -> None

Wire subscribe/notify into this client after construction.

The manager needs this client's transport, which exists only once the client does, so a caller building both cannot pass them to __init__ -- construction order forces attachment to come second. Attach before connect(): auto-subscription and poll suppression consult the manager during connection.

In a client without own-EndDevice work (see :attr:own_end_device) this sets the manager's maintains_set to False, so the manager never renews, cancels or removes the shared subscriptions.

Source code in src/py20305/client/csip_client.py
def attach_subscriptions(
    self,
    manager: SubscriptionManager,
    notification_server: NotificationServer | None = None,
) -> None:
    """Wire subscribe/notify into this client after construction.

    The manager needs this client's transport, which exists only once the
    client does, so a caller building both cannot pass them to
    ``__init__`` -- construction order forces attachment to come second.
    Attach before ``connect()``: auto-subscription and poll suppression
    consult the manager during connection.

    In a client without own-EndDevice work (see :attr:`own_end_device`)
    this sets the manager's ``maintains_set`` to False, so the manager
    never renews, cancels or removes the shared subscriptions.
    """
    if not self._own_end_device:
        manager.maintains_set = False
    self._subscription_manager = manager
    if notification_server is not None:
        self._notification_server = notification_server
        notification_server.on_notification = self._handle_notification
        notification_server.observers = self._http.observers
set_on_structural_change
set_on_structural_change(callback: Callable[[], Awaitable[None]] | None) -> None

Set the structural-change callback after construction.

The same construction-order problem as attach_subscriptions, one step later: telemetry is started once discovery has produced the hrefs it posts to, so what wants to hear about a later structural change does not exist when this client is built.

Note what this callback replaces. Where a structural notification would otherwise call trigger_rediscovery(), it calls this instead, so a callback that does not rediscover leaves the state it was called about unrefreshed. To act on rebuilt state, use set_on_rediscovered.

Source code in src/py20305/client/csip_client.py
def set_on_structural_change(self, callback: Callable[[], Awaitable[None]] | None) -> None:
    """Set the structural-change callback after construction.

    The same construction-order problem as ``attach_subscriptions``, one
    step later: telemetry is started once discovery has produced the hrefs
    it posts to, so what wants to hear about a later structural change does
    not exist when this client is built.

    Note what this callback replaces. Where a structural notification would
    otherwise call ``trigger_rediscovery()``, it calls this instead, so a
    callback that does not rediscover leaves the state it was called about
    unrefreshed. To act on rebuilt state, use ``set_on_rediscovered``.
    """
    self._on_structural_change = callback
set_on_rediscovered
set_on_rediscovered(callback: Callable[[], Awaitable[None]] | None) -> None

Set a callback to run after any rediscovery pass that completed.

Every path that rebuilds discovered state ends here -- a structural notification, 404 recovery, comms-loss recovery, a subscription lost, an operator's request through the management API -- so anything holding hrefs from the previous state refreshes once, in one place, rather than each trigger remembering to say so.

Not called when discovery raised, because the state was not rebuilt, nor when the call coalesced into a running rediscovery, because that one calls it on completion.

Source code in src/py20305/client/csip_client.py
def set_on_rediscovered(self, callback: Callable[[], Awaitable[None]] | None) -> None:
    """Set a callback to run after any rediscovery pass that completed.

    Every path that rebuilds discovered state ends here -- a structural
    notification, 404 recovery, comms-loss recovery, a subscription lost,
    an operator's request through the management API -- so anything holding
    hrefs from the previous state refreshes once, in one place, rather than
    each trigger remembering to say so.

    Not called when discovery raised, because the state was not rebuilt,
    nor when the call coalesced into a running rediscovery, because that
    one calls it on completion.
    """
    self._on_rediscovered = callback
add_reconnect_listener
add_reconnect_listener(callback: Callable[[], None]) -> None

Also call callback when on_reconnect would be called.

For a component built after this client, such as the telemetry coordinator, that has to treat what it sent before an outage as lost. Listeners run in the order added, before on_reconnect, and one that raises is logged without stopping the others or the rediscovery.

Source code in src/py20305/client/csip_client.py
def add_reconnect_listener(self, callback: Callable[[], None]) -> None:
    """Also call ``callback`` when ``on_reconnect`` would be called.

    For a component built after this client, such as the telemetry
    coordinator, that has to treat what it sent before an outage as lost.
    Listeners run in the order added, before ``on_reconnect``, and one that
    raises is logged without stopping the others or the rediscovery.
    """
    self._reconnect_listeners.append(callback)
aggregation
aggregation(lfdi: str) -> AggregationInfo | None

Return the aggregation the discovered EndDevice lfdi represents, or None.

None when no discovered EndDevice has that LFDI or the EndDevice describes no aggregation (IEEE 2030.5-2023 §8.7). Read from the discovered state: the EndDevice's distribution, its AggregatedDeviceList and, for a Priority distribution, its AggregationPriority (Annex C.3), as of the last discovery or EndDevice list refresh.

Source code in src/py20305/client/csip_client.py
def aggregation(self, lfdi: str) -> AggregationInfo | None:
    """Return the aggregation the discovered EndDevice ``lfdi`` represents, or None.

    None when no discovered EndDevice has that LFDI or the EndDevice
    describes no aggregation (IEEE 2030.5-2023 §8.7). Read from the
    discovered state: the EndDevice's ``distribution``, its
    AggregatedDeviceList and, for a Priority distribution, its
    AggregationPriority (Annex C.3), as of the last discovery or EndDevice
    list refresh.
    """
    wanted = bytes.fromhex(lfdi)
    for edev_state in self._state.end_devices.values():
        if edev_state.lfdi == wanted:
            return edev_state.aggregation
    return None
register_aggregated_device async
register_aggregated_device(*, aggregated_device_list_href: str, lfdi: str, device_category: int | None = None, check_duplicate: bool = True) -> str | None

POST an AggregatedDevice to an AggregatedDeviceList and return its Location.

IEEE 2030.5-2023 §8.7.1 lets a client create an AggregatedDevice by a POST to the list, where the server allows it (Annex A.2.5.2: POST optional). lfdi is a 40-hex string; the sFDI is derived from it. With check_duplicate, the list is first read with the sFDI query string parameter (§8.7.3.2), and an LFDI the list already holds raises ValueError: clients SHALL NOT POST an AggregatedDevice the list already contains (§8.7.3.1). Raises Sep2ProtocolError on a server rejection status and RuntimeError in a process without own-EndDevice duties (see :attr:own_end_device).

Source code in src/py20305/client/csip_client.py
async def register_aggregated_device(
    self,
    *,
    aggregated_device_list_href: str,
    lfdi: str,
    device_category: int | None = None,
    check_duplicate: bool = True,
) -> str | None:
    """POST an AggregatedDevice to an AggregatedDeviceList and return its ``Location``.

    IEEE 2030.5-2023 §8.7.1 lets a client create an AggregatedDevice by a POST
    to the list, where the server allows it (Annex A.2.5.2: POST optional).
    ``lfdi`` is a 40-hex string; the ``sFDI`` is derived from it. With
    ``check_duplicate``, the list is first read with the ``sFDI`` query
    string parameter (§8.7.3.2), and an LFDI the list already holds raises
    ``ValueError``: clients SHALL NOT POST an AggregatedDevice the list
    already contains (§8.7.3.1). Raises ``Sep2ProtocolError`` on a server
    rejection status and ``RuntimeError`` in a process without
    own-EndDevice duties (see :attr:`own_end_device`).
    """
    if not self._own_end_device:
        raise RuntimeError(
            "this client process does not do own-EndDevice work; register devices "
            "from the process that does"
        )
    from py20305.models.sep.sep import (
        AggregatedDevice,
        AggregatedDeviceList,
        DeviceCategoryType,
        Sfditype,
        TimeType,
    )
    from py20305.security.identity import compute_sfdi

    # An LFDI is 160 bits: 40 hex characters. A shorter or longer string
    # would register a different identity.
    try:
        lfdi_bytes = bytes.fromhex(lfdi)
    except ValueError:
        raise ValueError(f"lfdi must be 40 hexadecimal characters, got {lfdi!r}") from None
    if len(lfdi_bytes) != 20 or len(lfdi) != 40:
        raise ValueError(f"lfdi must be 40 hexadecimal characters, got {lfdi!r}")
    sfdi = compute_sfdi(lfdi)
    if device_category is not None and not 0 <= device_category < 2**32:
        raise ValueError("device_category must be between 0 and 4294967295 (32-bit bitmap)")
    if check_duplicate:
        separator = "&" if "?" in aggregated_device_list_href else "?"
        query = f"{aggregated_device_list_href}{separator}sFDI={sfdi}"
        for page in await self._http.get_list(query, AggregatedDeviceList):
            if any(device.l_fdi == lfdi_bytes for device in page.aggregated_device):
                raise ValueError(f"An AggregatedDevice with LFDI {lfdi} is already listed")
    device = AggregatedDevice(
        l_fdi=lfdi_bytes,
        s_fdi=Sfditype(value=sfdi),
        changed_time=TimeType(value=int(self._timebase.now())),
    )
    if device_category is not None:
        device.device_category = DeviceCategoryType(value=device_category.to_bytes(4, "big"))
    return await self._http.post(aggregated_device_list_href, device)
run_redirect_probe async
run_redirect_probe() -> dict[str, Any]

Run ERR-001 HTTP-to-HTTPS redirect probe using this client's session.

Source code in src/py20305/client/csip_client.py
async def run_redirect_probe(self) -> dict[str, Any]:
    """Run ERR-001 HTTP-to-HTTPS redirect probe using this client's session."""
    from py20305.client.redirect_probe import run_redirect_probe

    return await run_redirect_probe(self._http.host, self._http)
register_end_device async
register_end_device(*, lfdi: str, device_category: int | None = None, edev_list_href: str | None = None, check_duplicate: bool = True) -> str | None

In-band registration: POST a managed EndDevice to the server's EndDeviceList and return the Location (the new /edev/{id} href).

lfdi is a 40-hex string; sFDI is derived from it (never free-entered). device_category is an optional DeviceCategoryType bitmap. The target list defaults to the discovered dcap.end_device_list_link unless edev_list_href overrides it.

Raises ValueError for a missing EndDeviceList href or, when check_duplicate, an LFDI the server already lists (IEEE 2030.5 §8.5.3: clients SHALL NOT POST a duplicate EndDevice). Raises Sep2ProtocolError on a server rejection status. Raises RuntimeError in a process without own-EndDevice duties (see :attr:own_end_device).

Source code in src/py20305/client/csip_client.py
async def register_end_device(
    self,
    *,
    lfdi: str,
    device_category: int | None = None,
    edev_list_href: str | None = None,
    check_duplicate: bool = True,
) -> str | None:
    """In-band registration: POST a managed EndDevice to the server's
    EndDeviceList and return the ``Location`` (the new ``/edev/{id}`` href).

    ``lfdi`` is a 40-hex string; ``sFDI`` is derived from it (never
    free-entered). ``device_category`` is an optional DeviceCategoryType
    bitmap. The target list defaults to the discovered
    ``dcap.end_device_list_link`` unless ``edev_list_href`` overrides it.

    Raises ``ValueError`` for a missing EndDeviceList href or, when
    ``check_duplicate``, an LFDI the server already lists (IEEE 2030.5 §8.5.3:
    clients SHALL NOT POST a duplicate EndDevice). Raises ``Sep2ProtocolError``
    on a server rejection status. Raises ``RuntimeError`` in a process
    without own-EndDevice duties (see :attr:`own_end_device`).
    """
    if not self._own_end_device:
        raise RuntimeError(
            "this client process does not do own-EndDevice work; register EndDevices "
            "from the process that does"
        )
    from py20305.models.sep.sep import (
        DeviceCategoryType,
        EndDevice,
        Sfditype,
        TimeType,
    )
    from py20305.security.identity import compute_sfdi

    lfdi_bytes = bytes.fromhex(lfdi)
    href = edev_list_href or _extract_href(
        self._state.dcap.end_device_list_link if self._state.dcap else None
    )
    if not href:
        raise ValueError(
            "No EndDeviceList href -- the server's DeviceCapability has not been discovered"
        )

    if check_duplicate and await self._server_lists_end_device(lfdi, edev_list_href=href):
        raise ValueError(f"An EndDevice with LFDI {lfdi} is already registered")

    end_device = EndDevice(
        l_fdi=lfdi_bytes,
        s_fdi=Sfditype(value=compute_sfdi(lfdi)),
        changed_time=TimeType(value=int(self._timebase.now())),
    )
    if device_category is not None:
        if not 0 <= device_category < 2**32:
            raise ValueError("device_category must be between 0 and 4294967295 (32-bit bitmap)")
        end_device.device_category = DeviceCategoryType(
            value=device_category.to_bytes(4, "big")
        )
    return await self._http.post(href, end_device)
connect async
connect() -> None

Run initial resource discovery and auto-subscribe if configured.

Source code in src/py20305/client/csip_client.py
async def connect(self) -> None:
    """Run initial resource discovery and auto-subscribe if configured."""
    if self._notification_server and not self._notification_server.running:
        await self._notification_server.start()
    await discover(
        self._http,
        self._state,
        registration_pins=self._registration_pins,
        dcap_path=self._dcap_path,
        device_filter=self._device_filter,
    )
    # Capture the client's own LFDI for comms-loss self-reregistration.
    # Cert-derived and stable across reset_session, so a single capture holds.
    if self._own_lfdi is None:
        self._own_lfdi = self._http.client_lfdi
    if self._subscription_manager:
        if self._own_end_device and self._subscription_manager.active_subscriptions:
            valid, removed = await self._subscription_manager.validate_restored_subscriptions()
            logger.info("Subscription validation: %d valid, %d removed", valid, removed)
        if self._subscription_manager.adopt_on_connect:
            await self.adopt_subscriptions()
        await self._auto_subscribe()
        self._update_poll_suppression()
        self._start_renewal_task()
adopt_subscriptions async
adopt_subscriptions(*, remove_duplicates: bool | None = None) -> AdoptionResult | None

Take over the subscriptions the server already holds for this client.

Calls :meth:SubscriptionManager.adopt_existing with this client's SubscriptionList and the resources it subscribes to. connect() calls this before creating subscriptions when the manager was built with adopt_on_connect. remove_duplicates defaults to the manager's setting.

Returns None when no subscription manager is attached or discovery has found no SubscriptionListLink.

Source code in src/py20305/client/csip_client.py
async def adopt_subscriptions(
    self, *, remove_duplicates: bool | None = None
) -> AdoptionResult | None:
    """Take over the subscriptions the server already holds for this client.

    Calls :meth:`SubscriptionManager.adopt_existing` with this client's
    SubscriptionList and the resources it subscribes to. ``connect()`` calls
    this before creating subscriptions when the manager was built with
    ``adopt_on_connect``. ``remove_duplicates`` defaults to the manager's
    setting.

    Returns None when no subscription manager is attached or discovery has
    found no SubscriptionListLink.
    """
    mgr = self._subscription_manager
    if mgr is None:
        return None
    sub_list_href = self._find_agg_sub_href()
    if sub_list_href is None:
        return None
    if remove_duplicates is None:
        remove_duplicates = mgr.remove_duplicates
    result = await mgr.adopt_existing(
        sub_list_href,
        self._compute_desired_subscriptions(),
        remove_duplicates=remove_duplicates,
    )
    self._update_poll_suppression()
    return result
run async
run() -> None

Start polling and block until shutdown is signaled.

Source code in src/py20305/client/csip_client.py
async def run(self) -> None:
    """Start polling and block until shutdown is signaled."""
    logger.info("Starting poll loop")
    # Process controls from initial discovery immediately
    for href in list(self._state.der_programs):
        await self._event_processor.process_controls(href)
    self._start_polls()
    await self._shutdown_event.wait()
    logger.info("Poll loop exiting (shutdown signaled)")
simulate_comm_loss async
simulate_comm_loss(duration_seconds: float, *, drain_timeout: float = 5.0) -> dict[str, Any]

Cut this client off from the head-end for duration_seconds.

Mechanism, not policy: whether a simulation is permitted at all, and how long a window may reasonably run, are the caller's decisions. This method does what it is told and guarantees only that the window closes.

Notifications are suspended before the transport gate closes, so a control cannot slip in through the inbound path while the outbound one is being shut. Both then drain: a request or handler already running would otherwise finish after the window opened, refreshing last_contact_epoch or applying a setpoint to a DER the operator believes is isolated.

The returned status reports whether each drain completed. A drain that timed out does not abort the simulation -- the window is open either way -- but it tells the caller the isolation was not clean.

Source code in src/py20305/client/csip_client.py
async def simulate_comm_loss(
    self, duration_seconds: float, *, drain_timeout: float = 5.0
) -> dict[str, Any]:
    """Cut this client off from the head-end for ``duration_seconds``.

    Mechanism, not policy: whether a simulation is permitted at all, and how
    long a window may reasonably run, are the caller's decisions. This
    method does what it is told and guarantees only that the window closes.

    Notifications are suspended before the transport gate closes, so a
    control cannot slip in through the inbound path while the outbound one
    is being shut. Both then drain: a request or handler already running
    would otherwise finish after the window opened, refreshing
    ``last_contact_epoch`` or applying a setpoint to a DER the operator
    believes is isolated.

    The returned status reports whether each drain completed. A drain that
    timed out does not abort the simulation -- the window is open either way
    -- but it tells the caller the isolation was not clean.
    """
    if not math.isfinite(duration_seconds) or duration_seconds <= 0:
        # An infinite or nonsensical duration would defeat the one guarantee
        # this method makes. Rejected before anything is suspended so a bad
        # call cannot half-isolate the client.
        raise ValueError(
            f"duration_seconds must be finite and greater than zero, got {duration_seconds!r}"
        )
    if not math.isfinite(drain_timeout) or drain_timeout < 0:
        # An unbounded drain would stall between arming the gates and
        # scheduling the timer that closes them -- isolated, with nothing
        # left to restore it.
        raise ValueError(
            f"drain_timeout must be finite and not negative, got {drain_timeout!r}"
        )

    async with self._comm_loss_lifecycle:
        # A generation per window. Everything that ends a window checks it,
        # so a timer or clear belonging to a previous one cannot reach in
        # and disarm the window that replaced it.
        self._comm_loss_generation += 1
        generation = self._comm_loss_generation
        # Cancelled, not awaited: an older timer may itself be waiting on
        # this lock, and awaiting it here would deadlock. The generation
        # check is what makes cancelling sufficient.
        self._cancel_comm_loss_simulation_expiry()

        expires_at = time.time() + duration_seconds
        # The sleep runs on the monotonic clock, like the rest of the
        # scheduler, so a wall-clock step cannot cut a window short or
        # stretch it. `expires_at` stays wall-clock because that is what an
        # operator reads.
        monotonic_deadline = time.monotonic() + duration_seconds

        notifications_drained = True
        try:
            if self._notification_server is not None:
                notifications_drained = await self._notification_server.suspend(
                    drain_timeout=drain_timeout
                )
            requests_drained = await self._http.arm_comm_loss_simulation(
                expires_at=expires_at, drain_timeout=drain_timeout
            )
        except BaseException:
            # Both gates close before their drains await, so a cancellation
            # or failure part-way through would otherwise leave the client
            # isolated with no timer to restore it -- the one outcome this
            # must never produce. Roll back to a client that was never
            # isolated, and let the caller see the failure.
            self._http.comm_loss_simulation.abort()
            if self._notification_server is not None:
                self._notification_server.resume()
            raise

        self._comm_loss_simulation_expiry = asyncio.create_task(
            self._expire_comm_loss_simulation(monotonic_deadline, generation),
            name="comm-loss-simulation-expiry",
        )

        logger.warning(
            "Comms-loss simulation started for %.0fs (operator-triggered); "
            "upstream traffic suppressed",
            duration_seconds,
        )
        status = self.comm_loss_simulation_status()
        status["requests_drained"] = requests_drained
        status["notifications_drained"] = notifications_drained
        return status
clear_comm_loss_simulation async
clear_comm_loss_simulation() -> dict[str, Any]

End the simulation and drive recovery, without waiting for a probe.

Clearing the gate is not the same event as leaving comms-loss mode: the detector only exits once a request actually reaches the server, which waits on the connectivity heartbeat and then a probe tick -- some two and a half minutes of apparent inactivity on default settings. So recovery is driven here rather than left to the schedule.

Source code in src/py20305/client/csip_client.py
async def clear_comm_loss_simulation(self) -> dict[str, Any]:
    """End the simulation and drive recovery, without waiting for a probe.

    Clearing the gate is not the same event as leaving comms-loss mode: the
    detector only exits once a request actually reaches the server, which
    waits on the connectivity heartbeat and then a probe tick -- some two
    and a half minutes of apparent inactivity on default settings. So
    recovery is driven here rather than left to the schedule.
    """
    async with self._comm_loss_lifecycle:
        return await self._clear_comm_loss_simulation_locked()
recover_from_comms_loss_now async
recover_from_comms_loss_now() -> None

Run comms-loss recovery immediately, serialized with the scheduler.

Takes the same per-key lock as the scheduled comms-loss probe. Calling _recover_from_comms_loss directly would bypass it, letting a scheduled tick overlap this one -- and recovery checks for and possibly re-POSTs the client's own EndDevice before it reaches the rediscovery lock, so two overlapping runs can register twice against a live head-end.

The active check happens inside the lock, so a tick that recovered while this call was waiting is not undone by a second, redundant recovery.

Source code in src/py20305/client/csip_client.py
async def recover_from_comms_loss_now(self) -> None:
    """Run comms-loss recovery immediately, serialized with the scheduler.

    Takes the same per-key lock as the scheduled comms-loss probe. Calling
    ``_recover_from_comms_loss`` directly would bypass it, letting a
    scheduled tick overlap this one -- and recovery checks for and possibly
    re-POSTs the client's own EndDevice before it reaches the rediscovery
    lock, so two overlapping runs can register twice against a live
    head-end.

    The active check happens inside the lock, so a tick that recovered while
    this call was waiting is not undone by a second, redundant recovery.
    """

    async def _run() -> None:
        if not self._comms_loss.active:
            return
        await self._recover_from_comms_loss()

    await self._scheduler.run_exclusive("comms_loss", _run)
comm_loss_simulation_status
comm_loss_simulation_status() -> dict[str, Any]

Report the simulation window and its recovery phase.

Source code in src/py20305/client/csip_client.py
def comm_loss_simulation_status(self) -> dict[str, Any]:
    """Report the simulation window and its recovery phase."""
    return self._http.comm_loss_simulation.status()
poll_now async
poll_now() -> int

Trigger an immediate DER program control poll.

Unlike trigger_rediscovery(), this does NOT reset the event processor or re-run discovery. It simply refreshes DERControl lists for all known programs and processes any new/changed controls through the existing event processor.

Returns the number of programs polled.

Source code in src/py20305/client/csip_client.py
async def poll_now(self) -> int:
    """Trigger an immediate DER program control poll.

    Unlike trigger_rediscovery(), this does NOT reset the event processor
    or re-run discovery. It simply refreshes DERControl lists for all
    known programs and processes any new/changed controls through the
    existing event processor.

    Returns the number of programs polled.
    """
    logger.info("Immediate derp poll requested")
    await self._do_poll_derp()
    n = len(self._state.der_programs)
    logger.info("Immediate derp poll complete: %d program(s)", n)
    return n
process_notification async
process_notification(notification: bytes | Notification) -> bool

Process one notification received by any process acting as this client.

For a host that receives notifications in one process and processes them in another: the listener's forward hands the raw body to the host, the host queues it, and the process acting for the affected EndDevice calls this with it. notification is the raw XML body as the server sent it, or a parsed Notification.

Returns True when this process acted on the notification, False when it did not: it concerns no EndDevice this process acts for (see :meth:acts_for_resource), or a loss-of-communications simulation is running, during which notifications are dropped as the listener drops them. A host can therefore deliver every notification to every process.

Raises ValueError when the body is not a valid Notification. An error while acting on it (a failed GET, or a failed rediscovery the notification started) is raised to the caller, so the host can keep the notification and retry it; a retry is not skipped as a duplicate. A notification that arrives while a rediscovery is running is left to that rediscovery and counts as acted on.

Source code in src/py20305/client/csip_client.py
async def process_notification(self, notification: bytes | Notification) -> bool:
    """Process one notification received by any process acting as this client.

    For a host that receives notifications in one process and processes them
    in another: the listener's ``forward`` hands the raw body to the host,
    the host queues it, and the process acting for the affected EndDevice
    calls this with it. ``notification`` is the raw XML body as the server
    sent it, or a parsed Notification.

    Returns True when this process acted on the notification, False when it
    did not: it concerns no EndDevice this process acts for (see
    :meth:`acts_for_resource`), or a loss-of-communications simulation is
    running, during which notifications are dropped as the listener drops
    them. A host can therefore deliver every notification to every process.

    Raises ``ValueError`` when the body is not a valid Notification. An
    error while acting on it (a failed GET, or a failed rediscovery the
    notification started) is raised to the caller, so the host can keep the
    notification and retry it; a retry is not skipped as a duplicate. A
    notification that arrives while a rediscovery is running is left to
    that rediscovery and counts as acted on.
    """
    from py20305.models.sep.sep import Notification
    from py20305.subscription.notification_server import (
        parse_notification,
        validate_notification,
    )

    if isinstance(notification, Notification):
        parsed = notification
    else:
        try:
            parsed = parse_notification(notification)
        except Exception as exc:
            raise ValueError(f"not a valid IEEE 2030.5 Notification: {exc}") from exc
        if not validate_notification(parsed, notification):
            raise ValueError("not a valid IEEE 2030.5 Notification: xsi:type mismatch")
    if self._http.comm_loss_simulation.active:
        logger.info(
            "Notification for %s dropped: comms-loss simulation active",
            parsed.subscribed_resource or "<unknown>",
        )
        return False
    return await self._handle_notification(parsed, raise_failures=True)
acts_for_resource
acts_for_resource(href: str) -> bool

Return whether a notification about href concerns this process.

Always True without a device filter. With one, True for the EndDeviceList, for the EndDevices this process acts for and their FunctionSetAssignmentsLists, for the DERProgramLists of their FunctionSetAssignments, for their DERPrograms with their DERControlLists and DefaultDERControls, and for the Pricing resources it reads. Also True while a rediscovery is running, because the discovered state is being rebuilt then and the rediscovery deals with what arrives. A notification that reports a resource cancelled, moved, changed or deleted is not left to a running rediscovery: the client matches it against the EndDevices this process acted for when the rediscovery began, and those it has found since. After a rediscovery that failed, the hrefs this process acted for before it still count, so a notification retried after the failure is not dropped.

Source code in src/py20305/client/csip_client.py
def acts_for_resource(self, href: str) -> bool:
    """Return whether a notification about ``href`` concerns this process.

    Always True without a device filter. With one, True for the
    EndDeviceList, for the EndDevices this process acts for and their
    FunctionSetAssignmentsLists, for the DERProgramLists of their
    FunctionSetAssignments, for their DERPrograms with their DERControlLists
    and DefaultDERControls, and for the Pricing resources it reads. Also
    True while a rediscovery is running, because the discovered state is
    being rebuilt then and the rediscovery deals with what arrives. A
    notification that reports a resource cancelled, moved, changed or
    deleted is not left to a running rediscovery: the client matches it
    against the EndDevices this process acted for when the rediscovery
    began, and those it has found since. After a rediscovery that failed,
    the hrefs this process acted for before it still count, so a
    notification retried after the failure is not dropped.
    """
    if self._device_filter is None or self._rediscovery_lock.locked():
        return True
    return href in self._scope()
trigger_rediscovery async
trigger_rediscovery() -> bool

Re-run discovery and restart polling. Safe to call concurrently.

If called while rediscovery is already running, sets a pending flag so the running rediscovery will loop once more after completing. This ensures structural change notifications received mid-run are not silently dropped.

Returns True when a rediscovery pass completed (state rebuilt, polls re-armed); False when discovery raised (polls are still re-armed so the heartbeat can recover) or when the call coalesced into an already-running rediscovery. Most callers ignore the result; the comms-loss recovery path uses it to avoid clearing the outage gate on a failed re-poll.

Source code in src/py20305/client/csip_client.py
async def trigger_rediscovery(self) -> bool:
    """Re-run discovery and restart polling. Safe to call concurrently.

    If called while rediscovery is already running, sets a pending flag so
    the running rediscovery will loop once more after completing. This ensures
    structural change notifications received mid-run are not silently dropped.

    Returns True when a rediscovery pass completed (state rebuilt, polls
    re-armed); False when discovery raised (polls are still re-armed so the
    heartbeat can recover) or when the call coalesced into an already-running
    rediscovery. Most callers ignore the result; the comms-loss recovery path
    uses it to avoid clearing the outage gate on a failed re-poll.
    """
    if self._rediscovery_lock.locked():
        self._rediscovery_pending = True
        logger.debug("Rediscovery already in progress, will re-run after completion")
        return False

    async with self._rediscovery_lock:
        passes = 0
        # A status=1 resubscribe-suppression is deliberately NOT cleared here.
        # Rediscovery is frequent (structural-change notifications trigger it),
        # so clearing on every pass would undo an honoured server cancellation
        # -- a status=1 that lands just before an unrelated rediscovery would be
        # wiped and the resource re-subscribed. The suppression is
        # cleared only on a genuine reconnect (upstream-restart detection in
        # _note_successful_contact), where the server's subscription store is
        # presumed gone. The cancelled resource is still observed via polling
        # (mark_cancelled unsuppresses its poll key), so honouring the
        # cancellation does not lose changes.
        while True:
            self._rediscovery_pending = False
            passes += 1
            self._rediscovery_passes += 1
            this_pass = self._rediscovery_passes
            logger.info("Rediscovery: resetting polls (preserving event processor)")
            await self._scheduler.cancel_all()
            self._scheduler = self._new_scheduler()
            # Cancel renewal task (reconciliation handles subscriptions)
            if self._renewal_task and not self._renewal_task.done():
                self._renewal_task.cancel()
                with contextlib.suppress(asyncio.CancelledError):
                    await self._renewal_task
                self._renewal_task = None
            # Remember active programs before clearing state
            previous_programs = set(self._state.der_programs.keys())
            # Taken only from state a pass rebuilt: after a failed pass the
            # state is incomplete, and the earlier scope still applies.
            if self._device_filter is not None and self._rediscovery_error is None:
                self._scope_before_rediscovery = self._scoped_resource_hrefs()
            # Signal that state is being rebuilt — event timers that fire
            # during this window will wait until discover() repopulates it.
            self._state_ready.clear()
            # A rediscovery is commonly triggered by a *reconnect* (upstream
            # restart detection). If the server flaps -- recovers just long
            # enough to trigger this, then drops again -- a GET here (discover
            # or a refresh) raises. The scheduler was already rebuilt empty
            # above, so we MUST still re-arm it in that case: otherwise no
            # poll and no connectivity heartbeat fire, and server_alive is
            # stuck false forever while telemetry keeps last_contact fresh but
            # never chain-validates. _start_polls() therefore runs on both the
            # success and failure paths.
            try:
                try:
                    # Clear discovered state but preserve subscriptions for reconciliation.
                    # Registration PIN verification is a one-shot startup check
                    # (done in connect()), so it's intentionally not repeated on
                    # rediscovery -- no registration_pins passed here.
                    # discover() reads the tariff tree with no previous state, so
                    # a read that fails there would drop the schedule and the
                    # boundary timer would relay a false end. Re-read the tree
                    # from the previous one, which keeps what a failed read
                    # misses. This runs before subscriptions are reconciled, so
                    # they use the current interval-list links.
                    previous_tariffs = dict(self._state.tariff_profiles)
                    self._state.clear()
                    await discover(
                        self._http,
                        self._state,
                        dcap_path=self._dcap_path,
                        device_filter=self._device_filter,
                    )
                    if previous_tariffs:
                        self._state.tariff_profiles = previous_tariffs
                        previous_tariffs = {}
                        async with self._tariff_lock:
                            await refresh_tariffs(self._http, self._state)
                finally:
                    if previous_tariffs:
                        # discover() raised: keep the previous tree.
                        self._state.tariff_profiles = previous_tariffs
                    # Always unblock timer callbacks — if discover() failed they will
                    # see empty state (harmless no-ops) rather than hanging forever.
                    self._state_ready.set()
                if self._subscription_manager:
                    desired = self._compute_desired_subscriptions()
                    agg_sub_href = self._find_agg_sub_href()
                    if agg_sub_href and desired:
                        # renew_kept=False: rediscovery must not re-POST kept
                        # subscriptions. Re-POSTing makes some servers echo a
                        # current-state notification, which we classify as a
                        # structural change and rediscover again -- an infinite
                        # loop. The periodic renewal task owns subscription
                        # lifetime; rediscovery only reconciles membership.
                        # A process acting for some of the EndDevices sees only
                        # the resources it needs, so a subscription it does not
                        # want may be another process's. Only the process with
                        # own-EndDevice work, acting for every EndDevice,
                        # cancels what it does not want.
                        result = await self._subscription_manager.reconcile(
                            agg_sub_href,
                            desired,
                            renew_kept=False,
                            cancel_unwanted=self._maintains_whole_set(),
                        )
                        logger.info(
                            "Subscription reconciliation: kept=%d cancelled=%d"
                            " created=%d renewed=%d",
                            result.kept,
                            result.cancelled,
                            result.created,
                            result.renewed,
                        )
                        # renew_kept=False keeps subs without verifying them, so a
                        # subscription whose server-side URI has drifted would still
                        # be reported under its stale local URI. Adopt the server's
                        # current URIs so post-rediscovery state matches the server
                        # (a stale URI otherwise 404s on a later cancel/renew).
                        # reestablish_missing=False: adopt-only, no re-POST -- a
                        # re-POST here risks the rediscovery<->echo loop.
                        # Only records for resources this process wants are
                        # dropped: it cannot subscribe to the others again.
                        await self._subscription_manager.reconcile_with_server(
                            agg_sub_href,
                            reestablish_missing=False,
                            resources={resource for resource, _ in desired},
                        )
                    elif self._maintains_whole_set():
                        # No subscription list or no desired subs; cancel remaining
                        await self._subscription_manager.cancel_all()
                    self._update_poll_suppression()
                    self._start_renewal_task()
                # Re-fetch FSA and program hierarchy to catch resources added between
                # discover() and subscription creation (same race as DERControl refresh).
                await refresh_function_set_assignments(self._http, self._state)
                withdrawn_pairs: list[tuple[str, str]] = []
                pruned_programs = (
                    await refresh_der_programs(
                        self._http, self._state, withdrawn=withdrawn_pairs
                    )
                    or []
                )
                if self._subscription_manager:
                    await self._auto_subscribe()
                    self._update_poll_suppression()
                # Cancel events from programs that no longer exist after rediscovery
                current_programs = set(self._state.der_programs.keys())
                all_removed = (previous_programs - current_programs) | set(pruned_programs)
                for removed in all_removed:
                    self._event_processor.cancel_program(removed)
                    await self._event_processor.release_cancelled_program(removed)
                await self._event_processor.release_withdrawn_devices(withdrawn_pairs)
                # Re-fetch DER controls before processing: data from discover() may be
                # stale if controls were written during subscription reconciliation.
                await refresh_der_controls(self._http, self._state)
                # Process controls discovered during rediscovery immediately
                for href in list(self._state.der_programs):
                    await self._event_processor.process_controls(href)
                self._start_polls()
                self._rediscovery_error = None
                self._last_completed_pass = this_pass
                logger.info("Rediscovery complete")
            except Exception as exc:
                # Re-arm the scheduler so the connectivity heartbeat (and polls)
                # keep running: the heartbeat's validating GET recovers
                # server_alive, and a later successful poll re-triggers
                # rediscovery to rebuild state. Return rather than re-raise --
                # the caller is often a poll loop or the reconnect path, and the
                # empty-scheduler condition is now healed.
                from py20305.diagnostics import report

                report(
                    "warnings",
                    f"Rediscovery failed ({exc}); re-armed polls so the "
                    "connectivity heartbeat can recover server_alive",
                    source="client",
                    dedup_key="rediscovery:failed",
                    details={"error": str(exc)},
                    exc_info=True,
                )
                self._rediscovery_error = exc
                self._start_polls()
                return False
            if not self._rediscovery_pending:
                break
            if passes >= _MAX_REDISCOVERY_PASSES:
                self._rediscovery_pending = False
                from py20305.diagnostics import report

                report(
                    "warnings",
                    f"Rediscovery still pending after {passes} passes; stopping to "
                    "avoid an unbounded rediscovery/notification loop. A server that "
                    "echoes a structural notification on every subscription write can "
                    "cause this.",
                    source="client",
                    dedup_key="rediscovery:max_passes",
                    details={"passes": passes},
                )
                logger.warning(
                    "Rediscovery still pending after %d passes; stopping to avoid an "
                    "unbounded rediscovery<->notification loop",
                    passes,
                )
                break
            logger.info(
                "Re-running rediscovery (structural change received during previous run)"
            )
    if self._on_rediscovered:
        # Outside the pass loop: a re-run means the state is still moving,
        # and a listener that rescheduled work per pass would do it twice.
        await self._on_rediscovered()
    return True
take_over_devices async
take_over_devices(device_hrefs: Iterable[str]) -> bool

Start acting for EndDevices another process stopped acting for.

The host first makes this client's device filter accept the devices, and the other process releases them (:meth:release_devices). This loads their event state from the shared event state store, rediscovers so the devices' programs and controls are read from the server again, and then applies any running event a device never received. Events the previous process handled are carried on, not started again: no Response already posted is posted again, and no control already applied is applied again. A change the server made while no process acted for the devices is found by the rediscovery, as is one a notification reports while it runs.

Returns False when no rediscovery pass that started after this call completed, when the last pass failed, or when the rediscovery did not find every device; nothing is resumed then, and the call can be made again to complete the take-over. Without an event state store the devices are rediscovered and treated as new, as a fresh client would treat them.

Source code in src/py20305/client/csip_client.py
async def take_over_devices(self, device_hrefs: Iterable[str]) -> bool:
    """Start acting for EndDevices another process stopped acting for.

    The host first makes this client's device filter accept the devices,
    and the other process releases them (:meth:`release_devices`). This
    loads their event state from the shared event state store, rediscovers
    so the devices' programs and controls are read from the server again,
    and then applies any running event a device never received. Events the
    previous process handled are carried on, not started again: no
    Response already posted is posted again, and no control already applied
    is applied again. A change the server made while no process acted for
    the devices is found by the rediscovery, as is one a notification
    reports while it runs.

    Returns False when no rediscovery pass that started after this call
    completed, when the last pass failed, or when the rediscovery did not
    find every device; nothing is
    resumed then, and the call can be made again to complete the take-over.
    Without an event state store the devices are rediscovered and treated
    as new, as a fresh client would treat them.
    """
    hrefs = list(device_hrefs)
    started_before = self._rediscovery_passes
    self._event_processor.load_devices(hrefs)
    await self.trigger_rediscovery()
    # A rediscovery already running coalesces this one into a further pass;
    # wait for that pass, so the devices are mapped before they are resumed.
    async with self._rediscovery_lock:
        pass
    if self._last_completed_pass <= started_before or self._rediscovery_error is not None:
        logger.warning(
            "Take-over incomplete: the rediscovery failed; running events are not "
            "applied to the EndDevices taken over yet"
        )
        return False
    if not all(href in self._state.end_devices for href in hrefs):
        logger.warning(
            "Take-over incomplete: not every EndDevice taken over was discovered; "
            "running events are not applied to them yet"
        )
        return False
    await self._event_processor.resume_devices(hrefs)
    return True
release_devices async
release_devices(device_hrefs: Iterable[str]) -> bool

Stop acting for EndDevices another process will take over.

The host first makes this client's device filter reject the devices. This stops new event work for them, waits for work already running for them to finish, saves their event state to the shared event state store, forgets the events that concern only them without a Response or a write to a device, and rediscovers without them. Subscriptions stay as they are. Returns what :meth:trigger_rediscovery returns.

Raises the store's error when their event state cannot be saved. The devices then stay with this process; the host puts them back in its device filter, or calls this again.

Source code in src/py20305/client/csip_client.py
async def release_devices(self, device_hrefs: Iterable[str]) -> bool:
    """Stop acting for EndDevices another process will take over.

    The host first makes this client's device filter reject the devices.
    This stops new event work for them, waits for work already running for
    them to finish, saves their event state to the shared event state
    store, forgets the events that concern only them without a Response or
    a write to a device, and rediscovers without them. Subscriptions stay
    as they are. Returns what :meth:`trigger_rediscovery` returns.

    Raises the store's error when their event state cannot be saved. The
    devices then stay with this process; the host puts them back in its
    device filter, or calls this again.
    """
    await self._event_processor.release_devices(device_hrefs)
    return await self.trigger_rediscovery()
shutdown async
shutdown(timeout: float = 10.0, *, subscription_cancel_timeout: float | None = 2.0, unsubscribe: bool | None = None) -> None

Gracefully stop polling, event timers, and close the HTTP session.

timeout bounds the wait for the poll tasks. subscription_cancel_timeout bounds the DELETEs that cancel this client's subscriptions; None waits for every one. See :meth:SubscriptionManager.cancel_all.

unsubscribe decides whether those DELETEs are sent; None uses the subscription manager's unsubscribe_on_close. Pass False when another instance with the same identity takes over and adopts the subscriptions. A process without own-EndDevice work sends none even when asked: other processes share its subscriptions.

Source code in src/py20305/client/csip_client.py
async def shutdown(
    self,
    timeout: float = 10.0,
    *,
    subscription_cancel_timeout: float | None = 2.0,
    unsubscribe: bool | None = None,
) -> None:
    """Gracefully stop polling, event timers, and close the HTTP session.

    ``timeout`` bounds the wait for the poll tasks. ``subscription_cancel_timeout``
    bounds the DELETEs that cancel this client's subscriptions; ``None`` waits
    for every one. See :meth:`SubscriptionManager.cancel_all`.

    ``unsubscribe`` decides whether those DELETEs are sent; ``None`` uses the
    subscription manager's ``unsubscribe_on_close``. Pass False when another
    instance with the same identity takes over and adopts the subscriptions.
    A process without own-EndDevice work sends none even when asked: other
    processes share its subscriptions.
    """
    self._shutdown_event.set()
    # Before anything is torn down. An expiry firing later would call
    # clear_comm_loss_simulation() against a stopped notification server and
    # scheduler, and its recovery would reopen the HTTP session this method
    # is about to close.
    await self._stop_comm_loss_simulation_expiry()
    await self._cancel_notification_followups()
    await self._cancel_tariff_timer()
    if self._renewal_task and not self._renewal_task.done():
        self._renewal_task.cancel()
        with contextlib.suppress(asyncio.CancelledError):
            await self._renewal_task
    if self._subscription_manager:
        if unsubscribe is None:
            unsubscribe = (
                self._own_end_device and self._subscription_manager.unsubscribe_on_close
            )
        if unsubscribe:
            await self._subscription_manager.cancel_all(timeout=subscription_cancel_timeout)
        else:
            logger.info(
                "Leaving %d subscription(s) on the server at shutdown",
                len(self._subscription_manager.active_subscriptions),
            )
    if self._notification_server:
        await self._notification_server.stop()
    await self._event_processor.shutdown()
    await self._tariff_processor.shutdown()
    await self._scheduler.cancel_all(timeout=timeout)
    await self._http.close()

py20305.client.tls

TLS configuration for IEEE 2030.5 connections.

TlsConfig dataclass

TLS client configuration for IEEE 2030.5.

Source code in src/py20305/client/tls.py
@dataclass(frozen=True)
class TlsConfig:
    """TLS client configuration for IEEE 2030.5."""

    client_cert: Path
    client_key: Path
    ca_cert: Path
    check_hostname: bool = True
    ciphers: str = _IEEE_2030_5_CIPHERS
    additional_ciphers: tuple[str, ...] = ()
    # Send the client's cert-derived LFDI as an HTTP header on every outbound
    # request. Off by default: IEEE 2030.5 §6.11.7.2 says servers SHOULD
    # derive the LFDI from the client cert, so the header is non-spec and
    # can trip strict-validation WAFs. Enable for utility deployments
    # fronted by a TLS-terminating proxy that strips the client cert before
    # the backend sees it.
    send_lfdi_header: bool = False
    # Header name to carry the LFDI under. IEEE 2030.5 doesn't standardize
    # this -- known peer conventions vary, so the name is configurable. The
    # default ("LFDI") matches the only peer convention we have evidence for;
    # override per peer (e.g. "X-LFDI") when a peer expects a different header.
    # Only honored when send_lfdi_header is True.
    lfdi_header_name: str = "LFDI"

CertChainError

Bases: SSLCertVerificationError

IEEE 2030.5 certificate chain validation failure.

Raised when the peer's certificate chain violates IEEE 2030.5 Section 6.11.3 requirements that OpenSSL does not enforce natively.

Source code in src/py20305/client/tls.py
class CertChainError(ssl.SSLCertVerificationError):
    """IEEE 2030.5 certificate chain validation failure.

    Raised when the peer's certificate chain violates IEEE 2030.5
    Section 6.11.3 requirements that OpenSSL does not enforce natively.
    """

build_cipher_string

build_cipher_string(config: TlsConfig) -> str

Combine the baseline cipher list with any operator-supplied additions.

Each addition must name exactly one suite, which is checked against OpenSSL rather than assumed. An OpenSSL cipher string is an expression language, so a single entry can rewrite the policy instead of adding to it: ALL widens it to everything, !aNULL removes what the baseline exists to require, and plain-looking aliases such as RSA or AES quietly expand to dozens of suites. The result of this function goes straight to SSLContext.set_ciphers, which accepts all of that silently, so nothing downstream would catch it.

Raises:

Type Description
ValueError

If an addition does not name exactly one TLS 1.2 suite.

Source code in src/py20305/client/tls.py
def build_cipher_string(config: TlsConfig) -> str:
    """Combine the baseline cipher list with any operator-supplied additions.

    Each addition must name exactly one suite, which is checked against
    OpenSSL rather than assumed. An OpenSSL cipher string is an expression
    language, so a single entry can rewrite the policy instead of adding to
    it: ``ALL`` widens it to everything, ``!aNULL`` removes what the baseline
    exists to require, and plain-looking aliases such as ``RSA`` or ``AES``
    quietly expand to dozens of suites. The result of this function goes
    straight to ``SSLContext.set_ciphers``, which accepts all of that
    silently, so nothing downstream would catch it.

    Raises:
        ValueError: If an addition does not name exactly one TLS 1.2 suite.
    """
    if not config.additional_ciphers:
        return config.ciphers

    for name in config.additional_ciphers:
        if name not in _known_tls12_suites():
            raise ValueError(
                f"additional_ciphers entries must each name exactly one TLS 1.2 "
                f"cipher suite; {name!r} is unknown to OpenSSL or selects a group, "
                f"which would widen the IEEE 2030.5 baseline rather than add to it"
            )

    extras = ":".join(config.additional_ciphers)
    return f"{config.ciphers}:{extras}"

create_ssl_context

create_ssl_context(config: TlsConfig) -> ssl.SSLContext

Create an SSL context from a TlsConfig.

Source code in src/py20305/client/tls.py
def create_ssl_context(config: TlsConfig) -> ssl.SSLContext:
    """Create an SSL context from a TlsConfig."""
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ctx.minimum_version = ssl.TLSVersion.TLSv1_2
    ctx.maximum_version = ssl.TLSVersion.TLSv1_2
    ctx.verify_mode = ssl.CERT_REQUIRED
    ctx.check_hostname = config.check_hostname
    if config.additional_ciphers:
        logger.warning(
            "TLS cipher policy relaxed beyond IEEE 2030.5 baseline; additional ciphers: %s",
            ", ".join(config.additional_ciphers),
        )
    ctx.set_ciphers(build_cipher_string(config))
    ctx.load_cert_chain(str(config.client_cert), str(config.client_key))
    ctx.load_verify_locations(str(config.ca_cert))
    ctx.verify_flags |= ssl.VERIFY_X509_PARTIAL_CHAIN
    return ctx

verify_ieee2030_5_chain

verify_ieee2030_5_chain(chain_der: list[bytes]) -> None

Validate a verified cert chain against IEEE 2030.5 PKI requirements.

Checks intermediate CA certificates (all except leaf and root) for extensions that IEEE 2030.5 and RFC 5280 prohibit or constrain:

  • Extended Key Usage MUST NOT be marked critical on CA certs (IEEE 2030.5 Section 6.11.3)
  • Name Constraints MUST be critical if present (RFC 5280 Section 4.2.1.10)
  • Policy Constraints MUST NOT be present (IEEE 2030.5 Section 6.11.3)

Parameters:

Name Type Description Default
chain_der list[bytes]

List of DER-encoded certificates from get_verified_chain(), ordered leaf-first (index 0 = leaf, last = root).

required

Raises:

Type Description
CertChainError

If any CA cert violates IEEE 2030.5 requirements.

Source code in src/py20305/client/tls.py
def verify_ieee2030_5_chain(chain_der: list[bytes]) -> None:
    """Validate a verified cert chain against IEEE 2030.5 PKI requirements.

    Checks intermediate CA certificates (all except leaf and root) for
    extensions that IEEE 2030.5 and RFC 5280 prohibit or constrain:

    - Extended Key Usage MUST NOT be marked critical on CA certs
      (IEEE 2030.5 Section 6.11.3)
    - Name Constraints MUST be critical if present (RFC 5280 Section 4.2.1.10)
    - Policy Constraints MUST NOT be present (IEEE 2030.5 Section 6.11.3)

    Args:
        chain_der: List of DER-encoded certificates from get_verified_chain(),
            ordered leaf-first (index 0 = leaf, last = root).

    Raises:
        CertChainError: If any CA cert violates IEEE 2030.5 requirements.
    """
    if len(chain_der) < 2:
        return

    # Check CA certs (skip leaf at [0], skip root at [-1])
    ca_certs = chain_der[1:-1] if len(chain_der) > 2 else []

    for der_bytes in ca_certs:
        cert = x509.load_der_x509_certificate(der_bytes)
        cn = _cert_cn(cert)

        for ext in cert.extensions:
            # EKU on CA certs must not be critical
            if ext.oid == ExtensionOID.EXTENDED_KEY_USAGE and ext.critical:
                raise CertChainError(f"CA cert '{cn}' has critical ExtendedKeyUsage")

            # NameConstraints must be critical if present (RFC 5280)
            if ext.oid == ExtensionOID.NAME_CONSTRAINTS and not ext.critical:
                raise CertChainError(f"CA cert '{cn}' has non-critical NameConstraints")

            # PolicyConstraints forbidden in IEEE 2030.5 manufacturing PKI
            if ext.oid == ExtensionOID.POLICY_CONSTRAINTS:
                raise CertChainError(f"CA cert '{cn}' has PolicyConstraints (forbidden)")

py20305.client.discovery

Hierarchical IEEE 2030.5 resource discovery.

read_der_control_mrids async

read_der_control_mrids(client: Sep2Client, derp_state: DerProgramState) -> set[bytes] | None

Read the program's DERControl list again and return the mRIDs it serves.

Returns None when the list cannot be read completely. Does not change the program state.

Source code in src/py20305/client/discovery.py
async def read_der_control_mrids(
    client: Sep2Client, derp_state: DerProgramState
) -> set[bytes] | None:
    """Read the program's DERControl list again and return the mRIDs it serves.

    Returns None when the list cannot be read completely. Does not change the
    program state.
    """
    derc_list_href = _extract_href(derp_state.program.dercontrol_list_link)
    if not derc_list_href:
        return None
    try:
        derc_pages = await client.get_list(derc_list_href, DercontrolList, absent_as_empty=False)
    except Exception as exc:
        logger.warning("Could not read the DERControl list at %s again: %s", derc_list_href, exc)
        return None
    return {derc.m_rid.value for page in derc_pages for derc in page.dercontrol}

register_device_prefixes

register_device_prefixes(client: Sep2Client, state: DiscoveredState) -> None

Tell client each discovered EndDevice's LFDI, keyed by its href.

Exchange records then name the device a request concerns. A request for anything under an EndDevice href is attributed to that EndDevice.

Source code in src/py20305/client/discovery.py
def register_device_prefixes(client: Sep2Client, state: DiscoveredState) -> None:
    """Tell ``client`` each discovered EndDevice's LFDI, keyed by its href.

    Exchange records then name the device a request concerns. A request for
    anything under an EndDevice href is attributed to that EndDevice.
    """
    client.update_discovered_devices(
        {href: edev.lfdi.hex() for href, edev in state.end_devices.items() if edev.lfdi}
    )

discover async

discover(client: Sep2Client, state: DiscoveredState, *, registration_pins: dict[str, int] | None = None, dcap_path: str = '/dcap', device_filter: DeviceFilter | None = None) -> None

Run full hierarchical discovery: dcap -> sdev -> edev -> der -> fsa -> derp.

Populates state with all discovered resources and poll rates. Clears any existing state before starting.

Parameters:

Name Type Description Default
client Sep2Client

The Sep2Client for HTTP operations.

required
state DiscoveredState

DiscoveredState to populate.

required
registration_pins dict[str, int] | None

Optional mapping from LFDI hex -> expected PIN for registration verification.

None
dcap_path str

Path to DeviceCapability resource (default: /dcap).

'/dcap'
device_filter DeviceFilter | None

Walk only the EndDevices this accepts (see :mod:py20305.client.scope). An EndDevice it rejects is left out of state.end_devices and nothing below it is fetched; its SubscriptionListLink is still recorded. None walks every EndDevice.

None
Source code in src/py20305/client/discovery.py
async def discover(
    client: Sep2Client,
    state: DiscoveredState,
    *,
    registration_pins: dict[str, int] | None = None,
    dcap_path: str = "/dcap",
    device_filter: DeviceFilter | None = None,
) -> None:
    """Run full hierarchical discovery: dcap -> sdev -> edev -> der -> fsa -> derp.

    Populates ``state`` with all discovered resources and poll rates.
    Clears any existing state before starting.

    Args:
        client: The Sep2Client for HTTP operations.
        state: DiscoveredState to populate.
        registration_pins: Optional mapping from LFDI hex -> expected PIN
            for registration verification.
        dcap_path: Path to DeviceCapability resource (default: /dcap).
        device_filter: Walk only the EndDevices this accepts (see
            :mod:`py20305.client.scope`). An EndDevice it rejects is left out
            of ``state.end_devices`` and nothing below it is fetched; its
            SubscriptionListLink is still recorded. None walks every EndDevice.
    """
    # Kept for an EndDevice whose FSA list fails to read: the FSAs it named
    # before are still the best knowledge of what it is assigned.
    previous_fsa_hrefs = {href: set(fsas) for href, fsas in state.previous_fsa_hrefs.items()}
    state.clear()

    # 1. Device Capability (with raw body for CSIP-AUS detection)
    dcap, raw_dcap = await client.get_with_body(dcap_path, DeviceCapability)
    state.dcap = dcap
    # The server's edition comes from the DeviceCapability when it is not configured.
    client.observe_device_capability(raw_dcap)
    state.poll_rates["dcap"] = normalize_poll_rate(dcap.poll_rate, resource_key="dcap")

    # Auto-detect CSIP-AUS mode from namespace declaration in dcap response
    if b"csipaus" in raw_dcap:
        state.csip_aus_mode = True
        logger.info("CSIP-AUS mode detected from server dcap response")
    # The HTTP client writes the extensions on what discovery saw, unless the
    # caller forced the flag.
    client.observe_csip_aus_detection(state.csip_aus_mode)
    client.csipaus_namespace = _declared_csipaus_namespace(raw_dcap)
    # Extensions named in the configuration replace what the server declares.
    # The HTTP client's flag is forced too: the serializer and the DER resource
    # manager read it to decide whether to send the CSIP-AUS elements.
    configured = client.profile.extensions
    if configured is not None:
        state.csip_aus_mode = bool(configured)
        client.csip_aus_mode = bool(configured)
        if "csip-aus-1.3" in configured:
            client.csipaus_namespace = EXTENSIONS["csip-aus-1.3"]
        elif "csip-aus-1.2" in configured:
            client.csipaus_namespace = EXTENSIONS["csip-aus-1.2"]

    # 2. Time
    time_href = _extract_href(dcap.time_link)
    if time_href:
        state.time_href = time_href
        state.time = await client.get(time_href, Time)
        observe_time_resource(client.timebase, state.time, href=time_href)

    # 3. SelfDevice (optional): the server's own device description. Used by
    #    clients that need the server's electrical context -- e.g. a site
    #    limit published on SelfDevice:DER:DERSettings that connected devices
    #    operate within.
    state.self_device = await _discover_self_device(client, dcap)
    if state.self_device is not None:
        state.poll_rates["sdev"] = normalize_poll_rate(
            state.self_device.device.poll_rate, resource_key="sdev"
        )

    # 4. MUP list href, for telemetry
    mup_href = _extract_href(dcap.mirror_usage_point_list_link)
    if mup_href:
        state.mup_list_href = mup_href
        logger.info("Discovered MirrorUsagePointListLink: %s", mup_href)
    else:
        logger.warning("DeviceCapability has no MirrorUsagePointListLink")

    # 5. EndDevice list
    edev_list_href = _extract_href(dcap.end_device_list_link)
    if not edev_list_href:
        logger.warning("No EndDeviceListLink in DeviceCapability")
        _set_time_poll_rate(state)
        register_device_prefixes(client, state)
        return

    edev_pages = await client.get_list(edev_list_href, EndDeviceList)
    device_mapping = DeviceMapping()

    for page in edev_pages:
        # Capture edev poll rate from first page
        if "edev" not in state.poll_rates and page.poll_rate is not None:
            state.poll_rates["edev"] = normalize_poll_rate(page.poll_rate, resource_key="edev")

        for edev in page.end_device:
            edev_href = edev.href
            if not edev_href:
                continue

            lfdi = edev.l_fdi or b""
            edev_state = EndDeviceState(device=edev, href=edev_href, lfdi=lfdi)

            # Log event list link (for alarm posting)
            log_event_href = _extract_href(edev.log_event_list_link)
            if log_event_href:
                edev_state.log_event_list_href = log_event_href

            # Subscription list link (for subscription management)
            sub_list_href = _extract_href(edev.subscription_list_link)
            if sub_list_href:
                edev_state.subscription_list_href = sub_list_href
                # Capture the SubscriptionList's server-advertised pollRate (once,
                # for the client's own SubscriptionList) so the reconcile
                # poll runs at the rate the server prefers. normalize_poll_rate
                # falls back to the IEEE 900s default when the server omits it.
                if "sub" not in state.poll_rates:
                    try:
                        # Only pollRate is read, so one entry is enough.
                        sep = "&" if "?" in sub_list_href else "?"
                        sub_list = await client.get(
                            f"{sub_list_href}{sep}s=0&l=1", SubscriptionList
                        )
                        state.poll_rates["sub"] = normalize_poll_rate(
                            sub_list.poll_rate, resource_key="sub"
                        )
                    except Exception as exc:
                        # Best-effort: the pollRate is an optimization. Any failure
                        # (protocol, malformed body, transport) just leaves
                        # poll_rates["sub"] unset so the reconcile poll falls back
                        # to the IEEE 2030.5 default cadence.
                        logger.debug(
                            "Could not read SubscriptionList pollRate, using default: %s", exc
                        )
                if state.subscription_list_href is None:
                    state.subscription_list_href = sub_list_href

            if device_filter is not None and not device_filter(edev_href, lfdi.hex()):
                logger.debug("EndDevice %s is outside this client's device filter", edev_href)
                continue

            # Registration link (for PIN verification)
            reg_href = _extract_href(edev.registration_link)
            if reg_href:
                edev_state.registration_href = reg_href

            # An EndDevice that represents an aggregation: its AggregatedDeviceList
            # and, for a Priority distribution, its AggregationPriority (Annex C.3).
            edev_state.aggregation = await read_aggregation(client, edev)

            # 5a. DER list (optional; server may advertise link without resource).
            # Contained per device: one EndDevice's unreadable DER list must not
            # abort the walk for every other device, and at startup a raise here
            # never completes connect() at all (entry_point._connect_with_retry
            # retries every Sep2Error), which costs the control path as well.
            # The cost of continuing is that this device's DER resource PUTs stay
            # unscheduled (der_resource_manager skips None hrefs), so a real
            # fault logs at error rather than warning.
            der_href = _extract_href(edev.derlist_link)
            if der_href:
                try:
                    # Every page: a list GET without "l" returns one entry.
                    der_pages = await client.get_list(der_href, Derlist)
                    if not der_pages:
                        logger.warning("DER list at %s is absent (404 or 204), no DERs", der_href)
                    edev_state.ders = [der for page in der_pages for der in page.der]
                    hrefs = _der_child_hrefs(
                        edev_state.ders, der_href, legacy_multi_der=client.profile.allows_multi_der
                    )
                    edev_state.der_status_href = hrefs.status
                    edev_state.der_capability_href = hrefs.capability
                    edev_state.der_settings_href = hrefs.settings
                    edev_state.der_availability_href = hrefs.availability
                    edev_state.current_der_controls_href = hrefs.current_controls
                except Sep2ProtocolError as exc:
                    logger.error(
                        "DER list at %s failed to read, no DER resource PUTs for this device: %s",
                        der_href,
                        exc,
                    )
                except Sep2PayloadError as exc:
                    logger.error(
                        "DER list at %s did not parse, no DER resource PUTs for this device: %s",
                        der_href,
                        exc,
                    )

            # 5b. FSA list (optional; server may advertise link without resource)
            fsa_href = _extract_href(edev.function_set_assignments_list_link)
            fsa_list_read = True
            if fsa_href:
                try:
                    fsa_pages = await client.get_list(fsa_href, FunctionSetAssignmentsList)
                except Sep2ProtocolError as exc:
                    if _paging_fault(exc):
                        logger.error(
                            "FSA list at %s stopped answering between pages (%s), skipping",
                            fsa_href,
                            exc.status_code,
                        )
                        fsa_pages = []
                        fsa_list_read = False
                    else:
                        raise
                for fsa_page in fsa_pages:
                    # Capture FSA poll rate from first page
                    if "fsa" not in state.poll_rates and fsa_page.poll_rate is not None:
                        state.poll_rates["fsa"] = normalize_poll_rate(
                            fsa_page.poll_rate, resource_key="fsa"
                        )
                    if hasattr(fsa_page, "subscribable") and fsa_page.subscribable:
                        edev_state.fsa_list_subscribable = True

                    for fsa in fsa_page.function_set_assignments:
                        edev_state.fsa_list.append(fsa)
                        logger.debug("Found FSA %s for device %s", fsa.href, edev_href)

                        # Pricing function set (opt-in): tariff tree per FSA. Done
                        # before the DERProgram branch so an FSA that advertises a
                        # tariff link but no DERProgram list is still discovered.
                        if state.pricing_enabled:
                            await _discover_tariffs_for_fsa(client, fsa, state)

                        # 6. DERProgram list per FSA
                        derp_list_href = _extract_href(fsa.derprogram_list_link)
                        if not derp_list_href:
                            continue

                        try:
                            derp_pages = await client.get_list(derp_list_href, DerprogramList)
                        except Sep2ProtocolError as exc:
                            if _paging_fault(exc):
                                logger.error(
                                    "DERP list at %s stopped answering between pages (%s), "
                                    "skipping",
                                    derp_list_href,
                                    exc.status_code,
                                )
                                continue
                            raise
                        for derp_page in derp_pages:
                            if hasattr(derp_page, "subscribable") and derp_page.subscribable:
                                edev_state.derp_list_subscribable = True
                        await _discover_programs_from_pages(
                            client, state, device_mapping, derp_pages, edev_href, fsa.href
                        )

                        # Gap 9: fetch per-FSA Time resource if available
                        fsa_time_href = _extract_href(fsa.time_link)
                        if fsa_time_href and fsa.href:
                            # Recorded from the advertisement, before the fetch
                            # that may fail. The Time poll drives off this map,
                            # so an endpoint that is down right now is retried on
                            # the next poll instead of being forgotten until the
                            # next rediscovery.
                            state.fsa_time_hrefs[fsa.href] = fsa_time_href
                        if fsa_time_href and fsa.href and fsa.href not in state.fsa_time:
                            try:
                                fsa_time = await client.get(fsa_time_href, Time)
                                state.fsa_time[fsa.href] = (fsa_time_href, fsa_time)
                                # IEEE 9.2.3: events from this FSA's programs
                                # follow this Time resource.
                                observe_time_resource(
                                    client.timebase,
                                    fsa_time,
                                    fsa_href=fsa.href,
                                    href=fsa_time_href,
                                )
                            except Sep2ProtocolError as exc:
                                if exc.status_code in (404, 204):
                                    logger.debug(
                                        "FSA Time at %s %s",
                                        fsa_time_href,
                                        _absent_reason(exc.status_code),
                                    )
                                else:
                                    raise

            # Gap 1: dcap fallback -- when no FSA or no programs from FSAs,
            # try discovering DERPrograms directly from DeviceCapability.
            if not edev_state.fsa_list:
                dcap_derp_href = _extract_href(dcap.derprogram_list_link)
                if dcap_derp_href:
                    logger.info("No FSA for %s, falling back to dcap DERProgramListLink", edev_href)
                    try:
                        dcap_derp_pages = await client.get_list(dcap_derp_href, DerprogramList)
                        await _discover_programs_from_pages(
                            client, state, device_mapping, dcap_derp_pages, edev_href, None
                        )
                    except Sep2ProtocolError as exc:
                        if _paging_fault(exc):
                            logger.error(
                                "dcap DERProgramList at %s stopped answering between pages (%s)",
                                dcap_derp_href,
                                exc.status_code,
                            )
                        else:
                            raise

            # Snapshot the FSA hrefs for removal detection on the next poll. When
            # the list did not read through, keep the previous snapshot: the
            # unread list may be the one that names those FSAs.
            if fsa_list_read:
                state.previous_fsa_hrefs[edev_href] = {
                    fsa.href for fsa in edev_state.fsa_list if fsa.href
                }
            elif edev_href in previous_fsa_hrefs:
                state.previous_fsa_hrefs[edev_href] = previous_fsa_hrefs[edev_href]

            # Verify registration PIN if configured. Gate the fetch on map
            # membership so EndDevices without a configured PIN don't incur an
            # extra GET on every (re)discovery in mixed-config deployments.
            if registration_pins and edev_state.registration_href:
                lfdi_hex = edev_state.lfdi.hex() if edev_state.lfdi else ""
                if lfdi_hex in registration_pins:
                    await _verify_registration(
                        client, edev_state.registration_href, lfdi_hex, registration_pins
                    )

            state.end_devices[edev_href] = edev_state

    state.device_mapping = device_mapping

    # Set poll rate for DER program control refresh if not already set from the list.
    # Per IEEE 2030.5, the DeviceCapability pollRate is the default for the
    # entire hierarchy unless a more specific rate is provided on a sub-resource.
    if state.der_programs and "derp" not in state.poll_rates:
        dcap_rate = state.poll_rates.get("dcap")
        state.poll_rates["derp"] = normalize_poll_rate(
            None, resource_key="derp", default=dcap_rate or DEFAULT_POLL_RATE
        )

    _set_time_poll_rate(state)

    # Observations of FSAs the server stopped advertising are of no further use.
    # The snapshot holds each device's FSAs as read now, or its previous FSAs
    # when its list did not read through, so a failed read prunes nothing.
    client.timebase.retain_fsas(
        {href for hrefs in state.previous_fsa_hrefs.values() for href in hrefs}
    )

    register_device_prefixes(client, state)

    logger.info(
        "Discovery complete: %d devices, %d programs",
        len(state.end_devices),
        len(state.der_programs),
    )

refresh_der_controls async

refresh_der_controls(client: Sep2Client, state: DiscoveredState) -> None

Re-fetch DER control lists, DefaultDERControl, and DERCurves for all known programs.

Unlike discover(), this does not clear state or re-run the full hierarchy walk. It refreshes the DERControl list, DefaultDERControl, and DERCurve list for each already-discovered DERProgram, so new controls, DDERC changes, and freshly-created curves are picked up without a disruptive state clear.

Source code in src/py20305/client/discovery.py
async def refresh_der_controls(client: Sep2Client, state: DiscoveredState) -> None:
    """Re-fetch DER control lists, DefaultDERControl, and DERCurves for all
    known programs.

    Unlike ``discover()``, this does not clear state or re-run the full
    hierarchy walk. It refreshes the DERControl list, DefaultDERControl, and
    DERCurve list for each already-discovered DERProgram, so new controls,
    DDERC changes, and freshly-created curves are picked up without a
    disruptive state clear.
    """
    for _, derp_state in list(state.der_programs.items()):
        # Refresh DefaultDERControl
        dderc_href = _extract_href(derp_state.program.default_dercontrol_link)
        ticket = _start_read()
        if dderc_href:
            try:
                dderc = await client.get(dderc_href, DefaultDercontrol)
                if _read_lands(derp_state, "default_dercontrol", ticket):
                    derp_state.default_dercontrol = dderc
            except Sep2ProtocolError as exc:
                if exc.status_code in (404, 204):
                    logger.debug(
                        "DDERC at %s %s on refresh", dderc_href, _absent_reason(exc.status_code)
                    )
                    if _read_lands(derp_state, "default_dercontrol", ticket):
                        derp_state.default_dercontrol = None
                else:
                    raise
            except Sep2PayloadError as exc:
                logger.warning("Skipping DDERC refresh at %s: %s", dderc_href, exc)
            except Exception:
                logger.warning(
                    "Failed to parse DDERC at %s during refresh, skipping",
                    dderc_href,
                    exc_info=True,
                )
        elif _read_lands(derp_state, "default_dercontrol", ticket):
            # Server removed the DefaultDERControl link — clear cached value
            derp_state.default_dercontrol = None

        # Refresh DERCurve list before controls, so any new curves a fresh
        # control might reference are already cached when the translation
        # layer resolves the curve link.
        await _refresh_curves_for_program(client, derp_state)

        # Refresh DERControl list
        derc_list_href = _extract_href(derp_state.program.dercontrol_list_link)
        if not derc_list_href:
            continue

        ticket = _start_read()
        try:
            derc_pages = await client.get_list(
                derc_list_href, DercontrolList, absent_as_empty=False
            )
        except Sep2ListNotFoundError:
            # Not a list served empty: a server restarting can answer 404 for a
            # moment. Keep the controls held. A program the server removed is
            # dropped by the DERProgram list refresh.
            logger.debug("DERC list at %s not found (HTTP 404) during refresh", derc_list_href)
            continue
        except Sep2ProtocolError as exc:
            if _paging_fault(exc):
                logger.error(
                    "DERC list at %s stopped answering between pages (%s) during refresh",
                    derc_list_href,
                    exc.status_code,
                )
                continue
            raise
        except Sep2PayloadError as exc:
            logger.warning("Skipping DERControl refresh: %s", exc)
            continue
        except Exception:
            logger.warning(
                "Failed to parse DERControl list at %s during refresh, skipping",
                derc_list_href,
                exc_info=True,
            )
            continue

        _store_der_controls(derp_state, derc_pages, ticket)

    logger.debug("Refreshed DER controls for %d programs", len(state.der_programs))

refresh_der_controls_for_program async

refresh_der_controls_for_program(client: Sep2Client, state: DiscoveredState, program_href: str, *, strict: bool = False) -> bool

Re-fetch DERControl list and DefaultDERControl for a single program.

Returns True if the program was found and refreshed, False if program_href is not in state (e.g. removed between notification and fetch).

A failed fetch is logged and skipped, and the call still returns True, so a poll carries on. strict raises it instead, for a caller that hands the failure back to whoever asked, so the request can be retried. A DefaultDERControl the server reports absent (404, 204) is not a failure. A DERControl list that answers 404 keeps the controls held; strict raises that too, since the notified update was not read.

Source code in src/py20305/client/discovery.py
async def refresh_der_controls_for_program(
    client: Sep2Client, state: DiscoveredState, program_href: str, *, strict: bool = False
) -> bool:
    """Re-fetch DERControl list and DefaultDERControl for a single program.

    Returns True if the program was found and refreshed, False if
    *program_href* is not in state (e.g. removed between notification
    and fetch).

    A failed fetch is logged and skipped, and the call still returns True, so
    a poll carries on. ``strict`` raises it instead, for a caller that hands
    the failure back to whoever asked, so the request can be retried. A
    DefaultDERControl the server reports absent (404, 204) is not a failure.
    A DERControl list that answers 404 keeps the controls held; ``strict``
    raises that too, since the notified update was not read.
    """
    derp_state = state.der_programs.get(program_href)
    if derp_state is None:
        logger.warning("Program %s not found in state during targeted refresh", program_href)
        return False

    # Refresh DefaultDERControl
    dderc_href = _extract_href(derp_state.program.default_dercontrol_link)
    ticket = _start_read()
    if dderc_href:
        try:
            dderc = await client.get(dderc_href, DefaultDercontrol)
            if _read_lands(derp_state, "default_dercontrol", ticket):
                derp_state.default_dercontrol = dderc
        except Sep2ProtocolError as exc:
            if exc.status_code in (404, 204):
                logger.debug(
                    "DDERC at %s %s on targeted refresh",
                    dderc_href,
                    _absent_reason(exc.status_code),
                )
                if _read_lands(derp_state, "default_dercontrol", ticket):
                    derp_state.default_dercontrol = None
            else:
                raise
        except Sep2PayloadError as exc:
            if strict:
                raise
            logger.warning("Skipping DDERC targeted refresh at %s: %s", dderc_href, exc)
        except Exception:
            if strict:
                raise
            logger.warning(
                "Failed to parse DDERC at %s during targeted refresh, skipping",
                dderc_href,
                exc_info=True,
            )
    elif _read_lands(derp_state, "default_dercontrol", ticket):
        derp_state.default_dercontrol = None

    # Refresh DERCurve list before controls so any newly-referenced curves
    # are cached when the translation layer resolves curve links.
    await _refresh_curves_for_program(client, derp_state)

    # Refresh DERControl list
    derc_list_href = _extract_href(derp_state.program.dercontrol_list_link)
    if derc_list_href:
        ticket = _start_read()
        try:
            derc_pages = await client.get_list(
                derc_list_href, DercontrolList, absent_as_empty=False
            )
        except Sep2ListNotFoundError:
            # Not a list served empty: a server restarting can answer 404 for a
            # moment. Keep the controls held. A program the server removed is
            # dropped by the DERProgram list refresh. Under ``strict`` the list
            # was not read, so the caller is told and can retry the request.
            if strict:
                raise
            logger.debug(
                "DERC list at %s not found (HTTP 404) during targeted refresh", derc_list_href
            )
            return True
        except Sep2ProtocolError as exc:
            if _paging_fault(exc) and not strict:
                logger.error(
                    "DERC list at %s stopped answering between pages (%s) during targeted refresh",
                    derc_list_href,
                    exc.status_code,
                )
                return True
            raise
        except Sep2PayloadError as exc:
            if strict:
                raise
            logger.warning("Skipping DERControl targeted refresh: %s", exc)
            return True
        except Exception:
            if strict:
                raise
            logger.warning(
                "Failed to parse DERControl list at %s during targeted refresh, skipping",
                derc_list_href,
                exc_info=True,
            )
            return True

        _store_der_controls(derp_state, derc_pages, ticket)

    logger.debug("Targeted refresh complete for program %s", program_href)
    return True

refresh_end_device_lists async

refresh_end_device_lists(client: Sep2Client, state: DiscoveredState) -> None

Re-fetch the EndDeviceList from the DeviceCapability link.

This updates EndDevice data (like changedTime) but does NOT re-discover FSA or DERProgram hierarchies. Useful for catching device-level changes without full rediscovery.

Source code in src/py20305/client/discovery.py
async def refresh_end_device_lists(client: Sep2Client, state: DiscoveredState) -> None:
    """Re-fetch the EndDeviceList from the DeviceCapability link.

    This updates EndDevice data (like changedTime) but does NOT re-discover
    FSA or DERProgram hierarchies. Useful for catching device-level changes
    without full rediscovery.
    """
    # Get the EndDeviceList href from dcap
    if not state.dcap:
        logger.warning("No DeviceCapability in state, skipping EndDevice list refresh")
        return

    edev_list_href = _extract_href(state.dcap.end_device_list_link)
    if not edev_list_href:
        logger.warning("No EndDeviceListLink in DeviceCapability")
        return

    try:
        edev_pages = await client.get_list(edev_list_href, EndDeviceList)
    except Sep2ProtocolError as exc:
        logger.warning("Failed to refresh EndDeviceList at %s: %s", edev_list_href, exc)
        return

    # Update existing devices with refreshed data
    for page in edev_pages:
        for edev in page.end_device:
            edev_href = edev.href
            if not edev_href:
                continue

            if edev_href in state.end_devices:
                # Update the device object and LFDI
                state.end_devices[edev_href].device = edev
                state.end_devices[edev_href].lfdi = edev.l_fdi or b""
                # The distribution, members and priority may change (§8.7.3.1:
                # a member is disabled before it is removed).
                edev_state = state.end_devices[edev_href]
                edev_state.aggregation = await read_aggregation(
                    client, edev, edev_state.aggregation
                )

    register_device_prefixes(client, state)
    logger.debug("Refreshed EndDevice list with %d devices", len(state.end_devices))

aggregations_poll_interval

aggregations_poll_interval(state: DiscoveredState) -> int | None

Return the shortest poll interval of the discovered aggregations, or None.

Source code in src/py20305/client/discovery.py
def aggregations_poll_interval(state: DiscoveredState) -> int | None:
    """Return the shortest poll interval of the discovered aggregations, or None."""
    fallback = state.poll_rates.get("edev") or DEFAULT_POLL_RATE
    rates = [
        rate
        for edev_state in state.end_devices.values()
        if edev_state.aggregation is not None
        and (rate := aggregation_poll_rate(edev_state.aggregation, fallback)) is not None
    ]
    return min(rates) if rates else None

refresh_aggregations async

refresh_aggregations(client: Sep2Client, state: DiscoveredState, last_read: dict[str, float], now: float) -> None

Read again each aggregation whose poll interval has elapsed.

This runs on its own schedule, so an aggregation stays current while a subscription suppresses the EndDeviceList poll. last_read maps an EndDevice href to the monotonic time of its last read here; an EndDevice with no entry is due. A read that fails keeps the previous members and priority. A read that an EndDeviceList refresh overtook is dropped.

Source code in src/py20305/client/discovery.py
async def refresh_aggregations(
    client: Sep2Client, state: DiscoveredState, last_read: dict[str, float], now: float
) -> None:
    """Read again each aggregation whose poll interval has elapsed.

    This runs on its own schedule, so an aggregation stays current while a
    subscription suppresses the EndDeviceList poll. ``last_read`` maps an
    EndDevice href to the monotonic time of its last read here; an EndDevice
    with no entry is due. A read that fails keeps the previous members and
    priority. A read that an EndDeviceList refresh overtook is dropped.
    """
    fallback = state.poll_rates.get("edev") or DEFAULT_POLL_RATE
    for edev_href, edev_state in list(state.end_devices.items()):
        info = edev_state.aggregation
        if info is None:
            continue
        rate = aggregation_poll_rate(info, fallback)
        if rate is None:
            continue
        previous = last_read.get(edev_href)
        # One second of slack, so a tick that wakes just early still reads.
        if previous is not None and now - previous < rate - 1:
            continue
        device = edev_state.device
        fresh = await read_aggregation(client, device, info)
        last_read[edev_href] = now
        # An EndDeviceList refresh or a rediscovery may have replaced the
        # EndDevice or its aggregation while this read was in flight. Its data
        # is newer, so this read is dropped.
        if (
            state.end_devices.get(edev_href) is edev_state
            and edev_state.device is device
            and edev_state.aggregation is info
        ):
            edev_state.aggregation = fresh

refresh_function_set_assignments async

refresh_function_set_assignments(client: Sep2Client, state: DiscoveredState, *, skip_hrefs: set[str] | None = None) -> list[str]

Re-fetch FSA lists for all known end devices.

Returns a list of program hrefs from removed FSAs (Gap 3: IEEE 8.8.3). Callers should cancel active events from these programs.

skip_hrefs names FSA list hrefs that have an active subscription: the poll path passes these so it doesn't re-fetch a resource we're already subscribed to (IEEE 2030.5 §8.9.3.4 rule (r)). A single cancelled FSA can then be polled while its siblings stay subscription-only. Discovery/rediscovery pass nothing (a full re-sync refreshes every FSA).

Source code in src/py20305/client/discovery.py
async def refresh_function_set_assignments(
    client: Sep2Client, state: DiscoveredState, *, skip_hrefs: set[str] | None = None
) -> list[str]:
    """Re-fetch FSA lists for all known end devices.

    Returns a list of program hrefs from removed FSAs (Gap 3: IEEE 8.8.3).
    Callers should cancel active events from these programs.

    ``skip_hrefs`` names FSA list hrefs that have an active subscription: the poll
    path passes these so it doesn't re-fetch a resource we're already subscribed to
    (IEEE 2030.5 §8.9.3.4 rule (r)). A single cancelled FSA can then be polled while
    its siblings stay subscription-only. Discovery/rediscovery pass nothing (a full
    re-sync refreshes every FSA).
    """
    removed_program_hrefs: list[str] = []

    if not state.end_devices:
        return removed_program_hrefs

    for edev_href, edev_state in list(state.end_devices.items()):
        # Refresh FSA list
        fsa_href = _extract_href(edev_state.device.function_set_assignments_list_link)
        if not fsa_href:
            continue
        if skip_hrefs and fsa_href in skip_hrefs:
            continue  # active subscription -> rule (r): don't poll it

        try:
            fsa_pages = await client.get_list(fsa_href, FunctionSetAssignmentsList)
        except Sep2ProtocolError as exc:
            if _paging_fault(exc):
                logger.error(
                    "FSA list at %s stopped answering between pages (%s) during refresh",
                    fsa_href,
                    exc.status_code,
                )
                continue
            raise

        edev_state.fsa_list.clear()
        for fsa_page in fsa_pages:
            edev_state.fsa_list.extend(fsa_page.function_set_assignments)

        # Gap 3: detect removed FSAs by comparing to previous snapshot
        current_fsa_hrefs = {fsa.href for fsa in edev_state.fsa_list if fsa.href}
        previous = state.previous_fsa_hrefs.get(edev_href, set())
        removed_fsas = previous - current_fsa_hrefs

        if removed_fsas:
            logger.info(
                "Detected %d removed FSA(s) for %s: %s",
                len(removed_fsas),
                edev_href,
                removed_fsas,
            )
            for removed_fsa_href in removed_fsas:
                removed_program_hrefs.extend(state.programs_from_fsa(removed_fsa_href))
                # Stop polling the departed FSA's Time resource. Left in place
                # these entries are fetched forever: a 404 on a removed FSA is
                # benign to the Time poll (its siblings still refresh), so
                # nothing ever escalates and nothing ever removes them.
                state.fsa_time.pop(removed_fsa_href, None)
                state.fsa_time_hrefs.pop(removed_fsa_href, None)
                client.timebase.forget_fsa(removed_fsa_href)

        # Update snapshot for next poll
        state.previous_fsa_hrefs[edev_href] = current_fsa_hrefs

    logger.debug("Refreshed FSA lists for %d end devices", len(state.end_devices))
    return removed_program_hrefs

refresh_tariffs async

refresh_tariffs(client: Sep2Client, state: DiscoveredState) -> None

Re-walk the tariff tree from all known FSAs (Pricing poll refresh).

Rebuilds state.tariff_profiles so newly-added TimeTariffIntervals are picked up and expired ones drop out. No-op unless pricing is enabled. The walk dedupes within this refresh (the global tariff tree is reachable from every FSA), so each profile is fetched once.

The new tree is built in a separate dict and assigned when the walk completes. Readers never see a partly built tree, and a walk that raises leaves the previous tree in place.

Only a successful read removes anything. A list read that fails keeps what the previous tree held below it, so a transient failure does not end a live price. Each TariffProfileList is read once per refresh, however many FSAs link to it, so a failed second read cannot undo a removal the first read confirmed.

Source code in src/py20305/client/discovery.py
async def refresh_tariffs(client: Sep2Client, state: DiscoveredState) -> None:
    """Re-walk the tariff tree from all known FSAs (Pricing poll refresh).

    Rebuilds ``state.tariff_profiles`` so newly-added TimeTariffIntervals are
    picked up and expired ones drop out. No-op unless pricing is enabled. The
    walk dedupes within this refresh (the global tariff tree is reachable from
    every FSA), so each profile is fetched once.

    The new tree is built in a separate dict and assigned when the walk
    completes. Readers never see a partly built tree, and a walk that raises
    leaves the previous tree in place.

    Only a successful read removes anything. A list read that fails keeps
    what the previous tree held below it, so a transient failure does not end
    a live price. Each TariffProfileList is read once per refresh, however
    many FSAs link to it, so a failed second read cannot undo a removal the
    first read confirmed.
    """
    if not state.pricing_enabled:
        return
    previous = state.tariff_profiles
    profiles: dict[str, TariffProfileState] = {}
    # TariffProfileList href -> whether this refresh read it successfully.
    list_reads: dict[str, bool] = {}
    for edev_state in list(state.end_devices.values()):
        for fsa in list(edev_state.fsa_list):
            tp_list_href = _extract_href(fsa.tariff_profile_list_link)
            if tp_list_href is None or tp_list_href in list_reads:
                continue
            list_reads[tp_list_href] = await _discover_tariffs_for_fsa(
                client, fsa, state, profiles, previous
            )
    for href, tp_state in previous.items():
        # A profile missing from this walk may still be served when the list
        # it came from could not be read.
        if href not in profiles and list_reads.get(tp_state.tp_list_href or "") is False:
            profiles[href] = tp_state
    state.tariff_profiles = profiles

refresh_der_programs async

refresh_der_programs(client: Sep2Client, state: DiscoveredState, withdrawn: list[tuple[str, str]] | None = None) -> list[str]

Re-fetch DER program lists from all known FSAs.

Updates existing programs (primacy, program object) and discovers new programs that have appeared since the last full discovery. New programs are fully discovered (DDERC, DERC list, curves) and added to the device mapping.

Prunes programs that are no longer present in any FSA's DER program list. Programs from FSAs whose DERP list returned 404 are preserved (we can't verify their status).

Also drops each (program, device) pair the device's current function set assignments no longer name, so a device withdrawn from a program stops receiving that program's controls while other devices keep it. This gives the mapping a full discovery would build: a DER client discovers its DERPrograms through its function set assignments (IEEE 2030.5-2023 §10.10.4.1). A device with no assignments is mapped from the DeviceCapability program list when there is one, as full discovery does. A device with a program list that could not be read, or that the server answered with no list at all (404 or 204), keeps all of its pairs, because the unread list may be the one that names them.

withdrawn, when given, receives each (program_href, device_href) pair dropped from the mapping, so a caller can end the control a running event still holds on that device.

Returns a list of removed program hrefs so callers can cancel events.

Source code in src/py20305/client/discovery.py
async def refresh_der_programs(
    client: Sep2Client,
    state: DiscoveredState,
    withdrawn: list[tuple[str, str]] | None = None,
) -> list[str]:
    """Re-fetch DER program lists from all known FSAs.

    Updates existing programs (primacy, program object) and discovers new
    programs that have appeared since the last full discovery. New programs
    are fully discovered (DDERC, DERC list, curves) and added to the device
    mapping.

    Prunes programs that are no longer present in any FSA's DER program
    list. Programs from FSAs whose DERP list returned 404 are preserved
    (we can't verify their status).

    Also drops each (program, device) pair the device's current function set
    assignments no longer name, so a device withdrawn from a program stops
    receiving that program's controls while other devices keep it. This gives
    the mapping a full discovery would build: a DER client discovers its
    DERPrograms through its function set assignments (IEEE 2030.5-2023
    §10.10.4.1). A device with no assignments is mapped from the
    DeviceCapability program list when there is one, as full discovery does. A
    device with a program list that could not be read, or that the server
    answered with no list at all (404 or 204), keeps all of its pairs, because
    the unread list may be the one that names them.

    ``withdrawn``, when given, receives each ``(program_href, device_href)``
    pair dropped from the mapping, so a caller can end the control a running
    event still holds on that device.

    Returns a list of removed program hrefs so callers can cancel events.
    """
    if not state.end_devices:
        return []

    # Track which programs are confirmed alive and which FSAs we couldn't verify.
    seen_programs: set[str] = set()
    unreachable_fsas: set[str | None] = set()
    # The (program, device) pairs the walk found, and the devices whose pairs
    # cannot be judged because one of their DERP lists was not read.
    seen_pairs: set[tuple[str, str]] = set()
    unverified_devices: set[str] = set()

    async def walk(derp_pages: list[Any], edev_href: str, fsa_href: str | None) -> None:
        """Record the programs and pairs one program list names for a device."""
        for derp_page in derp_pages:
            for derp in derp_page.derprogram:
                derp_href = derp.href
                if not derp_href:
                    continue

                seen_programs.add(derp_href)
                seen_pairs.add((derp_href, edev_href))
                # Outside the branches below: those ask whether the
                # *program* is new, while the entry that needs creating is
                # the *(program, device)* pair. A device whose new function
                # set assignment names a program already known took the
                # update branch and was never mapped onto it, so it received
                # none of that program's controls until a full discovery
                # rebuilt the mapping. Unconditional here matches what full
                # discovery does, and is safe because `add` admits a pair
                # once -- appending on every refresh poll would grow the
                # dispatch target list without bound.
                state.device_mapping.add(derp_href, edev_href)

                if derp_href in state.der_programs:
                    # Update the program object and primacy
                    state.der_programs[derp_href].program = derp
                    state.der_programs[derp_href].primacy = (
                        derp.primacy.value if derp.primacy is not None else 0
                    )
                else:
                    # New program discovered during refresh
                    derp_state = await _discover_program(client, derp, derp_href)
                    derp_state.discovered_from_fsa_href = fsa_href
                    state.der_programs[derp_href] = derp_state
                    logger.info(
                        "Discovered new program %s (primacy=%d) during refresh",
                        derp_href,
                        derp_state.primacy,
                    )

    # The DeviceCapability program list serves a device with no assignments.
    # Read once however many devices need it; None when it could not be read.
    dcap_derp_href = _extract_href(state.dcap.derprogram_list_link) if state.dcap else None
    dcap_pages: list[Any] | None = None
    dcap_read = False

    for edev_href, edev_state in list(state.end_devices.items()):
        for fsa in edev_state.fsa_list:
            derp_list_href = _extract_href(fsa.derprogram_list_link)
            if not derp_list_href:
                continue

            try:
                derp_pages = await client.get_list(derp_list_href, DerprogramList)
            except Sep2ProtocolError as exc:
                if _paging_fault(exc):
                    logger.error(
                        "DERP list at %s stopped answering between pages (%s) during refresh",
                        derp_list_href,
                        exc.status_code,
                    )
                    unreachable_fsas.add(fsa.href)
                    unverified_devices.add(edev_href)
                    continue
                raise

            if not derp_pages:
                # ``get_list`` returns no page for a 404 or 204 on the first
                # one, which is a list the server did not serve, not a list
                # served empty. Only a served list can show a withdrawal.
                unverified_devices.add(edev_href)
            await walk(derp_pages, edev_href, fsa.href)

        if not edev_state.fsa_list and dcap_derp_href:
            if not dcap_read:
                dcap_read = True
                try:
                    dcap_pages = await client.get_list(dcap_derp_href, DerprogramList)
                except Sep2ProtocolError as exc:
                    if not _paging_fault(exc):
                        raise
                    logger.error(
                        "dcap DERProgramList at %s stopped answering between pages (%s) "
                        "during refresh",
                        dcap_derp_href,
                        exc.status_code,
                    )
            if dcap_pages:
                await walk(dcap_pages, edev_href, None)
            else:
                unverified_devices.add(edev_href)

    # Drop the pairs no current assignment names. Reconciled against what the
    # walk saw rather than driven by which FSA disappeared: a device can reach
    # one program through two FSAs, and withdrawing one of them leaves the pair.
    # Only devices this walk covered are judged.
    mapping = state.device_mapping
    for edev_href in state.end_devices:
        if edev_href in unverified_devices:
            continue
        for program_href in list(mapping.device_to_programs.get(edev_href, [])):
            if (program_href, edev_href) not in seen_pairs:
                logger.info(
                    "Removing %s from program %s (no longer in its function set assignments)",
                    edev_href,
                    program_href,
                )
                mapping.remove_pair(program_href, edev_href)
                if withdrawn is not None:
                    withdrawn.append((program_href, edev_href))

    # Prune programs no longer in any FSA's DERP list. Preserve programs
    # from FSAs we couldn't reach (404) since we can't confirm removal.
    removed: list[str] = []
    for href, ps in list(state.der_programs.items()):
        if href in seen_programs:
            continue
        if ps.discovered_from_fsa_href in unreachable_fsas:
            continue
        logger.info("Removing stale program %s (no longer in any DERP list)", href)
        del state.der_programs[href]
        state.device_mapping.remove_program(href)
        removed.append(href)

    logger.debug(
        "Refreshed DER programs for %d end devices (%d pruned)",
        len(state.end_devices),
        len(removed),
    )
    return removed

py20305.client.aggregation

Aggregations: an EndDevice that represents several devices (IEEE 2030.5-2023 §8.7).

IEEE 2030.5-2023 §8.7.1 lets an EndDevice "represent an aggregation of multiple assets". The server describes the aggregation on the EndDevice itself:

  • distribution (AbstractDevice, AggregationDistributionType, Annex B.5 and B.24) "specifies how controls SHALL be distributed among members of the aggregation": 0 unspecified, 1 Uniform (an equal value for each member), 2 Prorate (an equal percentage of nameplate rating), 3 Priority (each member of the AggregationPriority completely utilized before the next).
  • The AggregatedDeviceList (AggregatedDeviceListLink) lists members as AggregatedDevice resources. It "MAY NOT represent all components of the aggregation" (§8.7.1). A server marks a member enabled false before it removes it (§8.7.3.1).
  • The AggregationPriority (AggregationPriorityLink) lists the members' LFDIs, highest priority first. It SHALL be present when the distribution is Priority and SHALL NOT be present otherwise (Annex B.24).

Annex C.3 is the flow: the service provider sets all of this up out of band, and the client reads its EndDevice, then its AggregatedDeviceList, then, for a Priority distribution, its AggregationPriority. :func:read_aggregation does those reads for one EndDevice and returns an :class:AggregationInfo, which discovery keeps on the EndDevice's state. Sharing a control across the members is the application's job: the client delivers a control for the aggregation's EndDevice to that EndDevice's connector, as for any EndDevice, and reports the event's Responses for it.

AggregatedDeviceInfo dataclass

One AggregatedDevice of an AggregatedDeviceList (IEEE 2030.5-2023 Annex B.24).

lfdi is lowercase hex. enabled is the resource's enabled; an absent value is True, since a server sets it to false only to show that a member is no longer registered (§8.7.3.1).

Source code in src/py20305/client/aggregation.py
@dataclass(frozen=True)
class AggregatedDeviceInfo:
    """One AggregatedDevice of an AggregatedDeviceList (IEEE 2030.5-2023 Annex B.24).

    ``lfdi`` is lowercase hex. ``enabled`` is the resource's ``enabled``; an
    absent value is True, since a server sets it to false only to show that a
    member is no longer registered (§8.7.3.1).
    """

    lfdi: str
    sfdi: int
    enabled: bool
    href: str | None
    changed_time: int
    device_category: int | None = None

AggregationInfo dataclass

What the server says about an EndDevice that represents an aggregation.

distribution is the EndDevice's AggregationDistributionType value, or None when the EndDevice carries none (which the standard reads as 0). members is every AggregatedDevice the list served, in list order. priority holds the AggregationPriority's LFDIs, highest first; it is read only for a Priority distribution. list_poll_rate is the AggregatedDeviceList's pollRate in seconds, or None when the list was not read or its pollRate disables polling. list_poll_disabled is True when the list's pollRate is 0 or less, which disables polling.

Source code in src/py20305/client/aggregation.py
@dataclass(frozen=True)
class AggregationInfo:
    """What the server says about an EndDevice that represents an aggregation.

    ``distribution`` is the EndDevice's AggregationDistributionType value, or
    None when the EndDevice carries none (which the standard reads as 0).
    ``members`` is every AggregatedDevice the list served, in list order.
    ``priority`` holds the AggregationPriority's LFDIs, highest first; it is
    read only for a Priority distribution. ``list_poll_rate`` is the
    AggregatedDeviceList's ``pollRate`` in seconds, or None when the list was
    not read or its ``pollRate`` disables polling. ``list_poll_disabled`` is
    True when the list's ``pollRate`` is 0 or less, which disables polling.
    """

    distribution: int | None
    members: tuple[AggregatedDeviceInfo, ...] = ()
    priority: tuple[str, ...] = ()
    aggregated_device_list_href: str | None = None
    aggregation_priority_href: str | None = None
    list_poll_rate: int | None = None
    list_poll_disabled: bool = False

    @property
    def enabled_members(self) -> tuple[str, ...]:
        """Return the LFDIs of the members the server has not disabled."""
        return tuple(member.lfdi for member in self.members if member.enabled)

    @property
    def disabled_members(self) -> tuple[str, ...]:
        """Return the LFDIs of the members the server marked disabled (§8.7.3.1)."""
        return tuple(member.lfdi for member in self.members if not member.enabled)
enabled_members property
enabled_members: tuple[str, ...]

Return the LFDIs of the members the server has not disabled.

disabled_members property
disabled_members: tuple[str, ...]

Return the LFDIs of the members the server marked disabled (§8.7.3.1).

distribution_of

distribution_of(end_device: Any) -> int | None

Return an EndDevice's distribution value, or None when it carries none.

Source code in src/py20305/client/aggregation.py
def distribution_of(end_device: Any) -> int | None:
    """Return an EndDevice's ``distribution`` value, or None when it carries none."""
    distribution = getattr(end_device, "distribution", None)
    value = getattr(distribution, "value", None)
    return value if isinstance(value, int) and not isinstance(value, bool) else None

is_aggregation

is_aggregation(end_device: Any) -> bool

Return whether an EndDevice describes an aggregation.

It does when it carries a distribution other than 0, an AggregatedDeviceListLink or an AggregationPriorityLink.

Source code in src/py20305/client/aggregation.py
def is_aggregation(end_device: Any) -> bool:
    """Return whether an EndDevice describes an aggregation.

    It does when it carries a ``distribution`` other than 0, an
    AggregatedDeviceListLink or an AggregationPriorityLink.
    """
    return bool(
        distribution_of(end_device)
        or _href(getattr(end_device, "aggregated_device_list_link", None))
        or _href(getattr(end_device, "aggregation_priority_link", None))
    )

read_aggregation async

read_aggregation(client: Sep2Client, end_device: Any, previous: AggregationInfo | None = None) -> AggregationInfo | None

Read the aggregation an EndDevice describes, or return None when it describes none.

Follows Annex C.3: the AggregatedDeviceList when the EndDevice links one, then the AggregationPriority when the distribution is Priority. A list or a priority that cannot be read (the server does not serve it, including a list whose first page is 404 or 204, the request times out or the connection fails) or does not parse is logged, so one EndDevice's aggregation never stops discovery of the others. When previous read the same resource, its members or priority are kept; otherwise that part is left empty.

Source code in src/py20305/client/aggregation.py
async def read_aggregation(
    client: Sep2Client, end_device: Any, previous: AggregationInfo | None = None
) -> AggregationInfo | None:
    """Read the aggregation an EndDevice describes, or return None when it describes none.

    Follows Annex C.3: the AggregatedDeviceList when the EndDevice links one,
    then the AggregationPriority when the distribution is Priority. A list or a
    priority that cannot be read (the server does not serve it, including a
    list whose first page is 404 or 204, the request times out or the
    connection fails) or does not parse is logged, so one
    EndDevice's aggregation never stops discovery of the others. When
    ``previous`` read the same resource, its members or priority are kept;
    otherwise that part is left empty.
    """
    if not is_aggregation(end_device):
        return None
    edev_href = getattr(end_device, "href", None) or "<EndDevice>"
    distribution = distribution_of(end_device)
    list_href = _href(getattr(end_device, "aggregated_device_list_link", None))
    priority_href = _href(getattr(end_device, "aggregation_priority_link", None))

    members: tuple[AggregatedDeviceInfo, ...] = ()
    poll_rate: int | None = None
    poll_disabled = False
    if list_href:
        try:
            pages = await client.get_list(list_href, AggregatedDeviceList)
        except _READ_FAILURES as exc:
            logger.warning("AggregatedDeviceList at %s could not be read: %s", list_href, exc)
            if previous is not None and previous.aggregated_device_list_href == list_href:
                members, poll_rate = previous.members, previous.list_poll_rate
                poll_disabled = previous.list_poll_disabled
        else:
            if not pages:
                # The paged reader answers a first-page 404 or 204 with no
                # pages, where a served list, even an empty one, has a page.
                logger.warning("AggregatedDeviceList at %s was not served", list_href)
                if previous is not None and previous.aggregated_device_list_href == list_href:
                    members, poll_rate = previous.members, previous.list_poll_rate
                    poll_disabled = previous.list_poll_disabled
            read: list[AggregatedDeviceInfo] = []
            for index, page in enumerate(pages):
                if index == 0:
                    # The first page's pollRate stands, including one that
                    # disables polling (normalized to None).
                    poll_rate = normalize_poll_rate(page.poll_rate, resource_key="adev")
                    poll_disabled = poll_rate is None
                read.extend(_member(device) for device in page.aggregated_device)
            if pages:
                members = tuple(read)

    priority: tuple[str, ...] = ()
    if distribution == PRIORITY:
        if priority_href is None:
            # Annex B.24: the AggregationPriority SHALL be present for Priority.
            logger.warning(
                "%s has a Priority distribution and no AggregationPriorityLink", edev_href
            )
        else:
            try:
                order = await client.get(priority_href, AggregationPriority)
                priority = tuple(bytes(data.l_fdi).hex() for data in order.priority_data)
            except _READ_FAILURES as exc:
                logger.warning(
                    "AggregationPriority at %s could not be read: %s", priority_href, exc
                )
                if previous is not None and previous.aggregation_priority_href == priority_href:
                    priority = previous.priority
    elif priority_href is not None:
        # Annex B.24: the AggregationPriority SHALL NOT be present otherwise.
        logger.info(
            "%s links an AggregationPriority without a Priority distribution; it is not read",
            edev_href,
        )
    return AggregationInfo(
        distribution=distribution,
        members=members,
        priority=priority,
        aggregated_device_list_href=list_href,
        aggregation_priority_href=priority_href,
        list_poll_rate=poll_rate,
        list_poll_disabled=poll_disabled,
    )

aggregation_poll_rate

aggregation_poll_rate(info: AggregationInfo, fallback: int) -> int | None

Return how often to read info again, in seconds, or None to not poll it.

The AggregatedDeviceList's own pollRate decides; a list whose pollRate disables polling is not polled. An aggregation with no list readable yet, or with only a priority, uses fallback.

Source code in src/py20305/client/aggregation.py
def aggregation_poll_rate(info: AggregationInfo, fallback: int) -> int | None:
    """Return how often to read ``info`` again, in seconds, or None to not poll it.

    The AggregatedDeviceList's own ``pollRate`` decides; a list whose
    ``pollRate`` disables polling is not polled. An aggregation with no list
    readable yet, or with only a priority, uses ``fallback``.
    """
    if info.aggregated_device_list_href is not None and info.list_poll_disabled:
        return None
    return info.list_poll_rate or fallback

py20305.client.retry

Retry logic with exponential backoff for the SEP2 client.

RetryPolicy dataclass

Configuration for retry behavior.

Source code in src/py20305/client/retry.py
@dataclass(frozen=True)
class RetryPolicy:
    """Configuration for retry behavior."""

    max_transient: int = 3
    max_tls: int = 8
    base_delay: float = 0.25
    backoff_factor: float = 2.0

with_retry async

with_retry(policy: RetryPolicy, operation: Callable[..., Awaitable[T]], *args: object, peer: str | None = None, **kwargs: object) -> T

Execute an async operation with retry on transient/TLS errors.

peer is used to tag the diagnostic surfaced when transient retries are exhausted (Sep2ConnectionError). Callers that have it (e.g. the SEP2 HTTP client) should pass it so the UI dedup-collapses one flapping host to a single entry; callers without context can omit it and only the log line will fire.

Source code in src/py20305/client/retry.py
async def with_retry(
    policy: RetryPolicy,
    operation: Callable[..., Awaitable[T]],
    *args: object,
    peer: str | None = None,
    **kwargs: object,
) -> T:
    """Execute an async operation with retry on transient/TLS errors.

    ``peer`` is used to tag the diagnostic surfaced when transient retries
    are exhausted (``Sep2ConnectionError``). Callers that have it (e.g.
    the SEP2 HTTP client) should pass it so the UI dedup-collapses one
    flapping host to a single entry; callers without context can omit it
    and only the log line will fire.
    """
    transient_attempts = 0
    tls_attempts = 0

    while True:
        try:
            return await operation(*args, **kwargs)
        except ssl.SSLError as exc:
            tls_attempts += 1
            hostname_mismatch = _is_hostname_mismatch(exc)
            if hostname_mismatch and tls_attempts == 1:
                # Log the remediation hint once on first encounter; subsequent
                # retries fall through to the generic per-attempt log so the
                # noise stays bounded. By construction, getting this error at
                # all means check_hostname is currently True -- Python's ssl
                # layer doesn't run hostname matching otherwise.
                logger.warning(
                    "TLS hostname mismatch%s: %s. %s",
                    f" to {peer}" if peer else "",
                    exc,
                    _HOSTNAME_MISMATCH_HINT,
                )
            if tls_attempts >= policy.max_tls:
                if peer is not None:
                    from py20305.diagnostics import report

                    if hostname_mismatch:
                        message = (
                            f"TLS hostname mismatch to {peer} after "
                            f"{tls_attempts} attempts: {exc}. "
                            f"{_HOSTNAME_MISMATCH_HINT}"
                        )
                        dedup_key = f"tls_hostname_mismatch:{peer}"
                    else:
                        message = (
                            f"TLS handshake failure to {peer} after {tls_attempts} attempts: {exc}"
                        )
                        dedup_key = f"tls_handshake:{peer}:{type(exc).__name__}"
                    details: dict[str, object] = {
                        "peer": peer,
                        "error_kind": type(exc).__name__,
                        "error": str(exc),
                    }
                    if hostname_mismatch:
                        details["kind"] = "hostname_mismatch"
                    report(
                        "warnings",
                        message,
                        source="client",
                        dedup_key=dedup_key,
                        details=details,
                    )
                raise Sep2TlsError(f"TLS error after {tls_attempts} attempts: {exc}") from exc
            delay = policy.base_delay * (policy.backoff_factor ** (tls_attempts - 1))
            logger.warning(
                "TLS error (attempt %d/%d), retrying in %.2fs: %s",
                tls_attempts,
                policy.max_tls,
                delay,
                exc,
            )
            await asyncio.sleep(delay)
        except Sep2RateLimitError as exc:
            transient_attempts += 1
            if transient_attempts >= policy.max_transient:
                raise
            delay = (
                float(exc.retry_after)
                if exc.retry_after
                else (policy.base_delay * (policy.backoff_factor ** (transient_attempts - 1)))
            )
            logger.warning(
                "Rate limited (attempt %d/%d), retrying in %.2fs",
                transient_attempts,
                policy.max_transient,
                delay,
            )
            await asyncio.sleep(delay)
        except Sep2RedirectError as exc:
            # IEEE 5.5.2.7 says clients SHOULD re-discover on a redirect, but in
            # practice these (301/302/307/308) are usually transient (e.g. a
            # proxy returning `/Error/?errorCode=4&404` because of a malformed
            # upstream request). Retrying with backoff lets a transient cause
            # clear without taking the whole process down. After max_transient
            # attempts the original error propagates so an operator who is
            # actually facing a permanent move sees it; we don't follow the
            # Location blindly because the target may be a cross-origin
            # error page. Following + re-discovery is tracked separately.
            transient_attempts += 1
            if transient_attempts >= policy.max_transient:
                raise
            delay = policy.base_delay * (policy.backoff_factor ** (transient_attempts - 1))
            location = exc.location or "(no Location header)"
            if peer is not None:
                from py20305.diagnostics import report

                # Surfaces the redirect status + target so the operator sees
                # *what* and *where* the upstream is sending us (often the actual
                # diagnostic). Dedup per (peer, status, location) so a stable
                # redirect collapses to one entry; a fresh one still produces a
                # new entry.
                report(
                    "warnings",
                    f"HTTP {exc.status_code} redirect from {peer} -> {location} "
                    f"(attempt {transient_attempts}/{policy.max_transient})",
                    source="client",
                    dedup_key=f"redirect:{peer}:{exc.status_code}:{location}",
                    details={
                        "peer": peer,
                        "status_code": exc.status_code,
                        "location": location,
                        "attempt": transient_attempts,
                    },
                )
            else:
                logger.warning(
                    "HTTP %d redirect (attempt %d/%d) -> %s, retrying in %.2fs",
                    exc.status_code,
                    transient_attempts,
                    policy.max_transient,
                    location,
                    delay,
                )
            await asyncio.sleep(delay)
        except (TimeoutError, OSError) as exc:
            transient_attempts += 1
            if transient_attempts >= policy.max_transient:
                msg = f"Connection error after {transient_attempts} attempts: {exc}"
                if peer is not None:
                    from py20305.diagnostics import report

                    report(
                        "errors",
                        f"Cannot reach {peer}: {exc}",
                        source="client",
                        dedup_key=f"connection:{peer}",
                        details={"peer": peer, "error": str(exc)},
                    )
                raise Sep2ConnectionError(msg) from exc
            delay = policy.base_delay * (policy.backoff_factor ** (transient_attempts - 1))
            logger.warning(
                "Transient error (attempt %d/%d), retrying in %.2fs: %s",
                transient_attempts,
                policy.max_transient,
                delay,
                exc,
            )
            await asyncio.sleep(delay)

py20305.client.scope

Which EndDevices one client process acts for.

A host application that runs several processes as one client splits the EndDevices the certificate can see between them. Each process's client takes a device filter: discovery walks the FunctionSetAssignments, DERPrograms and DERControls of only the EndDevices the filter accepts, so the process builds event state for, dispatches to and responds for only those devices.

A filter is any callable that takes an EndDevice's href and its LFDI as 40 lowercase hex characters (empty when the server gives none) and returns whether this process acts for it. :class:DeviceSet is the ready-made one: a set of hrefs and LFDIs the host adds to and removes from as it moves devices between processes.

DeviceSet

A device filter that accepts the EndDevices named by href or by LFDI.

An EndDevice is accepted when its href or its LFDI is in the set. LFDIs are compared without regard to case.

Source code in src/py20305/client/scope.py
class DeviceSet:
    """A device filter that accepts the EndDevices named by href or by LFDI.

    An EndDevice is accepted when its href or its LFDI is in the set. LFDIs are
    compared without regard to case.
    """

    def __init__(self, *, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None:
        self.hrefs: set[str] = set(hrefs)
        self.lfdis: set[str] = {lfdi.lower() for lfdi in lfdis}

    def __call__(self, href: str, lfdi: str) -> bool:
        """Return whether the EndDevice at ``href`` with ``lfdi`` is in the set."""
        return href in self.hrefs or (bool(lfdi) and lfdi.lower() in self.lfdis)

    def add(self, *, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None:
        """Accept these EndDevices as well."""
        self.hrefs.update(hrefs)
        self.lfdis.update(lfdi.lower() for lfdi in lfdis)

    def discard(self, *, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None:
        """Stop accepting these EndDevices. Unknown entries are ignored."""
        self.hrefs.difference_update(hrefs)
        self.lfdis.difference_update(lfdi.lower() for lfdi in lfdis)
add
add(*, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None

Accept these EndDevices as well.

Source code in src/py20305/client/scope.py
def add(self, *, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None:
    """Accept these EndDevices as well."""
    self.hrefs.update(hrefs)
    self.lfdis.update(lfdi.lower() for lfdi in lfdis)
discard
discard(*, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None

Stop accepting these EndDevices. Unknown entries are ignored.

Source code in src/py20305/client/scope.py
def discard(self, *, hrefs: Iterable[str] = (), lfdis: Iterable[str] = ()) -> None:
    """Stop accepting these EndDevices. Unknown entries are ignored."""
    self.hrefs.difference_update(hrefs)
    self.lfdis.difference_update(lfdi.lower() for lfdi in lfdis)

py20305.client.request_budget

A hook that lets several processes share one client's request budget.

A server can limit how often one client sends requests. When a host application runs several processes as one client, they share that limit. The host passes each process's client a :class:RequestLimiter, and the HTTP layer awaits :meth:RequestLimiter.acquire before each request it sends to the server, every retry attempt included. A limiter backed by shared storage (a token bucket in a database, for example) then paces all the processes together.

No limiter is the default: requests go out as soon as they are made.

LimiterFailure

Bases: Exception

Carry an exception raised by :meth:RequestLimiter.acquire past the transport handling.

The HTTP layer retries transport failures and counts them against the server's health. A limiter that fails has sent nothing to the server, so its exception is wrapped in this class, which no transport handler catches, and unwrapped where the request returns to its caller.

Source code in src/py20305/client/request_budget.py
class LimiterFailure(Exception):  # noqa: N818 -- a carrier, not an error the caller sees
    """Carry an exception raised by :meth:`RequestLimiter.acquire` past the transport handling.

    The HTTP layer retries transport failures and counts them against the
    server's health. A limiter that fails has sent nothing to the server, so
    its exception is wrapped in this class, which no transport handler
    catches, and unwrapped where the request returns to its caller.
    """

    def __init__(self, error: Exception) -> None:
        super().__init__(str(error))
        #: The exception ``acquire`` raised.
        self.error = error

RequestLimiter

Bases: Protocol

Decides when the next request to the server may be sent.

Source code in src/py20305/client/request_budget.py
@runtime_checkable
class RequestLimiter(Protocol):
    """Decides when the next request to the server may be sent."""

    async def acquire(self) -> None:
        """Wait until one more request may be sent, then return.

        Called once per request, before it is sent. An exception raised here
        stops the request and reaches the code that made it unchanged, so a
        limiter should wait rather than raise when the budget is spent.
        """
        ...
acquire async
acquire() -> None

Wait until one more request may be sent, then return.

Called once per request, before it is sent. An exception raised here stops the request and reaches the code that made it unchanged, so a limiter should wait rather than raise when the budget is spent.

Source code in src/py20305/client/request_budget.py
async def acquire(self) -> None:
    """Wait until one more request may be sent, then return.

    Called once per request, before it is sent. An exception raised here
    stops the request and reaches the code that made it unchanged, so a
    limiter should wait rather than raise when the budget is spent.
    """
    ...

py20305.security.identity

LFDI/SFDI identity computation per IEEE 2030.5.

compute_lfdi

compute_lfdi(cert_pem: str) -> str

Compute the LFDI from a PEM-encoded certificate.

The leftmost 40 hex of the certificate's SHA-256 fingerprint, lowercase (per IEEE 2030.5). Derived from :func:compute_cert_fingerprint so the two can never disagree about the underlying digest. Raises ValueError if the certificate cannot be parsed.

Source code in src/py20305/security/identity.py
def compute_lfdi(cert_pem: str) -> str:
    """Compute the LFDI from a PEM-encoded certificate.

    The leftmost 40 hex of the certificate's SHA-256 fingerprint, lowercase
    (per IEEE 2030.5). Derived from :func:`compute_cert_fingerprint` so the two
    can never disagree about the underlying digest. Raises ValueError if the
    certificate cannot be parsed.
    """
    return compute_cert_fingerprint(cert_pem)[:40].lower()

compute_cert_fingerprint

compute_cert_fingerprint(cert_pem: str) -> str

Compute the full SHA-256 fingerprint of a PEM-encoded certificate.

SHA-256 of the DER-encoded certificate, all 64 hex characters, uppercase. This is the value some servers key device/aggregator enrollment on (the LFDI is the leftmost 40 hex of the same digest). Raises ValueError if the certificate cannot be parsed.

Source code in src/py20305/security/identity.py
def compute_cert_fingerprint(cert_pem: str) -> str:
    """Compute the full SHA-256 fingerprint of a PEM-encoded certificate.

    SHA-256 of the DER-encoded certificate, all 64 hex characters, uppercase.
    This is the value some servers key device/aggregator enrollment on (the
    LFDI is the leftmost 40 hex of the same digest). Raises ValueError if the
    certificate cannot be parsed.
    """
    try:
        cert = x509.load_pem_x509_certificate(cert_pem.encode("utf-8"))
    except Exception as exc:
        raise ValueError(f"Failed to parse PEM certificate: {exc}") from exc
    der_bytes = cert.public_bytes(Encoding.DER)
    return hashlib.sha256(der_bytes).hexdigest().upper()

compute_sfdi

compute_sfdi(lfdi: str) -> int

Compute the SFDI from an LFDI string.

Per IEEE 2030.5 §6.3.3: the SFDI is the 36 most-significant bits of the LFDI (its first 9 hex characters) as a decimal, with a sum-of-digits checksum digit appended so the whole SFDI's digit sum is a multiple of 10. This is a sum-of-digits checksum, NOT Luhn -- the two coincide for many values but diverge for others (e.g. LFDI 3e4f45ab3... -> ...391, not ...397), and the server rejects the wrong one.

Source code in src/py20305/security/identity.py
def compute_sfdi(lfdi: str) -> int:
    """Compute the SFDI from an LFDI string.

    Per IEEE 2030.5 §6.3.3: the SFDI is the 36 most-significant bits of the LFDI
    (its first 9 hex characters) as a decimal, with a **sum-of-digits** checksum
    digit appended so the whole SFDI's digit sum is a multiple of 10. This is a
    sum-of-digits checksum, NOT Luhn -- the two coincide for many values but
    diverge for others (e.g. LFDI ``3e4f45ab3...`` -> ``...391``, not ``...397``),
    and the server rejects the wrong one.
    """
    if len(lfdi) < 9:
        raise ValueError(f"LFDI must be at least 9 characters, got {len(lfdi)}")
    base = int(lfdi[:9], 16)
    check = (10 - sum(int(d) for d in str(base)) % 10) % 10
    return base * 10 + check

generate_proxied_lfdi

generate_proxied_lfdi(pen: int = _DEFAULT_PEN) -> str

Generate an LFDI for a proxied device that has no certificate of its own.

A device behind a gateway still needs an LFDI to be addressed by, but has no certificate to derive one from. This builds one instead: 128 random bits concatenated with the PEN, SHA-256 hashed, truncated to 40 hex characters. The PEN makes collisions across vendors improbable, and the randomness makes them improbable within one.

Parameters:

Name Type Description Default
pen int

IANA Private Enterprise Number to attribute the generated identity to. Pass your own organization's PEN; the default is only a placeholder so the function is callable out of the box.

_DEFAULT_PEN

Returns:

Type Description
str

40-character lowercase hex LFDI string.

Source code in src/py20305/security/identity.py
def generate_proxied_lfdi(pen: int = _DEFAULT_PEN) -> str:
    """Generate an LFDI for a proxied device that has no certificate of its own.

    A device behind a gateway still needs an LFDI to be addressed by, but has
    no certificate to derive one from. This builds one instead: 128 random
    bits concatenated with the PEN, SHA-256 hashed, truncated to 40 hex
    characters. The PEN makes collisions across vendors improbable, and the
    randomness makes them improbable within one.

    Args:
        pen: IANA Private Enterprise Number to attribute the generated
            identity to. Pass your own organization's PEN; the default is
            only a placeholder so the function is callable out of the box.

    Returns:
        40-character lowercase hex LFDI string.
    """
    random_bytes = secrets.token_bytes(16)
    pen_bytes = pen.to_bytes(4, byteorder="big")
    combined = random_bytes + pen_bytes
    digest = hashlib.sha256(combined).hexdigest()
    return digest[:40]

py20305.config

The configuration file a deployed client is started from.

One document describes a running client: which server to talk to, which certificate to present, which devices to drive, and what to expose locally. It is validated on load, so a mistake surfaces at startup with a message naming the field rather than hours later as a failed poll.

Loading is separate from the models on purpose. Anything embedding this client in its own application already has configuration of its own and should build :class:ClientConfig directly, or skip it and construct the client itself -- :func:load_config exists for the command-line runner, not as the only way in.

ConfigError

Bases: Exception

A configuration file could not be read, parsed, or validated.

Source code in src/py20305/config.py
class ConfigError(Exception):
    """A configuration file could not be read, parsed, or validated."""

ServerSchemaConfig

Bases: _Strict

Which IEEE 2030.5 schemas the server speaks, and so what the client may send it.

Inbound, the client reads any element either edition defines. Outbound, it sends only what the base edition defines, plus the elements of the listed features and extensions.

Source code in src/py20305/config.py
class ServerSchemaConfig(_Strict):
    """Which IEEE 2030.5 schemas the server speaks, and so what the client may send it.

    Inbound, the client reads any element either edition defines. Outbound, it
    sends only what the base edition defines, plus the elements of the listed
    ``features`` and ``extensions``.
    """

    base: Literal["auto", "2018", "2023"] = Field(
        default="auto",
        description=(
            "The edition the server implements. `auto` reads the DeviceCapability: "
            'schemaVer "2.2" or higher means 2023, and anything else, including no '
            "schemaVer, means 2018. The decision and its evidence are logged."
        ),
    )
    features: list[str] = Field(
        default_factory=list,
        description=(
            "Functions the server borrows from another edition, for example "
            "`import_export_limits` on a 2018 server that takes the 2023 import and "
            "export limits. Never detected; listed here or off."
        ),
    )
    extensions: Literal["auto"] | list[str] = Field(
        default="auto",
        description=(
            "Schemas in other namespaces: `csip-aus-1.2`, `csip-aus-1.3`. `auto` detects "
            "them from the namespaces the server declares."
        ),
    )

    @field_validator("base", mode="before")
    @classmethod
    def _edition_as_text(cls, v: object) -> object:
        """Accept ``base: 2018`` and ``base: 2023``, which YAML reads as integers."""
        if isinstance(v, int) and not isinstance(v, bool) and v in (2018, 2023):
            return str(v)
        return v

    @field_validator("features")
    @classmethod
    def _known_features(cls, v: list[str]) -> list[str]:
        from py20305.server_profile import FEATURES

        unknown = sorted(set(v) - set(FEATURES))
        if unknown:
            known = ", ".join(sorted(FEATURES))
            raise ValueError(f"unknown feature {', '.join(unknown)}; known: {known}")
        return v

    @field_validator("extensions")
    @classmethod
    def _known_extensions(cls, v: str | list[str]) -> str | list[str]:
        from py20305.server_profile import EXTENSIONS

        if isinstance(v, list):
            unknown = sorted(set(v) - set(EXTENSIONS))
            if unknown:
                known = ", ".join(sorted(EXTENSIONS))
                raise ValueError(f"unknown extension {', '.join(unknown)}; known: {known}")
        return v

ServerConfig

Bases: _Strict

The IEEE 2030.5 server this client talks to.

Source code in src/py20305/config.py
class ServerConfig(_Strict):
    """The IEEE 2030.5 server this client talks to."""

    model_config = ConfigDict(extra="forbid", populate_by_name=True)

    url: str | None = Field(
        default=None,
        description=(
            "Base URL of the server, e.g. https://server.example.com:8443. Omit it to "
            "have the client locate a server by DNS-SD query at startup, which is what "
            "IEEE 2030.5 §6.9.2 asks a client to do; `discovery` then has to be enabled."
        ),
    )

    dcap_path: str = Field(
        default="/dcap",
        description="Path of the DeviceCapability resource. Standard servers use /dcap.",
    )
    poll_now_on_start: bool = Field(
        default=True,
        description="Poll once immediately after connecting rather than waiting for the schedule.",
    )
    schema_profile: ServerSchemaConfig = Field(
        default_factory=ServerSchemaConfig,
        alias="schema",
        description="Which IEEE 2030.5 schemas the server speaks (`server.schema`).",
    )
    missing_location_fallback: bool = Field(
        default=False,
        description=(
            "Accept a subscription the server created without a Location header, under a "
            "synthetic URI. Both editions require the header on 201 Created, so this is a "
            "workaround for a non-conforming server."
        ),
    )
    server_2018_compat: bool | None = Field(
        default=None,
        description=(
            "Deprecated. `true` is `schema.base: 2018` plus "
            "`missing_location_fallback: true`; use those."
        ),
    )
    use_server_time: bool = Field(
        default=True,
        description=(
            "Follow the server's Time resource for scheduling rather than the local clock. "
            "The local clock is never modified either way."
        ),
    )
    fsa_stale_seconds: float | None = Field(
        default=None,
        gt=0,
        description=(
            "How old an FSA's own Time observation may get before scheduling for its "
            "programs falls back to the global timebase. Unset derives it from the Time "
            "poll cadence (three polls, at least an hour), which is what a healthy "
            "deployment wants; set it only to override that."
        ),
    )

    @model_validator(mode="after")
    def _warn_about_the_deprecated_flag(self) -> ServerConfig:
        if self.server_2018_compat is not None:
            message = (
                "server.server_2018_compat is deprecated; use server.schema.base: 2018 and, "
                "for a server that omits the Location header on a created subscription, "
                "server.missing_location_fallback: true"
            )
            warnings.warn(message, DeprecationWarning, stacklevel=2)
            logger.warning(message)
        return self

    def profile(self) -> ServerProfile:
        """Return the schema profile this configuration describes.

        The deprecated ``server_2018_compat: true`` stands for base 2018 plus the
        missing-Location fallback; ``false`` for base 2023. An explicit
        ``schema.base`` wins over it.
        """
        schema = self.schema_profile
        base = None if schema.base == "auto" else Edition(int(schema.base))
        fallback = self.missing_location_fallback
        evidence = "configured" if base is not None else ""
        if self.server_2018_compat is not None:
            if base is None:
                base = Edition.E2018 if self.server_2018_compat else Edition.E2023
                evidence = "server_2018_compat"
            fallback = fallback or self.server_2018_compat
        return ServerProfile(
            base=base,
            features=frozenset(schema.features),
            extensions=None if schema.extensions == "auto" else frozenset(schema.extensions),
            missing_location_fallback=fallback,
            evidence=evidence,
        )

    @field_validator("url")
    @classmethod
    def _must_be_a_usable_https_url(cls, v: str | None) -> str | None:
        """Reject an unusable URL at load rather than at handshake.

        IEEE 2030.5 is mutual TLS throughout, so a plain http:// URL cannot
        work. The URL is parsed rather than prefix-matched because
        ``https://`` alone passes a prefix check and then fails much later
        inside the HTTP client, outside this module's error handling and
        looking like the server is down.
        """
        if v is None:
            return None
        parsed = urlparse(v)
        if parsed.scheme != "https":
            raise ValueError(f"must be an https:// URL (IEEE 2030.5 requires TLS), got {v!r}")
        if not parsed.hostname:
            raise ValueError(f"must include a host, got {v!r}")
        return v.rstrip("/")
profile
profile() -> ServerProfile

Return the schema profile this configuration describes.

The deprecated server_2018_compat: true stands for base 2018 plus the missing-Location fallback; false for base 2023. An explicit schema.base wins over it.

Source code in src/py20305/config.py
def profile(self) -> ServerProfile:
    """Return the schema profile this configuration describes.

    The deprecated ``server_2018_compat: true`` stands for base 2018 plus the
    missing-Location fallback; ``false`` for base 2023. An explicit
    ``schema.base`` wins over it.
    """
    schema = self.schema_profile
    base = None if schema.base == "auto" else Edition(int(schema.base))
    fallback = self.missing_location_fallback
    evidence = "configured" if base is not None else ""
    if self.server_2018_compat is not None:
        if base is None:
            base = Edition.E2018 if self.server_2018_compat else Edition.E2023
            evidence = "server_2018_compat"
        fallback = fallback or self.server_2018_compat
    return ServerProfile(
        base=base,
        features=frozenset(schema.features),
        extensions=None if schema.extensions == "auto" else frozenset(schema.extensions),
        missing_location_fallback=fallback,
        evidence=evidence,
    )

DiscoveryConfig

Bases: _Strict

Locate an IEEE 2030.5 server by DNS-SD query, per Clause 7.

On by default because the standard puts it on the client: §6.9.2 says "Clients SHALL locate local services by performing DNS service discovery (DNS-SD) queries to the local network." It only runs when no server.url is configured, though, because §7.6 a) lists "use known URI(s) to DeviceCapability resource(s) of interest" as an equally valid way to find a server, and an operator who named one has already answered the question.

Retrying a query that found nothing is governed by the connection block, the same as retrying a connection that failed. A client that starts before its server is on the network and a client whose server is briefly down are the same situation to whoever set that policy.

Source code in src/py20305/config.py
class DiscoveryConfig(_Strict):
    """Locate an IEEE 2030.5 server by DNS-SD query, per Clause 7.

    On by default because the standard puts it on the client: §6.9.2 says
    "Clients SHALL locate local services by performing DNS service discovery
    (DNS-SD) queries to the local network." It only runs when no `server.url`
    is configured, though, because §7.6 a) lists "use known URI(s) to
    DeviceCapability resource(s) of interest" as an equally valid way to find
    a server, and an operator who named one has already answered the question.

    Retrying a query that found nothing is governed by the `connection` block,
    the same as retrying a connection that failed. A client that starts before
    its server is on the network and a client whose server is briefly down are
    the same situation to whoever set that policy.
    """

    enabled: bool = Field(
        default=True,
        description=(
            "Locate a server by DNS-SD query when no server.url is set. Turning this "
            "off makes server.url required."
        ),
    )
    transport: Literal["mdns", "xmdns", "both"] = Field(
        default="mdns",
        description=(
            "Which multicast transport to query over. mdns is normative in "
            "IEEE 2030.5-2023 (.local); xmdns is the 2018 transport (.site) and is "
            "deprecated; both queries each in turn."
        ),
    )
    timeout_seconds: float = Field(
        default=3.0,
        gt=0,
        le=60,
        description="How long to listen for answers, per transport.",
    )
    subtype: str | None = Field(
        default=None,
        description=(
            "Narrow the query to one function set using a IEEE 2030.5 §7.5 Table 17 "
            "subtype, such as `derp`. Unset asks for the client's own EndDevice first "
            "and then for any server, which is the sequence Annex C describes."
        ),
    )
    interface: str | None = Field(
        default=None,
        description=(
            "Interface to query from -- an address for IPv4, a name for IPv6. Unset "
            "uses the route to the multicast group, which on a gateway with both a "
            "utility uplink and a device LAN may not be the intended one."
        ),
    )

    @field_validator("subtype")
    @classmethod
    def _subtype_is_one_label(cls, v: str | None) -> str | None:
        """§7.5: a subtype string is one DNS label and does not start with `_`."""
        if v is None:
            return v
        try:
            return validate_subtype(v)
        except ValueError as exc:
            raise ValueError(str(exc)) from exc

AdvertiseConfig

Bases: _Strict

Announce this client on the local network over multicast DNS-SD.

IEEE 2030.5 §7.1 defines two multicast transports and the editions differ over which is normative: the 2018 edition on xmDNS (site-local FF05::FB, the .site domain) and the 2023 edition on plain mDNS (link-local, .local), where xmDNS is retained but deprecated. The records themselves are identical either way, so the choice is a transport rather than a format.

Source code in src/py20305/config.py
class AdvertiseConfig(_Strict):
    """Announce this client on the local network over multicast DNS-SD.

    IEEE 2030.5 §7.1 defines two multicast transports and the editions differ
    over which is normative: the 2018 edition on xmDNS (site-local
    ``FF05::FB``, the ``.site`` domain) and the 2023 edition on plain mDNS
    (link-local, ``.local``), where xmDNS is retained but deprecated. The
    records themselves are identical either way, so the choice is a transport
    rather than a format.
    """

    transport: Literal["mdns", "xmdns", "both", "off"] = Field(
        default="off",
        description=(
            "Which multicast transport to announce on. mdns uses the .local domain "
            "and xmdns the .site one, both announces on each, and off announces "
            "nothing. Off by default: announcing is not part of IEEE 2030.5, and it "
            "publishes this client's LFDI and SFDI to everything on the segment."
        ),
    )
    service: str = Field(
        default=DEFAULT_SERVICE,
        description=(
            "The DNS-SD service to announce under. The default is deliberately not "
            "the registered _smartenergy._tcp: this client is not an IEEE 2030.5 "
            "server, and claiming that name would have conformant clients try to "
            "read a DeviceCapability resource it does not serve."
        ),
    )
    port: int | None = Field(
        default=None,
        gt=0,
        le=65535,
        description=(
            "The TCP port to advertise. Unset advertises the management API when it "
            "is enabled, otherwise the notification server, and announces nothing "
            "when neither is -- a service record has to point at something."
        ),
    )
    instance: str | None = Field(
        default=None,
        description=(
            "Override the instance name. The default ends with this client's SFDI, "
            "which IEEE 2030.5 §7.2 gives as the way to keep the name unique; set "
            "this only when two clients share one certificate."
        ),
    )
    interface: str | None = Field(
        default=None,
        description=(
            "Interface to announce from -- an address for IPv4, a name for IPv6. "
            "Unset uses the route to the multicast group, which on a gateway with "
            "both a utility uplink and a device LAN may not be the intended one."
        ),
    )
    txt: dict[str, str] = Field(
        default_factory=dict,
        description="Additional TXT record keys to publish alongside the defaults.",
    )

    @field_validator("service")
    @classmethod
    def _must_be_a_dns_sd_service(cls, v: str) -> str:
        """Reject a malformed service name at load rather than at announcement.

        Deferring it would surface as a name nothing ever answers, which looks
        like a network fault rather than a typo in this field.
        """
        try:
            validate_service(v)
        except ValueError as exc:
            raise ValueError(str(exc)) from exc
        return v

    @field_validator("instance")
    @classmethod
    def _instance_fits_a_label(cls, v: str | None) -> str | None:
        """§7.2: an instance label is up to 63 bytes of UTF-8."""
        if v is None:
            return v
        if not v:
            raise ValueError("instance must not be empty")
        if len(v.encode("utf-8")) > 63:
            raise ValueError(f"instance {v!r} is longer than 63 bytes (IEEE 2030.5 §7.2)")
        return v

TlsFileConfig

Bases: _Strict

The certificate material this client presents and trusts.

Paths are resolved relative to the configuration file, so a deployment can be moved without rewriting them.

Source code in src/py20305/config.py
class TlsFileConfig(_Strict):
    """The certificate material this client presents and trusts.

    Paths are resolved relative to the configuration file, so a deployment can
    be moved without rewriting them.
    """

    client_cert: Path = Field(description="Client certificate (PEM). Its LFDI identifies you.")
    client_key: Path = Field(description="Private key for the client certificate (PEM).")
    ca_cert: Path = Field(description="CA bundle that signed the server's certificate (PEM).")

    check_hostname: bool = Field(
        default=True,
        description=(
            "Verify the server's certificate names the host being connected to. "
            "Turn off only for a server reached by an address its certificate does not carry."
        ),
    )
    additional_ciphers: tuple[str, ...] = Field(
        default=(),
        description=(
            "Extra TLS 1.2 cipher suites, each naming exactly one suite. For peers presenting "
            "RSA certificates, which the IEEE 2030.5 ECDSA-only baseline cannot handshake with."
        ),
    )

SubscriptionConfig

Bases: _Strict

Subscribe/notify instead of relying on polling alone.

Off by default: enabling it opens a listening socket for the server's notifications, which a deployment should choose rather than inherit.

Source code in src/py20305/config.py
class SubscriptionConfig(_Strict):
    """Subscribe/notify instead of relying on polling alone.

    Off by default: enabling it opens a listening socket for the server's
    notifications, which a deployment should choose rather than inherit.
    """

    enabled: bool = Field(
        default=False,
        description="Subscribe to server resources and receive notifications",
    )
    notification_host: str = Field(
        default="0.0.0.0",
        description="Address the notification listener binds",
    )
    notification_port: int = Field(
        default=10443,
        ge=1,
        le=65535,
        description="Port the notification listener binds",
    )
    notification_external_host: str | None = Field(
        default=None,
        description=(
            "Hostname or IP the server should deliver notifications to -- what goes "
            "into each subscription's notificationURI. Required when enabled: the "
            "bind address is not it, and advertising 0.0.0.0 would subscribe with a "
            "callback no server can reach."
        ),
    )
    notification_client_cert_mode: Literal["off", "warn", "enforce"] = Field(
        default="warn",
        description="How strictly the listener checks the notifying server's client certificate",
    )
    renewal_method: Literal["post", "put"] = Field(
        default="post",
        description=(
            "How a subscription is renewed. 'post' re-POSTs it to its SubscriptionList. "
            "'put' PUTs it to its own href (IEEE 2030.5 Annex A.2.7.2), and POSTs a new "
            "one to the list when the server answers 404 or 405."
        ),
    )
    adopt_existing: bool = Field(
        default=False,
        description=(
            "At startup, take over the subscriptions the server already holds for this "
            "EndDevice with this client's notificationURI, instead of creating new ones. "
            "For a client that takes over from another instance of itself with the same "
            "certificate and notification address."
        ),
    )
    remove_duplicates: bool = Field(
        default=False,
        description=(
            "With adopt_existing, DELETE the further subscriptions of this client's that "
            "the server holds for a resource it adopted. Subscriptions with another "
            "notificationURI are never touched."
        ),
    )
    unsubscribe_on_close: bool = Field(
        default=True,
        description=(
            "DELETE this client's subscriptions when it shuts down. Set false for an "
            "instance that hands over to another instance with the same certificate and "
            "notification address, which adopts them (adopt_existing)."
        ),
    )
    backstop_poll_seconds: float = Field(
        default=900.0,
        gt=0,
        description=(
            "How often a resource with an active subscription is still polled, in case "
            "the server stops notifying while it still lists the subscription. Never "
            "faster than the resource's pollRate."
        ),
    )

    @model_validator(mode="after")
    def _external_host_when_enabled(self) -> SubscriptionConfig:
        """The advertised callback host cannot be guessed, so it must be stated."""
        if self.enabled and not self.notification_external_host:
            raise ValueError(
                "subscription.enabled requires subscription.notification_external_host -- "
                "it becomes the notificationURI the server delivers to"
            )
        return self

    @model_validator(mode="after")
    def _remove_duplicates_needs_adoption(self) -> SubscriptionConfig:
        """Duplicates are found while adopting, so removal has nothing to act on alone."""
        if self.remove_duplicates and not self.adopt_existing:
            raise ValueError(
                "subscription.remove_duplicates requires subscription.adopt_existing: true"
            )
        return self

TelemetryConfig

Bases: _Strict

Reporting each device's measurements and state back to the server.

On by default: a utility program expects the devices in it to report, and a client that registers and then says nothing looks to the server like one that has failed. Set enabled: false for a deployment that should observe and dispatch without reporting.

One switch covers both halves, because they are one conversation with the utility: readings mirrored as MirrorUsagePoints, and the DER resources (DERCapability, DERSettings, DERStatus, DERAvailability) PUT for each device. A server exposing no MirrorUsagePointList disables the readings only -- it still expects the DER resources.

Source code in src/py20305/config.py
class TelemetryConfig(_Strict):
    """Reporting each device's measurements and state back to the server.

    On by default: a utility program expects the devices in it to report, and a
    client that registers and then says nothing looks to the server like one
    that has failed. Set ``enabled: false`` for a deployment that should
    observe and dispatch without reporting.

    One switch covers both halves, because they are one conversation with the
    utility: readings mirrored as MirrorUsagePoints, and the DER resources
    (DERCapability, DERSettings, DERStatus, DERAvailability) PUT for each
    device. A server exposing no MirrorUsagePointList disables the readings
    only -- it still expects the DER resources.
    """

    enabled: bool = Field(
        default=True,
        description="Read each configured device on a schedule and report it to the server",
    )
    post_rate_seconds: int = Field(
        default=300,
        gt=0,
        description=(
            "How often each device is read, its readings posted and its DERStatus PUT. "
            "The server's EndDevice.postRate takes precedence when it specifies one."
        ),
    )
    der_capability_poll_rate_seconds: int = Field(
        default=86400,
        gt=0,
        description="How often DERCapability is PUT. Nameplate data, so a day by default.",
    )
    der_settings_poll_rate_seconds: int = Field(
        default=60,
        gt=0,
        description="How often DERSettings is PUT, when it has changed",
    )
    der_resource_put_policy: Literal["periodic", "on_change"] = Field(
        default="periodic",
        description=(
            "When DERCapability and DERSettings are PUT. 'periodic' PUTs them on their "
            "poll rates. 'on_change' PUTs each once, then only when its content changes "
            "(a changed DERCapability also resends DERSettings), and again after a "
            "reconnect from a sustained loss of contact (CSIP G29)."
        ),
    )
    reading_profile: Literal["default", "signed_load_convention", "signed_as_reported"] = Field(
        default="default",
        description=(
            "How MirrorMeterReadings are encoded. 'signed_load_convention' carries the "
            "sign on each value (flowDirection 0) with export and reactive power "
            "injection negative, and adds line-to-line voltage and State of Energy "
            "readings when the device supplies them. 'signed_as_reported' posts real power, "
            "reactive power and power factor with the sign the connector reports, in "
            "mRIDs of its own, omits flowDirection, "
            "registers no energy totals, and sends no readings on a cycle with none. "
            "Applied at startup."
        ),
    )
    power_direction: Literal["default", "net", "split"] = Field(
        default="default",
        description=(
            "How real and reactive power carry their direction under the default "
            "reading profile. 'default' sets flowDirection once, from the sign when the "
            "MirrorUsagePoint is created, and posts magnitudes, so a battery that later "
            "changes direction keeps the first one. 'net' posts flowDirection 4 with the "
            "sign on the value. 'split' posts a Forward (1) and a Reverse (19) reading for "
            "each quantity. Applied at startup."
        ),
    )

    state_file: Path | None = Field(
        default=None,
        description=(
            "A file that keeps the readings registered with the server across restarts. "
            "IEEE 2030.5-2023 10.10.3 rule n) forbids changing a registered ReadingType, "
            "and under the default reading profile a power reading's flowDirection comes "
            "from the sign of its value when it is first registered. With a state file the "
            "client repeats the earlier registration after a restart. Unset, nothing is "
            "kept and each restart registers from the current sign. A relative path is "
            "relative to the configuration file."
        ),
    )

    @model_validator(mode="after")
    def _direction_needs_the_default_profile(self) -> TelemetryConfig:
        if self.power_direction != "default" and self.reading_profile != "default":
            raise ValueError(
                "telemetry.power_direction applies to the default reading profile only; "
                f"{self.reading_profile} already signs every value"
            )
        return self

LoggingConfig

Bases: _Strict

How much the runner says while it works.

Source code in src/py20305/config.py
class LoggingConfig(_Strict):
    """How much the runner says while it works."""

    level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"
    file: Path | None = Field(
        default=None,
        description="Also write to this file. Logs go to stderr regardless.",
    )

ApiConfig

Bases: _Strict

The optional local management API.

Source code in src/py20305/config.py
class ApiConfig(_Strict):
    """The optional local management API."""

    enabled: bool = False
    host: str = Field(
        default="127.0.0.1",
        description=(
            "Bind address. Loopback by default: the API is unauthenticated, so exposing it "
            "on a routable address makes the client's controls available to that network."
        ),
    )
    port: int = Field(default=8080, ge=1, le=65535)

ConnectionConfig

Bases: _Strict

What to do when the server cannot be reached.

A client on a gateway is expected to outlive the server's outages, so it retries indefinitely by default rather than exiting and relying on something else to restart it.

Source code in src/py20305/config.py
class ConnectionConfig(_Strict):
    """What to do when the server cannot be reached.

    A client on a gateway is expected to outlive the server's outages, so it
    retries indefinitely by default rather than exiting and relying on
    something else to restart it.
    """

    retry_forever: bool = Field(default=True, description="Keep retrying the initial connection.")
    max_attempts: int = Field(
        default=0,
        ge=0,
        description="Give up after this many attempts. 0 means never, and requires retry_forever.",
    )
    initial_delay_seconds: float = Field(default=5.0, gt=0)
    max_delay_seconds: float = Field(default=300.0, gt=0)
    backoff_factor: float = Field(default=2.0, ge=1.0)

ClientConfig

Bases: _Strict

A complete description of one running client.

Source code in src/py20305/config.py
class ClientConfig(_Strict):
    """A complete description of one running client."""

    server: ServerConfig
    tls: TlsFileConfig
    devices: list[Annotated[DeviceConfig, Field(discriminator="type")]] = Field(
        default_factory=list,
        description="The devices this client drives. An empty list monitors without dispatching.",
    )

    logging: LoggingConfig = Field(default_factory=LoggingConfig)
    api: ApiConfig = Field(default_factory=ApiConfig)
    connection: ConnectionConfig = Field(default_factory=ConnectionConfig)
    telemetry: TelemetryConfig = Field(default_factory=TelemetryConfig)
    subscription: SubscriptionConfig = Field(default_factory=SubscriptionConfig)
    discovery: DiscoveryConfig = Field(default_factory=DiscoveryConfig)
    advertise: AdvertiseConfig = Field(default_factory=AdvertiseConfig)
    history: HistoryConfig = Field(
        default_factory=HistoryConfig,
        description=(
            "Keep an operations history on disk: event lifecycles and responses, "
            "device writes, and measured intervals. On by default; each part can be "
            "switched off, or all of it with `enabled: false`."
        ),
    )

    registration_pins: dict[str, int] = Field(
        default_factory=dict,
        description=(
            "Expected Registration PIN per device LFDI (40 hex characters). When a "
            "device's Registration resource carries a different pIN, the mismatch is "
            "reported at discovery -- the check a utility's registration flow expects."
        ),
    )
    register_on_start: bool = Field(
        default=True,
        description=(
            "Register an EndDevice for this client's own certificate identity if the server "
            "does not already have one. Registering when it does would create a duplicate."
        ),
    )

    @model_validator(mode="before")
    @classmethod
    def _forwarders_section_was_removed(cls, data: Any) -> Any:
        """Name the removed ``forwarders`` section rather than report an unknown key."""
        if isinstance(data, dict) and "forwarders" in data:
            raise ValueError(
                "the forwarders section was removed: the client no longer publishes "
                "traffic itself. An embedding application attaches observers "
                "(py20305.observers) and publishes the records it receives"
            )
        return data

    @field_validator("registration_pins")
    @classmethod
    def _pins_keyed_by_lfdi(cls, pins: dict[str, int]) -> dict[str, int]:
        """A mistyped key would silently verify nothing."""
        for lfdi in pins:
            if len(lfdi) != 40 or any(c not in "0123456789abcdefABCDEF" for c in lfdi):
                raise ValueError(f"registration_pins key {lfdi!r} is not a 40-hex LFDI")
        return {k.lower(): v for k, v in pins.items()}

    @field_validator("devices")
    @classmethod
    def _reject_duplicate_lfdis(cls, devices: list[Any]) -> list[Any]:
        """Two entries for one device is a configuration error, not a merge.

        The registry keys on LFDI, so a duplicate silently wins over the other
        and the operator sees one of their two devices never being polled.
        """
        seen: set[str] = set()
        for device in devices:
            lfdi = device.lfdi.lower()
            if lfdi in seen:
                raise ValueError(f"duplicate device lfdi {device.lfdi!r}")
            seen.add(lfdi)
        return devices

    @model_validator(mode="after")
    def _a_server_has_to_be_findable(self) -> ClientConfig:
        """Either name a server or leave discovery able to find one.

        With neither, the client has no way to reach anything and would fail
        at the first connect attempt, which reads as an unreachable server
        rather than as the configuration gap it is.
        """
        if self.server.url is None and not self.discovery.enabled:
            raise ValueError(
                "set server.url, or leave discovery.enabled on so the client can "
                "locate a server by DNS-SD query (IEEE 2030.5 §6.9.2)"
            )
        return self

    def resolve_paths(self, base: Path) -> ClientConfig:
        """Return a copy with relative certificate paths resolved against ``base``.

        Relative to the configuration file rather than the working directory,
        so the same deployment works whichever directory it is started from --
        which is not the same directory under systemd as it is by hand.
        """
        tls = self.tls.model_copy(
            update={
                field: (base / value).resolve()
                for field in ("client_cert", "client_key", "ca_cert")
                if not (value := getattr(self.tls, field)).is_absolute()
            }
        )
        logging_config = self.logging
        if logging_config.file is not None and not logging_config.file.is_absolute():
            logging_config = logging_config.model_copy(
                update={"file": (base / logging_config.file).resolve()}
            )

        telemetry = self.telemetry
        if telemetry.state_file is not None and not telemetry.state_file.is_absolute():
            telemetry = telemetry.model_copy(
                update={"state_file": (base / telemetry.state_file).resolve()}
            )

        # A device speaking Modbus over TLS carries its own certificate paths,
        # and they go straight to an SSL context. Resolving only the server's
        # would make the same relative path mean different things depending on
        # which section it appeared in.
        devices = [self._resolve_device_paths(device, base) for device in self.devices]

        return self.model_copy(
            update={
                "tls": tls,
                "logging": logging_config,
                "telemetry": telemetry,
                "devices": devices,
            }
        )

    @staticmethod
    def _resolve_device_paths(device: Any, base: Path) -> Any:
        """Resolve a device's own certificate paths, if it has any."""
        updates = {
            field: str((base / value).resolve())
            for field in ("ca_path", "cert_path", "key_path")
            if (value := getattr(device, field, None)) and not Path(value).is_absolute()
        }
        return device.model_copy(update=updates) if updates else device
resolve_paths
resolve_paths(base: Path) -> ClientConfig

Return a copy with relative certificate paths resolved against base.

Relative to the configuration file rather than the working directory, so the same deployment works whichever directory it is started from -- which is not the same directory under systemd as it is by hand.

Source code in src/py20305/config.py
def resolve_paths(self, base: Path) -> ClientConfig:
    """Return a copy with relative certificate paths resolved against ``base``.

    Relative to the configuration file rather than the working directory,
    so the same deployment works whichever directory it is started from --
    which is not the same directory under systemd as it is by hand.
    """
    tls = self.tls.model_copy(
        update={
            field: (base / value).resolve()
            for field in ("client_cert", "client_key", "ca_cert")
            if not (value := getattr(self.tls, field)).is_absolute()
        }
    )
    logging_config = self.logging
    if logging_config.file is not None and not logging_config.file.is_absolute():
        logging_config = logging_config.model_copy(
            update={"file": (base / logging_config.file).resolve()}
        )

    telemetry = self.telemetry
    if telemetry.state_file is not None and not telemetry.state_file.is_absolute():
        telemetry = telemetry.model_copy(
            update={"state_file": (base / telemetry.state_file).resolve()}
        )

    # A device speaking Modbus over TLS carries its own certificate paths,
    # and they go straight to an SSL context. Resolving only the server's
    # would make the same relative path mean different things depending on
    # which section it appeared in.
    devices = [self._resolve_device_paths(device, base) for device in self.devices]

    return self.model_copy(
        update={
            "tls": tls,
            "logging": logging_config,
            "telemetry": telemetry,
            "devices": devices,
        }
    )

load_config

load_config(path: str | Path) -> ClientConfig

Load, parse and validate a configuration file.

Accepts YAML (.yaml/.yml) or JSON (.json). Certificate paths are resolved relative to the file.

Raises:

Type Description
ConfigError

If the file is missing, unparseable, or invalid. The message names the file and, for a validation failure, the field -- an operator reading a startup log should not have to map a pydantic traceback back onto their document.

Source code in src/py20305/config.py
def load_config(path: str | Path) -> ClientConfig:
    """Load, parse and validate a configuration file.

    Accepts YAML (``.yaml``/``.yml``) or JSON (``.json``). Certificate paths
    are resolved relative to the file.

    Raises:
        ConfigError: If the file is missing, unparseable, or invalid. The
            message names the file and, for a validation failure, the field --
            an operator reading a startup log should not have to map a
            pydantic traceback back onto their document.
    """
    path = Path(path)
    if not path.is_file():
        raise ConfigError(f"configuration file not found: {path}")

    suffix = path.suffix.lower()
    try:
        text = path.read_text(encoding="utf-8")
    except OSError as exc:
        raise ConfigError(f"could not read {path}: {exc}") from exc

    if suffix in _YAML_SUFFIXES:
        raw = _parse_yaml(text, path)
    elif suffix in _JSON_SUFFIXES:
        try:
            raw = json.loads(text)
        except json.JSONDecodeError as exc:
            raise ConfigError(f"{path} is not valid JSON: {exc}") from exc
    else:
        known = ", ".join(sorted(_YAML_SUFFIXES | _JSON_SUFFIXES))
        raise ConfigError(
            f"unsupported configuration format {suffix!r} for {path} (expected {known})"
        )

    if not isinstance(raw, dict):
        raise ConfigError(f"{path} must contain a mapping at the top level")

    try:
        config = ClientConfig.model_validate(raw)
    except ValidationError as exc:
        raise ConfigError(f"invalid configuration in {path}:\n{_describe(exc)}") from exc

    return config.resolve_paths(path.parent.resolve())

py20305.cli

Run a client from a configuration file.

py20305 --config client.yaml

What this adds over constructing a :class:~py20305.client.csip_client.CsipClient yourself is the part a deployment needs and a library should not assume: reading configuration from disk, configuring logging, retrying the initial connection instead of exiting when the server is briefly unavailable, and shutting down cleanly when the supervisor asks.

Deliberately not a supervisor itself. It retries the connection, but a crashed process is left for systemd, Docker or whatever else is already responsible for restarting it -- reimplementing that here would be a worse version of a solved problem, and one that hides failures from the thing meant to observe them.

ConnectionState dataclass

What the runner is doing about the upstream connection.

Published on the app so the management API can report "disconnected, retrying" rather than simply failing to answer. That is the state an operator most wants to see, and the API is least useful if it is only available once the thing it reports on is already working.

Source code in src/py20305/cli.py
@dataclass
class ConnectionState:
    """What the runner is doing about the upstream connection.

    Published on the app so the management API can report "disconnected,
    retrying" rather than simply failing to answer. That is the state an
    operator most wants to see, and the API is least useful if it is only
    available once the thing it reports on is already working.
    """

    phase: str = "connecting"
    detail: str | None = None
    attempts: int = 0
    retry_in_seconds: float | None = None

    def to_dict(self) -> dict[str, Any]:
        return {
            "phase": self.phase,
            "detail": self.detail,
            "attempts": self.attempts,
            "retry_in_seconds": self.retry_in_seconds,
        }

setup_logging

setup_logging(config: LoggingConfig) -> None

Configure logging for the runner.

Called only from here, never from library code, which uses getLogger and leaves handler policy to whoever is embedding it. A library that configures the root logger takes that decision away from its host.

Source code in src/py20305/cli.py
def setup_logging(config: LoggingConfig) -> None:
    """Configure logging for the runner.

    Called only from here, never from library code, which uses ``getLogger``
    and leaves handler policy to whoever is embedding it. A library that
    configures the root logger takes that decision away from its host.
    """
    handlers: list[logging.Handler] = [logging.StreamHandler(sys.stderr)]
    if config.file is not None:
        config.file.parent.mkdir(parents=True, exist_ok=True)
        handlers.append(logging.FileHandler(config.file, encoding="utf-8"))

    logging.basicConfig(
        level=getattr(logging, config.level),
        format="%(asctime)s %(levelname)-7s %(name)s: %(message)s",
        handlers=handlers,
        force=True,
    )
    # aiohttp logs every request at INFO, which buries this client's own
    # messages under traffic the operator did not ask to see.
    logging.getLogger("aiohttp.access").setLevel(logging.WARNING)

find_servers

find_servers(config: ClientConfig, own_sfdi: int | None) -> list[DiscoveredServer]

Query the local network for IEEE 2030.5 servers (IEEE 2030.5 §6.9.2).

Blocking, and called from a thread by the runner. The sockets underneath are plain blocking sockets on purpose: multicast behavior is the part that differs most between platforms, and keeping it out of the event loop keeps those differences away from the protocol client.

A configured subtype takes precedence over the SFDI sequence rather than suppressing it: the subtype goes into the PTR name, so an operator who named one asked a specific question and should get an answer to it.

Source code in src/py20305/cli.py
def find_servers(config: ClientConfig, own_sfdi: int | None) -> list[DiscoveredServer]:
    """Query the local network for IEEE 2030.5 servers (IEEE 2030.5 §6.9.2).

    Blocking, and called from a thread by the runner. The sockets underneath
    are plain blocking sockets on purpose: multicast behavior is the part that
    differs most between platforms, and keeping it out of the event loop keeps
    those differences away from the protocol client.

    A configured subtype takes precedence over the SFDI sequence rather than
    suppressing it: the subtype goes into the PTR name, so an operator who
    named one asked a specific question and should get an answer to it.
    """
    return discover_for_client(
        transports_for(config.discovery.transport),
        sfdi=own_sfdi,
        subtype=config.discovery.subtype,
        timeout=config.discovery.timeout_seconds,
        interface=config.discovery.interface,
    )

build_client

build_client(config: ClientConfig) -> tuple[CsipClient, str]

Construct a client from configuration, returning it and its own LFDI.

Source code in src/py20305/cli.py
def build_client(config: ClientConfig) -> tuple[CsipClient, str]:
    """Construct a client from configuration, returning it and its own LFDI."""
    # Resolved by discovery before this is reached whenever it was not
    # configured, so an unset URL here is a wiring mistake rather than an
    # operator one, and should not be reported as a connection failure.
    if config.server.url is None:
        raise ValueError("server.url was not resolved before the client was built")

    tls = TlsConfig(
        client_cert=config.tls.client_cert,
        client_key=config.tls.client_key,
        ca_cert=config.tls.ca_cert,
        check_hostname=config.tls.check_hostname,
        additional_ciphers=config.tls.additional_ciphers,
    )
    own_lfdi = compute_lfdi(config.tls.client_cert.read_text())

    from py20305.telemetry.mup import ReadingProfile

    registry = ConnectorConfigRegistry(
        config.devices, reading_profile=ReadingProfile(config.telemetry.reading_profile)
    )

    sole_device = config.devices[0].lfdi if len(config.devices) == 1 else None
    holder: dict[str, CsipClient] = {}
    resolve_lfdi = make_lfdi_resolver(lambda: holder.get("client"), sole_device)

    client = CsipClient(
        config.server.url,
        tls=tls,
        dispatcher=ConnectorDispatcher(registry, lfdi_resolver=resolve_lfdi),
        dcap_path=config.server.dcap_path,
        registration_pins=config.registration_pins or None,
        profile=config.server.profile(),
        use_server_time=config.server.use_server_time,
        fsa_stale_seconds=config.server.fsa_stale_seconds,
        heartbeat_seconds=config.subscription.backstop_poll_seconds,
    )

    # Subscribe/notify. Attached after construction because the manager needs
    # the client's transport, which exists only once the client does. The
    # client starts and stops the listener with its own lifecycle.
    if config.subscription.enabled:
        from py20305.subscription.manager import SubscriptionManager
        from py20305.subscription.notification_server import NotificationServer

        notification_server = NotificationServer(
            host=config.subscription.notification_host,
            port=config.subscription.notification_port,
            tls=tls,
            client_cert_mode=config.subscription.notification_client_cert_mode,
        )
        # The validator guarantees external_host when enabled.
        external_host = config.subscription.notification_external_host
        assert external_host is not None
        manager = SubscriptionManager(
            client=client.http,
            notification_uri=notification_server.build_notification_uri(external_host),
            missing_location_fallback=config.server.profile().missing_location_fallback,
            renewal_method=config.subscription.renewal_method,
            adopt_on_connect=config.subscription.adopt_existing,
            remove_duplicates=config.subscription.remove_duplicates,
            unsubscribe_on_close=config.subscription.unsubscribe_on_close,
            # A renewal answered 401/404 means the server has forgotten us;
            # rediscovery re-subscribes against its current state. Wrapped to
            # drop the bool trigger_rediscovery returns, which the manager's
            # callback contract does not carry.
            on_subscription_lost=_rediscover_quietly(client),
        )
        client.attach_subscriptions(manager, notification_server)
        logger.info(
            "subscriptions on; notifications delivered to %s",
            notification_server.build_notification_uri(external_host),
        )
    holder["client"] = client
    return client, own_lfdi

build_advertiser

build_advertiser(config: ClientConfig) -> MulticastAdvertiser | None

Build the local-network advertiser, or None when nothing is announced.

Returns None both when the operator turned announcement off and when there is no reachable local service to name. A DNS-SD service record has to carry a port, and pointing one at a port nothing answers on advertises a connection that will always be refused -- worse than staying quiet, because it is a fault the finder has to diagnose rather than an absence they can see.

The certificate is read only once both of those have passed, so a client that announces nothing -- the default -- never touches it here.

Source code in src/py20305/cli.py
def build_advertiser(config: ClientConfig) -> MulticastAdvertiser | None:
    """Build the local-network advertiser, or None when nothing is announced.

    Returns None both when the operator turned announcement off and when there
    is no reachable local service to name. A DNS-SD service record has to carry
    a port, and pointing one at a port nothing answers on advertises a
    connection that will always be refused -- worse than staying quiet, because
    it is a fault the finder has to diagnose rather than an absence they can
    see.

    The certificate is read only once both of those have passed, so a client
    that announces nothing -- the default -- never touches it here.
    """
    transports = transports_for(config.advertise.transport)
    if not transports:
        return None

    port = _advertised_port(config)
    if port is None:
        return None

    own_lfdi = compute_lfdi(config.tls.client_cert.read_text())
    advertisement = build_advertisement(
        lfdi=own_lfdi,
        sfdi=compute_sfdi(own_lfdi),
        port=port,
        service=config.advertise.service,
        instance=config.advertise.instance,
        version=get_package_version(),
        extra_txt=config.advertise.txt,
    )
    return MulticastAdvertiser(
        advertisement, transports, interface=config.advertise.interface
    )

make_lfdi_resolver

make_lfdi_resolver(get_client: Callable[[], CsipClient | None], sole_device: str | None) -> Callable[[str], str | None]

Map a server-side EndDevice href to the LFDI of the device it addresses.

A control names a device by href; the connector registry is keyed by LFDI. The discovered EndDevice carries both, so the mapping comes from what the server said rather than from a guess. Returning one configured LFDI for every href would mean that with two or more devices every control resolves to the wrong connector, or to none, and is silently never applied.

sole_device is the fallback for the single-device case, where the mapping is unambiguous and discovery may not have run yet. With several devices there is no safe fallback, so an unknown href resolves to nothing and the dispatcher reports it rather than guessing.

Source code in src/py20305/cli.py
def make_lfdi_resolver(
    get_client: Callable[[], CsipClient | None], sole_device: str | None
) -> Callable[[str], str | None]:
    """Map a server-side EndDevice href to the LFDI of the device it addresses.

    A control names a device by href; the connector registry is keyed by LFDI.
    The discovered EndDevice carries both, so the mapping comes from what the
    server said rather than from a guess. Returning one configured LFDI for
    every href would mean that with two or more devices every control resolves
    to the wrong connector, or to none, and is silently never applied.

    ``sole_device`` is the fallback for the single-device case, where the
    mapping is unambiguous and discovery may not have run yet. With several
    devices there is no safe fallback, so an unknown href resolves to nothing
    and the dispatcher reports it rather than guessing.
    """

    def resolve(device_href: str) -> str | None:
        client = get_client()
        state = client.state.end_devices.get(device_href) if client is not None else None
        if state is not None:
            lfdi = state.lfdi
            return (lfdi.hex() if isinstance(lfdi, bytes) else str(lfdi)).lower()
        return sole_device

    return resolve

connect_with_retry async

connect_with_retry(client: CsipClient, config: ClientConfig, stop: Event | None = None, state: ConnectionState | None = None, reconnect: Event | None = None) -> bool

Connect, retrying with backoff while the server is unreachable.

Returns True once connected, False if the attempt budget ran out or the caller asked to stop. A client on a gateway usually outlives the server's outages, so the default is to keep trying rather than exit and depend on something else noticing.

Only a transport failure is retried: a connection error, or the TLS error the transport raises once its own handshake retries are exhausted. The HTTP layer classifies that second one as an unreachable peer, and it is raised for a server-side handshake problem as readily as a local one, so it is retried rather than treated as fatal. Certificate material that is genuinely unusable -- missing, malformed, or a key that does not match -- fails when the SSL context is built, before any of this, and is reported as a configuration fault there.

A protocol or payload error is not retried. It produces the same result every time, so retrying only delays the operator seeing it -- and with the default of retrying forever, it would never be seen at all.

stop is awaited alongside the backoff, so a shutdown signal during an outage takes effect immediately. Without it, a systemd stop would hang until the server came back, which is exactly when it will not.

state is updated as the attempts proceed so the management API can report the outage, and reconnect short-circuits the wait when an operator asks for a retry now rather than at the end of the backoff.

Source code in src/py20305/cli.py
async def connect_with_retry(
    client: CsipClient,
    config: ClientConfig,
    stop: asyncio.Event | None = None,
    state: ConnectionState | None = None,
    reconnect: asyncio.Event | None = None,
) -> bool:
    """Connect, retrying with backoff while the server is unreachable.

    Returns True once connected, False if the attempt budget ran out or the
    caller asked to stop. A client on a gateway usually outlives the server's
    outages, so the default is to keep trying rather than exit and depend on
    something else noticing.

    Only a *transport* failure is retried: a connection error, or the TLS
    error the transport raises once its own handshake retries are exhausted.
    The HTTP layer classifies that second one as an unreachable peer, and it
    is raised for a server-side handshake problem as readily as a local one,
    so it is retried rather than treated as fatal. Certificate material that
    is genuinely unusable -- missing, malformed, or a key that does not match
    -- fails when the SSL context is built, before any of this, and is
    reported as a configuration fault there.

    A protocol or payload error is not retried. It produces the same result
    every time, so retrying only delays the operator seeing it -- and with the
    default of retrying forever, it would never be seen at all.

    ``stop`` is awaited alongside the backoff, so a shutdown signal during an
    outage takes effect immediately. Without it, a systemd stop would hang
    until the server came back, which is exactly when it will not.

    ``state`` is updated as the attempts proceed so the management API can
    report the outage, and ``reconnect`` short-circuits the wait when an
    operator asks for a retry now rather than at the end of the backoff.
    """
    delay = min(config.connection.initial_delay_seconds, config.connection.max_delay_seconds)
    attempt = 0

    while True:
        if stop is not None and stop.is_set():
            logger.info("stop requested before a connection was established")
            return False

        attempt += 1
        if state is not None:
            state.phase = "connecting"
            state.attempts = attempt
            state.retry_in_seconds = None
        try:
            await client.connect()
        except (Sep2ConnectionError, Sep2TlsError) as exc:
            budget = config.connection.max_attempts
            exhausted = budget and attempt >= budget
            if exhausted or not config.connection.retry_forever:
                logger.error("could not reach the server after %d attempt(s): %s", attempt, exc)
                if state is not None:
                    state.phase = "failed"
                    state.detail = str(exc)
                return False

            logger.warning(
                "connection attempt %d failed (%s); retrying in %.0fs", attempt, exc, delay
            )
            if state is not None:
                state.phase = "disconnected"
                state.detail = str(exc)
                state.retry_in_seconds = delay
            if await _sleep_or_stop(delay, stop, reconnect):
                if stop is not None and stop.is_set():
                    logger.info("stop requested while waiting to retry")
                    return False
                logger.info("reconnect requested; retrying now")
                if reconnect is not None:
                    reconnect.clear()
            delay = min(
                delay * config.connection.backoff_factor,
                config.connection.max_delay_seconds,
            )
        else:
            if attempt > 1:
                logger.info("connected after %d attempts", attempt)
            if state is not None:
                state.phase = "connected"
                state.detail = None
                state.retry_in_seconds = None
            return True

run async

run(config: ClientConfig) -> int

Run until stopped, returning the process exit code.

Source code in src/py20305/cli.py
async def run(config: ClientConfig) -> int:
    """Run until stopped, returning the process exit code."""
    stop = asyncio.Event()
    reconnect = asyncio.Event()
    connection = ConnectionState()
    # Installed before discovery, not after, so Ctrl-C during a retrying
    # discovery stops the process rather than being ignored until it finds
    # something -- which, on a network with no server, is never.
    _install_signal_handlers(stop)

    logger.info("%s", get_version_string())

    # Started before discovery, not after it. Announcing does not depend on
    # finding a server, and a client that only became visible once it had
    # found one would be invisible during exactly the local outage where
    # someone goes looking for it. It is also why this is torn down in the
    # `finally` below rather than alongside the client.
    advertiser = build_advertiser(config)
    if advertiser is not None:
        advertiser.start()

    try:
        return await _run_client(config, stop, reconnect, connection)
    finally:
        if advertiser is not None:
            # In a thread: stop() sends the goodbye records and joins.
            await asyncio.to_thread(advertiser.stop)

save_telemetry_state

save_telemetry_state(coordinator: TelemetryCoordinator, path: Path) -> None

Write the readings registered with the server to path; log, never raise.

Source code in src/py20305/cli.py
def save_telemetry_state(coordinator: TelemetryCoordinator, path: Path) -> None:
    """Write the readings registered with the server to ``path``; log, never raise."""
    checkpoint = coordinator.to_checkpoint()
    if checkpoint is None:
        return
    try:
        save_checkpoint(path, checkpoint)
    except OSError as exc:
        logger.warning("Cannot write the telemetry state file %s: %s", path, exc)

main

main(argv: list[str] | None = None) -> int

Entry point. Returns the process exit code.

Source code in src/py20305/cli.py
def main(argv: list[str] | None = None) -> int:
    """Entry point. Returns the process exit code."""
    args = parse_args(argv)

    try:
        config = load_config(args.config)
    except ConfigError as exc:
        # Before logging is configured, so this goes straight to stderr.
        print(f"error: {exc}", file=sys.stderr)
        return EXIT_CONFIG_ERROR

    if args.telemetry_state_file:
        config = config.model_copy(
            update={
                "telemetry": config.telemetry.model_copy(
                    update={"state_file": args.telemetry_state_file.resolve()}
                )
            }
        )

    if args.log_level:
        config = config.model_copy(
            update={"logging": config.logging.model_copy(update={"level": args.log_level})}
        )
    if args.multicast_transport:
        try:
            config = _apply_transport_override(config, args.multicast_transport)
        except ConfigError as exc:
            print(f"error: {exc}", file=sys.stderr)
            return EXIT_CONFIG_ERROR

    try:
        setup_logging(config.logging)
    except OSError as exc:
        # An unwritable log directory is a configuration fault like any other,
        # and it happens before logging exists to report it -- so this goes to
        # stderr and exits with the documented code rather than as a traceback.
        print(f"error: could not configure logging: {exc}", file=sys.stderr)
        return EXIT_CONFIG_ERROR

    if args.check:
        try:
            lfdi = compute_lfdi(config.tls.client_cert.read_text())
        except (OSError, ValueError) as exc:
            # ValueError as well as OSError: a certificate can be present and
            # unreadable *as a certificate*, which is a configuration problem
            # and should report as one rather than as a traceback.
            print(f"error: could not read the client certificate: {exc}", file=sys.stderr)
            return EXIT_CONFIG_ERROR
        server = config.server.url or (
            f"(discovered at startup over {config.discovery.transport})"
        )
        print(f"configuration OK: {args.config}")
        print(f"  server      : {server}")
        print(f"  client LFDI : {lfdi}")
        print(f"  client SFDI : {compute_sfdi(lfdi)}")
        print(f"  devices     : {len(config.devices)}")
        state_file = config.telemetry.state_file
        print(f"  telemetry state : {state_file if state_file is not None else 'off'}")
        finding = f"on, {config.discovery.transport}" if config.discovery.enabled else "off"
        print(f"  discovery   : {finding}")
        print(f"  announcing  : {config.advertise.transport}")
        return EXIT_OK

    if args.discover:
        return _report_discovery(config)

    try:
        return asyncio.run(run(config))
    except (OSError, ValueError, ssl.SSLError) as exc:
        # The TLS material is only truly checked when it is loaded, which is
        # inside the run. A missing key, malformed PEM or a key that does not
        # match its certificate arrives here, and it is a configuration fault:
        # it deserves the documented exit code rather than a traceback and the
        # generic 1 that tells a supervisor nothing.
        #
        # Sep2TlsError is deliberately absent. The transport raises it once its
        # own handshake retries are exhausted, and the HTTP layer treats it as
        # an unreachable peer -- so a server-side handshake problem would stop
        # the process here with the documented systemd unit then refusing to
        # restart it. It is retried as a connection failure instead.
        logger.error("could not start: %s", exc)
        return EXIT_CONFIG_ERROR
    except KeyboardInterrupt:
        # Windows, where add_signal_handler is unavailable and Ctrl-C arrives
        # as an exception instead.
        return EXIT_OK