Skip to content

Commit

Permalink
Bump sfrbox-api to 0.0.10 (#125405)
Browse files Browse the repository at this point in the history
* bump sfr_box requirement to 0.0.10

* upate manifest file

* Handle None values

---------

Co-authored-by: epenet <[email protected]>
  • Loading branch information
2 people authored and balloob committed Sep 6, 2024
1 parent 973e43a commit a3f42e3
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 40 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/sfr_box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import asyncio
from typing import TYPE_CHECKING

from sfrbox_api.bridge import SFRBox
from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError
Expand Down Expand Up @@ -46,6 +47,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Preload system information
await data.system.async_config_entry_first_refresh()
system_info = data.system.data
if TYPE_CHECKING:
assert system_info is not None

# Preload other coordinators (based on net infrastructure)
tasks = [data.wan.async_config_entry_first_refresh()]
Expand Down
14 changes: 10 additions & 4 deletions homeassistant/components/sfr_box/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from collections.abc import Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING

from sfrbox_api.models import DslInfo, FtthInfo, SystemInfo, WanInfo

Expand Down Expand Up @@ -65,19 +66,22 @@ async def async_setup_entry(
) -> None:
"""Set up the sensors."""
data: DomainData = hass.data[DOMAIN][entry.entry_id]
system_info = data.system.data
if TYPE_CHECKING:
assert system_info is not None

entities: list[SFRBoxBinarySensor] = [
SFRBoxBinarySensor(data.wan, description, data.system.data)
SFRBoxBinarySensor(data.wan, description, system_info)
for description in WAN_SENSOR_TYPES
]
if (net_infra := data.system.data.net_infra) == "adsl":
if (net_infra := system_info.net_infra) == "adsl":
entities.extend(
SFRBoxBinarySensor(data.dsl, description, data.system.data)
SFRBoxBinarySensor(data.dsl, description, system_info)
for description in DSL_SENSOR_TYPES
)
elif net_infra == "ftth":
entities.extend(
SFRBoxBinarySensor(data.ftth, description, data.system.data)
SFRBoxBinarySensor(data.ftth, description, system_info)
for description in FTTH_SENSOR_TYPES
)

Expand Down Expand Up @@ -111,4 +115,6 @@ def __init__(
@property
def is_on(self) -> bool | None:
"""Return the native value of the device."""
if self.coordinator.data is None:
return None
return self.entity_description.value_fn(self.coordinator.data)
8 changes: 5 additions & 3 deletions homeassistant/components/sfr_box/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import dataclass
from functools import wraps
from typing import Any, Concatenate
from typing import TYPE_CHECKING, Any, Concatenate

from sfrbox_api.bridge import SFRBox
from sfrbox_api.exceptions import SFRBoxError
Expand Down Expand Up @@ -69,10 +69,12 @@ async def async_setup_entry(
) -> None:
"""Set up the buttons."""
data: DomainData = hass.data[DOMAIN][entry.entry_id]
system_info = data.system.data
if TYPE_CHECKING:
assert system_info is not None

entities = [
SFRBoxButton(data.box, description, data.system.data)
for description in BUTTON_TYPES
SFRBoxButton(data.box, description, system_info) for description in BUTTON_TYPES
]
async_add_entities(entities)

Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/sfr_box/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import Any
from typing import TYPE_CHECKING, Any

from sfrbox_api.bridge import SFRBox
from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError
Expand Down Expand Up @@ -51,6 +51,8 @@ async def async_step_user(
except SFRBoxError:
errors["base"] = "cannot_connect"
else:
if TYPE_CHECKING:
assert system_info is not None
await self.async_set_unique_id(system_info.mac_addr)
self._abort_if_unique_id_configured()
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/sfr_box/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
_SCAN_INTERVAL = timedelta(minutes=1)


class SFRDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
class SFRDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT | None]):
"""Coordinator to manage data updates."""

def __init__(
self,
hass: HomeAssistant,
box: SFRBox,
name: str,
method: Callable[[SFRBox], Coroutine[Any, Any, _DataT]],
method: Callable[[SFRBox], Coroutine[Any, Any, _DataT | None]],
) -> None:
"""Initialize coordinator."""
self.box = box
self._method = method
super().__init__(hass, _LOGGER, name=name, update_interval=_SCAN_INTERVAL)

async def _async_update_data(self) -> _DataT:
async def _async_update_data(self) -> _DataT | None:
"""Update data."""
try:
return await self._method(self.box)
Expand Down
31 changes: 14 additions & 17 deletions homeassistant/components/sfr_box/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import dataclasses
from typing import Any
from typing import TYPE_CHECKING, Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
Expand All @@ -12,9 +12,18 @@
from .const import DOMAIN
from .models import DomainData

if TYPE_CHECKING:
from _typeshed import DataclassInstance

TO_REDACT = {"mac_addr", "serial_number", "ip_addr", "ipv6_addr"}


def _async_redact_data(obj: DataclassInstance | None) -> dict[str, Any] | None:
if obj is None:
return None
return async_redact_data(dataclasses.asdict(obj), TO_REDACT)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]:
Expand All @@ -27,21 +36,9 @@ async def async_get_config_entry_diagnostics(
"data": dict(entry.data),
},
"data": {
"dsl": async_redact_data(
dataclasses.asdict(await data.system.box.dsl_get_info()),
TO_REDACT,
),
"ftth": async_redact_data(
dataclasses.asdict(await data.system.box.ftth_get_info()),
TO_REDACT,
),
"system": async_redact_data(
dataclasses.asdict(await data.system.box.system_get_info()),
TO_REDACT,
),
"wan": async_redact_data(
dataclasses.asdict(await data.system.box.wan_get_info()),
TO_REDACT,
),
"dsl": _async_redact_data(await data.system.box.dsl_get_info()),
"ftth": _async_redact_data(await data.system.box.ftth_get_info()),
"system": _async_redact_data(await data.system.box.system_get_info()),
"wan": _async_redact_data(await data.system.box.wan_get_info()),
},
}
2 changes: 1 addition & 1 deletion homeassistant/components/sfr_box/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/sfr_box",
"integration_type": "device",
"iot_class": "local_polling",
"requirements": ["sfrbox-api==0.0.8"]
"requirements": ["sfrbox-api==0.0.10"]
}
32 changes: 25 additions & 7 deletions homeassistant/components/sfr_box/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections.abc import Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING

