updated sensor availability detection

This commit is contained in:
Patrick762 committed 2023-08-01 19:46:48 +02:00
1 parent b7c705f224
commit e1d4446fb8
2 files changed
+12 -15

No files matched your search

@@ -4,7 +4,6 @@ from __future__ import annotations
import logging import logging
from homeassistant.components import bluetooth
from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@@ -89,15 +88,10 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
self._attr_device_info = device_info self._attr_device_info = device_info
self._attr_name = e_name self._attr_name = e_name
self._attr_available = False
self._attr_unique_id = get_unique_id(e_name) self._attr_unique_id = get_unique_id(e_name)
self._attr_entity_category = category 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 @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator.""" """Handle updated data from the coordinator."""
@@ -106,10 +100,12 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
_LOGGER.error( _LOGGER.error(
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id "Invalid data from coordinator (sensor.%s)", self._attr_unique_id
) )
self._attr_available = False
return return
response_data = self.coordinator.data.get(self._response_key) response_data = self.coordinator.data.get(self._response_key)
if response_data is None: if response_data is None:
self._attr_available = False
return return
if not isinstance(response_data, bool): if not isinstance(response_data, bool):
@@ -118,7 +114,9 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
self._attr_unique_id, self._attr_unique_id,
response_data, response_data,
) )
self._attr_available = False
return return
self._attr_available = True
self._attr_is_on = self.coordinator.data[self._response_key] is True self._attr_is_on = self.coordinator.data[self._response_key] is True
self.async_write_ha_state() self.async_write_ha_state()
+7 -8
View File
@@ -3,9 +3,8 @@
from __future__ import annotations from __future__ import annotations
import logging 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.components.sensor import SensorEntity, CONF_STATE_CLASS
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@@ -112,6 +111,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
self._attr_device_info = device_info self._attr_device_info = device_info
self._attr_name = e_name self._attr_name = e_name
self._attr_available = False
self._attr_unique_id = get_unique_id(e_name) self._attr_unique_id = get_unique_id(e_name)
self._attr_native_unit_of_measurement = unit_of_measurement self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_device_class = device_class self._attr_device_class = device_class
@@ -119,12 +119,6 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
self._attr_entity_category = category self._attr_entity_category = category
self._options = options 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 @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator.""" """Handle updated data from the coordinator."""
@@ -133,10 +127,12 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
_LOGGER.error( _LOGGER.error(
"Invalid data from coordinator (sensor.%s)", self._attr_unique_id "Invalid data from coordinator (sensor.%s)", self._attr_unique_id
) )
self._attr_available = False
return return
response_data = self.coordinator.data.get(self._response_key) response_data = self.coordinator.data.get(self._response_key)
if response_data is None: if response_data is None:
self._attr_available = False
return return
if ( if (
@@ -151,8 +147,11 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
response_data, response_data,
type(response_data), type(response_data),
) )
self._attr_available = False
return return
self._attr_available = True
# Different for enum and numeric # Different for enum and numeric
if ( if (
self._options is not None self._options is not None