File: igd.py

package info (click to toggle)
async-upnp-client 0.45.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,076 kB
  • sloc: python: 11,946; xml: 2,826; sh: 32; makefile: 6
file content (986 lines) | stat: -rw-r--r-- 33,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# -*- coding: utf-8 -*-
"""async_upnp_client.profiles.igd module."""

import asyncio
import logging
from datetime import datetime, timedelta
from enum import Enum
from ipaddress import IPv4Address, IPv6Address
from typing import List, NamedTuple, Optional, Sequence, Set, Union, cast

from async_upnp_client.client import UpnpAction, UpnpDevice, UpnpStateVariable
from async_upnp_client.event_handler import UpnpEventHandler
from async_upnp_client.profiles.profile import UpnpProfileDevice

TIMESTAMP = "timestamp"
BYTES_RECEIVED = "bytes_received"
BYTES_SENT = "bytes_sent"
PACKETS_RECEIVED = "packets_received"
PACKETS_SENT = "packets_sent"
KIBIBYTES_PER_SEC_RECEIVED = "kibytes_sec_received"
KIBIBYTES_PER_SEC_SENT = "kibytes_sec_sent"
PACKETS_SEC_RECEIVED = "packets_sec_received"
PACKETS_SEC_SENT = "packets_sec_sent"
STATUS_INFO = "status_info"
EXTERNAL_IP_ADDRESS = "external_ip_address"

_LOGGER = logging.getLogger(__name__)


class CommonLinkProperties(NamedTuple):
    """Common link properties."""

    wan_access_type: str
    layer1_upstream_max_bit_rate: int
    layer1_downstream_max_bit_rate: int
    physical_link_status: str


class ConnectionTypeInfo(NamedTuple):
    """Connection type info."""

    connection_type: str
    possible_connection_types: str


class StatusInfo(NamedTuple):
    """Status info."""

    connection_status: str
    last_connection_error: str
    uptime: int


class NatRsipStatusInfo(NamedTuple):
    """NAT RSIP status info."""

    nat_enabled: bool
    rsip_available: bool


class PortMappingEntry(NamedTuple):
    """Port mapping entry."""

    remote_host: Optional[IPv4Address]
    external_port: int
    protocol: str
    internal_port: int
    internal_client: IPv4Address
    enabled: bool
    description: str
    lease_duration: Optional[timedelta]


class FirewallStatus(NamedTuple):
    """IPv6 Firewall status."""

    firewall_enabled: bool
    inbound_pinhole_allowed: bool


class Pinhole(NamedTuple):
    """IPv6 Pinhole."""

    remote_host: str
    remote_port: int
    internal_client: str
    internal_port: int
    protocol: int
    lease_time: int


class TrafficCounterState(NamedTuple):
    """Traffic state."""

    timestamp: datetime
    bytes_received: Union[None, BaseException, int]
    bytes_sent: Union[None, BaseException, int]
    packets_received: Union[None, BaseException, int]
    packets_sent: Union[None, BaseException, int]
    bytes_received_original: Union[None, BaseException, int]
    bytes_sent_original: Union[None, BaseException, int]
    packets_received_original: Union[None, BaseException, int]
    packets_sent_original: Union[None, BaseException, int]


class IgdState(NamedTuple):
    """IGD state."""

    timestamp: datetime
    bytes_received: Union[None, BaseException, int]
    bytes_sent: Union[None, BaseException, int]
    packets_received: Union[None, BaseException, int]
    packets_sent: Union[None, BaseException, int]
    connection_status: Union[None, BaseException, str]
    last_connection_error: Union[None, BaseException, str]
    uptime: Union[None, BaseException, int]
    external_ip_address: Union[None, BaseException, str]
    port_mapping_number_of_entries: Union[None, BaseException, int]

    # Derived values.
    kibibytes_per_sec_received: Union[None, float]
    kibibytes_per_sec_sent: Union[None, float]
    packets_per_sec_received: Union[None, float]
    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
    CONNECTION_STATUS = 5
    LAST_CONNECTION_ERROR = 6
    UPTIME = 7
    EXTERNAL_IP_ADDRESS = 8
    PORT_MAPPING_NUMBER_OF_ENTRIES = 9

    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,
    current_value: Union[None, BaseException, StatusInfo, int, str],
    last_timestamp: Union[None, BaseException, datetime],
    last_value: Union[None, BaseException, StatusInfo, int, str],
) -> Union[None, float]:
    """Calculate average based on current and last value."""
    if (
        not isinstance(current_timestamp, datetime)
        or not isinstance(current_value, int)
        or not isinstance(last_timestamp, datetime)
        or not isinstance(last_value, int)
    ):
        return None

    if last_value > current_value:
        # Value has overflowed, don't try to calculate anything.
        return None

    delta_time = current_timestamp - last_timestamp
    delta_value: Union[int, float] = current_value - last_value
    if value_name in (BYTES_RECEIVED, BYTES_SENT):
        delta_value = delta_value / 1024  # 1KB
    return delta_value / delta_time.total_seconds()


