Skip to content

Commit

Permalink
fix: remove mac workaround (#115)
Browse files Browse the repository at this point in the history
* fix: remove mac workaround
  • Loading branch information
Ernst79 authored Mar 18, 2024
1 parent 9999e21 commit 303f833
Showing 1 changed file with 0 additions and 33 deletions.
33 changes: 0 additions & 33 deletions src/bthome_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import logging
import struct
import sys
from datetime import datetime, timezone
from enum import Enum
from typing import Any
Expand Down Expand Up @@ -145,11 +144,6 @@ def __init__(self, bindkey: bytes | None = None) -> None:
# The packet_id is used to filter duplicate messages in BTHome V2.
self.packet_id: float | None = 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 @@ -183,19 +177,6 @@ def set_bindkey(self, bindkey: bytes | None) -> None:
def supported(self, data: BluetoothServiceInfoBleak) -> bool:
if not super().supported(data):
return False

# Where a device uses encryption we need to know 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: BluetoothServiceInfoBleak) -> None:
Expand Down Expand Up @@ -254,14 +235,7 @@ def _parse_bthome_v1(
# Encrypted BTHome BLE format
self.encryption_scheme = EncryptionScheme.BTHOME_BINDKEY
self.set_device_sw_version("BTHome BLE v1 (encrypted)")

mac_readable = service_info.address
if len(mac_readable) != 17 and mac_readable[2] != ":":
# On macOS, we get a UUID, which is useless for BTHome sensors
self.mac_known = False
return False
else:
self.mac_known = True
source_mac = bytes.fromhex(mac_readable.replace(":", ""))

try:
Expand Down Expand Up @@ -359,13 +333,6 @@ def _parse_bthome_v2(
self.set_device_type(device_type)

if self.encryption_scheme == EncryptionScheme.BTHOME_BINDKEY:
if len(mac_readable) != 17 and mac_readable[2] != ":":
# On macOS, we get a UUID, which is useless for BTHome sensors
# Unless the MAC address is specified in the payload
self.mac_known = False
return False
else:
self.mac_known = True
bthome_mac = bytes.fromhex(mac_readable.replace(":", ""))
# Decode encrypted payload
try:
Expand Down

0 comments on commit 303f833

Please sign in to comment.