mirror of https://github.com/home-assistant/core
Fix bthome not remembering a device is a sleepy device (#97517)
This commit is contained in:
parent
b266514068
commit
7bda873c2a
|
@ -20,6 +20,7 @@ from .const import (
|
||||||
BTHOME_BLE_EVENT,
|
BTHOME_BLE_EVENT,
|
||||||
CONF_BINDKEY,
|
CONF_BINDKEY,
|
||||||
CONF_DISCOVERED_EVENT_CLASSES,
|
CONF_DISCOVERED_EVENT_CLASSES,
|
||||||
|
CONF_SLEEPY_DEVICE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
BTHomeBleEvent,
|
BTHomeBleEvent,
|
||||||
)
|
)
|
||||||
|
@ -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():
|
||||||
|
@ -113,6 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, [])
|
entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, [])
|
||||||
),
|
),
|
||||||
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)
|
||||||
|
|
||||||
|
|
|
@ -203,7 +203,4 @@ class BTHomeBluetoothBinarySensorEntity(
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
coordinator: BTHomePassiveBluetoothProcessorCoordinator = (
|
return self.processor.coordinator.sleepy_device or super().available
|
||||||
self.processor.coordinator
|
|
||||||
)
|
|
||||||
return coordinator.device_data.sleepy_device or super().available
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ DOMAIN = "bthome"
|
||||||
|
|
||||||
CONF_BINDKEY: Final = "bindkey"
|
CONF_BINDKEY: Final = "bindkey"
|
||||||
CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events"
|
CONF_DISCOVERED_EVENT_CLASSES: Final = "known_events"
|
||||||
|
CONF_SLEEPY_DEVICE: Final = "sleepy_device"
|
||||||
CONF_SUBTYPE: Final = "subtype"
|
CONF_SUBTYPE: Final = "subtype"
|
||||||
|
|
||||||
EVENT_TYPE: Final = "event_type"
|
EVENT_TYPE: Final = "event_type"
|
||||||
|
|
|
@ -13,8 +13,11 @@ from homeassistant.components.bluetooth.passive_update_processor import (
|
||||||
PassiveBluetoothDataProcessor,
|
PassiveBluetoothDataProcessor,
|
||||||
PassiveBluetoothProcessorCoordinator,
|
PassiveBluetoothProcessorCoordinator,
|
||||||
)
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import CONF_SLEEPY_DEVICE
|
||||||
|
|
||||||
|
|
||||||
class BTHomePassiveBluetoothProcessorCoordinator(PassiveBluetoothProcessorCoordinator):
|
class BTHomePassiveBluetoothProcessorCoordinator(PassiveBluetoothProcessorCoordinator):
|
||||||
"""Define a BTHome Bluetooth Passive Update Processor Coordinator."""
|
"""Define a BTHome Bluetooth Passive Update Processor Coordinator."""
|
||||||
|
@ -28,12 +31,19 @@ class BTHomePassiveBluetoothProcessorCoordinator(PassiveBluetoothProcessorCoordi
|
||||||
update_method: Callable[[BluetoothServiceInfoBleak], Any],
|
update_method: Callable[[BluetoothServiceInfoBleak], Any],
|
||||||
device_data: BTHomeBluetoothDeviceData,
|
device_data: BTHomeBluetoothDeviceData,
|
||||||
discovered_device_classes: set[str],
|
discovered_device_classes: set[str],
|
||||||
|
entry: ConfigEntry,
|
||||||
connectable: bool = False,
|
connectable: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the BTHome Bluetooth Passive Update Processor Coordinator."""
|
"""Initialize the BTHome Bluetooth Passive Update Processor Coordinator."""
|
||||||
super().__init__(hass, logger, address, mode, update_method, connectable)
|
super().__init__(hass, logger, address, mode, update_method, connectable)
|
||||||
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 BTHomePassiveBluetoothDataProcessor(PassiveBluetoothDataProcessor):
|
class BTHomePassiveBluetoothDataProcessor(PassiveBluetoothDataProcessor):
|
||||||
|
|
|
@ -400,7 +400,4 @@ class BTHomeBluetoothSensorEntity(
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
coordinator: BTHomePassiveBluetoothProcessorCoordinator = (
|
return self.processor.coordinator.sleepy_device or super().available
|
||||||
self.processor.coordinator
|
|
||||||
)
|
|
||||||
return coordinator.device_data.sleepy_device or super().available
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import pytest
|
||||||
from homeassistant.components.bluetooth import (
|
from homeassistant.components.bluetooth import (
|
||||||
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
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.components.sensor import ATTR_STATE_CLASS
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
|
@ -1138,6 +1138,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."""
|
||||||
|
@ -1191,3 +1193,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
|
||||||
|
|
Loading…
Reference in New Issue