From 8c8df6fc3a1d2f594ec6d9da36bbbe517e4f30ef Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 9 Oct 2026 07:09:22 +0200 Subject: [PATCH] Keep a missing meter history from blocking Peblar setup (#184824) --- homeassistant/components/peblar/__init__.py | 6 +- homeassistant/components/peblar/event.py | 22 ++++- tests/components/peblar/test_event.py | 91 ++++++++++++++++++++- 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/peblar/__init__.py b/homeassistant/components/peblar/__init__.py index ee4ccf598fd8..cf58e7f7e878 100644 --- a/homeassistant/components/peblar/__init__.py +++ b/homeassistant/components/peblar/__init__.py @@ -92,9 +92,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: PeblarConfigEntry) -> bo meter_coordinator.async_config_entry_first_refresh(), user_configuration_coordinator.async_config_entry_first_refresh(), version_coordinator.async_config_entry_first_refresh(), - authorization_coordinator.async_config_entry_first_refresh(), ) + # Reading back who was shown in is an extra the rest does not depend on, + # and the endpoint it needs is missing on older firmware. Asking for it + # above would take the whole integration down over a single entity. + await authorization_coordinator.async_refresh() + # Store the runtime data entry.runtime_data = PeblarRuntimeData( authorization_coordinator=authorization_coordinator, diff --git a/homeassistant/components/peblar/event.py b/homeassistant/components/peblar/event.py index 6df24dedbd03..692c6d4eb0ae 100644 --- a/homeassistant/components/peblar/event.py +++ b/homeassistant/components/peblar/event.py @@ -58,12 +58,28 @@ class PeblarAuthorizationEventEntity( was watching, and dating it now would put the wrong time on it. """ + _has_bearings: bool = False _session_number: int | None = None @override async def async_added_to_hass(self) -> None: - """Take note of the session already running, without reporting it.""" + """Take note of where the charger is, without reporting it.""" await super().async_added_to_hass() + self._take_bearings() + + @callback + def _take_bearings(self) -> None: + """Note the session the charger is on, and report nothing for it. + + A read that only lands after this entity started says nothing about + when its session began, so the first one to come back is a starting + point rather than news. Reading nothing is different: the charger + answered and had nothing to report, and whatever comes next is new. + """ + if not self.coordinator.last_update_success: + return + + self._has_bearings = True if (authorization := self.coordinator.data) is not None: self._session_number = authorization.session_number @@ -72,7 +88,9 @@ class PeblarAuthorizationEventEntity( def _handle_coordinator_update(self) -> None: """Report a session that was not there the last time we looked.""" authorization = self.coordinator.data - if ( + if not self._has_bearings: + self._take_bearings() + elif ( authorization is not None and authorization.session_number != self._session_number ): diff --git a/tests/components/peblar/test_event.py b/tests/components/peblar/test_event.py index d274afdb1366..f472ea144e78 100644 --- a/tests/components/peblar/test_event.py +++ b/tests/components/peblar/test_event.py @@ -4,11 +4,12 @@ from datetime import timedelta from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from peblar import PeblarMeterHistory +from peblar import PeblarError, PeblarMeterHistory import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.const import STATE_UNKNOWN, Platform +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.util import dt as dt_util @@ -204,3 +205,89 @@ async def test_nothing_to_report( async def test_no_reader_no_entity(hass: HomeAssistant) -> None: """Test a charger without a reader shows nobody in.""" assert hass.states.get(ENTITY_ID) is None + + +async def test_a_charger_without_meter_history( + hass: HomeAssistant, + mock_peblar: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test a charger that cannot answer for its meter history still loads. + + Firmware older than the statistics endpoint answers it with a 404. + Reading back who was shown in is one entity, and it must not take the + rest of the integration down with it. The entity says it has nothing + rather than going missing: a read can fail for a moment too, and the + poll that follows is free to put it right. + """ + mock_peblar.meter_history.side_effect = PeblarError("Not Found") + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.LOADED + state = hass.states.get(ENTITY_ID) + assert state + assert state.state == STATE_UNAVAILABLE + + +async def test_a_read_that_failed_once_recovers( + hass: HomeAssistant, + mock_peblar: MagicMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test a read that failed at startup is not held against the entity. + + The charger answers three other coordinators right before this one, so + a failure here is usually the endpoint missing. It can just as well be + a moment of bad luck on the heaviest request of the lot, and the next + poll should be allowed to settle it. + + What that poll finds is the session already running, which says nothing + about when it began. It is taken as a starting point rather than + reported, the same as a session already running at startup. + """ + mock_peblar.meter_history.side_effect = PeblarError("Blip") + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE + + mock_peblar.meter_history.side_effect = None + await _async_poll(hass, freezer) + + assert hass.states.get(ENTITY_ID).state == STATE_UNKNOWN + + +async def test_a_new_session_after_a_failed_read_is_reported( + hass: HomeAssistant, + mock_peblar: MagicMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test taking bearings does not swallow the sessions that follow. + + The first read back only sets the starting point. A card shown after + that is news like any other. + """ + mock_peblar.meter_history.side_effect = PeblarError("Blip") + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + mock_peblar.meter_history.side_effect = None + await _async_poll(hass, freezer) + + mock_peblar.meter_history.return_value = _history(99, "1D0A0B0C0D0E03") + await _async_poll(hass, freezer) + + state = hass.states.get(ENTITY_ID) + assert state + assert state.state != STATE_UNKNOWN + assert state.attributes["session_number"] == 99 + assert state.attributes["token"] == "Frenck"