From e1d4446fb8f8db5aa9b04c2935f58ddd0fdb21b7 Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Tue, 1 Aug 2023 19:46:48 +0200 Subject: [PATCH 1/2] updated sensor availability detection --- custom_components/bluetti_bt/binary_sensor.py | 12 +++++------- custom_components/bluetti_bt/sensor.py | 15 +++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/custom_components/bluetti_bt/binary_sensor.py b/custom_components/bluetti_bt/binary_sensor.py index 58e5622..50df6b2 100644 --- a/custom_components/bluetti_bt/binary_sensor.py +++ b/custom_components/bluetti_bt/binary_sensor.py @@ -4,7 +4,6 @@ from __future__ import annotations import logging -from homeassistant.components import bluetooth from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -89,15 +88,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): self._attr_device_info = device_info self._attr_name = e_name + self._attr_available = False self._attr_unique_id = get_unique_id(e_name) self._attr_entity_category = category - @property - def available(self) -> bool: - if self._address is None: - return False - return bluetooth.async_address_present(self.hass, self._address) - @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" @@ -106,10 +100,12 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): _LOGGER.error( "Invalid data from coordinator (sensor.%s)", self._attr_unique_id ) + self._attr_available = False return response_data = self.coordinator.data.get(self._response_key) if response_data is None: + self._attr_available = False return if not isinstance(response_data, bool): @@ -118,7 +114,9 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): self._attr_unique_id, response_data, ) + self._attr_available = False return + self._attr_available = True self._attr_is_on = self.coordinator.data[self._response_key] is True self.async_write_ha_state() diff --git a/custom_components/bluetti_bt/sensor.py b/custom_components/bluetti_bt/sensor.py index e72dcde..4586ee1 100644 --- a/custom_components/bluetti_bt/sensor.py +++ b/custom_components/bluetti_bt/sensor.py @@ -3,9 +3,8 @@ from __future__ import annotations import logging -from decimal import * +from decimal import Decimal -from homeassistant.components import bluetooth from homeassistant.components.sensor import SensorEntity, CONF_STATE_CLASS from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -112,6 +111,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): self._attr_device_info = device_info self._attr_name = e_name + self._attr_available = False self._attr_unique_id = get_unique_id(e_name) self._attr_native_unit_of_measurement = unit_of_measurement self._attr_device_class = device_class @@ -119,12 +119,6 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): self._attr_entity_category = category self._options = options - @property - def available(self) -> bool: - if self._address is None: - return False - return bluetooth.async_address_present(self.hass, self._address) - @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" @@ -133,10 +127,12 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): _LOGGER.error( "Invalid data from coordinator (sensor.%s)", self._attr_unique_id ) + self._attr_available = False return response_data = self.coordinator.data.get(self._response_key) if response_data is None: + self._attr_available = False return if ( @@ -151,8 +147,11 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): response_data, type(response_data), ) + self._attr_available = False return + self._attr_available = True + # Different for enum and numeric if ( self._options is not None From 92c130b2d6d8e2752cabd52c4e0efa59fadc54db Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Tue, 1 Aug 2023 19:46:48 +0200 Subject: [PATCH 2/2] updated sensor availability detection --- custom_components/bluetti_bt/binary_sensor.py | 12 +++++------- custom_components/bluetti_bt/sensor.py | 15 +++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/custom_components/bluetti_bt/binary_sensor.py b/custom_components/bluetti_bt/binary_sensor.py index 93fbfcb..f2e53d0 100644 --- a/custom_components/bluetti_bt/binary_sensor.py +++ b/custom_components/bluetti_bt/binary_sensor.py @@ -4,7 +4,6 @@ from __future__ import annotations import logging -from homeassistant.components import bluetooth from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -97,15 +96,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): self._attr_device_info = device_info self._attr_name = e_name + self._attr_available = False self._attr_unique_id = get_unique_id(e_name) self._attr_entity_category = category - @property - def available(self) -> bool: - if self._address is None: - return False - return bluetooth.async_address_present(self.hass, self._address) - @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" @@ -114,10 +108,12 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): _LOGGER.error( "Invalid data from coordinator (sensor.%s)", self._attr_unique_id ) + self._attr_available = False return response_data = self.coordinator.data.get(self._response_key) if response_data is None: + self._attr_available = False return if not isinstance(response_data, bool): @@ -126,7 +122,9 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity): self._attr_unique_id, response_data, ) + self._attr_available = False return + self._attr_available = True self._attr_is_on = self.coordinator.data[self._response_key] is True self.async_write_ha_state() diff --git a/custom_components/bluetti_bt/sensor.py b/custom_components/bluetti_bt/sensor.py index 5bb825f..fa32848 100644 --- a/custom_components/bluetti_bt/sensor.py +++ b/custom_components/bluetti_bt/sensor.py @@ -3,9 +3,8 @@ from __future__ import annotations import logging -from decimal import * +from decimal import Decimal -from homeassistant.components import bluetooth from homeassistant.components.sensor import SensorEntity, CONF_STATE_CLASS from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -118,6 +117,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): self._attr_device_info = device_info self._attr_name = e_name + self._attr_available = False self._attr_unique_id = get_unique_id(e_name) self._attr_native_unit_of_measurement = unit_of_measurement self._attr_device_class = device_class @@ -125,12 +125,6 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): self._attr_entity_category = category self._options = options - @property - def available(self) -> bool: - if self._address is None: - return False - return bluetooth.async_address_present(self.hass, self._address) - @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" @@ -139,10 +133,12 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): _LOGGER.error( "Invalid data from coordinator (sensor.%s)", self._attr_unique_id ) + self._attr_available = False return response_data = self.coordinator.data.get(self._response_key) if response_data is None: + self._attr_available = False return if ( @@ -157,8 +153,11 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): response_data, type(response_data), ) + self._attr_available = False return + self._attr_available = True + # Different for enum and numeric if ( self._options is not None