Skip to content

Commit

Permalink
Merge branch 'master' into fix-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius authored Dec 13, 2024
2 parents b8fffa0 + d819fc2 commit 8b75d11
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
rev: v0.8.2
hooks:
- id: ruff
args:
Expand Down
30 changes: 25 additions & 5 deletions custom_components/bbox/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@

_LOGGER = logging.getLogger(__name__)

BUTTON_RESTART: tuple[ButtonEntityDescription, ...] = ButtonEntityDescription(
key="restart", name="Restart", icon="mdi:restart-alert"
)


async def async_setup_entry(
hass: HomeAssistant, entry: BBoxConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up sensor."""
coordinator = entry.runtime_data
entities = [RestartButton(coordinator, BUTTON_RESTART)]
entities = [
RestartButton(
coordinator,
ButtonEntityDescription(
key="restart", name="Restart", icon="mdi:restart-alert"
),
),
RefreshButton(
coordinator,
ButtonEntityDescription(
key="refresh", name="Refresh", icon="mdi:refresh-circle"
),
),
]
async_add_entities(entities)


Expand All @@ -36,3 +45,14 @@ async def async_press(self) -> None:
await self.coordinator.bbox.device.async_reboot()
except BboxException as error:
_LOGGER.error(error)


class RefreshButton(BboxEntity, ButtonEntity):
"""Representation of a button for refreshing integration data."""

async def async_press(self) -> None:
"""Handle the button press."""
try:
await self.coordinator.async_request_refresh()
except BboxException as error:
_LOGGER.error(error)
32 changes: 10 additions & 22 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,36 +46,26 @@ async def async_step_user(
)
await api.async_login()
infos = await api.device.async_get_bbox_summary()

try:
sn = infos[0]["device"]["serialnumber"]
assert sn is not None, "Null Bbox serial number retrieved"
except (IndexError, KeyError, AssertionError) as err:
raise CannotConnect("Serial number of device not found") from err

if (
len(infos) > 0
and (sn := infos[0].get("device", {}).get("serialnumber")) is None
):
raise HttpRequestError("Serial number of device not found") from err
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."""
6 changes: 3 additions & 3 deletions custom_components/bbox/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import DOMAIN, TO_REDACT
from .const import TO_REDACT


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

_datas = {}

Expand Down Expand Up @@ -46,6 +46,6 @@ async def diag(func: Callable[..., Any], *args: Any) -> None:
"data": async_redact_data(entry.data, TO_REDACT),
"options": async_redact_data(entry.options, TO_REDACT),
},
"data": async_redact_data(hass.data[DOMAIN][entry.entry_id].data, TO_REDACT),
"data": async_redact_data(coordinator.data, TO_REDACT),
"raw": async_redact_data(_datas, TO_REDACT),
}

0 comments on commit 8b75d11

Please sign in to comment.