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