diff --git a/custom_components/ecocito/client.py b/custom_components/ecocito/client.py index 8658016..d4084a0 100644 --- a/custom_components/ecocito/client.py +++ b/custom_components/ecocito/client.py @@ -14,6 +14,9 @@ from .const import ( ECOCITO_COLLECTION_ENDPOINT, ECOCITO_COLLECTION_TYPE_ENDPOINT, ECOCITO_DEFAULT_COLLECTION_TYPE, + ECOCITO_ERROR_AUTHENTICATION, + ECOCITO_ERROR_FETCHING, + ECOCITO_ERROR_UNHANDLED, ECOCITO_GARBAGE_COLLECTION_TYPE, ECOCITO_LOGIN_ENDPOINT, ECOCITO_LOGIN_PASSWORD_KEY, @@ -78,7 +81,7 @@ class EcocitoClient: raise InvalidAuthenticationError(error[0].find("li").text) LOGGER.debug("Connected as %s", self._username) except aiohttp.ClientError as e: - raise EcocitoError(f"Authentication error: {e}") from e + raise EcocitoError(ECOCITO_ERROR_AUTHENTICATION.format(exc=e)) from e async def get_collection_types(self) -> dict: """Return the mapping of collection type ID with their label.""" @@ -100,7 +103,9 @@ class EcocitoClient: except Exception as e: # noqa: BLE001 await self._handle_error(content, e) except aiohttp.ClientError as e: - raise EcocitoError(f"Unable to get collection types: {e}") from e + raise EcocitoError( + ECOCITO_ERROR_FETCHING.format(exc=e, type="collection types") + ) from e async def get_collection_events( self, event_type: str, year: int @@ -138,7 +143,9 @@ class EcocitoClient: await self._handle_error(content, e) except aiohttp.ClientError as e: - raise EcocitoError(f"Unable to get collection events: {e}") from e + raise EcocitoError( + ECOCITO_ERROR_FETCHING.format(exc=e, type="collection events") + ) from e async def get_garbage_collections(self, year: int) -> list[CollectionEvent]: """Return the list of the garbage collections for a year.""" @@ -179,10 +186,12 @@ class EcocitoClient: await self._handle_error(content, e) except aiohttp.ClientError as e: - raise EcocitoError(f"Unable to get waste deposit visits: {e}") from e + raise EcocitoError( + ECOCITO_ERROR_FETCHING.format(exc=e, type="waste deposit events") + ) from e - async def _handle_error(self, content: str, e: Exception): - """Handle a request error by checking for login form and re-authenticating if necessary.""" + async def _handle_error(self, content: str, e: Exception) -> None: + """Handle request errors by checking for login form and re-auth if necessary.""" html = bs(content, "html.parser") form = html.find("form", action=re.compile(f"{ECOCITO_LOGIN_URI}")) @@ -190,4 +199,4 @@ class EcocitoClient: LOGGER.debug("The session has expired, try to login again.") await self.authenticate() else: - raise EcocitoError("Unhandled request error") from e + raise EcocitoError(ECOCITO_ERROR_UNHANDLED) from e diff --git a/custom_components/ecocito/const.py b/custom_components/ecocito/const.py index 096ea5f..2ccf4f1 100644 --- a/custom_components/ecocito/const.py +++ b/custom_components/ecocito/const.py @@ -8,18 +8,15 @@ LOGGER = logging.getLogger(__package__) # Config Flow # Service Device - DEVICE_ATTRIBUTION = "Données fournies par Ecocito" DEVICE_NAME = "Ecocito" DEVICE_MANUFACTURER = "Ecocito" DEVICE_MODEL = "Calendrier Ecocito" # Ecocito - Base - ECOCITO_DOMAIN = "{}.ecocito.com" # Ecocito - Login - ECOCITO_LOGIN_URI = "/Usager/Profil/Connexion" ECOCITO_LOGIN_ENDPOINT = f"https://{ECOCITO_DOMAIN}{ECOCITO_LOGIN_URI}" ECOCITO_LOGIN_USERNAME_KEY = "Identifiant" @@ -36,3 +33,12 @@ ECOCITO_COLLECTION_TYPE_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Collecte" # Ecocito - Waste deposit visits ECOCITO_WASTE_DEPOSIT_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Apport/GetApport" + +# Ecocito - Errors +ECOCITO_ERROR_AUTHENTICATION = "Authentication error: {exc}" +ECOCITO_ERROR_FETCHING = "Unable to get {type}: {exc}" +ECOCITO_ERROR_UNHANDLED = "Unhandled request error" + +ECOCITO_MESSAGE_REAUTHENTICATE = ( + "Credentials are no longer valid. Please reauthenticate" +) diff --git a/custom_components/ecocito/coordinator.py b/custom_components/ecocito/coordinator.py index 9a23e0f..18dd62f 100644 --- a/custom_components/ecocito/coordinator.py +++ b/custom_components/ecocito/coordinator.py @@ -13,7 +13,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .client import CollectionEvent, EcocitoClient, WasteDepotVisit -from .const import DOMAIN, LOGGER +from .const import DOMAIN, ECOCITO_MESSAGE_REAUTHENTICATE, LOGGER from .errors import CannotConnectError, InvalidAuthenticationError T = TypeVar("T", bound=list[CollectionEvent] | list[WasteDepotVisit]) @@ -46,9 +46,7 @@ class EcocitoDataUpdateCoordinator(DataUpdateCoordinator[T], Generic[T], ABC): except CannotConnectError as ex: raise UpdateFailed(ex) from ex except InvalidAuthenticationError as ex: - raise ConfigEntryAuthFailed( - "Credentials are no longer valid. Please reauthenticate" - ) from ex + raise ConfigEntryAuthFailed(ECOCITO_MESSAGE_REAUTHENTICATE) from ex @abstractmethod async def _fetch_data(self) -> T: