Skip to content

Instantly share code, notes, and snippets.

@Jc2k
Created July 15, 2022 14:17
Show Gist options
  • Save Jc2k/182814f5ef90de9bdc9519eb94c97305 to your computer and use it in GitHub Desktop.
Save Jc2k/182814f5ef90de9bdc9519eb94c97305 to your computer and use it in GitHub Desktop.
diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py
index ac840eb068..21631b9ec5 100644
--- a/homeassistant/components/homekit_controller/config_flow.py
+++ b/homeassistant/components/homekit_controller/config_flow.py
@@ -3,16 +3,18 @@ from __future__ import annotations
import logging
import re
+import struct
from typing import Any
import aiohomekit
+from aiohomekit.controller.ble.manufacturer_data import HomeKitAdvertisement
from aiohomekit.exceptions import AuthenticationError
from aiohomekit.model import Accessories, CharacteristicsTypes, ServicesTypes
from aiohomekit.utils import domain_supported, domain_to_name
import voluptuous as vol
from homeassistant import config_entries
-from homeassistant.components import zeroconf
+from homeassistant.components import bluetooth, zeroconf
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers import device_registry as dr
@@ -111,8 +113,10 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
if user_input is not None:
key = user_input["device"]
self.hkid = self.devices[key].description.id
- self.model = self.devices[key].description.model
- self.name = self.devices[key].description.name
+ self.model = getattr(
+ self.devices[key].description, "model", "Bluetooth device"
+ )
+ self.name = self.devices[key].description.name or "Bluetooth device"
await self.async_set_unique_id(
normalize_hkid(self.hkid), raise_on_progress=False
@@ -334,6 +338,40 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
# pairing code)
return self._async_step_pair_show_form()
+ async def async_step_bluetooth(
+ self, discovery_info: bluetooth.BluetoothServiceInfo
+ ) -> FlowResult:
+ """Handle the bluetooth discovery step."""
+ if not aiohomekit.const.BLE_TRANSPORT_SUPPORTED:
+ return self.async_abort(reason="ignored_model")
+
+ await self.async_set_unique_id(discovery_info.address)
+ self._abort_if_unique_id_configured()
+
+ mfr_data = discovery_info.manufacturer_data
+
+ try:
+ device = HomeKitAdvertisement.from_manufacturer_data(discovery_info.name, discovery_info.address, mfr_data)
+ except ValueError:
+ return self.async_abort(reason="ignored_model")
+
+ if self.controller is None:
+ await self._async_setup_controller()
+
+ try:
+ device = await self.controller.async_find(device.id)
+ except aiohomekit.AccessoryNotFoundError:
+ return self.async_abort(reason="accessory_not_found_error")
+
+ if device.paired:
+ return self.async_abort(reason="already_paired")
+
+ self.name = device.description.name
+ self.model = "Bluetooth device"
+ self.hkid = device.description.id
+
+ return self._async_step_pair_show_form()
+
async def async_step_pair(self, pair_info=None):
"""Pair with a new HomeKit accessory."""
# If async_step_pair is called with no pairing code then we do the M1
diff --git a/homeassistant/components/homekit_controller/manifest.json b/homeassistant/components/homekit_controller/manifest.json
index 0275afa07f..adbee17e51 100644
--- a/homeassistant/components/homekit_controller/manifest.json
+++ b/homeassistant/components/homekit_controller/manifest.json
@@ -5,7 +5,8 @@
"documentation": "https://www.home-assistant.io/integrations/homekit_controller",
"requirements": ["aiohomekit==1.0.0"],
"zeroconf": ["_hap._tcp.local.", "_hap._udp.local."],
- "after_dependencies": ["zeroconf"],
+ "bluetooth": [{"manufacturer_id": 76}],
+ "after_dependencies": ["zeroconf", "bluetooth"],
"codeowners": ["@Jc2k", "@bdraco"],
"iot_class": "local_push",
"loggers": ["aiohomekit", "commentjson"]
diff --git a/homeassistant/generated/bluetooth.py b/homeassistant/generated/bluetooth.py
index 49596c4773..b9c27e0754 100644
--- a/homeassistant/generated/bluetooth.py
+++ b/homeassistant/generated/bluetooth.py
@@ -7,6 +7,10 @@ from __future__ import annotations
# fmt: off
BLUETOOTH: list[dict[str, str | int]] = [
+ {
+ "domain": "homekit_controller",
+ "manufacturer_id": 76
+ },
{
"domain": "switchbot",
"service_uuid": "cba20d00-224d-11e6-9fb8-0002a5d5c51b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment