Skip to content

Commit

Permalink
Fix bthome not remembering a device is a sleepy device (home-assistan…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 31, 2023
1 parent b266514 commit 7bda873
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
7 changes: 7 additions & 0 deletions homeassistant/components/bthome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
BTHOME_BLE_EVENT,
CONF_BINDKEY,
CONF_DISCOVERED_EVENT_CLASSES,
CONF_SLEEPY_DEVICE,
DOMAIN,
BTHomeBleEvent,
)
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 @@ -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)

Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/bthome/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,4 @@ def is_on(self) -> bool | None:
@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
1 change: 1 addition & 0 deletions homeassistant/components/bthome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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"
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/bthome/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
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."""
Expand All @@ -28,12 +31,19 @@ def __init__(
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):
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/components/bthome/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,4 @@ def native_value(self) -> int | float | None:
@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
6 changes: 5 additions & 1 deletion tests/components/bthome/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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,
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

0 comments on commit 7bda873

Please sign in to comment.