Skip to content

Conversation

@mangelajo
Copy link
Contributor

@mangelajo mangelajo commented Dec 21, 2025

This commit adds mouse HID functionality, and provides a
context-manager for the whole NanoKVMClient, in a way that
a separate context manager is not necessary anymore.

This is backwards imcompatible, but it should be easy to switch

from:

    async with ClientSession() as session:
        client = NanoKVMClient("http://kvm-8b76.local/api/", session)
        await client.authenticate("username", "password")

to:

    async with NanoKVMClient("http://kvm-8b76.local/api/", session) as client:
        await client.authenticate("username", "password")

This change provides the new following methods:

  • mouse_move_rel
  • mouse_move_abs
  • mouse_down
  • mouse_up
  • mouse_click
  • mouse_scroll

@puddly
Copy link
Owner

puddly commented Dec 21, 2025

What about a context manager API? It can be a breaking change. Any async cleanup in __del__ is basically a race condition. Something like this maybe?

async with ClientSession() as session:
    async with NanoKVMClient(...) as client:
        ...

Then, NanoKVMClient.__aenter__ can be a no-op that returns the current object (since the WS connection is opened lazily) and __aexit__ runs the cleanup code in close_ws (which I guess can be made private).

@mangelajo
Copy link
Contributor Author

I suspect we probably should have constants for the buttons "left" "right" "middle", etc ..

@mangelajo
Copy link
Contributor Author

What about a context manager API? It can be a breaking change. Any async cleanup in __del__ is basically a race condition. Something like this maybe?

async with ClientSession() as session:
    async with NanoKVMClient(...) as client:
        ...

Then, NanoKVMClient.__aenter__ can be a no-op that returns the current object (since the WS connection is opened lazily) and __aexit__ runs the cleanup code in close_ws (which I guess can be made private).

Yes, you are right.

Look at my last patch, I think it would be clean, and using a context manager, we can even create the ClientSession automatically.

It's made to be backwards compatible. I am testing it out right now. will report back and clear the draft.

@mangelajo mangelajo changed the title Add mouse HID actions Add mouse HID actions and make NanoKVMClient a context manager Dec 21, 2025
Comment on lines 144 to 149
async def close(self) -> None:
"""Close all connections and cleanup resources."""
await self.close_ws()
if self._session is not None: # Defensive check for idempotency
await self._session.close()
self._session = None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can probably pull this all under aexit

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only support a context manager, yeah, that would be best. I don't think anything will break with a double close but there's no need for public API to close the client explicitly

@mangelajo mangelajo force-pushed the mouse-move branch 2 times, most recently from 8aae9db to c32eef0 Compare December 22, 2025 12:20
This commit adds mouse HID functionality, and provides a
context-manager for the whole NanoKVMClient, in a way that
a separate context manager is not necessary anymore.

This is backwards imcompatible, but it should be easy to switch
from:
```
async with ClientSession() as session:
    client = NanoKVMClient("http://kvm-8b76.local/api/", session)
    await client.authenticate("username", "password")
```

to:
```
async with NanoKVMClient("http://kvm-8b76.local/api/", session) as client:
    await client.authenticate("username", "password")
```
@mangelajo
Copy link
Contributor Author

👋 :D

@puddly puddly merged commit e021d02 into puddly:dev Jan 19, 2026
1 check passed
@puddly
Copy link
Owner

puddly commented Jan 19, 2026

Ah, sorry! I forgot to actually merge and release this 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants