updated sensor availability detection
This commit is contained in:
1 parent
f8e382347e
commit
92c130b2d6
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
|
||||||
@@ -97,15 +96,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."""
|
||||||
@@ -114,10 +108,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):
|
||||||
@@ -126,7 +122,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()
|
||||||
@@ -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
|
||||||
@@ -118,6 +117,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
|
||||||
@@ -125,12 +125,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."""
|
||||||
@@ -139,10 +133,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 (
|
||||||
@@ -157,8 +153,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
|
||||||
|
|||||||
Reference in new issue
Block a user