zigpy / zigpy-xbee

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

Channel energy scan #149

Closed Shulyaka closed 9 months ago

Shulyaka commented 9 months ago

Add support for per-channel energy detect for XBee coordinators.

Logs:

2023-09-26 09:05:26.779 DEBUG (MainThread) [zigpy_xbee.api] at command: ED (4,)
2023-09-26 09:05:26.779 DEBUG (MainThread) [zigpy_xbee.api] Command at (b'ED', b'\x00\x00\x00\x00')
2023-09-26 09:05:26.779 DEBUG (MainThread) [zigpy_xbee.uart] Sending: b'\x08\x14ED\x00\x00\x00\x00'
2023-09-26 09:05:27.153 DEBUG (MainThread) [homeassistant.bootstrap] Running timeout Zones: {'xbee_humidifier': <xbee_humidifier: 0 / 1>}
2023-09-26 09:05:27.154 DEBUG (MainThread) [homeassistant.bootstrap] Integration remaining: {'zha': 14.551212}
2023-09-26 09:05:27.467 DEBUG (MainThread) [zigpy_xbee.uart] Frame received: b'\x88\x14ED\x00/D1>\x06"\x16.*90IEKK7'
2023-09-26 09:05:27.467 DEBUG (MainThread) [zigpy_xbee.api] Frame received: at_response
2023-09-26 09:05:27.478 DEBUG (MainThread) [zigpy.application] Startup energy scan: {11: -47, 12: -68, 13: -49, 14: -62, 15: -6, 16: -34, 17: -22, 18: -46, 19: -42, 20: -57, 21: -48, 22: -73, 23: -69, 24: -75, 25: -75, 26: -55}

Prtially implements #112 though it is not a stand-alone tool.

puddly commented 9 months ago

The energy scanning API is expected to return a number between 0 and 255, with 0 being "low" and 255 being "high". This is to make values comparable across radios, since all others return a number 0-255.

In bellows, I implemented it like so: https://github.com/zigpy/bellows/blob/58f8a8079a6fa1d25fe536e5d9e3fcfcbf41389c/bellows/zigbee/util.py#L105-L132. RSSI_MIN and RSSI_MAX can be found above. I generated the test tones with bellows as well: bellows --baudrate 115200 -d /dev/serial/by-id/... tone -c 15 -p 20

Shulyaka commented 9 months ago

Thanks! Will update the code.

Shulyaka commented 9 months ago

Fixed. Using the same formula for compatibility (i.e. compare values between radios)

2023-09-26 17:50:06.980 DEBUG (MainThread) [zigpy_xbee.api] at command: ED (4,)
2023-09-26 17:50:06.980 DEBUG (MainThread) [zigpy_xbee.api] Command at (b'ED', b'\x00\x00\x00\x00')
2023-09-26 17:50:06.980 DEBUG (MainThread) [zigpy_xbee.uart] Sending: b'\x08\x14ED\x00\x00\x00\x00'
2023-09-26 17:50:07.669 DEBUG (MainThread) [zigpy_xbee.uart] Frame received: b'\x88\x14ED\x00QRGI;\x1d@%JCFEKLKM'
2023-09-26 17:50:07.669 DEBUG (MainThread) [zigpy_xbee.api] Frame received: at_response
2023-09-26 17:50:07.673 DEBUG (MainThread) [zigpy.application] Startup energy scan: {11: 6.400294434546168, 12: 5.637317837053025, 13: 22.00997262297472, 14: 17.31295187283794, 15: 79.08378338907617, 16: 244.0129080387192, 17: 48.47002474921316, 18: 226.18609483339876, 19: 15.32929809960923, 20: 34.963161543474534, 21: 24.768782664129375, 22: 27.832084152058194, 23: 13.559957792182884, 24: 11.984626537983768, 25: 13.559957792182884, 26: 10.584286741584284}
Shulyaka commented 9 months ago

Rebased and added tests

codecov[bot] commented 9 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (f387a0f) 98.86% compared to head (6b08af3) 99.13%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #149 +/- ## ========================================== + Coverage 98.86% 99.13% +0.26% ========================================== Files 6 6 Lines 793 807 +14 ========================================== + Hits 784 800 +16 + Misses 9 7 -2 ``` | [Files](https://app.codecov.io/gh/zigpy/zigpy-xbee/pull/149?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/149?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zigpy#diff-emlncHlfeGJlZS9hcGkucHk=) | `99.37% <ø> (ø)` | | | [zigpy\_xbee/zigbee/application.py](https://app.codecov.io/gh/zigpy/zigpy-xbee/pull/149?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zigpy#diff-emlncHlfeGJlZS96aWdiZWUvYXBwbGljYXRpb24ucHk=) | `97.78% <100.00%> (+1.08%)` | :arrow_up: |

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