Add exception translations for pyLoad integration (#120520)

This commit is contained in:
Mr. Bubbles 2024-06-26 11:00:31 +02:00 committed by GitHub
parent e06d7050f2
commit 45dedf73c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 5 deletions

View File

@ -20,6 +20,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.aiohttp_client import async_create_clientsession
from .const import DOMAIN
from .coordinator import PyLoadCoordinator from .coordinator import PyLoadCoordinator
PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR, Platform.SWITCH] PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
@ -51,13 +52,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: PyLoadConfigEntry) -> bo
await pyloadapi.login() await pyloadapi.login()
except CannotConnect as e: except CannotConnect as e:
raise ConfigEntryNotReady( raise ConfigEntryNotReady(
"Unable to connect and retrieve data from pyLoad API" translation_domain=DOMAIN,
translation_key="setup_request_exception",
) from e ) from e
except ParserError as e: except ParserError as e:
raise ConfigEntryNotReady("Unable to parse data from pyLoad API") from e raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="setup_parse_exception",
) from e
except InvalidAuth as e: except InvalidAuth as e:
raise ConfigEntryAuthFailed( raise ConfigEntryAuthFailed(
f"Authentication failed for {entry.data[CONF_USERNAME]}, check your login credentials" translation_domain=DOMAIN,
translation_key="setup_authentication_exception",
translation_placeholders={CONF_USERNAME: entry.data[CONF_USERNAME]},
) from e ) from e
coordinator = PyLoadCoordinator(hass, pyloadapi) coordinator = PyLoadCoordinator(hass, pyloadapi)

View File

@ -7,6 +7,7 @@ import logging
from pyloadapi import CannotConnect, InvalidAuth, ParserError, PyLoadAPI from pyloadapi import CannotConnect, InvalidAuth, ParserError, PyLoadAPI
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -64,7 +65,9 @@ class PyLoadCoordinator(DataUpdateCoordinator[pyLoadData]):
await self.pyload.login() await self.pyload.login()
except InvalidAuth as exc: except InvalidAuth as exc:
raise ConfigEntryAuthFailed( raise ConfigEntryAuthFailed(
f"Authentication failed for {self.pyload.username}, check your login credentials", translation_domain=DOMAIN,
translation_key="setup_authentication_exception",
translation_placeholders={CONF_USERNAME: self.pyload.username},
) from exc ) from exc
raise UpdateFailed( raise UpdateFailed(

View File

@ -89,6 +89,17 @@
} }
} }
}, },
"exceptions": {
"setup_request_exception": {
"message": "Unable to connect and retrieve data from pyLoad API, try again later"
},
"setup_parse_exception": {
"message": "Unable to parse data from pyLoad API, try again later"
},
"setup_authentication_exception": {
"message": "Authentication failed for {username}, verify your login credentials"
}
},
"issues": { "issues": {
"deprecated_yaml_import_issue_cannot_connect": { "deprecated_yaml_import_issue_cannot_connect": {
"title": "The pyLoad YAML configuration import failed", "title": "The pyLoad YAML configuration import failed",

View File

@ -96,7 +96,7 @@ async def test_sensor_invalid_auth(
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
"Authentication failed for username, check your login credentials" "Authentication failed for username, verify your login credentials"
in caplog.text in caplog.text
) )