File: dump.py

package info (click to toggle)
zwave-js-server-python 0.67.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: python: 15,886; sh: 21; javascript: 16; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,523 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
"""Dump helper."""

from __future__ import annotations

import asyncio

import aiohttp

from .client import INITIALIZE_MESSAGE_ID
from .const import MAX_SERVER_SCHEMA_VERSION, PACKAGE_NAME, __version__


async def dump_msgs(
    url: str,
    session: aiohttp.ClientSession,
    additional_user_agent_components: dict[str, str] | None = None,
    timeout: float | None = None,
) -> list[dict]:
    """Dump server state."""
    client = await session.ws_connect(url, compress=15, max_msg_size=0)
    msgs = []

    version = await client.receive_json()
    msgs.append(version)

    for to_send in (
        {
            "command": "initialize",
            "messageId": INITIALIZE_MESSAGE_ID,
            "schemaVersion": MAX_SERVER_SCHEMA_VERSION,
            "additionalUserAgentComponents": {
                PACKAGE_NAME: __version__,
                **(additional_user_agent_components or {}),
            },
        },
        {"command": "start_listening", "messageId": "start-listening"},
    ):
        await client.send_json(to_send)
        msgs.append(await client.receive_json())

    if timeout is None:
        await client.close()
        return msgs

    current_task = asyncio.current_task()
    assert current_task is not None
    asyncio.get_running_loop().call_later(timeout, current_task.cancel)

    while True:
        try:
            msg = await client.receive_json()
            msgs.append(msg)
        except asyncio.CancelledError:
            break

    await client.close()
    return msgs