| 1 |
"""async_upnp_client.advertisement module.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import logging |
| 5 |
import socket |
| 6 |
from asyncio.events import AbstractEventLoop |
| 7 |
from asyncio.transports import BaseTransport, DatagramTransport |
| 8 |
from typing import Any, Callable, Coroutine |
| 9 |
|
| 10 |
from async_upnp_client.const import AddressTupleVXType, NotificationSubType, SsdpSource |
| 11 |
from async_upnp_client.ssdp import ( |
| 12 |
SSDP_DISCOVER, |
| 13 |
SsdpProtocol, |
| 14 |
determine_source_target, |
| 15 |
get_ssdp_socket, |
| 16 |
) |
| 17 |
from async_upnp_client.utils import CaseInsensitiveDict |
| 18 |
|
| 19 |
_LOGGER = logging.getLogger(__name__) |
| 20 |
|
| 21 |
|
| 22 |
class SsdpAdvertisementListener: |
| 23 |
"""SSDP Advertisement listener.""" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def __init__( |
| 28 |
self, |
| 29 |
async_on_alive: (Callable[[CaseInsensitiveDict], Coroutine[Any, Any, None]] | None) = None, |
| 30 |
async_on_byebye: (Callable[[CaseInsensitiveDict], Coroutine[Any, Any, None]] | None) = None, |
| 31 |
async_on_update: (Callable[[CaseInsensitiveDict], Coroutine[Any, Any, None]] | None) = None, |
| 32 |
on_alive: Callable[[CaseInsensitiveDict], None] | None = None, |
| 33 |
on_byebye: Callable[[CaseInsensitiveDict], None] | None = None, |
| 34 |
on_update: Callable[[CaseInsensitiveDict], None] | None = None, |
| 35 |
source: AddressTupleVXType | None = None, |
| 36 |
target: AddressTupleVXType | None = None, |
| 37 |
loop: AbstractEventLoop | None = None, |
| 38 |
) -> None: |
| 39 |
"""Initialize.""" |
| 40 |
|
| 41 |
assert async_on_alive or async_on_byebye or async_on_update or on_alive or on_byebye or on_update, ( |
| 42 |
"Provide at least one callback" |
| 43 |
) |
| 44 |
|
| 45 |
self.async_on_alive = async_on_alive |
| 46 |
self.async_on_byebye = async_on_byebye |
| 47 |
self.async_on_update = async_on_update |
| 48 |
self.on_alive = on_alive |
| 49 |
self.on_byebye = on_byebye |
| 50 |
self.on_update = on_update |
| 51 |
self.source, self.target = determine_source_target(source, target) |
| 52 |
self.loop: AbstractEventLoop = loop or asyncio.get_event_loop() |
| 53 |
self._transport: BaseTransport | None = None |
| 54 |
|
| 55 |
def _on_data(self, request_line: str, headers: CaseInsensitiveDict) -> None: |
| 56 |
"""Handle data.""" |
| 57 |
if headers.get_lower("man") == SSDP_DISCOVER: |
| 58 |
|
| 59 |
return |
| 60 |
|
| 61 |
notification_sub_type = headers.get_lower("nts") |
| 62 |
if notification_sub_type is None: |
| 63 |
_LOGGER.debug("Got non-advertisement packet: %s, %s", request_line, headers) |
| 64 |
return |
| 65 |
|
| 66 |
if _LOGGER.isEnabledFor(logging.DEBUG): |
| 67 |
_LOGGER.debug( |
| 68 |
"Received advertisement, _remote_addr: %s, NT: %s, NTS: %s, USN: %s, location: %s", |
| 69 |
headers.get_lower("_remote_addr", ""), |
| 70 |
headers.get_lower("nt", "<no NT>"), |
| 71 |
headers.get_lower("nts", "<no NTS>"), |
| 72 |
headers.get_lower("usn", "<no USN>"), |
| 73 |
headers.get_lower("location", ""), |
| 74 |
) |
| 75 |
|
| 76 |
headers["_source"] = SsdpSource.ADVERTISEMENT |
| 77 |
if notification_sub_type == NotificationSubType.SSDP_ALIVE: |
| 78 |
if self.async_on_alive: |
| 79 |
coro = self.async_on_alive(headers) |
| 80 |
self.loop.create_task(coro) |
| 81 |
if self.on_alive: |
| 82 |
self.on_alive(headers) |
| 83 |
elif notification_sub_type == NotificationSubType.SSDP_BYEBYE: |
| 84 |
if self.async_on_byebye: |
| 85 |
coro = self.async_on_byebye(headers) |
| 86 |
self.loop.create_task(coro) |
| 87 |
if self.on_byebye: |
| 88 |
self.on_byebye(headers) |
| 89 |
elif notification_sub_type == NotificationSubType.SSDP_UPDATE: |
| 90 |
if self.async_on_update: |
| 91 |
coro = self.async_on_update(headers) |
| 92 |
self.loop.create_task(coro) |
| 93 |
if self.on_update: |
| 94 |
self.on_update(headers) |
| 95 |
|
| 96 |
def _on_connect(self, transport: DatagramTransport) -> None: |
| 97 |
sock: socket.socket | None = transport.get_extra_info("socket") |
| 98 |
_LOGGER.debug("On connect, transport: %s, socket: %s", transport, sock) |
| 99 |
self._transport = transport |
| 100 |
|
| 101 |
async def async_start(self) -> None: |
| 102 |
"""Start listening for advertisements.""" |
| 103 |
_LOGGER.debug("Start listening for advertisements") |
| 104 |
|
| 105 |
|
| 106 |
sock, _source, _target = get_ssdp_socket(self.source, self.target) |
| 107 |
|
| 108 |
|
| 109 |
address = ("", self.target[1]) |
| 110 |
_LOGGER.debug("Binding socket, socket: %s, address: %s", sock, address) |
| 111 |
sock.bind(address) |
| 112 |
|
| 113 |
|
| 114 |
await self.loop.create_datagram_endpoint( |
| 115 |
lambda: SsdpProtocol( |
| 116 |
self.loop, |
| 117 |
on_connect=self._on_connect, |
| 118 |
on_data=self._on_data, |
| 119 |
), |
| 120 |
sock=sock, |
| 121 |
) |
| 122 |
|
| 123 |
async def async_stop(self) -> None: |
| 124 |
"""Stop listening for advertisements.""" |
| 125 |
_LOGGER.debug("Stop listening for advertisements") |
| 126 |
if self._transport: |
| 127 |
self._transport.close() |