Skip to content

Add national holidays for 2026 and 2027; prevent KeyError for unknown years - #91

Open
jmbarragan wants to merge 2 commits into
azogue:masterfrom
jmbarragan:fix/holidays-2026-2027
Open

Add national holidays for 2026 and 2027; prevent KeyError for unknown years#91
jmbarragan wants to merge 2 commits into
azogue:masterfrom
jmbarragan:fix/holidays-2026-2027

Conversation

@jmbarragan

Copy link
Copy Markdown

Problem

_NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD only contains entries up to 2025. Any Home Assistant installation running in 2026 or later raises an unhandled exception when computing the tariff period:

KeyError: 2026
  File ".../aiopvpc/pvpc_tariff.py", line 83, in _tariff_period_key
    national_holiday = day in _NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD[day.year]

This causes the pvpc_hourly_pricing integration to enter setup_retry state and all PVPC sensors to become unavailable.

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):

Date Day Holiday
2026-01-01 Thursday Año nuevo
2026-01-06 Tuesday Epifanía del Señor
2026-04-03 Friday Viernes Santo
2026-05-01 Friday Día del Trabajador
2026-10-12 Monday Día de la Hispanidad
2026-12-08 Tuesday La Inmaculada Concepción
2026-12-25 Friday Navidad

2. Add 2027 national holidays

Proactively added to avoid the same issue next year.

3. Prevent future KeyError for unknown years

Changed _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)

jmbarragan and others added 2 commits April 6, 2026 03:07
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
@r-jordan

r-jordan commented Apr 6, 2026

Copy link
Copy Markdown

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.

@jmbarragan

Copy link
Copy Markdown
Author

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

  • Before: homeassistant.components.pvpc_hourly_pricing crashed repeatedly with:
    • KeyError: 2026 at _NATIONAL_EXTRA_HOLIDAYS_FOR_P3_PERIOD[day.year]
  • After applying equivalent hotfix logic, PVPC entities recovered correctly:
    • sensor.esios_pvpc
    • sensor.esios_injection_price
  • No regressions observed in tariff update cycle.

Request

Could 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.

@rodrimmb

Copy link
Copy Markdown

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:

docker exec -it homeassistant bash

1 Go to the folder containing the buggy file pvpc_tariff.py:

cd /usr/local/lib/python3.14/site-packages/aiopvpc

2 Copy the file to create a backup:

cp pvpc_tariff.py pvpc_tariff.py.bak

3 Edit the file pvpc_tariff.py:

vi pvpc_tariff.py

4 Apply the changes from this PR.

5 Exit the container and restart Home Assistant:

exit
docker compose down
docker compose up -d

Thanks for this PR @jmbarragan. I hope it will be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants