Skip to content

Commit 2dd6b38

Browse files
Lash-Lagittins
andauthored
Add manufacturer to prefname. (#352)
* Add manufacturer to prefname. * Add slugify * Add more manufacture data * bugfix * Update coordinator.py Co-authored-by: Ashley Gittins <[email protected]> --------- Co-authored-by: Ashley Gittins <[email protected]>
1 parent e18727a commit 2dd6b38

File tree

2 files changed

+1369
-1
lines changed

2 files changed

+1369
-1
lines changed

custom_components/bermuda/coordinator.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import re
66
from collections.abc import Callable
77
from datetime import datetime, timedelta
8+
from pathlib import Path
89
from typing import TYPE_CHECKING, cast
910

1011
import voluptuous as vol
12+
import yaml
1113
from homeassistant.components import bluetooth
1214
from homeassistant.components.bluetooth import (
1315
MONOTONIC_TIME,
@@ -136,6 +138,16 @@ def __init__(
136138
self.stamp_last_update: float = 0 # Last time we ran an update, from MONOTONIC_TIME()
137139
self.stamp_last_prune: float = 0 # When we last pruned device list
138140

141+
self.member_uuids = {}
142+
def load_manufacturer_ids():
143+
"""Import yaml file containing manufacturer name mappings."""
144+
file_path = Path(__file__).parent / "manufacturer_identification" / "member_uuids.yaml"
145+
146+
with file_path.open("r") as f:
147+
member_uuids_yaml = yaml.safe_load(f)["uuids"]
148+
self.member_uuids = {hex(member["uuid"])[2:]: member["name"] for member in member_uuids_yaml}
149+
150+
hass.async_add_executor_job(load_manufacturer_ids)
139151
super().__init__(
140152
hass,
141153
_LOGGER,
@@ -581,11 +593,22 @@ async def _async_update_data(self):
581593
if device.local_name is None and service_info.advertisement.local_name:
582594
device.local_name = clean_charbuf(service_info.advertisement.local_name)
583595
device.manufacturer = device.manufacturer or service_info.manufacturer
596+
if device.manufacturer is None:
597+
if (
598+
service_info.service_uuids
599+
and (member_uuid := service_info.service_uuids[0][4:8]) in self.member_uuids
600+
):
601+
# https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/uuids/member_uuids.yaml
602+
device.manufacturer = self.member_uuids[member_uuid]
584603
device.connectable = service_info.connectable
585604

586605
# Try to make a nice name for prefname.
587606
if device.prefname is None or device.prefname.startswith(DOMAIN + "_"):
588-
device.prefname = device.name or device.local_name or DOMAIN + "_" + slugify(device.address)
607+
if device.manufacturer:
608+
default_prefix = f"{slugify(device.manufacturer)}"
609+
else:
610+
default_prefix = DOMAIN
611+
device.prefname = device.name or device.local_name or f"{default_prefix}_{slugify(device.address)}"
589612

590613
# Work through the scanner entries...
591614
matched_scanners = bluetooth.async_scanner_devices_by_address(self.hass, service_info.address, False)

0 commit comments

Comments
 (0)