Debian Codegraph @

async_upnp_client/device_updater.py

raw symbols SCIP
1 """async_upnp_client.device_updater module."""
2
3 import logging
4
5 from async_upnp_client.advertisement import SsdpAdvertisementListener
6 from async_upnp_client.client import UpnpDevice
7 from async_upnp_client.client_factory import UpnpFactory
8 from async_upnp_client.const import AddressTupleVXType
9 from async_upnp_client.utils import CaseInsensitiveDict
10
11 _LOGGER = logging.getLogger(__name__)
12
13
14 class DeviceUpdater:
15 """
16 Device updater.
17
18 Listens for SSDP advertisements and updates device inline when needed.
19 Inline meaning that it keeps the original UpnpDevice instance.
20 So be sure to keep only references to the UpnpDevice,
21 as a device might decide to remove a service after an update!
22 """
23
24 def __init__(
25 self,
26 device: UpnpDevice,
27 factory: UpnpFactory,
28 source: AddressTupleVXType | None = None,
29 ) -> None:
30 """Initialize."""
31 self._device = device
32 self._factory = factory
33 self._listener = SsdpAdvertisementListener(
34 async_on_alive=self._async_on_alive,
35 async_on_byebye=self._async_on_byebye,
36 async_on_update=self._async_on_update,
37 source=source,
38 )
39
40 async def async_start(self) -> None:
41 """Start listening for notifications."""
42 _LOGGER.debug("Start listening for notifications.")
43 await self._listener.async_start()
44
45 async def async_stop(self) -> None:
46 """Stop listening for notifications."""
47 _LOGGER.debug("Stop listening for notifications.")
48 await self._listener.async_stop()
49
50 async def _async_on_alive(self, headers: CaseInsensitiveDict) -> None:
51 """Handle on alive."""
52 # Ensure for root devices only.
53 if headers.get("nt") != "upnp:rootdevice":
54 return
55
56 # Ensure for our device.
57 if headers.get("_udn") != self._device.udn:
58 return
59
60 _LOGGER.debug("Handling alive: %s", headers)
61 await self._async_handle_alive_update(headers)
62
63 async def _async_on_byebye(self, headers: CaseInsensitiveDict) -> None:
64 """Handle on byebye."""
65 _LOGGER.debug("Handling on_byebye: %s", headers)
66 self._device.available = False
67
68 async def _async_on_update(self, headers: CaseInsensitiveDict) -> None:
69 """Handle on update."""
70 # Ensure for root devices only.
71 if headers.get("nt") != "upnp:rootdevice":
72 return
73
74 # Ensure for our device.
75 if headers.get("_udn") != self._device.udn:
76 return
77
78 _LOGGER.debug("Handling update: %s", headers)
79 await self._async_handle_alive_update(headers)
80
81 async def _async_handle_alive_update(self, headers: CaseInsensitiveDict) -> None:
82 """Handle on_alive or on_update."""
83 do_reinit = False
84
85 # Handle BOOTID.UPNP.ORG.
86 boot_id = headers.get("BOOTID.UPNP.ORG")
87 device_boot_id = self._device.ssdp_headers.get("BOOTID.UPNP.ORG")
88 if boot_id and boot_id != device_boot_id:
89 _LOGGER.debug("New boot_id: %s, old boot_id: %s", boot_id, device_boot_id)
90 do_reinit = True
91
92 # Handle CONFIGID.UPNP.ORG.
93 config_id = headers.get("CONFIGID.UPNP.ORG")
94 device_config_id = self._device.ssdp_headers.get("CONFIGID.UPNP.ORG")
95 if config_id and config_id != device_config_id:
96 _LOGGER.debug(
97 "New config_id: %s, old config_id: %s",
98 config_id,
99 device_config_id,
100 )
101 do_reinit = True
102
103 # Handle LOCATION.
104 location = headers.get("LOCATION")
105 if location and self._device.device_url != location:
106 _LOGGER.debug("New location: %s, old location: %s", location, self._device.device_url)
107 do_reinit = True
108
109 if location and do_reinit:
110 await self._reinit_device(location, headers)
111
112 # We heard from it, so mark it available.
113 self._device.available = True
114
115 async def _reinit_device(self, location: str, ssdp_headers: CaseInsensitiveDict) -> None:
116 """Reinitialize device."""
117 # pylint: disable=protected-access
118 _LOGGER.debug("Reinitializing device, location: %s", location)
119
120 new_device = await self._factory.async_create_device(location)
121 self._device.reinit(new_device)
122 self._device.ssdp_headers = ssdp_headers