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

Add push server implementation to enable event handling #1446

Merged
merged 17 commits into from Jul 14, 2022

Conversation

starkillerOG
Copy link
Contributor

@starkillerOG starkillerOG commented Jul 7, 2022

This is a split out version of PR #1288.
It only includes the implementation of the push server to make it less confusing.

Test script to listen for alarm triggering of a aqara gateway:

import logging
import asyncio
from miio import Gateway, PushServer
from miio.push_server import EventInfo

_LOGGER = logging.getLogger(__name__)
logging.basicConfig(level="INFO")

gateway_ip = "192.168.1.IP"
token = "TokenTokenToken"


async def asyncio_demo(loop):
    def alarm_callback(source_device, action, params):
        _LOGGER.info("callback '%s' from '%s', params: '%s'", action, source_device, params)
    
    push_server = PushServer(gateway_ip)
    gateway = Gateway(gateway_ip, token)
    
    await push_server.start()

    push_server.register_miio_device(gateway, alarm_callback)
    
    event_info = EventInfo(
        action="alarm_triggering",
        extra="[1,19,1,111,[0,1],2,0]",
        trigger_token=gateway.token,
    )

    await loop.run_in_executor(None, push_server.subscribe_event, gateway, event_info)
    
    _LOGGER.info("Listening")
    
    await asyncio.sleep(30)

    push_server.stop()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio_demo(loop))

Test script to register on a button press of aqara zigbee button connected to a aqara gateway:

import logging
import asyncio
from miio import Gateway, PushServer
from miio.push_server import EventInfo

_LOGGER = logging.getLogger(__name__)
logging.basicConfig(level="INFO")

gateway_ip = "192.168.1.IP"
token = "TokenTokenToken"
button_sid = "lumi.123456789abcdef"


async def asyncio_demo(loop):
    def subdevice_callback(source_device, action, params):
        _LOGGER.info("callback '%s' from '%s', params: '%s'", action, source_device, params)
    
    push_server = PushServer(gateway_ip)
    gateway = Gateway(gateway_ip, token)
    
    await push_server.start()

    push_server.register_miio_device(gateway, subdevice_callback)

    await loop.run_in_executor(None, gateway.discover_devices)
    
    button = gateway.devices[button_sid]
    
    event_info = EventInfo(
        action="click_ch0",
        extra="[1,13,1,85,[0,1],0,0]",
        source_sid=button.sid,
        source_model=button.zigbee_model,
    )

    await loop.run_in_executor(None, push_server.subscribe_event, gateway, event_info)
    
    _LOGGER.info("Listening")
    
    await asyncio.sleep(30)

    push_server.stop()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio_demo(loop))

@codecov-commenter
Copy link

codecov-commenter commented Jul 7, 2022

Codecov Report

Merging #1446 (0600f4e) into master (74b6b42) will decrease coverage by 0.82%.
The diff coverage is 29.57%.

@@            Coverage Diff             @@
##           master    #1446      +/-   ##
==========================================
- Coverage   84.18%   83.35%   -0.83%     
==========================================
  Files         135      139       +4     
  Lines       13448    13670     +222     
  Branches     1499     3248    +1749     
==========================================
+ Hits        11321    11395      +74     
- Misses       1909     2057     +148     
  Partials      218      218              
Impacted Files Coverage Δ
miio/push_server/server.py 23.48% <23.48%> (ø)
miio/push_server/serverprotocol.py 25.75% <25.75%> (ø)
miio/__init__.py 100.00% <100.00%> (ø)
miio/push_server/__init__.py 100.00% <100.00%> (ø)
miio/push_server/eventinfo.py 100.00% <100.00%> (ø)
miio/integrations/vacuum/roborock/vacuum.py 65.20% <0.00%> (+0.60%) ⬆️
miio/integrations/vacuum/roborock/vacuum_tui.py 32.09% <0.00%> (+2.46%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@starkillerOG
Copy link
Contributor Author

@rytilahti This is ready for review/merge.
I processed all the feedback from our previous conversation.
Sorry for taking so long, was bussy with other PRs.

I think it is now much cleaner more general and simpler.

Copy link
Owner

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

Thanks for splitting it up, makes it much easier to review! I added some comments inline, alas, I haven't had time to setup my gateway for testing :(

A couple of things would be nice to have:

  • Avoid bare dictionaries, and use some container classes (like attrs) that are then serialized to JSON
  • Some tests would be nice, if you extract the payload generation into their own functions, you could at least test that given EventInfos creates expected payloads to be send, subscribing and unsubscribing from events can also be tested without involving any testing on socket-level.

P.S. I updated the PR title to make it clearer for anyone reading the changelogs, hope that makes sense.

miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
miio/push_server.py Outdated Show resolved Hide resolved
@rytilahti rytilahti changed the title push server implementation Add push server implementation to enable event handling Jul 7, 2022
@starkillerOG
Copy link
Contributor Author

@rytilahti I think I have processed all feedback, could you have a look again/merge?

@starkillerOG
Copy link
Contributor Author

@rytilahti I feel like this is really ready to be merged now.
Could you please read over the docs and check my grammer and fix some of the styling?
I am not particularly good in docs...

I feel like the tests can be added later, I would really like to focus on implementing it for the gateway next.
Than users can really start using it and we can see where problems might occur or what is unclear.

miio/push_server.py Outdated Show resolved Hide resolved
@rytilahti
Copy link
Owner

Could you please read over the docs and check my grammer and fix some of the styling? I am not particularly good in docs...

Done, I revised and simplified the docs (e.g., by removing the details about how to perform traffic captures, which should actually be described somewhere in the docs..), and I removed also the full description of the EventInfo class, as this should be documented in the code itself.

Please give the docs a quick read, and adjust accordingly if I removed too much or if some piece of information is missing.

I feel like the tests can be added later, I would really like to focus on implementing it for the gateway next. Than users can really start using it and we can see where problems might occur or what is unclear.

Okay, let's skip the tests for now. However, please move move the server into its own package, and extract separate classes into their own files. I think a directory structure like this would be good, and will allow adding tests later on without polluting the main package directory:

  • miio/push_server/
    • __init__.py (to expose the classes that are part of the public API: PushServer, EventInfo)
    • server.py
    • eventinfo.py
    • serverprotocol.py

@rytilahti rytilahti added this to the 0.5.12 milestone Jul 13, 2022
@rytilahti rytilahti mentioned this pull request Jul 13, 2022
@starkillerOG
Copy link
Contributor Author

I read over the docs and made some small tweaks, I think they are now good to go.

@starkillerOG
Copy link
Contributor Author

starkillerOG commented Jul 14, 2022

@rytilahti I restructured the files as you sugested, could you revieuw/merge?
Just tested the latest version again and everything still works :)

Copy link
Owner

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @starkillerOG! 🎉

@rytilahti rytilahti merged commit 4dd3cd8 into rytilahti:master Jul 14, 2022
@starkillerOG
Copy link
Contributor Author

@rytilahti thank you for all the time you spent on reviewing and all the greath suggestions!
I will continue with the gateway implementation now, hopfully I will have a PR ready for review later today :)

@rytilahti
Copy link
Owner

Sure thing, please consider adding some tests afterwards at some point after you are done with the gateway implementation! I'll review the gateway impl PR as soon as possible, so that we can get it into the next release :-)

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