Add national holidays for 2026 and 2027; prevent KeyError for unknown years - #91
Add national holidays for 2026 and 2027; prevent KeyError for unknown years#91jmbarragan wants to merge 2 commits into
Conversation
The dictionary _NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD only contained entries
up to 2025. Any installation running in 2026 or later would raise:
KeyError: 2026
in _tariff_period_key() when computing the P1/P2/P3 tariff period.
Changes:
- Add 2026 national holidays (BOE reference)
- Add 2027 national holidays (BOE reference)
- Replace dict[day.year] with dict.get(day.year, {}) to prevent KeyError
for years not yet included, falling back to non-holiday treatment
for more information, see https://pre-commit.ci
|
Hello, the code was wrong because in Spain the electricity tariff is P3 only on the fixed date holidays. And Viernes Santo is not a fixed date holiday because it changes every year. It's non sense having to add every year the same dates to the code. Anyway it seems that the only aprover to this repository is the owner @azogue and he's not online since a very long time. Then this PR is not going to be merged soon. |
|
Hi all — this PR has been validated in a live Home Assistant 2026.4.2 deployment affected by KeyError: 2026 in �iopvpc/pvpc_tariff.py. Runtime validation
RequestCould a maintainer please review and merge this PR, and publish a new �iopvpc release so Home Assistant can consume the fix without local overrides? Thanks a lot. |
|
I had the same problem, and with your changes the integration works fine for me. I use a Docker installation, and here's how I fixed the bug: 0 Enter the Home Assistant container: 1 Go to the folder containing the buggy file pvpc_tariff.py: 2 Copy the file to create a backup: 3 Edit the file pvpc_tariff.py: 4 Apply the changes from this PR. 5 Exit the container and restart Home Assistant: Thanks for this PR @jmbarragan. I hope it will be merged soon. |
Problem
_NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIODonly contains entries up to 2025. Any Home Assistant installation running in 2026 or later raises an unhandled exception when computing the tariff period:This causes the
pvpc_hourly_pricingintegration to entersetup_retrystate and all PVPC sensors to becomeunavailable.Changes
1. Add 2026 national holidays
Spanish national holidays that affect P3 period computation (weekday-only; weekend dates commented out as they are already P3):
2. Add 2027 national holidays
Proactively added to avoid the same issue next year.
3. Prevent future
KeyErrorfor unknown yearsChanged
_NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD[day.year]to_NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD.get(day.year, {})in_tariff_period_key().This means installations running in years not yet in the dictionary will fall back to treating all days as non-holidays (conservative behaviour — no tariff period misclassification possible), rather than crashing with an unhandled
KeyError.Testing
Verified manually:
_tariff_period_key(datetime(2026, 1, 1, 14, 0), False)→P3✓ (Año nuevo)_tariff_period_key(datetime(2026, 4, 3, 14, 0), False)→P3✓ (Viernes Santo)_tariff_period_key(datetime(2026, 4, 6, 14, 0), False)→P1✓ (normal Monday)_tariff_period_key(datetime(2028, 6, 15, 14, 0), False)→P1✓ (no KeyError)