Fix bthome not remembering a device is a sleepy device (#97517)

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

View File

@ -20,6 +20,7 @@ from .const import (
BTHOME_BLE_EVENT,
CONF_BINDKEY,
CONF_DISCOVERED_EVENT_CLASSES,
CONF_SLEEPY_DEVICE,
DOMAIN,
BTHomeBleEvent,
)
@ -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():
@ -113,6 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, [])
),
connectable=False,
entry=entry,
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

View File

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

View File

@ -7,6 +7,7 @@ DOMAIN = "bthome"
CONF_BINDKEY: Final = "bindkey"
CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events"
CONF_SLEEPY_DEVICE: Final = "sleepy_device"
CONF_SUBTYPE: Final = "subtype"
EVENT_TYPE: Final = "event_type"

View File

@ -13,8 +13,11 @@ from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor,
PassiveBluetoothProcessorCoordinator,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import CONF_SLEEPY_DEVICE
class BTHomePassiveBluetoothProcessorCoordinator(PassiveBluetoothProcessorCoordinator):
"""Define a BTHome Bluetooth Passive Update Processor Coordinator."""
@ -28,12 +31,19 @@ class BTHomePassiveBluetoothProcessorCoordinator(PassiveBluetoothProcessorCoordi
update_method: Callable[[BluetoothServiceInfoBleak], Any],
device_data: BTHomeBluetoothDeviceData,
discovered_device_classes: set[str],
entry: ConfigEntry,
connectable: bool = False,
) -> None:
"""Initialize the BTHome Bluetooth Passive Update Processor Coordinator."""
super().__init__(hass, logger, address, mode, update_method, connectable)
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 BTHomePassiveBluetoothDataProcessor(PassiveBluetoothDataProcessor):

View File

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

View File

@ -9,7 +9,7 @@ import pytest
from homeassistant.components.bluetooth import (
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
)
from homeassistant.components.bthome.const import DOMAIN
from homeassistant.components.bthome.const import CONF_SLEEPY_DEVICE, DOMAIN
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
@ -1138,6 +1138,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."""
@ -1191,3 +1193,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