fix availability states
This commit is contained in:
1 parent
f5b8e09fb4
commit
b2672ec302
5 files changed
+49
-10
No files matched your search
@@ -100,6 +100,11 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
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:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
@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."""
|
||||||
@@ -113,11 +118,13 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
"Invalid data from coordinator (binary_sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
"Invalid data from coordinator (binary_sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
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
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(response_data, bool):
|
if not isinstance(response_data, bool):
|
||||||
@@ -127,6 +134,7 @@ class BluettiBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|||||||
response_data,
|
response_data,
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ class DeviceReader:
|
|||||||
_LOGGER.warning("Got a parse exception...")
|
_LOGGER.warning("Got a parse exception...")
|
||||||
|
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
_LOGGER.error("Polling timed out")
|
_LOGGER.warning("Polling timed out. Trying again later")
|
||||||
return None
|
return None
|
||||||
except BleakError as err:
|
except BleakError as err:
|
||||||
_LOGGER.error("Bleak error: %s", err)
|
_LOGGER.error("Bleak error: %s", err)
|
||||||
@@ -153,10 +153,18 @@ class DeviceReader:
|
|||||||
# Disconnect if connection not persistant
|
# Disconnect if connection not persistant
|
||||||
if not self.persistent_conn:
|
if not self.persistent_conn:
|
||||||
if self.has_notifier:
|
if self.has_notifier:
|
||||||
await self.client.stop_notify(NOTIFY_UUID)
|
try:
|
||||||
|
await self.client.stop_notify(NOTIFY_UUID)
|
||||||
|
except:
|
||||||
|
# Ignore errors here
|
||||||
|
pass
|
||||||
self.has_notifier = False
|
self.has_notifier = False
|
||||||
await self.client.disconnect()
|
await self.client.disconnect()
|
||||||
|
|
||||||
|
# Check if dict is empty
|
||||||
|
if not parsed_data:
|
||||||
|
return None
|
||||||
|
|
||||||
return parsed_data
|
return parsed_data
|
||||||
|
|
||||||
async def _async_send_command(self, command: ReadHoldingRegisters) -> bytes:
|
async def _async_send_command(self, command: ReadHoldingRegisters) -> bytes:
|
||||||
@@ -187,10 +195,8 @@ class DeviceReader:
|
|||||||
err,
|
err,
|
||||||
)
|
)
|
||||||
except (BadConnectionError, BleakError) as err:
|
except (BadConnectionError, BleakError) as err:
|
||||||
_LOGGER.warning(
|
# Ignore other errors
|
||||||
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)",
|
pass
|
||||||
err,
|
|
||||||
)
|
|
||||||
|
|
||||||
# caught an exception, return empty bytes object
|
# caught an exception, return empty bytes object
|
||||||
return bytes()
|
return bytes()
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
update_interval=timedelta(seconds=polling_interval),
|
update_interval=timedelta(seconds=polling_interval),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.address = address
|
||||||
|
|
||||||
# Create client
|
# Create client
|
||||||
self.logger.debug("Creating client")
|
self.logger.debug("Creating client")
|
||||||
device = bluetooth.async_ble_device_from_address(hass, address)
|
device = bluetooth.async_ble_device_from_address(hass, address)
|
||||||
@@ -67,4 +69,10 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
so entities can quickly look up their data.
|
so entities can quickly look up their data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Check if device is connected
|
||||||
|
if bluetooth.async_address_present(self.hass, self.address, connectable=True) is False:
|
||||||
|
self.logger.warning("Device not connected")
|
||||||
|
self.last_update_success = False
|
||||||
|
return None
|
||||||
|
|
||||||
return await self.reader.read_data()
|
return await self.reader.read_data()
|
||||||
@@ -125,6 +125,11 @@ 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:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
@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 +138,11 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if self.coordinator.data is None:
|
if self.coordinator.data is None:
|
||||||
_LOGGER.warning(
|
_LOGGER.debug(
|
||||||
"Data from coordinator is None. Skipping update for %s",
|
"Data from coordinator is None",
|
||||||
unique_id_loggable(self._attr_unique_id)
|
|
||||||
)
|
)
|
||||||
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
||||||
@@ -145,12 +151,14 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
"Invalid data from coordinator (sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
"Invalid data from coordinator (sensor.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
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:
|
||||||
_LOGGER.warning("No data for available for (%s)", self._response_key)
|
_LOGGER.warning("No data for available for (%s)", self._response_key)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -167,6 +175,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
type(response_data),
|
type(response_data),
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
|
|||||||
@@ -108,6 +108,11 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
self._attr_entity_category = category
|
self._attr_entity_category = category
|
||||||
self._attr_device_class = SwitchDeviceClass.OUTLET
|
self._attr_device_class = SwitchDeviceClass.OUTLET
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self._attr_available
|
||||||
|
|
||||||
@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."""
|
||||||
@@ -117,15 +122,17 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
|
|
||||||
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
_LOGGER.debug("Updating state of %s", unique_id_loggable(self._attr_unique_id))
|
||||||
if not isinstance(self.coordinator.data, dict):
|
if not isinstance(self.coordinator.data, dict):
|
||||||
_LOGGER.error(
|
_LOGGER.debug(
|
||||||
"Invalid data from coordinator (switch.%s)", unique_id_loggable(self._attr_unique_id)
|
"Invalid data from coordinator (switch.%s)", unique_id_loggable(self._attr_unique_id)
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
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
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isinstance(response_data, bool):
|
if not isinstance(response_data, bool):
|
||||||
@@ -135,6 +142,7 @@ class BluettiSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
response_data,
|
response_data,
|
||||||
)
|
)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
|
|||||||
Reference in new issue
Block a user