| 1 |
Async UPnP Client |
| 2 |
================= |
| 3 |
|
| 4 |
Asyncio UPnP Client library for Python/asyncio. |
| 5 |
|
| 6 |
Written initially for use in `Home Assistant <https://github.com/home-assistant/home-assistant>`_ to drive `DLNA DMR`-capable devices, but useful for other projects as well. |
| 7 |
|
| 8 |
Status |
| 9 |
------ |
| 10 |
|
| 11 |
.. image:: https://github.com/StevenLooman/async_upnp_client/workflows/Build/badge.svg |
| 12 |
:target: https://github.com/StevenLooman/async_upnp_client/actions/workflows/ci-cd.yml |
| 13 |
|
| 14 |
.. image:: https://img.shields.io/pypi/v/async_upnp_client.svg |
| 15 |
:target: https://pypi.python.org/pypi/async_upnp_client |
| 16 |
|
| 17 |
.. image:: https://img.shields.io/pypi/format/async_upnp_client.svg |
| 18 |
:target: https://pypi.python.org/pypi/async_upnp_client |
| 19 |
|
| 20 |
.. image:: https://img.shields.io/pypi/pyversions/async_upnp_client.svg |
| 21 |
:target: https://pypi.python.org/pypi/async_upnp_client |
| 22 |
|
| 23 |
.. image:: https://img.shields.io/pypi/l/async_upnp_client.svg |
| 24 |
:target: https://pypi.python.org/pypi/async_upnp_client |
| 25 |
|
| 26 |
|
| 27 |
General set up |
| 28 |
-------------- |
| 29 |
|
| 30 |
The `UPnP Device Architecture <https://openconnectivity.org/upnp-specs/UPnP-arch-DeviceArchitecture-v2.0-20200417.pdf>`_ document contains several sections describing different parts of the UPnP standard. These chapters/sections can mostly be mapped to the following modules: |
| 31 |
|
| 32 |
* Chapter 1 Discovery |
| 33 |
* Section 1.1 SSDP: ``async_upnp_client.ssdp`` |
| 34 |
* Section 1.2 Advertisement: ``async_upnp_client.advertisement`` provides basic functionality to receive advertisements. |
| 35 |
* Section 1.3 Search: ``async_upnp_client.search`` provides basic functionality to do search requests and gather the responses. |
| 36 |
* ``async_upnp_client.ssdp_client`` contains the ``SsdpListener`` which combines advertisements and search to get the known devices and provides callbacks on changes. It is meant as something which runs continuously to provide useful information about the SSDP-active devices. |
| 37 |
* Chapter 2 Description / Chapter 3 Control |
| 38 |
* ``async_upnp_client.client_factory``/``async_upnp_client.client`` provide a series of classes to get information about the device/services using the 'description', and interact with these devices. |
| 39 |
* ``async_upnp_client.server`` provides a series of classes to set up a UPnP server, including SSDP discovery/advertisements. |
| 40 |
* Chapter 4 Eventing |
| 41 |
* ``async_upnp_client.event_handler`` provides functionality to handle events received from the device. |
| 42 |
|
| 43 |
There are several 'profiles' which a device can implement to provide a standard interface to talk to. Some of these profiles are added to this library. The following profiles are currently available: |
| 44 |
|
| 45 |
* Internet Gateway Device (IGD) |
| 46 |
* ``async_upnp_client.profiles.igd`` |
| 47 |
* Digital Living Network Alliance (DLNA) |
| 48 |
* ``async_upnp_client.profiles.dlna`` |
| 49 |
* Printers |
| 50 |
* ``async_upnp_client.profiles.printer`` |
| 51 |
|
| 52 |
For examples on how to use ``async_upnp_client``, see ``examples``/ . |
| 53 |
|
| 54 |
Note that this library is most likely does not fully implement all functionality from the UPnP Device Architecture document and/or contains errors/bugs/mis-interpretations. |
| 55 |
|
| 56 |
|
| 57 |
Contributing |
| 58 |
------------ |
| 59 |
|
| 60 |
See ``CONTRIBUTING.rst``. |
| 61 |
|
| 62 |
|
| 63 |
Development |
| 64 |
----------- |
| 65 |
|
| 66 |
Development is done on the ``development`` branch. |
| 67 |
|
| 68 |
``pre-commit`` is used to run several checks before committing. You can install ``pre-commit`` and the git-hook by doing:: |
| 69 |
|
| 70 |
$ pip install pre-commit |
| 71 |
$ pre-commit --install |
| 72 |
|
| 73 |
The `Open Connectivity Foundation <https://openconnectivity.org/>`_ provides a bundle with all `UPnP Specifications <https://openconnectivity.org/developer/specifications/upnp-resources/upnp/>`_. |
| 74 |
|
| 75 |
|
| 76 |
Changes |
| 77 |
------- |
| 78 |
|
| 79 |
Changes are recorded using `Towncier <https://towncrier.readthedocs.io/>`_. Once a new release is created, towncrier is used to create the file ``CHANGES.rst``. |
| 80 |
|
| 81 |
To create a new change run: |
| 82 |
|
| 83 |
$ towncrier create <pr-number>.<change type> |
| 84 |
|
| 85 |
A change type can be one of: |
| 86 |
|
| 87 |
- feature: Signifying a new feature. |
| 88 |
- bugfix: Signifying a bug fix. |
| 89 |
- doc: Signifying a documentation improvement. |
| 90 |
- removal: Signifying a deprecation or removal of public API. |
| 91 |
- misc: A ticket has been closed, but it is not of interest to users. |
| 92 |
|
| 93 |
A new file is then created in the ``changes`` directory. Add a short description of the change to that file. |
| 94 |
|
| 95 |
|
| 96 |
Releasing |
| 97 |
--------- |
| 98 |
|
| 99 |
Steps for releasing: |
| 100 |
|
| 101 |
- Switch to development: ``git checkout development`` |
| 102 |
- Do a pull: ``git pull`` |
| 103 |
- Run towncrier: ``towncrier build --version <version>`` |
| 104 |
- Commit towncrier results: ``git commit -m "Towncrier"`` |
| 105 |
- Run bump2version (note that this creates a new commit + tag): ``bump2version --tag major/minor/patch`` |
| 106 |
- Push to github: ``git push && git push --tags`` |
| 107 |
|
| 108 |
|
| 109 |
Profiling |
| 110 |
--------- |
| 111 |
|
| 112 |
To do profiling it is recommended to install `pytest-profiling <https://pypi.org/project/pytest-profiling>`_. Then run a test with profiling enabled, and write the results to a graph:: |
| 113 |
|
| 114 |
# Run tests with profiling and svg-output enabled. This will generate prof/*.prof files, and a svg file. |
| 115 |
$ pytest --profile-svg -k test_case_insensitive_dict_profile |
| 116 |
... |
| 117 |
|
| 118 |
# Open generated SVG file. |
| 119 |
$ xdg-open prof/combined.svg |
| 120 |
|
| 121 |
|
| 122 |
Alternatively, you can generate a profiling data file, use `pyprof2calltree <https://github.com/pwaller/pyprof2calltree/>`_ to convert the data and open `kcachegrind <http://kcachegrind.sourceforge.net/html/Home.html>`_. For example:: |
| 123 |
|
| 124 |
# Run tests with profiling enabled, this will generate prof/*.prof files. |
| 125 |
$ pytest --profile -k test_case_insensitive_dict_profile |
| 126 |
... |
| 127 |
|
| 128 |
$ pyprof2calltree -i prof/combined.prof -k |
| 129 |
launching kcachegrind |
| 130 |
|
| 131 |
|
| 132 |
upnp-client |
| 133 |
----------- |
| 134 |
|
| 135 |
A command line interface is provided via the ``upnp-client`` script. This script can be used to: |
| 136 |
|
| 137 |
- call an action |
| 138 |
- subscribe to services and listen for events |
| 139 |
- show UPnP traffic (--debug-traffic) from and to the device |
| 140 |
- show pretty printed JSON (--pprint) for human readability |
| 141 |
- search for devices |
| 142 |
- listen for advertisements |
| 143 |
|
| 144 |
The output of the script is a single line of JSON for each action-call or subscription-event. See the programs help for more information. |
| 145 |
|
| 146 |
An example of calling an action:: |
| 147 |
|
| 148 |
$ upnp-client --pprint call-action http://192.168.178.10:49152/description.xml RC/GetVolume InstanceID=0 Channel=Master |
| 149 |
{ |
| 150 |
"timestamp": 1531482271.5603056, |
| 151 |
"service_id": "urn:upnp-org:serviceId:RenderingControl", |
| 152 |
"service_type": "urn:schemas-upnp-org:service:RenderingControl:1", |
| 153 |
"action": "GetVolume", |
| 154 |
"in_parameters": { |
| 155 |
"InstanceID": 0, |
| 156 |
"Channel": "Master" |
| 157 |
}, |
| 158 |
"out_parameters": { |
| 159 |
"CurrentVolume": 70 |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
An example of subscribing to all services, note that the program stays running until you stop it (ctrl-c):: |
| 165 |
|
| 166 |
$ upnp-client --pprint subscribe http://192.168.178.10:49152/description.xml \* |
| 167 |
{ |
| 168 |
"timestamp": 1531482518.3663802, |
| 169 |
"service_id": "urn:upnp-org:serviceId:RenderingControl", |
| 170 |
"service_type": "urn:schemas-upnp-org:service:RenderingControl:1", |
| 171 |
"state_variables": { |
| 172 |
"LastChange": "<Event xmlns=\"urn:schemas-upnp-org:metadata-1-0/AVT_RCS\">\n<InstanceID val=\"0\">\n<Mute channel=\"Master\" val=\"0\"/>\n<Volume channel=\"Master\" val=\"70\"/>\n</InstanceID>\n</Event>\n" |
| 173 |
} |
| 174 |
} |
| 175 |
{ |
| 176 |
"timestamp": 1531482518.366804, |
| 177 |
"service_id": "urn:upnp-org:serviceId:RenderingControl", |
| 178 |
"service_type": "urn:schemas-upnp-org:service:RenderingControl:1", |
| 179 |
"state_variables": { |
| 180 |
"Mute": false, |
| 181 |
"Volume": 70 |
| 182 |
} |
| 183 |
} |
| 184 |
... |
| 185 |
|
| 186 |
You can subscribe to list of services by providing these names or abbreviated names, such as:: |
| 187 |
|
| 188 |
$ upnp-client --pprint subscribe http://192.168.178.10:49152/description.xml RC AVTransport |
| 189 |
|
| 190 |
|
| 191 |
An example of searching for devices:: |
| 192 |
|
| 193 |
$ upnp-client --pprint search |
| 194 |
{ |
| 195 |
"Cache-Control": "max-age=3600", |
| 196 |
"Date": "Sat, 27 Oct 2018 10:43:42 GMT", |
| 197 |
"EXT": "", |
| 198 |
"Location": "http://192.168.178.1:49152/description.xml", |
| 199 |
"OPT": "\"http://schemas.upnp.org/upnp/1/0/\"; ns=01", |
| 200 |
"01-NLS": "906ad736-cfc4-11e8-9c22-8bb67c653324", |
| 201 |
"Server": "Linux/4.14.26+, UPnP/1.0, Portable SDK for UPnP devices/1.6.20.jfd5", |
| 202 |
"X-User-Agent": "redsonic", |
| 203 |
"ST": "upnp:rootdevice", |
| 204 |
"USN": "uuid:e3a17dd5-9d85-3131-3c34-b827eb498d72::upnp:rootdevice", |
| 205 |
"_timestamp": "2018-10-27 12:43:09.125408", |
| 206 |
"_host": "192.168.178.1", |
| 207 |
"_port": 49152 |
| 208 |
"_udn": "uuid:e3a17dd5-9d85-3131-3c34-b827eb498d72", |
| 209 |
"_source": "search" |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
An example of listening for advertisements, note that the program stays running until you stop it (ctrl-c):: |
| 214 |
|
| 215 |
$ upnp-client --pprint advertisements |
| 216 |
{ |
| 217 |
"Host": "239.255.255.250:1900", |
| 218 |
"Cache-Control": "max-age=30", |
| 219 |
"Location": "http://192.168.178.1:1900/WFADevice.xml", |
| 220 |
"NTS": "ssdp:alive", |
| 221 |
"Server": "POSIX, UPnP/1.0 UPnP Stack/2013.4.3.0", |
| 222 |
"NT": "urn:schemas-wifialliance-org:device:WFADevice:1", |
| 223 |
"USN": "uuid:99cb221c-1f15-c620-dc29-395f415623c6::urn:schemas-wifialliance-org:device:WFADevice:1", |
| 224 |
"_timestamp": "2018-12-23 11:22:47.154293", |
| 225 |
"_host": "192.168.178.1", |
| 226 |
"_port": 1900 |
| 227 |
"_udn": "uuid:99cb221c-1f15-c620-dc29-395f415623c6", |
| 228 |
"_source": "advertisement" |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
IPv6 support |
| 233 |
------------ |
| 234 |
|
| 235 |
IPv6 is supported for the UPnP client functionality as well as the SSDP functionality. Please do note that multicast over IPv6 does require a ``scope_id``/interface ID. The ``scope_id`` is used to specify which interface should be used. |
| 236 |
|
| 237 |
There are several ways to get the ``scope_id``. Via Python this can be done via the `ifaddr <https://github.com/pydron/ifaddr>`_ library. From the (Linux) command line the ``scope_id`` can be found via the `ip` command:: |
| 238 |
|
| 239 |
$ ip address |
| 240 |
... |
| 241 |
6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 |
| 242 |
link/ether 00:15:5d:38:97:cf brd ff:ff:ff:ff:ff:ff |
| 243 |
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0 |
| 244 |
valid_lft forever preferred_lft forever |
| 245 |
inet6 fe80::215:5dff:fe38:97cf/64 scope link |
| 246 |
valid_lft forever preferred_lft forever |
| 247 |
|
| 248 |
In this case, the interface index is 6 (start of the line) and thus the ``scope_id`` is ``6``. |
| 249 |
|
| 250 |
Or on Windows using the ``ipconfig`` command:: |
| 251 |
|
| 252 |
C:\> ipconfig /all |
| 253 |
... |
| 254 |
Ethernet adapter Ethernet: |
| 255 |
... |
| 256 |
Link-local IPv6 Address . . . . . : fe80::e530:c739:24d7:c8c7%8(Preferred) |
| 257 |
... |
| 258 |
|
| 259 |
The ``scope_id`` is ``8`` in this example, as shown after the ``%`` character at the end of the IPv6 address. |
| 260 |
|
| 261 |
Or on macOS using the ``ifconfig`` command:: |
| 262 |
|
| 263 |
% ifconfig |
| 264 |
... |
| 265 |
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 |
| 266 |
options=50b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV,CHANNEL_IO> |
| 267 |
ether 38:c9:86:30:fe:be |
| 268 |
inet6 fe80::215:5dff:fe38:97cf%en0 prefixlen 64 secured scopeid 0x4 |
| 269 |
... |
| 270 |
|
| 271 |
The ``scope_id`` is ``4`` in this example, as shown by ``scopeid 0x4``. Note that this is a hexadecimal value. |
| 272 |
|
| 273 |
All functionality regarding SSDP uses ``AddressTupleVXType`` to specify addresses. |
| 274 |
|
| 275 |
For consistency, the ``AiohttpNotifyServer`` also uses ``AddressTupleVXType`` to specify the ``source`` (the address and port the notify server listens on.) |