Skip to content

Commit

Permalink
fix: remove mac workaround (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 authored Mar 17, 2024
1 parent 07eae36 commit c5a900c
Showing 1 changed file with 1 addition and 25 deletions.
26 changes: 1 addition & 25 deletions src/xiaomi_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import logging
import math
import struct
import sys
from typing import Any

from bleak import BleakClient
Expand Down Expand Up @@ -1481,11 +1480,6 @@ def __init__(self, bindkey: bytes | None = None) -> None:
# frame.
self.encryption_scheme = EncryptionScheme.NONE

# If true, then we know the actual MAC of the device.
# On macOS, we don't unless the device includes it in the advertisement
# (CoreBluetooth uses UUID's generated by CoreBluetooth instead of the MAC)
self.mac_known = sys.platform != "darwin"

# If true then we have used the provided encryption key to decrypt at least
# one payload.
# If false then we have either not seen an encrypted payload, the key is wrong
Expand Down Expand Up @@ -1522,19 +1516,6 @@ def set_bindkey(self, bindkey: bytes | None) -> None:
def supported(self, data: BluetoothServiceInfo) -> bool:
if not super().supported(data):
return False

# Where a device uses encryption we need to known its actual MAC address.
# As the encryption uses it as part of the nonce.
# On macOS we instead only know its CoreBluetooth UUID.
# It seems its impossible to automatically get that in the general case.
# So devices do duplicate the MAC in the advertisement, we use that
# when we can on macOS.
# We may want to ask the user for the MAC address during config flow
# For now, just hide these devices for macOS users.
if self.encryption_scheme != EncryptionScheme.NONE:
if not self.mac_known:
return False

return True

def _start_update(self, service_info: BluetoothServiceInfo) -> None:
Expand Down Expand Up @@ -1594,10 +1575,6 @@ def _parse_xiaomi(
return False

mac_readable = service_info.address
if len(mac_readable) != 17 and mac_readable[2] != ":":
# On macOS we get a UUID, which is useless for MiBeacons
mac_readable = "00:00:00:00:00:00"

source_mac = bytes.fromhex(mac_readable.replace(":", ""))

# extract frame control bits
Expand Down Expand Up @@ -1637,14 +1614,13 @@ def _parse_xiaomi(
return False
xiaomi_mac_reversed = data[5:11]
xiaomi_mac = xiaomi_mac_reversed[::-1]
if sys.platform != "darwin" and xiaomi_mac != source_mac:
if xiaomi_mac != source_mac:
_LOGGER.debug(
"MAC address doesn't match data frame. Expected: %s, Got: %s)",
to_mac(xiaomi_mac),
to_mac(source_mac),
)
return False
self.mac_known = True
else:
xiaomi_mac = source_mac

Expand Down

0 comments on commit c5a900c

Please sign in to comment.