From e23007322dafb71184d108d8ba51c7429a5b3d00 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 1 Dec 2022 11:37:22 -1000 Subject: [PATCH] Defer xiaomi ble polling until after startup (#83074) fixes https://github.com/home-assistant/core/issues/77178 --- homeassistant/components/xiaomi_ble/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/xiaomi_ble/__init__.py b/homeassistant/components/xiaomi_ble/__init__.py index f899600a8d1..201e4f14582 100644 --- a/homeassistant/components/xiaomi_ble/__init__.py +++ b/homeassistant/components/xiaomi_ble/__init__.py @@ -17,7 +17,7 @@ from homeassistant.components.bluetooth.active_update_coordinator import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform -from homeassistant.core import HomeAssistant +from homeassistant.core import CoreState, HomeAssistant from .const import DOMAIN @@ -60,7 +60,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: def _needs_poll( service_info: BluetoothServiceInfoBleak, last_poll: float | None ) -> bool: - return data.poll_needed(service_info, last_poll) + # Only poll if hass is running, we need to poll, + # and we actually have a way to connect to the device + return ( + hass.state == CoreState.running + and data.poll_needed(service_info, last_poll) + and bool( + async_ble_device_from_address( + hass, service_info.device.address, connectable=True + ) + ) + ) async def _async_poll(service_info: BluetoothServiceInfoBleak): # BluetoothServiceInfoBleak is defined in HA, otherwise would just pass it