Skip to content

Commit

Permalink
Added download and upload rate attributes for devices and the WAN
Browse files Browse the repository at this point in the history
  • Loading branch information
vmakeev committed Jan 7, 2024
1 parent 1aa4879 commit 1857550
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
6 changes: 6 additions & 0 deletions custom_components/huawei_mesh_router/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get_past_moment,
)
from .update_coordinator import HuaweiDataUpdateCoordinator
from .utils import get_readable_rate

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,4 +160,9 @@ def _handle_coordinator_update(self) -> None:
self._attr_extra_state_attributes["connected_at"] = get_past_moment(
wan_info.uptime
)
self._attr_extra_state_attributes["upload_rate_kilobytes_s"] = wan_info.upload_rate
self._attr_extra_state_attributes["download_rate_kilobytes_s"] = wan_info.download_rate
self._attr_extra_state_attributes["upload_rate"] = get_readable_rate(wan_info.upload_rate)
self._attr_extra_state_attributes["download_rate"] = get_readable_rate(wan_info.download_rate)

super()._handle_coordinator_update()
14 changes: 14 additions & 0 deletions custom_components/huawei_mesh_router/client/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

MAC_ADDR: TypeAlias = str

KILOBYTES_PER_SECOND: TypeAlias = int


# ---------------------------
# Feature
Expand Down Expand Up @@ -207,6 +209,8 @@ class HuaweiConnectionInfo:
uptime: int
connected: bool
address: str | None
upload_rate: KILOBYTES_PER_SECOND
download_rate: KILOBYTES_PER_SECOND


# ---------------------------
Expand Down Expand Up @@ -277,6 +281,16 @@ def interface_type(self) -> str | None:
"""Return the connection interface type."""
return self._data.get("InterfaceType")

@property
def upload_rate(self) -> KILOBYTES_PER_SECOND:
"""Return the upload rate in kilobytes per second."""
return self._data.get("UpRate", 0)

@property
def download_rate(self) -> KILOBYTES_PER_SECOND:
"""Return the download rate in kilobytes per second."""
return self._data.get("DownRate", 0)


# ---------------------------
# HuaweiClientDevice
Expand Down
1 change: 1 addition & 0 deletions custom_components/huawei_mesh_router/client/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
URL_WLAN_FILTER: Final = "api/ntwk/wlanfilterenhance"
URL_URL_FILTER: Final = "api/ntwk/urlfilter"
URL_GUEST_NETWORK: Final = "api/ntwk/guest_network?type=notshowpassall"
URL_WAN_INFO : Final = "api/ntwk/wan?type=active"
4 changes: 4 additions & 0 deletions custom_components/huawei_mesh_router/client/huaweiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
URL_URL_FILTER,
URL_WANDETECT,
URL_WLAN_FILTER,
URL_WAN_INFO,
WIFI_SECURITY_ENCRYPTED,
WIFI_SECURITY_OPEN,
)
Expand Down Expand Up @@ -157,11 +158,14 @@ async def get_wan_connection_info(self) -> HuaweiConnectionInfo:
data = await self._core_api.get(
URL_WANDETECT, check_authorized=HuaweiApi._wan_info_check_authorized
)
rate_data = await self._core_api.get(URL_WAN_INFO)

return HuaweiConnectionInfo(
uptime=data.get("Uptime", 0),
connected=data.get("Status") == _STATUS_CONNECTED,
address=data.get("ExternalIPAddress"),
upload_rate=rate_data.get("UpBandwidth", 0),
download_rate=rate_data.get("DownBandwidth", 0)
)

async def get_switch_state(self, switch: Switch) -> bool:
Expand Down
10 changes: 6 additions & 4 deletions custom_components/huawei_mesh_router/update_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from .client.huaweiapi import HuaweiApi
from .const import ATTR_MANUFACTURER, DOMAIN
from .options import HuaweiIntegrationOptions
from .utils import HuaweiChangesWatcher, TagsMap, ZonesMap, _TItem, _TKey
from .utils import HuaweiChangesWatcher, TagsMap, ZonesMap, _TItem, _TKey, get_readable_rate

_PRIMARY_ROUTER_IDENTITY: Final = "primary_router"

Expand Down Expand Up @@ -905,9 +905,11 @@ def get_mesh_routers(
is_hilink=device_data.is_hilink,
is_router=device_data.is_router,
connected_via_id=connected_via.get("id"),
zone=ZoneInfo(name=zone_name, entity_id=zone_id)
if zone_id
else None,
zone=ZoneInfo(name=zone_name, entity_id=zone_id) if zone_id else None,
upload_rate_kilobytes_s=device_data.upload_rate,
download_rate_kilobytes_s=device_data.download_rate,
upload_rate=get_readable_rate(device_data.upload_rate),
download_rate=get_readable_rate(device_data.download_rate),
)
else:
device.update_device_data(name, host_name, False, tags, filter_mode)
Expand Down
19 changes: 18 additions & 1 deletion custom_components/huawei_mesh_router/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.helpers.storage import Store

from .classes import DEVICE_TAG
from .client.classes import MAC_ADDR
from .client.classes import MAC_ADDR, KILOBYTES_PER_SECOND

_TKey = TypeVar("_TKey")
_TItem = TypeVar("_TItem")
Expand Down Expand Up @@ -159,3 +159,20 @@ async def set_zone_id(self, device_id: str, zone_id: str | None) -> None:

self._devices_to_zones[device_id] = zone_id
await self._storage.async_save(self._devices_to_zones)


def get_readable_rate(
rate: KILOBYTES_PER_SECOND
) -> str:
unit: str = "Kbps"
value = rate * 8

if value > 1024:
unit = "Mbps"
value = round(value / 1024, 1)

if value > 1024:
unit = "Gbps"
value = round(value / 1024, 1)

return f"{value} {unit}"

0 comments on commit 1857550

Please sign in to comment.