Skip to content

Commit

Permalink
Only fetch wanted IgdState items in IGD profile (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman authored May 20, 2024
1 parent 3ba3793 commit cd4d884
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
69 changes: 60 additions & 9 deletions async_upnp_client/profiles/igd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import asyncio
import logging
from datetime import datetime, timedelta
from enum import Enum
from ipaddress import IPv4Address
from typing import List, NamedTuple, Optional, Sequence, Union, cast
from typing import AbstractSet, List, NamedTuple, Optional, Sequence, Union, cast

from async_upnp_client.client import UpnpAction, UpnpDevice
from async_upnp_client.event_handler import UpnpEventHandler
Expand Down Expand Up @@ -93,7 +94,7 @@ class IgdState(NamedTuple):
packets_received: Union[None, BaseException, int]
packets_sent: Union[None, BaseException, int]
status_info: Union[None, BaseException, StatusInfo]
external_ip_address: Union[str, BaseException, None]
external_ip_address: Union[None, BaseException, str]

# Derived values.
kibibytes_per_sec_received: Union[None, float]
Expand All @@ -102,6 +103,27 @@ class IgdState(NamedTuple):
packets_per_sec_sent: Union[None, float]


class IgdStateItem(Enum):
"""
IGD state item.
Used to specify what to request from the device.
"""

BYTES_RECEIVED = 1
BYTES_SENT = 2
PACKETS_RECEIVED = 3
PACKETS_SENT = 4

STATUS_INFO = 5
EXTERNAL_IP_ADDRESS = 6

KIBIBYTES_PER_SEC_RECEIVED = 11
KIBIBYTES_PER_SEC_SENT = 12
PACKETS_PER_SEC_RECEIVED = 13
PACKETS_PER_SEC_SENT = 14


def _derive_value_per_second(
value_name: str,
current_timestamp: datetime,
Expand Down Expand Up @@ -614,7 +636,7 @@ async def async_set_default_connection_service(self, service: str) -> None:
await action.async_call(NewDefaultConnectionService=service)

async def async_get_traffic_and_status_data(
self,
self, items: Optional[AbstractSet[IgdStateItem]] = None
) -> IgdState:
"""
Get all traffic data at once, including derived data.
Expand All @@ -632,14 +654,42 @@ async def async_get_traffic_and_status_data(
* connection status
* uptime
"""
items = items or {
IgdStateItem.BYTES_RECEIVED,
IgdStateItem.BYTES_SENT,
IgdStateItem.PACKETS_RECEIVED,
IgdStateItem.PACKETS_SENT,
IgdStateItem.STATUS_INFO,
IgdStateItem.EXTERNAL_IP_ADDRESS,
}

async def nop() -> None:
"""Pass."""

timestamp = datetime.now()
values = await asyncio.gather(
self.async_get_total_bytes_received(),
self.async_get_total_bytes_sent(),
self.async_get_total_packets_received(),
self.async_get_total_packets_sent(),
self.async_get_status_info(),
self.async_get_external_ip_address(),
self.async_get_total_bytes_received()
if IgdStateItem.BYTES_RECEIVED in items
or IgdStateItem.KIBIBYTES_PER_SEC_RECEIVED in items
else nop(),
self.async_get_total_bytes_sent()
if IgdStateItem.BYTES_SENT in items
or IgdStateItem.KIBIBYTES_PER_SEC_SENT in items
else nop(),
self.async_get_total_packets_received()
if IgdStateItem.PACKETS_RECEIVED in items
or IgdStateItem.PACKETS_PER_SEC_RECEIVED in items
else nop(),
self.async_get_total_packets_sent()
if IgdStateItem.PACKETS_SENT in items
or IgdStateItem.PACKETS_PER_SEC_SENT in items
else nop(),
self.async_get_status_info()
if IgdStateItem.STATUS_INFO in items
else nop(),
self.async_get_external_ip_address()
if IgdStateItem.EXTERNAL_IP_ADDRESS in items
else nop(),
return_exceptions=True,
)

Expand Down Expand Up @@ -684,6 +734,7 @@ async def async_get_traffic_and_status_data(
packets_sent_original=values[3],
)

# Test if any of the calls were ok. If not, raise the exception.
non_exceptions = [
value for value in values if not isinstance(value, BaseException)
]
Expand Down
1 change: 1 addition & 0 deletions changes/227.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only fetch wanted IgdState items in IGD profile.

0 comments on commit cd4d884

Please sign in to comment.