Skip to content

Commit

Permalink
Add exception bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed Dec 13, 2024
1 parent e947551 commit d819fc2
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions custom_components/bbox/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import logging
from typing import Any

from bboxpy import Bbox
from bboxpy.exceptions import BboxException, HttpRequestError
from bboxpy import AuthorizationError, Bbox, BboxException, HttpRequestError
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .const import BBOX_URL, CONF_HOST, CONF_PASSWORD, CONF_USE_TLS, DOMAIN
Expand Down Expand Up @@ -48,32 +46,27 @@ async def async_step_user(
)
await api.async_login()
infos = await api.device.async_get_bbox_summary()
if sn := infos.get("device", {}).get("serialnumber") is None:
raise CannotConnect("Serial number of device not found")
if (
len(infos) > 0
and (sn := infos[0].get("device", {}).get("serialnumber")) is None
):
raise HttpRequestError("Serial number of device not found")

await self.async_set_unique_id(sn)
self._abort_if_unique_id_configured()

except (HttpRequestError, CannotConnect) as err:
except HttpRequestError as err:
_LOGGER.warning("Can not to connect at Bbox: %s", err)
errors["base"] = "cannot_connect"
except InvalidAuth as err:
except AuthorizationError as err:
_LOGGER.warning("Fail to authenticate to the Bbox: %s", err)
errors["base"] = "invalid_auth"
except BboxException as err:
_LOGGER.exception("Unknown error connecting to the Bbox: %s", err)
except BboxException:
_LOGGER.exception("Unknown error connecting to the Bbox")
errors["base"] = "unknown"
else:
return self.async_create_entry(title="Bouygues Bbox", data=user_input)

return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)


class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""


class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""

0 comments on commit d819fc2

Please sign in to comment.