Skip to content

Commit

Permalink
Bump to 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed Aug 24, 2023
1 parent c54f600 commit 3383dcb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion custom_components/bbox2/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BboxBinarySensor(BboxEntity, BinarySensorEntity):
def is_on(self):
"""Return sensor state."""
_LOGGER.debug("%s %s", self.name, self.entity_description.key)
data = finditem(self.coordinator.data, self.entity_description)
data = finditem(self.coordinator.data, self.entity_description.key)
if self.entity_description.value_fn is not None:
return self.entity_description.value_fn(data)
return data
8 changes: 6 additions & 2 deletions custom_components/bbox2/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ async def diag(func: Callable[..., Any], *args: Any) -> None:
else vars(rsp)
)

_datas[0].update({func.__name__: rslt})
_datas.update({func.__name__: rslt})

await diag(coordinator.bbox.device.async_get_bbox_info)
await diag(coordinator.bbox.lan.async_get_connected_devices)
await diag(coordinator.bbox.wan.async_get_wan_ip_stats)
await diag(coordinator.bbox.wan.async_get_wan_ip)
await diag(coordinator.bbox.iptv.async_get_iptv_info)
await diag(coordinator.bbox.lan.async_get_lan_stats)
await diag(coordinator.bbox.lan.async_get_voip_voicemail)
await diag(coordinator.bbox.lan.async_get_device_infos)

return {
"entry": {
Expand Down
13 changes: 10 additions & 3 deletions custom_components/bbox2/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from .const import BBOX_NAME, DOMAIN, MANUFACTURER, CONF_HOST
from .coordinator import BboxDataUpdateCoordinator
from .helpers import finditem

_LOGGER = logging.getLogger(__name__)

Expand All @@ -20,15 +21,21 @@ class BboxEntity(CoordinatorEntity[BboxDataUpdateCoordinator], Entity):
def __init__(self, coordinator: BboxDataUpdateCoordinator, description) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
device = coordinator.data.get("info", {}).get("device", {})
device = finditem(coordinator.data, "info.device")
self.box_id = device.get("serialnumber", "ABC12345")
self._attr_unique_id = f"{self.box_id}-{description.key}"
self.entity_description = description
self._attr_device_info = {
"identifiers": {(DOMAIN, self.box_id)},
# "connections": {
# (
# CONNECTION_NETWORK_MAC,
# finditem(coordinator.data, "device_info.hosts.macaddress"),
# )
# },
"manufacturer": MANUFACTURER,
"name": BBOX_NAME,
"model": device.get("modelname"),
"sw_version": device.get("main", {}).get("version"),
"configuration_url": f'https://{coordinator.config_entry.data[CONF_HOST]}',
"configuration_url": f"https://{coordinator.config_entry.data[CONF_HOST]}",
}
12 changes: 9 additions & 3 deletions custom_components/bbox2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.helpers.typing import StateType
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.components.binary_sensor import BinarySensorEntityDescription
from typing import Any


@dataclass
Expand All @@ -24,9 +25,14 @@ class BboxBinarySensorDescription(BboxValueFnMixin, BinarySensorEntityDescriptio
"""Describes a sensor."""


def finditem(data, entity_description):
if (keys := entity_description.key.split(".")) and isinstance(keys, list):
def finditem(data: dict[str, Any], key_chain: str, default: Any = None) -> Any:
"""Get recursive key and return value.
data is a mandatory dictonnary
key , string with dot for key delimited (ex: "key.key.key")
"""
if (keys := key_chain.split(".")) and isinstance(keys, list):
for key in keys:
if isinstance(data, dict):
data = data.get(key)
return data
return default if data is None and default is not None else data
2 changes: 1 addition & 1 deletion custom_components/bbox2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"loggers": ["bboxpy"],
"requirements": ["bboxpy==1.1.5"],
"ssdp": [],
"version": "1.1.0",
"version": "1.2.1",
"zeroconf": []
}
2 changes: 1 addition & 1 deletion custom_components/bbox2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BboxSensor(BboxEntity, SensorEntity):
@property
def native_value(self):
"""Return sensor state."""
data = finditem(self.coordinator.data, self.entity_description)
data = finditem(self.coordinator.data, self.entity_description.key)
if self.entity_description.value_fn is not None:
return self.entity_description.value_fn(data)
return data

0 comments on commit 3383dcb

Please sign in to comment.