Fix xiaomi_ble not remembering a device is a sleepy device (#97518)

This commit is contained in:
J. Nick Koston 2023-07-31 01:02:36 -07:00 committed by GitHub
parent 7bda873c2a
commit 28bebf338f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 9 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.helpers.device_registry import DeviceRegistry, async_get
from .const import ( from .const import (
CONF_DISCOVERED_EVENT_CLASSES, CONF_DISCOVERED_EVENT_CLASSES,
CONF_SLEEPY_DEVICE,
DOMAIN, DOMAIN,
XIAOMI_BLE_EVENT, XIAOMI_BLE_EVENT,
XiaomiBleEvent, XiaomiBleEvent,
@ -43,6 +44,11 @@ def process_service_info(
entry.entry_id entry.entry_id
] ]
discovered_device_classes = coordinator.discovered_device_classes discovered_device_classes = coordinator.discovered_device_classes
if entry.data.get(CONF_SLEEPY_DEVICE, False) != data.sleepy_device:
hass.config_entries.async_update_entry(
entry,
data=entry.data | {CONF_SLEEPY_DEVICE: data.sleepy_device},
)
if update.events: if update.events:
address = service_info.device.address address = service_info.device.address
for device_key, event in update.events.items(): for device_key, event in update.events.items():
@ -157,6 +163,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# since we will trade the BLEDevice for a connectable one # since we will trade the BLEDevice for a connectable one
# if we need to poll it # if we need to poll it
connectable=False, connectable=False,
entry=entry,
) )
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload( entry.async_on_unload(

View File

@ -136,7 +136,4 @@ class XiaomiBluetoothSensorEntity(
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
coordinator: XiaomiActiveBluetoothProcessorCoordinator = ( return self.processor.coordinator.sleepy_device or super().available
self.processor.coordinator
)
return coordinator.device_data.sleepy_device or super().available

View File

@ -7,6 +7,7 @@ DOMAIN = "xiaomi_ble"
CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events" CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events"
CONF_SLEEPY_DEVICE: Final = "sleepy_device"
CONF_EVENT_PROPERTIES: Final = "event_properties" CONF_EVENT_PROPERTIES: Final = "event_properties"
EVENT_PROPERTIES: Final = "event_properties" EVENT_PROPERTIES: Final = "event_properties"
EVENT_TYPE: Final = "event_type" EVENT_TYPE: Final = "event_type"

View File

@ -15,9 +15,12 @@ from homeassistant.components.bluetooth.active_update_processor import (
from homeassistant.components.bluetooth.passive_update_processor import ( from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor, PassiveBluetoothDataProcessor,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.debounce import Debouncer
from .const import CONF_SLEEPY_DEVICE
class XiaomiActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordinator): class XiaomiActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordinator):
"""Define a Xiaomi Bluetooth Active Update Processor Coordinator.""" """Define a Xiaomi Bluetooth Active Update Processor Coordinator."""
@ -39,6 +42,7 @@ class XiaomiActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordina
] ]
| None = None, | None = None,
poll_debouncer: Debouncer[Coroutine[Any, Any, None]] | None = None, poll_debouncer: Debouncer[Coroutine[Any, Any, None]] | None = None,
entry: ConfigEntry,
connectable: bool = True, connectable: bool = True,
) -> None: ) -> None:
"""Initialize the Xiaomi Bluetooth Active Update Processor Coordinator.""" """Initialize the Xiaomi Bluetooth Active Update Processor Coordinator."""
@ -55,6 +59,12 @@ class XiaomiActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordina
) )
self.discovered_device_classes = discovered_device_classes self.discovered_device_classes = discovered_device_classes
self.device_data = device_data self.device_data = device_data
self.entry = entry
@property
def sleepy_device(self) -> bool:
"""Return True if the device is a sleepy device."""
return self.entry.data.get(CONF_SLEEPY_DEVICE, self.device_data.sleepy_device)
class XiaomiPassiveBluetoothDataProcessor(PassiveBluetoothDataProcessor): class XiaomiPassiveBluetoothDataProcessor(PassiveBluetoothDataProcessor):

View File

@ -200,7 +200,4 @@ class XiaomiBluetoothSensorEntity(
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
coordinator: XiaomiActiveBluetoothProcessorCoordinator = ( return self.processor.coordinator.sleepy_device or super().available
self.processor.coordinator
)
return coordinator.device_data.sleepy_device or super().available

View File

@ -7,7 +7,7 @@ from unittest.mock import patch
from homeassistant.components.bluetooth import ( from homeassistant.components.bluetooth import (
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS, FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
) )
from homeassistant.components.xiaomi_ble.const import DOMAIN from homeassistant.components.xiaomi_ble.const import CONF_SLEEPY_DEVICE, DOMAIN
from homeassistant.const import ( from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_FRIENDLY_NAME,
STATE_OFF, STATE_OFF,
@ -313,6 +313,8 @@ async def test_unavailable(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert CONF_SLEEPY_DEVICE not in entry.data
async def test_sleepy_device(hass: HomeAssistant) -> None: async def test_sleepy_device(hass: HomeAssistant) -> None:
"""Test sleepy device does not go to unavailable after 60 minutes.""" """Test sleepy device does not go to unavailable after 60 minutes."""
@ -363,3 +365,5 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entry.data[CONF_SLEEPY_DEVICE] is True