from sfrbox_api.models import DslInfo, SystemInfo, WanInfo

Expand Down Expand Up @@ -129,7 +130,7 @@ class SFRBoxSensorEntityDescription[_T](SensorEntityDescription):
"unknown",
],
translation_key="dsl_line_status",
value_fn=lambda x: x.line_status.lower().replace(" ", "_"),
value_fn=lambda x: _value_to_option(x.line_status),
),
SFRBoxSensorEntityDescription[DslInfo](
key="training",
Expand All @@ -149,7 +150,7 @@ class SFRBoxSensorEntityDescription[_T](SensorEntityDescription):
"unknown",
],
translation_key="dsl_training",
value_fn=lambda x: x.training.lower().replace(" ", "_").replace(".", "_"),
value_fn=lambda x: _value_to_option(x.training),
),
)
SYSTEM_SENSOR_TYPES: tuple[SFRBoxSensorEntityDescription[SystemInfo], ...] = (
Expand Down Expand Up @@ -181,7 +182,7 @@ class SFRBoxSensorEntityDescription[_T](SensorEntityDescription):
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
value_fn=lambda x: None if x.temperature is None else x.temperature / 1000,
value_fn=lambda x: _get_temperature(x.temperature),
),
)
WAN_SENSOR_TYPES: tuple[SFRBoxSensorEntityDescription[WanInfo], ...] = (
Expand All @@ -203,23 +204,38 @@ class SFRBoxSensorEntityDescription[_T](SensorEntityDescription):
)


def _value_to_option(value: str | None) -> str | None:
if value is None:
return value
return value.lower().replace(" ", "_").replace(".", "_")


def _get_temperature(value: float | None) -> float | None:
if value is None or value < 1000:
return value
return value / 1000


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the sensors."""
data: DomainData = hass.data[DOMAIN][entry.entry_id]
system_info = data.system.data
if TYPE_CHECKING:
assert system_info is not None

entities: list[SFRBoxSensor] = [
SFRBoxSensor(data.system, description, data.system.data)
SFRBoxSensor(data.system, description, system_info)
for description in SYSTEM_SENSOR_TYPES
]
entities.extend(
SFRBoxSensor(data.wan, description, data.system.data)
SFRBoxSensor(data.wan, description, system_info)
for description in WAN_SENSOR_TYPES
)
if data.system.data.net_infra == "adsl":
if system_info.net_infra == "adsl":
entities.extend(
SFRBoxSensor(data.dsl, description, data.system.data)
SFRBoxSensor(data.dsl, description, system_info)
for description in DSL_SENSOR_TYPES
)

Expand Down Expand Up @@ -251,4 +267,6 @@ def __init__(
@property
def native_value(self) -> StateType:
"""Return the native value of the device."""
if self.coordinator.data is None:
return None
return self.entity_description.value_fn(self.coordinator.data)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ sensorpush-ble==1.6.2
sentry-sdk==1.40.3

# homeassistant.components.sfr_box
sfrbox-api==0.0.8
sfrbox-api==0.0.10

# homeassistant.components.sharkiq
sharkiq==1.0.2
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ sensorpush-ble==1.6.2
sentry-sdk==1.40.3

# homeassistant.components.sfr_box
sfrbox-api==0.0.8
sfrbox-api==0.0.10

# homeassistant.components.sharkiq
sharkiq==1.0.2
Expand Down
4 changes: 2 additions & 2 deletions tests/components/sfr_box/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'product_id': 'NB6VAC-FXC-r0',
'refclient': '',
'serial_number': '**REDACTED**',
'temperature': 27560,
'temperature': 27560.0,
'uptime': 2353575,
'version_bootloader': 'NB6VAC-BOOTLOADER-R4.0.8',
'version_dsldriver': 'NB6VAC-XDSL-A2pv6F039p',
Expand Down Expand Up @@ -90,7 +90,7 @@
'product_id': 'NB6VAC-FXC-r0',
'refclient': '',
'serial_number': '**REDACTED**',
'temperature': 27560,
'temperature': 27560.0,
'uptime': 2353575,
'version_bootloader': 'NB6VAC-BOOTLOADER-R4.0.8',
'version_dsldriver': 'NB6VAC-XDSL-A2pv6F039p',
Expand Down

0 comments on commit a3f42e3

Please sign in to comment.