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

fix: remove mac workaround #75

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading