Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This helps with adding BT Devices. #74

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)