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

Adjust locking to avoid data errors #97

Merged
merged 81 commits into from Jul 15, 2022
Merged

Adjust locking to avoid data errors #97

merged 81 commits into from Jul 15, 2022

Conversation

bdraco
Copy link
Collaborator

@bdraco bdraco commented Jul 14, 2022

No description provided.

@codecov
Copy link

codecov bot commented Jul 14, 2022

Codecov Report

Merging #97 (5ca6f2e) into main (d0cd358) will decrease coverage by 1.55%.
The diff coverage is 3.38%.

@@            Coverage Diff             @@
##             main      #97      +/-   ##
==========================================
- Coverage   68.18%   66.62%   -1.56%     
==========================================
  Files          67       67              
  Lines        5264     5397     +133     
==========================================
+ Hits         3589     3596       +7     
- Misses       1675     1801     +126     
Flag Coverage Δ
unittests 66.62% <3.38%> (-1.56%) ⬇️

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

Impacted Files Coverage Δ
aiohomekit/controller/ble/client.py 0.00% <0.00%> (ø)
aiohomekit/controller/ble/const.py 0.00% <0.00%> (ø)
aiohomekit/controller/ble/discovery.py 0.00% <0.00%> (ø)
aiohomekit/controller/ble/pairing.py 0.00% <0.00%> (ø)
aiohomekit/model/__init__.py 96.15% <100.00%> (+0.10%) ⬆️
aiohomekit/protocol/tlv.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0cd358...5ca6f2e. Read the comment docs.

@bdraco
Copy link
Collaborator Author

bdraco commented Jul 15, 2022

Getting the MTU doesn't seem to work

https://github.com/hbldh/bleak/blob/master/examples/mtu_size.py

2022-07-14 19:15:59.961 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry  for homekit_controller                                                                       
Traceback (most recent call last):                                                                                                                                                           
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 533, in _acquire_mtu                                                                                               
    char = next(                                                                                                                                                                                           
StopIteration                                                                                                                                                                                      
                                                                                                                                                                                         
During handling of the above exception, another exception occurred:                                                                                                                            
                                                                                                                                                                                                                                                   
Traceback (most recent call last):                                                                                                                                                           
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 540, in _acquire_mtu                                                                               
    char = next(                                                                                                                                                                                                                                   
StopIteration                                                                                                                                                                                                                               
                                                                                                                                                                                                                   
The above exception was the direct cause of the following exception:                                                                                                                           
                                                                                                                                                                                                               
Traceback (most recent call last):                                                                                                                                               
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 356, in async_setup                                                                                                                      
    result = await component.async_setup_entry(hass, self)                                                                                                                       
  File "/usr/src/homeassistant/homeassistant/components/homekit_controller/__init__.py", line 228, in async_setup_entry                                                                              
    if not await conn.async_setup():                                                                                                                                            
  File "/usr/src/homeassistant/homeassistant/components/homekit_controller/connection.py", line 207, in async_setup                                                                                                
    if not await self.async_ensure_available():                                                                                                                            
  File "/usr/src/homeassistant/homeassistant/components/homekit_controller/connection.py", line 189, in async_ensure_available                                                             
    await self.pairing.get_characteristics([(aid, iid)])                                                                                                                               
  File "/usr/local/lib/python3.10/site-packages/aiohomekit/controller/ble/pairing.py", line 355, in get_characteristics                                                          
    await self._ensure_connected()                                                                                                                                                                         
  File "/usr/local/lib/python3.10/site-packages/aiohomekit/controller/ble/pairing.py", line 173, in _ensure_connected                                                                                  
    await self.client._acquire_mtu()                                                                                                                                                           
RuntimeError: coroutine raised StopIteration     

@bdraco
Copy link
Collaborator Author

bdraco commented Jul 15, 2022

Seems to be mostly working now

the config num needs to be figured out since when you pair it has to sync 3 times now because we don't remember that

Comment on lines +78 to +91
while not self.client.is_connected:
logger.debug("Connecting to %s", self.device)
try:
await self.client.connect()
break
except BleakError as e:
logger.debug(
"Failed to connect to %s: %s", self.client.address, str(e)
)

if self.description.address != self.client.address:
self.client = BleakClient(self.description.address)

await asyncio.sleep(5)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This probably shouldn't try forever

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah it blocks shutdown

2022-07-15 02:54:59.770 ERROR (MainThread) [homeassistant.components.hassio.handler] Timeout on /homeassistant/restart request
2022-07-15 02:55:06.038 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Failed to connect to FB:EC:42:A9:79:F9: Device with address FB:EC:42:A9:79:F9 was not found.
2022-07-15 02:55:11.040 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] FB:EC:42:A9:79:F9: Connecting
2022-07-15 02:55:21.137 DEBUG (MainThread) [aiohomekit.controller.ble.pairing] Failed to connect to FB:EC:42:A9:79:F9: Device with address FB:EC:42:A9:79:F9 was not found.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened an issue so we don't forget #98

)

async def _establish_connection(self):
address = self.address
while not self.client or not self.client.is_connected:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This probably shouldn't try forever

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened an issue so we don't forget #98

@bdraco
Copy link
Collaborator Author

bdraco commented Jul 15, 2022

We also need to avoid this line for BLE devices and use the local name for the name so we don't read all the chars 2x
https://github.com/home-assistant/core/blob/b4003713b67d3e83397382f4af5d10e8d9a94660/homeassistant/components/homekit_controller/config_flow.py#L484

@Jc2k
Copy link
Owner

Jc2k commented Jul 15, 2022

We also need to avoid this line for BLE devices and use the local name for the name so we don't read all the chars 2x

https://github.com/home-assistant/core/blob/b4003713b67d3e83397382f4af5d10e8d9a94660/homeassistant/components/homekit_controller/config_flow.py#L484

I've seen local name be too vague quite often. Eg for my own Eve devices their local name seems to be "Eve" (most of the time).

Think we should just make sure to only read the gatt database once and then retrieve the name from our cached copy so we don't even do a char read.

@bdraco
Copy link
Collaborator Author

bdraco commented Jul 15, 2022

We also need to avoid this line for BLE devices and use the local name for the name so we don't read all the chars 2x
home-assistant/core@b400371/homeassistant/components/homekit_controller/config_flow.py#L484

I've seen local name be too vague quite often. Eg for my own Eve devices their local name seems to be "Eve" (most of the time).

Think we should just make sure to only read the gatt database once and then retrieve the name from our cached copy so we don't even do a char read.

👍 I'll make getting the cache working a priority in the next PR

@bdraco bdraco marked this pull request as ready for review July 15, 2022 15:23
CharacteristicsTypes.PAIR_VERIFY,
get_session_keys(self.pairing_data, self._session_id, self._derive),
)
# FIXME: this should not be a broad except handler
Copy link
Owner

@Jc2k Jc2k Jul 15, 2022

Choose a reason for hiding this comment

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

I fixed the obvious bug in resume_m3, so I don't think we need this try/except block any more.

The resume handshake is supposed to be graceful in that if a device doesn't support resume or resume fails, the device will act as though we are already doing a full pair verify. So we should just be able to code defensively and fall through into a normal pair resume. There is logic to do that, but i wasn't handling the case where the method field wasn't present at all, which I do now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will adjust in the next run

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

#99

@bdraco bdraco merged commit f817cef into main Jul 15, 2022
@bdraco bdraco deleted the locking branch July 15, 2022 15:53
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.

None yet

2 participants