Skip to content

Commit 47b52a1

Browse files
committed
Handle indexed PSCR rate book sections
1 parent fb15f12 commit 47b52a1

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

custom_components/dte_rates/pscr_parser.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,38 @@ def parse_pscr_rates_from_pdf(pdf_bytes: bytes) -> dict[str, Decimal]:
1919
text = "\n".join(page.extract_text() or "" for page in reader.pages)
2020
lines = [re.sub(r"\s+", " ", line).strip() for line in text.splitlines()]
2121

22-
section = _power_supply_surcharge_lines(lines)
2322
rates: dict[str, Decimal] = {}
24-
for line in section:
25-
match = _RATE_ROW_RE.match(line)
26-
if match:
27-
rates[match.group("code").upper()] = Decimal(match.group("pscr"))
23+
for section in _power_supply_surcharge_sections(lines):
24+
for line in section:
25+
match = _RATE_ROW_RE.match(line)
26+
if match:
27+
rates[match.group("code").upper()] = Decimal(match.group("pscr"))
28+
if rates:
29+
break
2830

2931
if not rates:
3032
raise ValueError("MPSC DTE rate book does not contain PSCR tariff rows")
3133
return rates
3234

3335

34-
def _power_supply_surcharge_lines(lines: list[str]) -> list[str]:
35-
start = None
36+
def _power_supply_surcharge_sections(lines: list[str]) -> list[list[str]]:
37+
sections: list[list[str]] = []
3638
for idx, line in enumerate(lines):
3739
lower = line.lower()
3840
if "c8.5 surcharges and credits applicable to power supply service" in lower:
39-
start = idx
40-
break
41+
end = _next_delivery_surcharge_index(lines, idx + 1)
42+
sections.append(lines[idx:end])
4143

42-
if start is None:
43-
return lines
44+
if not sections:
45+
sections.append(lines)
46+
return sections
4447

48+
49+
def _next_delivery_surcharge_index(lines: list[str], start: int) -> int:
4550
end = len(lines)
46-
for idx in range(start + 1, len(lines)):
51+
for idx in range(start, len(lines)):
4752
lower = lines[idx].lower()
4853
if "c9 surcharges and credits applicable to delivery service" in lower:
4954
end = idx
5055
break
51-
52-
return lines[start:end]
56+
return end

docs/research/rider18-export-rates.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ For net metering, keep the existing behavior: export uses the full active import
4040

4141
The setup flow reports how many MPSC tariff PSCR factors were loaded. Export entities expose the selected rate's PSCR value, the PSCR source URL, and the full parsed `pscr_rates` map.
4242

43+
The MPSC rate book can mention `C8.5 Surcharges and Credits Applicable to Power Supply Service` in the front-matter index before the real tariff table. Parser logic must scan candidate C8.5 sections until it finds tariff rows, otherwise it can stop at the index, see no PSCR rows, and incorrectly log that the rate book does not contain PSCR tariff rows.
44+
4345
Export entities expose whether the active period has enough data for the full formula:
4446

4547
- `export_rate_source: rider18_formula` when generation components and PSCR are present.

tests/test_pscr_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77

88
SAMPLE_RATE_BOOK_TEXT = """
9+
C8 SURCHARGES AND CREDITS APPLICABLE TO POWER SUPPLY SERVICE
10+
C8.5 Surcharges and Credits Applicable to Power Supply Service C-65.00
11+
C9 SURCHARGES AND CREDITS APPLICABLE TO DELIVERY SERVICE
12+
C9.1 Nuclear Surcharge C-66.00
913
C8.5 SURCHARGES AND CREDITS APPLICABLE TO POWER SUPPLY SERVICE
1014
Rate Schedule Description PSCR Factor Other Charges
1115
D1 Non Transmitting Meter 1.877 0.0222 0.2305 2.1297

0 commit comments

Comments
 (0)