Skip to content

Commit

Permalink
Fix xiaomi_ble not remembering a device is a sleepy device (home-assi…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 31, 2023
1 parent 7bda873 commit 28bebf3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
7 changes: 7 additions & 0 deletions homeassistant/components/xiaomi_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .const import (
CONF_DISCOVERED_EVENT_CLASSES,
CONF_SLEEPY_DEVICE,
DOMAIN,
XIAOMI_BLE_EVENT,
XiaomiBleEvent,
Expand All @@ -43,6 +44,11 @@ def process_service_info(
entry.entry_id
]
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:
address = service_info.device.address
for device_key, event in update.events.items():
Expand Down Expand Up @@ -157,6 +163,7 @@ async def _async_poll(service_info: BluetoothServiceInfoBleak):
# since we will trade the BLEDevice for a connectable one
# if we need to poll it
connectable=False,
entry=entry,
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/xiaomi_ble/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,4 @@ def is_on(self) -> bool | None:
@property
def available(self) -> bool:
"""Return True if entity is available."""
coordinator: XiaomiActiveBluetoothProcessorCoordinator = (
self.processor.coordinator
)
return coordinator.device_data.sleepy_device or super().available
return self.processor.coordinator.sleepy_device or super().available
1 change: 1 addition & 0 deletions homeassistant/components/xiaomi_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events"
CONF_SLEEPY_DEVICE: Final = "sleepy_device"
CONF_EVENT_PROPERTIES: Final = "event_properties"
EVENT_PROPERTIES: Final = "event_properties"
EVENT_TYPE: Final = "event_type"
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/xiaomi_ble/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.debounce import Debouncer

from .const import CONF_SLEEPY_DEVICE


class XiaomiActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordinator):
"""Define a Xiaomi Bluetooth Active Update Processor Coordinator."""
Expand All @@ -39,6 +42,7 @@ def __init__(
]
| None = None,
poll_debouncer: Debouncer[Coroutine[Any, Any, None]] | None = None,
entry: ConfigEntry,
connectable: bool = True,
) -> None:
"""Initialize the Xiaomi Bluetooth Active Update Processor Coordinator."""
Expand All @@ -55,6 +59,12 @@ def __init__(
)
self.discovered_device_classes = discovered_device_classes
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):
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/xiaomi_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,4 @@ def native_value(self) -> int | float | None:
@property
def available(self) -> bool:
"""Return True if entity is available."""
coordinator: XiaomiActiveBluetoothProcessorCoordinator = (
self.processor.coordinator
)
return coordinator.device_data.sleepy_device or super().available
return self.processor.coordinator.sleepy_device or super().available
6 changes: 5 additions & 1 deletion tests/components/xiaomi_ble/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.components.bluetooth import (
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 (
ATTR_FRIENDLY_NAME,
STATE_OFF,
Expand Down Expand Up @@ -313,6 +313,8 @@ async def test_unavailable(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()

assert CONF_SLEEPY_DEVICE not in entry.data


async def test_sleepy_device(hass: HomeAssistant) -> None:
"""Test sleepy device does not go to unavailable after 60 minutes."""
Expand Down Expand Up @@ -363,3 +365,5 @@ async def test_sleepy_device(hass: HomeAssistant) -> None:

assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()

assert entry.data[CONF_SLEEPY_DEVICE] is True

0 comments on commit 28bebf3

Please sign in to comment.