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 support for encrypted notifications #197

Merged
merged 126 commits into from
Oct 22, 2022
Merged

Add support for encrypted notifications #197

merged 126 commits into from
Oct 22, 2022

Conversation

bdraco
Copy link
Collaborator

@bdraco bdraco commented Oct 19, 2022

We now try to get the char change via an encrypted notification instead of a disconnected
event when possible. This avoids the need to connect to the accessory for changes that
are made to it from outside of aiohomekit which drastically speeds up updates and frees
up BLE connection slots.

@codecov
Copy link

codecov bot commented Oct 19, 2022

Codecov Report

Base: 69.63% // Head: 68.29% // Decreases project coverage by -1.34% ⚠️

Coverage data is based on head (94d0a2b) compared to base (5165c2e).
Patch coverage: 29.32% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
- Coverage   69.63%   68.29%   -1.35%     
==========================================
  Files          71       71              
  Lines        6152     6361     +209     
==========================================
+ Hits         4284     4344      +60     
- Misses       1868     2017     +149     
Flag Coverage Δ
unittests 68.29% <29.32%> (-1.35%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiohomekit/controller/ble/controller.py 36.29% <0.00%> (-3.55%) ⬇️
aiohomekit/controller/ble/values.py 15.00% <0.00%> (ø)
aiohomekit/controller/ble/pairing.py 22.46% <15.03%> (-2.81%) ⬇️
aiohomekit/crypto/chacha20poly1305.py 70.45% <31.57%> (-29.55%) ⬇️
aiohomekit/controller/ble/key.py 42.30% <50.00%> (+2.30%) ⬆️
aiohomekit/model/__init__.py 95.07% <50.00%> (-0.91%) ⬇️
aiohomekit/controller/ble/manufacturer_data.py 60.34% <54.83%> (-0.27%) ⬇️
aiohomekit/controller/ble/structs.py 44.49% <55.55%> (+0.45%) ⬆️
aiohomekit/model/characteristics/characteristic.py 75.00% <75.00%> (ø)
...ekit/model/characteristics/characteristic_types.py 100.00% <100.00%> (ø)
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@bdraco
Copy link
Collaborator Author

bdraco commented Oct 21, 2022

It looks like these are widely supported

I haven't been able to get anything to give up a key yet but I think I might need to turn at least one on first

@bdraco
Copy link
Collaborator Author

bdraco commented Oct 21, 2022

home-assistant/core#80733 to open up the filter

@bdraco bdraco changed the title WIP: Add support for encrypted notifications Add support for encrypted notifications Oct 22, 2022
@bdraco
Copy link
Collaborator Author

bdraco commented Oct 22, 2022

Works well with dropped advertisements as well as the the next adv gets picked up and it makes a connection

@bdraco bdraco marked this pull request as ready for review October 22, 2022 16:04
@bdraco bdraco marked this pull request as draft October 22, 2022 16:13
@bdraco
Copy link
Collaborator Author

bdraco commented Oct 22, 2022

Found a race on first connection

@bdraco bdraco marked this pull request as ready for review October 22, 2022 16:26
@bdraco bdraco requested a review from Jc2k October 22, 2022 16:29
@Jc2k
Copy link
Owner

Jc2k commented Oct 22, 2022

Tested with my Eve Motion first, it was just the first thing i pulled out my drawer:

2022-10-22 20:59:29.021 DEBUG (MainThread) [aiohomekit.controller.ble.controller] Got encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb1\x9a?uI\x1c\xae~\x98J>\xb9\xc6\xbf^Q')
2022-10-22 20:59:29.021 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Trying state_num 8 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb1\x9a?uI\x1c\xae~\x98J>\xb9\xc6\xbf^Q')
2022-10-22 20:59:29.022 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: encrypted =  b'\xb1\x9a?uI\x1c\xae~\x98J>\xb9\xc6\xbf^Q' - decrypted = bytearray(b'\x08\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00') - gsn=8 - iid=19 - value=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
2022-10-22 20:59:29.022 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: results = {(1, 19): {'value': False}}
2022-10-22 21:01:08.034 DEBUG (MainThread) [aiohomekit.controller.ble.controller] Got encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P')
2022-10-22 21:01:08.034 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Trying state_num 9 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P')
2022-10-22 21:01:08.034 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: encrypted =  b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P' - decrypted = bytearray(b'\t\x00\x13\x00\x01\x00\x00\x00\x00\x00\x00\x00') - gsn=9 - iid=19 - value=bytearray(b'\x01\x00\x00\x00\x00\x00\x00\x00')
2022-10-22 21:01:08.034 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: results = {(1, 19): {'value': True}}
2022-10-22 21:01:08.200 DEBUG (MainThread) [aiohomekit.controller.ble.controller] Got encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P')
2022-10-22 21:01:08.200 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Trying state_num 10 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P')
2022-10-22 21:01:08.200 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Trying state_num 9 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='2D09B63D-8C80-4B17-BCB4-07332216CE00', id='4f:92:ec:1c:7e:66', advertising_identifier=b'O\x92\xec\x1c~f', encrypted_payload=b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P')
2022-10-22 21:01:08.200 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: encrypted =  b'\xb6\x11vw\x1a\xd5\xb6v]@\x98\x06\xa6&?P' - decrypted = bytearray(b'\t\x00\x13\x00\x01\x00\x00\x00\x00\x00\x00\x00') - gsn=9 - iid=19 - value=bytearray(b'\x01\x00\x00\x00\x00\x00\x00\x00')
2022-10-22 21:01:08.201 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [2D09B63D-8C80-4B17-BCB4-07332216CE00] (id=4F:92:EC:1C:7E:66): Received notification: results = {(1, 19): {'value': True}}

Success!

@Jc2k
Copy link
Owner

Jc2k commented Oct 22, 2022

An Eve Degree:

2022-10-22 21:21:15.061 DEBUG (MainThread) [aiohomekit.controller.ble.controller] Got encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:15.061 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Trying state_num 3 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:15.062 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Trying state_num 2 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:15.062 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Trying state_num 4 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:15.062 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Received notification: encrypted =  b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c' - decrypted = bytearray(b'\x04\x00\x18\x00h\xa6\xd7A\x00\x00\x00\x00') - gsn=4 - iid=24 - value=bytearray(b'h\xa6\xd7A\x00\x00\x00\x00')
2022-10-22 21:21:15.062 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Received notification: results = {(1, 24): {'value': 26.956253051757812}}
2022-10-22 21:21:17.174 DEBUG (MainThread) [aiohomekit.controller.ble.controller] Got encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:17.175 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Trying state_num 5 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:17.175 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Trying state_num 4 for encrypted notification: HomeKitEncryptedNotification(name='Eve', address='E4E430D0-32B3-4A10-8776-2382924DFBC0', id='f2:1d:f1:99:54:e5', advertising_identifier=b'\xf2\x1d\xf1\x99T\xe5', encrypted_payload=b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c')
2022-10-22 21:21:17.175 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Received notification: encrypted =  b'\xc7t\xcd\x98H\xa3\xe8\x01\xf2\x04\x84P&\x0b\x91\x8c' - decrypted = bytearray(b'\x04\x00\x18\x00h\xa6\xd7A\x00\x00\x00\x00') - gsn=4 - iid=24 - value=bytearray(b'h\xa6\xd7A\x00\x00\x00\x00')
2022-10-22 21:21:17.175 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Eve [E4E430D0-32B3-4A10-8776-2382924DFBC0] (id=F2:1D:F1:99:54:E5): Received notification: results = {(1, 24): {'value': 26.956253051757812}}

@bdraco
Copy link
Collaborator Author

bdraco commented Oct 22, 2022

verified its recovering ok when accessory isn't available at startup.

@bdraco bdraco merged commit 9ff8d68 into main Oct 22, 2022
@bdraco bdraco deleted the encrypted_notifications branch October 22, 2022 21:04
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