Skip to content

Commit

Permalink
Merge pull request #74 from Abe-Telo/main
Browse files Browse the repository at this point in the history
This helps with adding BT Devices.
  • Loading branch information
dave-code-ruiz authored Nov 27, 2024
2 parents 9a4e512 + bde5a83 commit 917e487
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
24 changes: 22 additions & 2 deletions custom_components/elkbledom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,40 @@ def __init__(self) -> None:
self._discovery_info: BluetoothServiceInfoBleak | None = None
self._discovered_devices = []

# async def async_step_bluetooth(
# self, discovery_info: BluetoothServiceInfoBleak
# ) -> FlowResult:
# """Handle the bluetooth discovery step."""
# LOGGER.debug("Discovered bluetooth devices, step bluetooth, : %s , %s", discovery_info.address, discovery_info.name)
# await self.async_set_unique_id(discovery_info.address)
# self._abort_if_unique_id_configured()
# device = DeviceData(self.hass, discovery_info)
# if device.is_supported:
# self._discovered_devices.append(device)
# return await self.async_step_bluetooth_confirm()
# else:
# return self.async_abort(reason="not_supported")

async def async_step_bluetooth(
self, discovery_info: BluetoothServiceInfoBleak
) -> FlowResult:
"""Handle the bluetooth discovery step."""
LOGGER.debug("Discovered bluetooth devices, step bluetooth, : %s , %s", discovery_info.address, discovery_info.name)
LOGGER.debug("Discovered device: address=%s, name=%s", discovery_info.address, discovery_info.name)
if not discovery_info.address or not discovery_info.name:
LOGGER.error("Invalid discovery info: %s", discovery_info)
return self.async_abort(reason="invalid_discovery_info")

await self.async_set_unique_id(discovery_info.address)
self._abort_if_unique_id_configured()
device = DeviceData(self.hass, discovery_info)
if device.is_supported:
self._discovered_devices.append(device)
return await self.async_step_bluetooth_confirm()
else:
LOGGER.debug("Device not supported: %s", discovery_info.name)
return self.async_abort(reason="not_supported")



async def async_step_bluetooth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
Expand Down
11 changes: 8 additions & 3 deletions custom_components/elkbledom/elkbledom.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,21 @@ async def _execute_timed_disconnect(self) -> None:
)
await self._execute_disconnect()


async def _execute_disconnect(self) -> None:
"""Execute disconnection."""
async with self._connect_lock:
read_char = self._read_uuid
client = self._client
LOGGER.debug("Disconnecting: READ_UUID=%s, CLIENT_CONNECTED=%s", read_char, client.is_connected if client else "No Client")
self._expected_disconnect = True
self._client = None
self._write_uuid = None
self._read_uuid = None
if client and client.is_connected:
if not self._device.name.lower().startswith("melk"):
await client.stop_notify(read_char)
await client.disconnect()
try:
if not self._device.name.lower().startswith("melk"):
await client.stop_notify(read_char)
await client.disconnect()
except Exception as e:
LOGGER.error("Error during disconnection: %s", e)

0 comments on commit 917e487

Please sign in to comment.