From e9d4e1b1e7fd3f3f42ffdda728f6a7ba08756a09 Mon Sep 17 00:00:00 2001 From: rhammen <75572839+rhammen@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:10:04 +0100 Subject: [PATCH] Fail setup when no installations or chargers are found (fixes #443) Co-Authored-By: Claude Opus 5.5 --- custom_components/zaptec/__init__.py | 9 +++ custom_components/zaptec/manager.py | 3 - custom_components/zaptec/translations/de.json | 5 ++ custom_components/zaptec/translations/en.json | 5 ++ custom_components/zaptec/translations/fr.json | 5 ++ custom_components/zaptec/translations/nb.json | 5 ++ custom_components/zaptec/translations/nl.json | 5 ++ custom_components/zaptec/translations/nn.json | 5 ++ custom_components/zaptec/translations/pl.json | 5 ++ custom_components/zaptec/translations/sv.json | 5 ++ tests/test_manager.py | 55 ++++++++++++++++++- 11 files changed, 103 insertions(+), 4 deletions(-) diff --git a/custom_components/zaptec/__init__.py b/custom_components/zaptec/__init__.py index acb5e111..90df8e4b 100644 --- a/custom_components/zaptec/__init__.py +++ b/custom_components/zaptec/__init__.py @@ -15,6 +15,7 @@ CONF_CHARGERS, CONF_MANUAL_SELECT, CONF_PREFIX, + DOMAIN, REDACT_DUMP_ON_STARTUP, REDACT_LOGS, ZAPTEC_POLL_INTERVAL_BUILD, @@ -113,6 +114,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: zaptec=zaptec, configured_chargers=configured_chargers, ) + if not tracked_devices: + msg = "No Zaptec installations or chargers found for this account or selection" + raise ConfigEntryError( + msg, + translation_domain=DOMAIN, + translation_key="no_devices_found", + translation_placeholders={"username": entry.data[CONF_USERNAME]}, + ) # Setup the manager which will be where all the instance data is stored manager = ZaptecManager( diff --git a/custom_components/zaptec/manager.py b/custom_components/zaptec/manager.py index ef9a5a71..c996a45c 100644 --- a/custom_components/zaptec/manager.py +++ b/custom_components/zaptec/manager.py @@ -333,9 +333,6 @@ async def first_time_setup(zaptec: Zaptec, configured_chargers: set[str] | None) if charger.installation: keep.add(charger.installation.id) - if not keep: - _LOGGER.error("No zaptec objects will be added") - # These objects will be updated by the coordinator tracked_devices = keep diff --git a/custom_components/zaptec/translations/de.json b/custom_components/zaptec/translations/de.json index 69bd2f26..d9cfd3e3 100644 --- a/custom_components/zaptec/translations/de.json +++ b/custom_components/zaptec/translations/de.json @@ -205,5 +205,10 @@ "name": "Firmware-Update" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Keine Zaptec-Installationen oder -Ladegeräte für das Konto {username} gefunden. Prüfe über Neu konfigurieren, ob dies das richtige Zaptec-Konto ist und ob die ausgewählten Ladegeräte noch existieren." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/en.json b/custom_components/zaptec/translations/en.json index 5233ce1d..fde733c5 100644 --- a/custom_components/zaptec/translations/en.json +++ b/custom_components/zaptec/translations/en.json @@ -205,5 +205,10 @@ "name": "Firmware update" } } + }, + "exceptions": { + "no_devices_found": { + "message": "No Zaptec installations or chargers found for account {username}. Check that this is the right Zaptec account and that the selected chargers still exist, using Reconfigure." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/fr.json b/custom_components/zaptec/translations/fr.json index d7390187..0d1ed26d 100644 --- a/custom_components/zaptec/translations/fr.json +++ b/custom_components/zaptec/translations/fr.json @@ -205,5 +205,10 @@ "name": "Mise à jour du micrologiciel" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Aucune installation ni borne Zaptec trouvée pour le compte {username}. Vérifiez via Reconfigurer qu'il s'agit du bon compte Zaptec et que les bornes sélectionnées existent toujours." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/nb.json b/custom_components/zaptec/translations/nb.json index f7a8acc6..ad9c2c81 100644 --- a/custom_components/zaptec/translations/nb.json +++ b/custom_components/zaptec/translations/nb.json @@ -205,5 +205,10 @@ "name": "Fastvareoppdatering" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Fant ingen Zaptec-installasjoner eller -ladere for kontoen {username}. Bruk Rekonfigurer for å sjekke at dette er riktig Zaptec-konto og at de valgte laderne fortsatt finnes." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/nl.json b/custom_components/zaptec/translations/nl.json index 26befadd..48ddf794 100644 --- a/custom_components/zaptec/translations/nl.json +++ b/custom_components/zaptec/translations/nl.json @@ -205,5 +205,10 @@ "name": "Firmware" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Geen Zaptec-installaties of -laders gevonden voor account {username}. Controleer via Opnieuw configureren of dit het juiste Zaptec-account is en of de geselecteerde laders nog bestaan." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/nn.json b/custom_components/zaptec/translations/nn.json index 7f71efd5..fb4a2e31 100644 --- a/custom_components/zaptec/translations/nn.json +++ b/custom_components/zaptec/translations/nn.json @@ -205,5 +205,10 @@ "name": "Fastvareoppdatering" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Fann ingen Zaptec-installasjonar eller -ladarar for kontoen {username}. Bruk Rekonfigurer for å sjekke at dette er rett Zaptec-konto og at dei valde ladarane framleis finst." + } } } diff --git a/custom_components/zaptec/translations/pl.json b/custom_components/zaptec/translations/pl.json index 427ced5b..c501ba05 100644 --- a/custom_components/zaptec/translations/pl.json +++ b/custom_components/zaptec/translations/pl.json @@ -205,5 +205,10 @@ "name": "Aktualizacja oprogramowania" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Nie znaleziono instalacji ani ładowarek Zaptec dla konta {username}. Sprawdź za pomocą Zmień konfigurację, czy to właściwe konto Zaptec i czy wybrane ładowarki nadal istnieją." + } } } \ No newline at end of file diff --git a/custom_components/zaptec/translations/sv.json b/custom_components/zaptec/translations/sv.json index a05c09d4..200f66a7 100644 --- a/custom_components/zaptec/translations/sv.json +++ b/custom_components/zaptec/translations/sv.json @@ -205,5 +205,10 @@ "name": "Uppdatera mjukvara" } } + }, + "exceptions": { + "no_devices_found": { + "message": "Inga Zaptec-installationer eller laddare hittades för kontot {username}. Kontrollera via Konfigurera om att detta är rätt Zaptec-konto och att de valda laddarna fortfarande finns." + } } } diff --git a/tests/test_manager.py b/tests/test_manager.py index a2f61600..014d6bfd 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -3,7 +3,7 @@ from __future__ import annotations import asyncio -from collections.abc import Callable +from collections.abc import Callable, Iterator import logging from types import SimpleNamespace from typing import Any @@ -351,3 +351,56 @@ async def stream_main(*, on_connect: Callable[[], None], **kwargs: object) -> No warnings = [r for r in caplog.records if r.levelno == logging.WARNING] assert len(warnings) == 1 # the second failure never connected, so it is the same outage + + +class FakeZaptec: + """Stands in for the Zaptec account hierarchy built by first_time_setup.""" + + def __init__(self, charger_ids: list[str], installation_id: str | None = None) -> None: + """Initialize the hierarchy.""" + installation = SimpleNamespace(id=installation_id) if installation_id else None + self.chargers = [ + SimpleNamespace(id=cid, installation=installation) for cid in charger_ids + ] + self.build = AsyncMock() + self._ids = set(charger_ids) | ({installation_id} if installation_id else set()) + + def __iter__(self) -> Iterator[str]: + """Iterate over all object ids.""" + return iter(self._ids) + + +@pytest.mark.asyncio +async def test_first_time_setup_without_chargers_tracks_nothing() -> None: + """An account that logs in but sees no devices tracks nothing.""" + assert await ZaptecManager.first_time_setup(FakeZaptec([]), configured_chargers=None) == set() + + +@pytest.mark.asyncio +async def test_first_time_setup_without_selected_chargers_tracks_nothing() -> None: + """Manually selected chargers that are all gone track nothing.""" + tracked = await ZaptecManager.first_time_setup( + FakeZaptec(["other"], installation_id="inst"), configured_chargers={CHARGER_ID} + ) + + assert tracked == set() + + +@pytest.mark.asyncio +async def test_first_time_setup_tracks_charger_and_installation() -> None: + """A selected charger is tracked together with its installation.""" + tracked = await ZaptecManager.first_time_setup( + FakeZaptec([CHARGER_ID], installation_id="inst"), configured_chargers={CHARGER_ID} + ) + + assert tracked == {CHARGER_ID, "inst"} + + +@pytest.mark.asyncio +async def test_first_time_setup_tracks_installation_without_chargers() -> None: + """An installation without chargers is still set up.""" + tracked = await ZaptecManager.first_time_setup( + FakeZaptec([], installation_id="inst"), configured_chargers=None + ) + + assert tracked == {"inst"}