From 0cadbb9931216590426f58b39bbc1be5b2584a4e Mon Sep 17 00:00:00 2001 From: SupaFlav <52390958+SupaFlav@users.noreply.github.com> Date: Mon, 28 Aug 2023 15:03:43 -0400 Subject: [PATCH 1/2] Remove extra connection attempt, rm callback debug login when not connected --- custom_components/bluetti_bt/binary_sensor.py | 4 ++++ custom_components/bluetti_bt/coordinator.py | 16 ---------------- custom_components/bluetti_bt/sensor.py | 4 ++++ custom_components/bluetti_bt/switch.py | 4 ++++ 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/custom_components/bluetti_bt/binary_sensor.py b/custom_components/bluetti_bt/binary_sensor.py index b177e1c..9395da9 100644 --- a/custom_components/bluetti_bt/binary_sensor.py +++ b/custom_components/bluetti_bt/binary_sensor.py @@ -104,6 +104,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" + + if not self.coordinator.client.is_connected: + return + _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id)) if not isinstance(self.coordinator.data, dict): _LOGGER.debug( diff --git a/custom_components/bluetti_bt/coordinator.py b/custom_components/bluetti_bt/coordinator.py index f8ded43..66caf77 100644 --- a/custom_components/bluetti_bt/coordinator.py +++ b/custom_components/bluetti_bt/coordinator.py @@ -179,22 +179,6 @@ class PollingCoordinator(DataUpdateCoordinator): parsed_data: dict = {} - # Connect to device - try: - async with async_timeout.timeout(20): - await self.client.connect() - - await self.client.start_notify( - BluetoothClient.NOTIFY_UUID, self._notification_handler - ) - self.has_notifier = True - except TimeoutError: - self.logger.debug("Connection timed out for device %s", mac_loggable(self._address)) - return None - except BleakError as err: - self.logger.debug("Bleak error: %s", err) - return None - try: async with async_timeout.timeout(15): diff --git a/custom_components/bluetti_bt/sensor.py b/custom_components/bluetti_bt/sensor.py index 9721f70..286f391 100644 --- a/custom_components/bluetti_bt/sensor.py +++ b/custom_components/bluetti_bt/sensor.py @@ -130,6 +130,10 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" + + if not self.coordinator.client.is_connected: + return + _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id)) if not isinstance(self.coordinator.data, dict): _LOGGER.debug( diff --git a/custom_components/bluetti_bt/switch.py b/custom_components/bluetti_bt/switch.py index 6e4d5f0..1f44dc4 100644 --- a/custom_components/bluetti_bt/switch.py +++ b/custom_components/bluetti_bt/switch.py @@ -114,6 +114,10 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity): @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" + + if not self.coordinator.client.is_connected: + return + _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id)) if not isinstance(self.coordinator.data, dict): _LOGGER.error( From 296c9cbfd357054845428f718cb87f7d5931cde0 Mon Sep 17 00:00:00 2001 From: SupaFlav <52390958+SupaFlav@users.noreply.github.com> Date: Mon, 28 Aug 2023 15:33:45 -0400 Subject: [PATCH 2/2] Ensure this only applies to persistent connections --- custom_components/bluetti_bt/binary_sensor.py | 4 ++-- custom_components/bluetti_bt/sensor.py | 2 +- custom_components/bluetti_bt/switch.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/custom_components/bluetti_bt/binary_sensor.py b/custom_components/bluetti_bt/binary_sensor.py index 9395da9..a52a269 100644 --- a/custom_components/bluetti_bt/binary_sensor.py +++ b/custom_components/bluetti_bt/binary_sensor.py @@ -105,8 +105,8 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - if not self.coordinator.client.is_connected: - return + if self.coordinator.persistent_conn and not self.coordinator.client.is_connected: + return _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id)) if not isinstance(self.coordinator.data, dict): diff --git a/custom_components/bluetti_bt/sensor.py b/custom_components/bluetti_bt/sensor.py index 286f391..9dedc9b 100644 --- a/custom_components/bluetti_bt/sensor.py +++ b/custom_components/bluetti_bt/sensor.py @@ -131,7 +131,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - if not self.coordinator.client.is_connected: + if self.coordinator.persistent_conn and not self.coordinator.client.is_connected: return _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id)) diff --git a/custom_components/bluetti_bt/switch.py b/custom_components/bluetti_bt/switch.py index 1f44dc4..1ca2c69 100644 --- a/custom_components/bluetti_bt/switch.py +++ b/custom_components/bluetti_bt/switch.py @@ -115,7 +115,7 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity): def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - if not self.coordinator.client.is_connected: + if self.coordinator.persistent_conn and not self.coordinator.client.is_connected: return _LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))