class IgdDevice(UpnpProfileDevice):
    """Representation of a IGD device."""

    # pylint: disable=too-many-public-methods

    DEVICE_TYPES = [
        "urn:schemas-upnp-org:device:InternetGatewayDevice:1",
        "urn:schemas-upnp-org:device:InternetGatewayDevice:2",
    ]

    _SERVICE_TYPES = {
        "WANIP6FC": {
            "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1",
        },
        "WANPPPC": {
            "urn:schemas-upnp-org:service:WANPPPConnection:1",
        },
        "WANIPC": {
            "urn:schemas-upnp-org:service:WANIPConnection:1",
            "urn:schemas-upnp-org:service:WANIPConnection:2",
        },
        "WANCIC": {
            "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
        },
        "L3FWD": {
            "urn:schemas-upnp-org:service:Layer3Forwarding:1",
        },
    }

    def __init__(
        self, device: UpnpDevice, event_handler: Optional[UpnpEventHandler]
    ) -> None:
        """Initialize."""
        super().__init__(device, event_handler)

        self._last_traffic_state = TrafficCounterState(
            timestamp=datetime.now(),
            bytes_received=None,
            bytes_sent=None,
            packets_received=None,
            packets_sent=None,
            bytes_received_original=None,
            bytes_sent_original=None,
            packets_received_original=None,
            packets_sent_original=None,
        )
        self._offset_bytes_received = 0
        self._offset_bytes_sent = 0
        self._offset_packets_received = 0
        self._offset_packets_sent = 0

    def _any_action(
        self, service_names: Sequence[str], action_name: str
    ) -> Optional[UpnpAction]:
        for service_name in service_names:
            action = self._action(service_name, action_name)
            if action is not None:
                return action

        _LOGGER.debug("Could not find action %s/%s", service_names, action_name)
        return None

    def _any_state_variable(
        self, service_names: Sequence[str], variable_name: str
    ) -> Optional[UpnpStateVariable]:
        for service_name in service_names:
            state_var = self._state_variable(service_name, variable_name)
            if state_var is not None:
                return state_var

        _LOGGER.debug(
            "Could not find state variable %s/%s", service_names, variable_name
        )
        return None

    @property
    def external_ip_address(self) -> Optional[str]:
        """
        Get the external IP address, from the state variable ExternalIPAddress.

        This requires a subscription to the WANIPC/WANPPPC service.
        """
        services = ["WANIPC", "WANPPPC"]
        state_var = self._any_state_variable(services, "ExternalIPAddress")
        if not state_var:
            return None

        external_ip_address: Optional[str] = state_var.value
        return external_ip_address

    @property
    def connection_status(self) -> Optional[str]:
        """
        Get the connection status, from the state variable ConnectionStatus.

        This requires a subscription to the WANIPC/WANPPPC service.
        """
        services = ["WANIPC", "WANPPPC"]
        state_var = self._any_state_variable(services, "ConnectionStatus")
        if not state_var:
            return None

        connection_status: Optional[str] = state_var.value
        return connection_status

    @property
    def port_mapping_number_of_entries(self) -> Optional[int]:
        """
        Get number of port mapping entries, from the state variable `PortMappingNumberOfEntries`.

        This requires a subscription to the WANIPC/WANPPPC service.
        """
        services = ["WANIPC", "WANPPPC"]
        state_var = self._any_state_variable(services, "PortMappingNumberOfEntries")
        if not state_var:
            return None

        number_of_entries: Optional[int] = state_var.value
        return number_of_entries

    async def async_get_total_bytes_received(self) -> Optional[int]:
        """Get total bytes received."""
        action = self._action("WANCIC", "GetTotalBytesReceived")
        if not action:
            return None

        result = await action.async_call()
        total_bytes_received: Optional[int] = result.get("NewTotalBytesReceived")

        if total_bytes_received is None:
            return None

        if total_bytes_received < 0:
            self._offset_bytes_received = 2**31

        return total_bytes_received + self._offset_bytes_received

    async def async_get_total_bytes_sent(self) -> Optional[int]:
        """Get total bytes sent."""
        action = self._action("WANCIC", "GetTotalBytesSent")
        if not action:
            return None

        result = await action.async_call()
        total_bytes_sent: Optional[int] = result.get("NewTotalBytesSent")

        if total_bytes_sent is None:
            return None

        if total_bytes_sent < 0:
            self._offset_bytes_sent = 2**31

        return total_bytes_sent + self._offset_bytes_sent

    async def async_get_total_packets_received(self) -> Optional[int]:
        """Get total packets received."""
        action = self._action("WANCIC", "GetTotalPacketsReceived")
        if not action:
            return None

        result = await action.async_call()
        total_packets_received: Optional[int] = result.get("NewTotalPacketsReceived")

        if total_packets_received is None:
            return None

        if total_packets_received < 0:
            self._offset_packets_received = 2**31

        return total_packets_received + self._offset_packets_received

    async def async_get_total_packets_sent(self) -> Optional[int]:
        """Get total packets sent."""
        action = self._action("WANCIC", "GetTotalPacketsSent")
        if not action:
            return None

        result = await action.async_call()
        total_packets_sent: Optional[int] = result.get("NewTotalPacketsSent")

        if total_packets_sent is None:
            return None

        if total_packets_sent < 0:
            self._offset_packets_sent = 2**31

        return total_packets_sent + self._offset_packets_sent

    async def async_get_enabled_for_internet(self) -> Optional[bool]:
        """Get internet access enabled state."""
        action = self._action("WANCIC", "GetEnabledForInternet")
        if not action:
            return None

        result = await action.async_call()
        enabled_for_internet: Optional[bool] = result.get("NewEnabledForInternet")
        return enabled_for_internet

    async def async_set_enabled_for_internet(self, enabled: bool) -> None:
        """
        Set internet access enabled state.

        :param enabled whether access should be enabled
        """
        action = self._action("WANCIC", "SetEnabledForInternet")
        if not action:
            return

        await action.async_call(NewEnabledForInternet=enabled)

    async def async_get_common_link_properties(self) -> Optional[CommonLinkProperties]:
        """Get common link properties."""
        action = self._action("WANCIC", "GetCommonLinkProperties")
        if not action:
            return None

        result = await action.async_call()
        return CommonLinkProperties(
            result["NewWANAccessType"],
            int(result["NewLayer1UpstreamMaxBitRate"]),
            int(result["NewLayer1DownstreamMaxBitRate"]),
            result["NewPhysicalLinkStatus"],
        )

    async def async_get_external_ip_address(
        self, services: Optional[Sequence[str]] = None
    ) -> Optional[str]:
        """
        Get the external IP address.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetExternalIPAddress")
        if not action:
            return None

        result = await action.async_call()
        external_ip_address: Optional[str] = result.get("NewExternalIPAddress")
        return external_ip_address

    async def async_get_generic_port_mapping_entry(
        self, port_mapping_index: int, services: Optional[List[str]] = None
    ) -> Optional[PortMappingEntry]:
        """
        Get generic port mapping entry.

        :param port_mapping_index Index of port mapping entry
        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetGenericPortMappingEntry")
        if not action:
            return None

        result = await action.async_call(NewPortMappingIndex=port_mapping_index)
        return PortMappingEntry(
            (
                IPv4Address(result["NewRemoteHost"])
                if result.get("NewRemoteHost")
                else None
            ),
            result["NewExternalPort"],
            result["NewProtocol"],
            result["NewInternalPort"],
            IPv4Address(result["NewInternalClient"]),
            result["NewEnabled"],
            result["NewPortMappingDescription"],
            (
                timedelta(seconds=result["NewLeaseDuration"])
                if result.get("NewLeaseDuration")
                else None
            ),
        )

    async def async_get_specific_port_mapping_entry(
        self,
        remote_host: Optional[IPv4Address],
        external_port: int,
        protocol: str,
        services: Optional[List[str]] = None,
    ) -> Optional[PortMappingEntry]:
        """
        Get specific port mapping entry.

        :param remote_host Address of remote host or None
        :param external_port External port
        :param protocol Protocol, 'TCP' or 'UDP'
        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetSpecificPortMappingEntry")
        if not action:
            return None

        result = await action.async_call(
            NewRemoteHost=remote_host.exploded if remote_host else "",
            NewExternalPort=external_port,
            NewProtocol=protocol,
        )
        return PortMappingEntry(
            remote_host,
            external_port,
            protocol,
            result["NewInternalPort"],
            IPv4Address(result["NewInternalClient"]),
            result["NewEnabled"],
            result["NewPortMappingDescription"],
            (
                timedelta(seconds=result["NewLeaseDuration"])
                if result.get("NewLeaseDuration")
                else None
            ),
        )

    async def async_add_port_mapping(
        self,
        remote_host: IPv4Address,
        external_port: int,
        protocol: str,
        internal_port: int,
        internal_client: IPv4Address,
        enabled: bool,
        description: str,
        lease_duration: timedelta,
        services: Optional[List[str]] = None,
    ) -> None:
        """
        Add a port mapping.

        :param remote_host Address of remote host or None
        :param external_port External port
        :param protocol Protocol, 'TCP' or 'UDP'
        :param internal_port Internal port
        :param internal_client Address of internal host
        :param enabled Port mapping enabled
        :param description Description for port mapping
        :param lease_duration Lease duration
        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        # pylint: disable=too-many-arguments,too-many-positional-arguments
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "AddPortMapping")
        if not action:
            return

        await action.async_call(
            NewRemoteHost=remote_host.exploded,
            NewExternalPort=external_port,
            NewProtocol=protocol,
            NewInternalPort=internal_port,
            NewInternalClient=internal_client.exploded,
            NewEnabled=enabled,
            NewPortMappingDescription=description,
            NewLeaseDuration=int(lease_duration.seconds),
        )

    async def async_delete_port_mapping(
        self,
        remote_host: IPv4Address,
        external_port: int,
        protocol: str,
        services: Optional[List[str]] = None,
    ) -> None:
        """
        Delete an existing port mapping.

        :param remote_host Address of remote host or None
        :param external_port External port
        :param protocol Protocol, 'TCP' or 'UDP'
        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "DeletePortMapping")
        if not action:
            return

        await action.async_call(
            NewRemoteHost=remote_host.exploded,
            NewExternalPort=external_port,
            NewProtocol=protocol,
        )

    async def async_get_firewall_status(self) -> Optional[FirewallStatus]:
        """Get (IPv6) firewall status."""
        action = self._action("WANIP6FC", "GetFirewallStatus")
        if not action:
            return None

        result = await action.async_call()
        return FirewallStatus(
            result["FirewallEnabled"],
            result["InboundPinholeAllowed"],
        )

    async def async_add_pinhole(
        self,
        remote_host: IPv6Address,
        remote_port: int,
        internal_client: IPv6Address,
        internal_port: int,
        protocol: int,
        lease_time: timedelta,
    ) -> Optional[int]:
        """Add a pinhole."""
        # pylint: disable=too-many-arguments,too-many-positional-arguments
        action = self._action("WANIP6FC", "AddPinhole")
        if not action:
            return None

        result = await action.async_call(
            RemoteHost=str(remote_host),
            RemotePort=remote_port,
            InternalClient=str(internal_client),
            InternalPort=internal_port,
            Protocol=protocol,
            LeaseTime=int(lease_time.total_seconds()),
        )
        return cast(int, result["UniqueID"])

    async def async_update_pinhole(self, pinhole_id: int, new_lease_time: int) -> None:
        """Update pinhole."""
        action = self._action("WANIP6FC", "UpdatePinhole")
        if not action:
            return

        await action.async_call(
            UniqueID=pinhole_id,
            NewLeaseTime=new_lease_time,
        )

    async def async_delete_pinhole(self, pinhole_id: int) -> None:
        """Delete an existing pinhole."""
        action = self._action("WANIP6FC", "DeletePinhole")
        if not action:
            return

        await action.async_call(
            UniqueID=pinhole_id,
        )

    async def async_get_pinhole_packets(self, pinhole_id: int) -> Optional[int]:
        """Get pinhole packet count."""
        action = self._action("WANIP6FC", "GetPinholePackets")
        if not action:
            return None

        result = await action.async_call(
            UniqueID=pinhole_id,
        )
        return cast(int, result["PinholePackets"])

    async def async_get_connection_type_info(
        self, services: Optional[Sequence[str]] = None
    ) -> Optional[ConnectionTypeInfo]:
        """
        Get connection type info.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetConnectionTypeInfo")
        if not action:
            return None

        result = await action.async_call()
        return ConnectionTypeInfo(
            result["NewConnectionType"], result["NewPossibleConnectionTypes"]
        )

    async def async_set_connection_type(
        self, connection_type: str, services: Optional[List[str]] = None
    ) -> None:
        """
        Set connection type.

        :param connection_type connection type
        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "SetConnectionType")
        if not action:
            return

        await action.async_call(NewConnectionType=connection_type)

    async def async_request_connection(
        self, services: Optional[Sequence[str]] = None
    ) -> None:
        """
        Request connection.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "RequestConnection")
        if not action:
            return

        await action.async_call()

    async def async_request_termination(
        self, services: Optional[Sequence[str]] = None
    ) -> None:
        """
        Request connection termination.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "RequestTermination")
        if not action:
            return

        await action.async_call()

    async def async_force_termination(
        self, services: Optional[Sequence[str]] = None
    ) -> None:
        """
        Force connection termination.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "ForceTermination")
        if not action:
            return

        await action.async_call()

    async def async_get_status_info(
        self, services: Optional[Sequence[str]] = None
    ) -> Optional[StatusInfo]:
        """
        Get status info.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetStatusInfo")
        if not action:
            return None

        try:
            result = await action.async_call()
        except ValueError:
            _LOGGER.debug("Caught ValueError parsing results")
            return None

        return StatusInfo(
            result["NewConnectionStatus"],
            result["NewLastConnectionError"],
            result["NewUptime"],
        )

    async def async_get_port_mapping_number_of_entries(
        self, services: Optional[Sequence[str]] = None
    ) -> Optional[int]:
        """
        Get number of port mapping entries.

        Note that this action is not officially supported by the IGD specification.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetPortMappingNumberOfEntries")
        if not action:
            return None

        result = await action.async_call()
        number_of_entries: Optional[str] = result.get(
            "NewPortMappingNumberOfEntries"
        )  # str?
        if number_of_entries is None:
            return None
        return int(number_of_entries)

    async def async_get_nat_rsip_status(
        self, services: Optional[Sequence[str]] = None
    ) -> Optional[NatRsipStatusInfo]:
        """
        Get NAT enabled and RSIP availability statuses.

        :param services List of service names to try to get action from,
                        defaults to [WANIPC,WANPPPC]
        """
        services = services or ["WANIPC", "WANPPPC"]
        action = self._any_action(services, "GetNATRSIPStatus")
        if not action:
            return None

        result = await action.async_call()
        return NatRsipStatusInfo(result["NewNATEnabled"], result["NewRSIPAvailable"])

    async def async_get_default_connection_service(self) -> Optional[str]:
        """Get default connection service."""
        action = self._action("L3FWD", "GetDefaultConnectionService")
        if not action:
            return None

        result = await action.async_call()
        default_connection_service: Optional[str] = result.get(
            "NewDefaultConnectionService"
        )
        return default_connection_service

    async def async_set_default_connection_service(self, service: str) -> None:
        """
        Set default connection service.

        :param service default connection service
        """
        action = self._action("L3FWD", "SetDefaultConnectionService")
        if not action:
            return

        await action.async_call(NewDefaultConnectionService=service)

    async def async_get_traffic_and_status_data(
        self,
        items: Optional[Set[IgdStateItem]] = None,
        force_poll: bool = False,
    ) -> IgdState:
        """
        Get all traffic data at once, including derived data.

        Data:
        * total bytes received
        * total bytes sent
        * total packets received
        * total packets sent
        * bytes per second received (derived from last update)
        * bytes per second sent (derived from last update)
        * packets per second received (derived from last update)
        * packets per second sent (derived from last update)
        * connection status (status info)
        * last connection error (status info)
        * uptime (status info)
        * external IP address
        * number of port mapping entries
        """
        # pylint: disable=too-many-locals
        items = items or set(IgdStateItem)

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

        external_ip_address: Optional[str] = None
        connection_status: Optional[str] = None
        port_mapping_number_of_entries: Optional[int] = None
        if not force_poll:
            if (
                IgdStateItem.EXTERNAL_IP_ADDRESS in items
                and (external_ip_address := self.external_ip_address) is not None
            ):
                items.remove(IgdStateItem.EXTERNAL_IP_ADDRESS)

            if (
                IgdStateItem.CONNECTION_STATUS in items
                and (connection_status := self.connection_status) is not None
            ):
                items.remove(IgdStateItem.CONNECTION_STATUS)

            if (
                IgdStateItem.PORT_MAPPING_NUMBER_OF_ENTRIES in items
                and (
                    port_mapping_number_of_entries := self.port_mapping_number_of_entries
                )
                is not None
            ):
                items.remove(IgdStateItem.PORT_MAPPING_NUMBER_OF_ENTRIES)

        timestamp = datetime.now()
        values = await asyncio.gather(
            (
                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.CONNECTION_STATUS in items
                or IgdStateItem.LAST_CONNECTION_ERROR in items
                or IgdStateItem.UPTIME in items
                else nop()
            ),
            (
                self.async_get_external_ip_address()
                if IgdStateItem.EXTERNAL_IP_ADDRESS in items
                else nop()
            ),
            (
                self.async_get_port_mapping_number_of_entries()
                if IgdStateItem.PORT_MAPPING_NUMBER_OF_ENTRIES in items
                else nop()
            ),
            return_exceptions=True,
        )

        kibibytes_per_sec_received = _derive_value_per_second(
            BYTES_RECEIVED,
            timestamp,
            values[0],
            self._last_traffic_state.timestamp,
            self._last_traffic_state.bytes_received,
        )
        kibibytes_per_sec_sent = _derive_value_per_second(
            BYTES_SENT,
            timestamp,
            values[1],
            self._last_traffic_state.timestamp,
            self._last_traffic_state.bytes_sent,
        )
        packets_per_sec_received = _derive_value_per_second(
            PACKETS_RECEIVED,
            timestamp,
            values[2],
            self._last_traffic_state.timestamp,
            self._last_traffic_state.packets_received,
        )
        packets_per_sec_sent = _derive_value_per_second(
            PACKETS_SENT,
            timestamp,
            values[3],
            self._last_traffic_state.timestamp,
            self._last_traffic_state.packets_sent,
        )

        self._last_traffic_state = TrafficCounterState(
            timestamp=timestamp,
            bytes_received=cast(Union[int, BaseException, None], values[0]),
            bytes_sent=cast(Union[int, BaseException, None], values[1]),
            packets_received=cast(Union[int, BaseException, None], values[2]),
            packets_sent=cast(Union[int, BaseException, None], values[3]),
            bytes_received_original=cast(Union[int, BaseException, None], values[0]),
            bytes_sent_original=cast(Union[int, BaseException, None], values[1]),
            packets_received_original=cast(Union[int, BaseException, None], values[2]),
            packets_sent_original=cast(Union[int, BaseException, None], 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)
        ]
        if not non_exceptions:
            # Raise any exception to indicate something was very wrong.
            exc = cast(BaseException, values[0])
            raise exc

        return IgdState(
            timestamp=timestamp,
            bytes_received=cast(Union[None, BaseException, int], values[0]),
            bytes_sent=cast(Union[None, BaseException, int], values[1]),
            packets_received=cast(Union[None, BaseException, int], values[2]),
            packets_sent=cast(Union[None, BaseException, int], values[3]),
            kibibytes_per_sec_received=kibibytes_per_sec_received,
            kibibytes_per_sec_sent=kibibytes_per_sec_sent,
            packets_per_sec_received=packets_per_sec_received,
            packets_per_sec_sent=packets_per_sec_sent,
            connection_status=(
                values[4].connection_status
                if isinstance(values[4], StatusInfo)
                else connection_status
            ),
            last_connection_error=(
                values[4].last_connection_error
                if isinstance(values[4], StatusInfo)
                else None
            ),
            uptime=values[4].uptime if isinstance(values[4], StatusInfo) else None,
            external_ip_address=cast(
                Union[None, BaseException, str], values[5] or external_ip_address
            ),
            port_mapping_number_of_entries=cast(
                Union[None, int], values[6] or port_mapping_number_of_entries
            ),
        )