Also handle DiscovergyClientError as UpdateFailed (#100038)
* Also handle DiscovergyClientError as UpdateFailed * Change AccessTokenExpired to InvalidLogin * Also add DiscovergyClientError to config flow and tests
This commit is contained in:
parent
20d0ebe3fa
commit
a4cb06d09f
3 changed files with 24 additions and 5 deletions
|
@ -85,7 +85,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
httpx_client=get_async_client(self.hass),
|
httpx_client=get_async_client(self.hass),
|
||||||
authentication=BasicAuth(),
|
authentication=BasicAuth(),
|
||||||
).meters()
|
).meters()
|
||||||
except discovergyError.HTTPError:
|
except (discovergyError.HTTPError, discovergyError.DiscovergyClientError):
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except discovergyError.InvalidLogin:
|
except discovergyError.InvalidLogin:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
|
|
|
@ -5,7 +5,7 @@ from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pydiscovergy import Discovergy
|
from pydiscovergy import Discovergy
|
||||||
from pydiscovergy.error import AccessTokenExpired, HTTPError
|
from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
|
||||||
from pydiscovergy.models import Meter, Reading
|
from pydiscovergy.models import Meter, Reading
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -44,11 +44,11 @@ class DiscovergyUpdateCoordinator(DataUpdateCoordinator[Reading]):
|
||||||
"""Get last reading for meter."""
|
"""Get last reading for meter."""
|
||||||
try:
|
try:
|
||||||
return await self.discovergy_client.meter_last_reading(self.meter.meter_id)
|
return await self.discovergy_client.meter_last_reading(self.meter.meter_id)
|
||||||
except AccessTokenExpired as err:
|
except InvalidLogin as err:
|
||||||
raise ConfigEntryAuthFailed(
|
raise ConfigEntryAuthFailed(
|
||||||
f"Auth expired while fetching last reading for meter {self.meter.meter_id}"
|
f"Auth expired while fetching last reading for meter {self.meter.meter_id}"
|
||||||
) from err
|
) from err
|
||||||
except HTTPError as err:
|
except (HTTPError, DiscovergyClientError) as err:
|
||||||
raise UpdateFailed(
|
raise UpdateFailed(
|
||||||
f"Error while fetching last reading for meter {self.meter.meter_id}"
|
f"Error while fetching last reading for meter {self.meter.meter_id}"
|
||||||
) from err
|
) from err
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""Test the Discovergy config flow."""
|
"""Test the Discovergy config flow."""
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from pydiscovergy.error import HTTPError, InvalidLogin
|
from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.discovergy.const import DOMAIN
|
from homeassistant.components.discovergy.const import DOMAIN
|
||||||
|
@ -114,6 +114,25 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
assert result2["errors"] == {"base": "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_form_client_error(hass: HomeAssistant) -> None:
|
||||||
|
"""Test we handle cannot connect error."""
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("pydiscovergy.Discovergy.meters", side_effect=DiscovergyClientError):
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_EMAIL: "test@example.com",
|
||||||
|
CONF_PASSWORD: "test-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
|
assert result2["errors"] == {"base": "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
|
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
|
Loading…
Add table
Reference in a new issue