Skip to content

Commit 476393f

Browse files
Add command to dump the state of a server (#57)
* Add command to dump the state of a server * Always clean up client * remove unused param * Types * Apply suggestions from code review Co-authored-by: Martin Hjelmare <[email protected]> * Fix import Co-authored-by: Martin Hjelmare <[email protected]>
1 parent b45d598 commit 476393f

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ Python library for communicating with zwave-js-server. Goal for this library is
55
## Trying it out
66

77
```shell
8-
python3 -m zwave_js_server ws://localhost:3000/zjs
8+
python3 -m zwave_js_server ws://localhost:3000
9+
```
10+
11+
Or get the version of the server
12+
13+
```shell
14+
python3 -m zwave_js_server ws://localhost:3000 --server-version
15+
```
16+
17+
Or dump the state. Optionally add `--event-timeout 5` if you want to listen 5 seconds extra for events.
18+
19+
```shell
20+
python3 -m zwave_js_server ws://localhost:3000 --dump-state
921
```
1022

1123
## Sending commands

zwave_js_server/__main__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .client import Client
99
from .version import get_server_version
10+
from .dump import dump_msgs
1011

1112
logging.basicConfig(level=logging.INFO)
1213
logger = logging.getLogger(__package__)
@@ -19,6 +20,13 @@ def get_arguments() -> argparse.Namespace:
1920
parser.add_argument(
2021
"--server-version", action="store_true", help="Print the version of the server"
2122
)
23+
parser.add_argument(
24+
"--dump-state", action="store_true", help="Dump the driver state"
25+
)
26+
parser.add_argument(
27+
"--event-timeout",
28+
help="How long to listen for events when dumping state",
29+
)
2230
parser.add_argument(
2331
"url",
2432
type=str,
@@ -36,6 +44,8 @@ async def main() -> None:
3644
async with aiohttp.ClientSession() as session:
3745
if args.server_version:
3846
await print_version(args, session)
47+
elif args.dump_state:
48+
await handle_dump_state(args, session)
3949
else:
4050
await connect(args, session)
4151

@@ -50,6 +60,16 @@ async def print_version(
5060
print("Home ID:", version.home_id)
5161

5262

63+
async def handle_dump_state(
64+
args: argparse.Namespace, session: aiohttp.ClientSession
65+
) -> None:
66+
"""Dump the state of the server."""
67+
timeout = None if args.event_timeout is None else float(args.event_timeout)
68+
msgs = await dump_msgs(args.url, session, timeout=timeout)
69+
for msg in msgs:
70+
print(msg)
71+
72+
5373
async def connect(args: argparse.Namespace, session: aiohttp.ClientSession) -> None:
5474
"""Connect to the server."""
5575
client = Client(args.url, session)

zwave_js_server/dump.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Dump helper."""
2+
import asyncio
3+
from typing import List, Optional
4+
5+
import aiohttp
6+
7+
8+
async def dump_msgs(
9+
url: str, session: aiohttp.ClientSession, timeout: Optional[float] = None
10+
) -> List[dict]:
11+
"""Dump server state."""
12+
client = await session.ws_connect(url)
13+
msgs = []
14+
15+
version = await client.receive_json()
16+
msgs.append(version)
17+
18+
await client.send_json({"command": "start_listening"})
19+
state = await client.receive_json()
20+
msgs.append(state)
21+
22+
if timeout is None:
23+
await client.close()
24+
return msgs
25+
26+
current_task = asyncio.current_task()
27+
assert current_task is not None
28+
asyncio.get_running_loop().call_later(timeout, current_task.cancel)
29+
30+
try:
31+
event = await client.receive_json()
32+
msgs.append(event)
33+
except asyncio.CancelledError:
34+
pass
35+
36+
await client.close()
37+
return msgs

0 commit comments

Comments
 (0)