Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pushserver more generic #1531

Merged
merged 3 commits into from Sep 21, 2022
Merged

Make pushserver more generic #1531

merged 3 commits into from Sep 21, 2022

Conversation

rytilahti
Copy link
Owner

@rytilahti rytilahti commented Sep 19, 2022

This generalizes the push server to allow using it for other uses besides event handling, e.g., to create device simulators.
When receiving a datagram from an unregistered device, the implementation will now try to decrypt the contents using a token full of 0s. If the decryption succeeds, the list of previously registered methods (using add_method(name: str, response: Union[Dict, Callable)) is used to obtain a response to be send back to the requester. An error is send to the client if no method with the given name is found.

The main motivation behind this change is to make it easier for developers to test miiocli & the library against devices they do not possess by providing easy-to-use device simulators for both miio and miot protocols. The simulator implementations will follow in separate PRs.

@starkillerOG could you please take a look at this PR to make sure I'm not breaking anything event related? I'll add some tests prior getting this merged so that we don't break it accidentally in the future.

TBD:

  • Add tests

@rytilahti rytilahti mentioned this pull request Sep 19, 2022
2 tasks
This generalizes the push server to allow using it for other uses besides event handling, e.g., to create device simulators.
The token for client connections is currently hard-coded to be full of 0s.
@codecov-commenter
Copy link

codecov-commenter commented Sep 21, 2022

Codecov Report

Merging #1531 (75f3adb) into master (26209d6) will increase coverage by 0.28%.
The diff coverage is 84.67%.

@@            Coverage Diff             @@
##           master    #1531      +/-   ##
==========================================
+ Coverage   82.33%   82.62%   +0.28%     
==========================================
  Files         145      146       +1     
  Lines       14248    14359     +111     
  Branches     1617     3489    +1872     
==========================================
+ Hits        11731    11864     +133     
+ Misses       2287     2262      -25     
- Partials      230      233       +3     
Impacted Files Coverage Δ
miio/push_server/server.py 24.82% <41.66%> (+0.74%) ⬆️
miio/push_server/serverprotocol.py 69.23% <70.21%> (+43.47%) ⬆️
miio/push_server/test_serverprotocol.py 100.00% <100.00%> (ø)
miio/protocol.py 94.73% <0.00%> (+6.31%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@rytilahti rytilahti merged commit 9fbff33 into master Sep 21, 2022
@rytilahti rytilahti deleted the feat/generalize_server branch September 21, 2022 19:25
Copy link
Contributor

@starkillerOG starkillerOG left a comment

Choose a reason for hiding this comment

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

Looks good to me.
Sorry for the late review, I was quite bussy.
I really like we now have some tests for the serverprotocol, thank you very much!

I have some small feedback, maybe you could adress that in a follow up PR?

I will try to test this new code with my devices tonight and let you know!

The response can be either a callable or a dictionary to send back as response.
"""
self._methods[name] = response

def register_miio_device(self, device: Device, callback: PushServerCallback):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a check here:

if self._device_ip is None:
   warning
   return

To make sure this is a proper server that can work with real devices

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm not sure I'm following what you mean, the device_ip is checked right below?

miio/push_server/serverprotocol.py Show resolved Hide resolved
callback(source_device_id, action, msg_value.get("params"))
if host in self.server._registered_devices:
return self._handle_datagram_from_registered_device(host, port, data)
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

I am slightly worried that we might exedently send incorrect messages to a real device if that device is for instance not properly disconnected and the server is restarted or other edge cases.
Maybe we schould make a register_client function and check against that instead of just handeling everything which is not registerd as a miio_device as a client.

Copy link
Owner Author

Choose a reason for hiding this comment

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

The incorrect messages would be encrypted with a hardcoded token that should be different from a real device, so sending a message to the server with some other will simply cause the device not to respond. That is why I don't think it's a non-issue.


if host not in self.server._registered_devices:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to have this error back when the host is not registered as a miio_device or a client.
See comment below.

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is now checked inside datagram_received, for registered devices. But you are right, we are responding to all non-registered devices as if they were real clients..

@starkillerOG
Copy link
Contributor

@rytilahti I just tested this code with gateway devices, and the event pushes still work just fine.
Would you be so kind to have a look at the feedback I left here?
Sorry for beeing late.

@rytilahti
Copy link
Owner Author

rytilahti commented Oct 30, 2022

@starkillerOG thanks for taking a look, and sorry for taking so long to respond... I changed the variable naming, but I'm not sure if we should do the other changes, see the linked PR and my commentary inline.

rytilahti added a commit that referenced this pull request Oct 30, 2022
Also, improve type hints and variable naming

Related to @starkillerOG review in #1531
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants