zigpy / zigpy-xbee

A library which communicates with XBee radios for zigpy
GNU General Public License v3.0
22 stars 18 forks source link

Joining with link key #150

Closed Shulyaka closed 9 months ago

Shulyaka commented 9 months ago

Add support for device joining via install codes.

Successful logs:

INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Running websocket_api script
INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Executing step call service
DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=zha, service=permit, service_data=duration=60, install_code=C9A7D2441A711695CD62170D3328EA2B423D, source_ieee=0013A200419823F9>
INFO (MainThread) [homeassistant.components.zha.websocket_api] Allowing join for 00:13:a2:00:41:98:23:f9 device with install code
DEBUG (MainThread) [zigpy_xbee.api] at command: KT (60,)
DEBUG (MainThread) [zigpy_xbee.api] Command at (b'KT', b'\x00<')
DEBUG (MainThread) [zigpy_xbee.uart] Sending: b'\x082KT\x00<'
DEBUG (MainThread) [zigpy_xbee.uart] Frame received: b'\x882KT\x00'
DEBUG (MainThread) [zigpy_xbee.api] Frame received: at_response
DEBUG (MainThread) [zigpy_xbee.api] Command register_joining_device (00:13:a2:00:41:98:23:f9, 65534, 1, b'\xc9\xa7\xd2D\x1aq\x16\x95\xcdb\x17\r3(\xea+B=')
DEBUG (MainThread) [zigpy_xbee.uart] Sending: b'$3\x00\x13\xa2\x00A\x98#\xf9\xff\xfe\x01\xc9\xa7\xd2D\x1aq\x16\x95\xcdb\x17\r3(\xea+B='
DEBUG (MainThread) [zigpy_xbee.uart] Frame received: b'\xa43\x00'
DEBUG (MainThread) [zigpy_xbee.api] Frame received: registration_status
DEBUG (MainThread) [zigpy_xbee.api] Registration Status: SUCCESS

Unsuccessful logs (invalid IEEE provided):

INFO [homeassistant.helpers.script.websocket_api_script] websocket_api script: Running websocket_api script
INFO [homeassistant.helpers.script.websocket_api_script] websocket_api script: Executing step call service
DEBUG [homeassistant.core] Bus:Handling <Event call_service[L]: domain=zha, service=permit, service_data=duration=60, source_ieee=0000000000000000, install_code=C9A7D2441A711695CD62170D3328EA2B423D>
INFO [homeassistant.components.zha.websocket_api] Allowing join for 00:00:00:00:00:00:00:00 device with install code
DEBUG [zigpy_xbee.api] at command: KT (60,)
DEBUG [zigpy_xbee.api] Command at (b'KT', b'\x00<')
DEBUG [zigpy_xbee.uart] Sending: b'\x085KT\x00<'
DEBUG [zigpy_xbee.uart] Frame received: b'\x885KT\x00'
DEBUG [zigpy_xbee.api] Frame received: at_response
DEBUG [zigpy_xbee.api] Command register_joining_device (00:00:00:00:00:00:00:00, 65534, 1, b'\xc9\xa7\xd2D\x1aq\x16\x95\xcdb\x17\r3(\xea+B=')
DEBUG [zigpy_xbee.uart] Sending: b'$6\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x01\xc9\xa7\xd2D\x1aq\x16\x95\xcdb\x17\r3(\xea+B='
DEBUG [zigpy_xbee.uart] Frame received: b'\xa46\xb3'
DEBUG [zigpy_xbee.api] Frame received: registration_status
ERROR [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1: Registration Status: INVALID_ADDRESS
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/script.py", line 468, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/script.py", line 704, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/script.py", line 666, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/core.py", line 1969, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/core.py", line 2006, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/helpers/service.py", line 980, in admin_handler
    await result
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/zha/websocket_api.py", line 1256, in permit
    await application_controller.permit_with_key(
  File "/srv/homeassistant/lib/python3.11/site-packages/zigpy_xbee/zigbee/application.py", line 304, in permit_with_key
    await self._api.register_joining_device(node, reserved, key_type, code)
RuntimeError: Registration Status: INVALID_ADDRESS
codecov[bot] commented 9 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (098b19c) 99.87% compared to head (d1b4cd2) 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #150 +/- ## =========================================== + Coverage 99.87% 100.00% +0.12% =========================================== Files 6 6 Lines 807 830 +23 =========================================== + Hits 806 830 +24 + Misses 1 0 -1 ``` | [Files](https://app.codecov.io/gh/zigpy/zigpy-xbee/pull/150?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zigpy) | Coverage Δ | | |---|---|---| | [zigpy\_xbee/api.py](https://app.codecov.io/gh/zigpy/zigpy-xbee/pull/150?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zigpy#diff-emlncHlfeGJlZS9hcGkucHk=) | `100.00% <100.00%> (ø)` | | | [zigpy\_xbee/zigbee/application.py](https://app.codecov.io/gh/zigpy/zigpy-xbee/pull/150?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zigpy#diff-emlncHlfeGJlZS96aWdiZWUvYXBwbGljYXRpb24ucHk=) | `100.00% <100.00%> (+0.44%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

puddly commented 9 months ago

For this PR, I'm hoping to implement https://github.com/zigpy/zigpy/pull/1261, since the install code can be converted into a link key before getting to the radio library. It looks like XBee implements both at the moment with a simple enum to swap between the two?

I think it's enough to just implement joining with a key and provide a stub implementation of the old method.

For comparison, here's a PR for zigpy-znp implementing this: https://github.com/zigpy/zigpy-znp/pull/226/files

I'll make a PR removing the stub from radio libraries in the future.

Shulyaka commented 9 months ago

Yes, XBee supports both link keys and install codes, converting the code to the key on device. I will update the PR.

Shulyaka commented 9 months ago

Done. When a default permit_with_key method is implemented, we can delete the xbee method (and the key_type argument).