Skip to content

Commit a5aadad

Browse files
authored
Add support for getting the bluetooth MAC form the primary MAC (#815)
* Add support for getting the bluetooth MAC form the primary MAC * Add support for getting the bluetooth MAC form the primary MAC * Add support for getting the bluetooth MAC form the primary MAC * Update aioshelly/rpc_device/utils.py
1 parent 290ac83 commit a5aadad

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

aioshelly/rpc_device/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Shelly Gen2 RPC based device."""
22

33
from .device import RpcDevice, RpcUpdateType
4+
from .utils import bluetooth_mac_from_primary_mac
45
from .wsrpc import WsServer
56

6-
__all__ = ["RpcDevice", "RpcUpdateType", "WsServer"]
7+
__all__ = ["RpcDevice", "RpcUpdateType", "WsServer", "bluetooth_mac_from_primary_mac"]

aioshelly/rpc_device/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Utilities for RPC devices."""
2+
3+
from __future__ import annotations
4+
5+
# The Bluetooth MAC address is the primary MAC address plus 2.
6+
# https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#mac-address
7+
8+
9+
def bluetooth_mac_from_primary_mac(primary_mac: str) -> str:
10+
"""Get Bluetooth MAC from primary MAC.
11+
12+
MAC address must be in format "[0-F]{16}"
13+
14+
:param primary_mac: Primary MAC address
15+
:return: Bluetooth MAC address
16+
"""
17+
return f"{(int(primary_mac, 16) + 2):012X}"

tests/rpc_device/test_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aioshelly import rpc_device
2+
3+
4+
def test_exports() -> None:
5+
"""Test objects are available at top level of rpc_device."""
6+
assert hasattr(rpc_device, "bluetooth_mac_from_primary_mac")
7+
assert hasattr(rpc_device, "RpcDevice")
8+
assert hasattr(rpc_device, "RpcUpdateType")
9+
assert hasattr(rpc_device, "WsServer")

tests/rpc_device/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aioshelly.rpc_device.utils import bluetooth_mac_from_primary_mac
2+
3+
4+
def test_bluetooth_mac_from_primary_mac() -> None:
5+
"""Test bluetooth_mac_from_primary_mac."""
6+
assert bluetooth_mac_from_primary_mac("0A1B2C3D4E5F") == "0A1B2C3D4E61"
7+
assert bluetooth_mac_from_primary_mac("0A1B2C3D4EA0") == "0A1B2C3D4EA2"
8+
assert bluetooth_mac_from_primary_mac("0A1B2C3D4EF0") == "0A1B2C3D4EF2"
9+
assert bluetooth_mac_from_primary_mac("0A1B2C3D4EC9") == "0A1B2C3D4ECB"

0 commit comments

Comments
 (0)