From 2ed6f370ecae5da6469c59207e56be57cbacb54b Mon Sep 17 00:00:00 2001 From: Ronen Gruengras Date: Thu, 30 May 2024 01:23:03 +0300 Subject: [PATCH 1/2] Call device.connect() aftre creating NukiDevice In latest pyNukiBT version, we need to call device.connect() so the correct nuki device_type can be identified. This was done to improve the retry mechanism. Plus, this way we can make sure HA's BT is able to connect to the Nuki device before we try more complex operations. And can fail properly if no connection can be made. --- custom_components/hass_nuki_bt/__init__.py | 5 +++++ custom_components/hass_nuki_bt/config_flow.py | 1 + custom_components/hass_nuki_bt/coordinator.py | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/custom_components/hass_nuki_bt/__init__.py b/custom_components/hass_nuki_bt/__init__.py index 1dabeba..a490345 100644 --- a/custom_components/hass_nuki_bt/__init__.py +++ b/custom_components/hass_nuki_bt/__init__.py @@ -5,6 +5,7 @@ """ from __future__ import annotations import logging +from bleak import BleakError from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform, CONF_NAME, CONF_PIN @@ -70,6 +71,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass, addr, connectable=True ), ) + try: + await device.connect() + except BleakError: + raise ConfigEntryNotReady(f"Could not connect to {address}") hass.data[DOMAIN][entry.entry_id] = coordinator = NukiDataUpdateCoordinator( hass=hass, diff --git a/custom_components/hass_nuki_bt/config_flow.py b/custom_components/hass_nuki_bt/config_flow.py index e3d33b7..76fe5d4 100644 --- a/custom_components/hass_nuki_bt/config_flow.py +++ b/custom_components/hass_nuki_bt/config_flow.py @@ -155,6 +155,7 @@ async def async_step_pair( self.hass, addr, connectable=True ), ) + await device.connect() try: ret = await device.pair() except NukiErrorException as ex: diff --git a/custom_components/hass_nuki_bt/coordinator.py b/custom_components/hass_nuki_bt/coordinator.py index 18d7e3c..9834bb0 100644 --- a/custom_components/hass_nuki_bt/coordinator.py +++ b/custom_components/hass_nuki_bt/coordinator.py @@ -107,10 +107,12 @@ async def _async_update( self, service_info: bluetooth.BluetoothServiceInfoBleak = None ) -> None: """Poll the device.""" + if service_info: + self.device.set_ble_device(service_info.device) await self.device.update_state() if self._security_pin: - # get the latest log enrty - # todo: check if Nuki looging is enabled + # get the latest log entry + # todo: check if Nuki logging is enabled logs = await self.device.request_log_entries( security_pin=self._security_pin, count=1 ) From 1db8f720b10ef88de94af7183152cc8883df5bfd Mon Sep 17 00:00:00 2001 From: Ronen Gruengras Date: Thu, 30 May 2024 01:28:59 +0300 Subject: [PATCH 2/2] Bump ronengr/pyNukiBT to 0.0.13 --- custom_components/hass_nuki_bt/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/hass_nuki_bt/manifest.json b/custom_components/hass_nuki_bt/manifest.json index afa088c..83de45f 100644 --- a/custom_components/hass_nuki_bt/manifest.json +++ b/custom_components/hass_nuki_bt/manifest.json @@ -20,7 +20,7 @@ "pyNukiBT" ], "requirements": [ - "pyNukiBT==0.0.12" + "pyNukiBT==0.0.13" ], "version": "0.0.0" }