Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fetch wanted IgdState items in IGD profile #227

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
* connection status
* uptime
"""
items = items or {

Check warning on line 657 in async_upnp_client/profiles/igd.py

View check run for this annotation

Codecov / codecov/patch

async_upnp_client/profiles/igd.py#L657

Added line #L657 was not covered by tests
IgdStateItem.BYTES_RECEIVED,
IgdStateItem.BYTES_SENT,
IgdStateItem.PACKETS_RECEIVED,
IgdStateItem.PACKETS_SENT,
IgdStateItem.STATUS_INFO,
IgdStateItem.EXTERNAL_IP_ADDRESS,
}

async def nop() -> None:

Check warning on line 666 in async_upnp_client/profiles/igd.py

View check run for this annotation

Codecov / codecov/patch

async_upnp_client/profiles/igd.py#L666

Added line #L666 was not covered by tests
"""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 @@
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.
Loading