Skip to content

Commit

Permalink
Add manufacturer to prefname. (#352)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Lash-L and agittins authored Nov 7, 2024
1 parent e18727a commit 2dd6b38
Show file tree
Hide file tree
Showing 2 changed files with 1,369 additions and 1 deletion.
25 changes: 24 additions & 1 deletion custom_components/bermuda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import re
from collections.abc import Callable
from datetime import datetime, timedelta
from pathlib import Path
from typing import TYPE_CHECKING, cast

import voluptuous as vol
import yaml
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth import (
MONOTONIC_TIME,
Expand Down Expand Up @@ -136,6 +138,16 @@ def __init__(
self.stamp_last_update: float = 0 # Last time we ran an update, from MONOTONIC_TIME()
self.stamp_last_prune: float = 0 # When we last pruned device list

self.member_uuids = {}
def load_manufacturer_ids():
"""Import yaml file containing manufacturer name mappings."""
file_path = Path(__file__).parent / "manufacturer_identification" / "member_uuids.yaml"

with file_path.open("r") as f:
member_uuids_yaml = yaml.safe_load(f)["uuids"]
self.member_uuids = {hex(member["uuid"])[2:]: member["name"] for member in member_uuids_yaml}

hass.async_add_executor_job(load_manufacturer_ids)
super().__init__(
hass,
_LOGGER,
Expand Down Expand Up @@ -581,11 +593,22 @@ async def _async_update_data(self):
if device.local_name is None and service_info.advertisement.local_name:
device.local_name = clean_charbuf(service_info.advertisement.local_name)
device.manufacturer = device.manufacturer or service_info.manufacturer
if device.manufacturer is None:
if (
service_info.service_uuids
and (member_uuid := service_info.service_uuids[0][4:8]) in self.member_uuids
):
# https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/uuids/member_uuids.yaml
device.manufacturer = self.member_uuids[member_uuid]
device.connectable = service_info.connectable

# Try to make a nice name for prefname.
if device.prefname is None or device.prefname.startswith(DOMAIN + "_"):
device.prefname = device.name or device.local_name or DOMAIN + "_" + slugify(device.address)
if device.manufacturer:
default_prefix = f"{slugify(device.manufacturer)}"
else:
default_prefix = DOMAIN
device.prefname = device.name or device.local_name or f"{default_prefix}_{slugify(device.address)}"

# Work through the scanner entries...
matched_scanners = bluetooth.async_scanner_devices_by_address(self.hass, service_info.address, False)
Expand Down
Loading

0 comments on commit 2dd6b38

Please sign in to comment.