Don't block SwitchBot migration when only passive scanners hear the device (#189400)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Joost Lekkerkerker
2026-11-02 12:14:28 +01:00
committed by GitHub
co-authored by Claude
parent ba12a137ec
commit 7a9bfcd13e
4 changed files with 142 additions and 10 deletions
+19 -9
View File
@@ -32,7 +32,6 @@ from .const import (
DEPRECATED_MODEL_TYPE_MIGRATIONS,
DEPRECATED_SENSOR_TYPE_AIR_PURIFIER,
DEPRECATED_SENSOR_TYPE_AIR_PURIFIER_TABLE,
DEPRECATED_SENSOR_TYPE_HYGROMETER,
DEPRECATED_SENSOR_TYPE_HYGROMETER_CO2,
DOMAIN,
ENCRYPTED_MODELS,
@@ -471,17 +470,30 @@ def _entry_address(entry: SwitchbotConfigEntry) -> str:
def _migrate_deprecated_model_type(
hass: HomeAssistant, entry: SwitchbotConfigEntry, sensor_type: str
) -> SupportedModels | None:
"""Resolve a deprecated sensor type from its BLE advertisement."""
"""Resolve a deprecated sensor type from its BLE advertisements."""
model_migrations = DEPRECATED_MODEL_TYPE_MIGRATIONS[sensor_type]
address = _entry_address(entry).upper()
# The model is only in the scan response, which passive scanners never
# receive, so the best advertisement alone may not identify the device.
scanner_devices = bluetooth.async_scanner_devices_by_address(
hass, address, connectable=False
)
advertisements = [
(scanner_device.ble_device, scanner_device.advertisement)
for scanner_device in scanner_devices
]
if service_info := bluetooth.async_last_service_info(
hass, address, connectable=True
) or bluetooth.async_last_service_info(hass, address, connectable=False):
parsed_adv = switchbot.parse_advertisement_data(
service_info.device, service_info.advertisement
)
advertisements.insert(0, (service_info.device, service_info.advertisement))
for device, advertisement in advertisements:
parsed_adv = switchbot.parse_advertisement_data(device, advertisement)
if parsed_adv and (adv_model := parsed_adv.data.get("modelName")):
return model_migrations.get(adv_model)
# Keep a legacy type that is still supported instead of blocking setup
# while the device is only heard without a scan response.
if scanner_devices and sensor_type in SupportedModels:
return SupportedModels(sensor_type)
return None
@@ -516,16 +528,14 @@ async def async_migrate_entry(hass: HomeAssistant, entry: SwitchbotConfigEntry)
address = _entry_address(entry)
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="device_not_found_error",
translation_key="model_not_determined_error",
translation_placeholders={
"sensor_type": sensor_type,
"address": address,
"reason": bluetooth.async_address_reachability_diagnostics(
hass,
address.upper(),
BluetoothReachabilityIntent.CONNECTION
if sensor_type != DEPRECATED_SENSOR_TYPE_HYGROMETER
else BluetoothReachabilityIntent.PASSIVE_ADVERTISEMENT,
BluetoothReachabilityIntent.ACTIVE_ADVERTISEMENT,
),
},
)
@@ -494,6 +494,9 @@
"device_without_config_entry": {
"message": "The device ID {device_id} is not associated with a config entry."
},
"model_not_determined_error": {
"message": "Could not determine the model of Switchbot {sensor_type} with address {address} from its Bluetooth advertisements; an actively scanning Bluetooth adapter or proxy in range may be required: {reason}"
},
"not_keypad_vision_device": {
"message": "This service is only supported for SwitchBot Keypad Vision devices."
},
+19
View File
@@ -401,6 +401,25 @@ WOCURTAIN3_SERVICE_INFO = BluetoothServiceInfoBleak(
)
# As heard by a passive scanner, which never receives the scan response.
WOCURTAIN3_PASSIVE_SERVICE_INFO = BluetoothServiceInfoBleak(
name="AA:BB:CC:DD:EE:FF",
address="AA:BB:CC:DD:EE:FF",
manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\x07\x03S\x11\x04\x01d"},
service_data={},
service_uuids=[],
rssi=-50,
source="passive_proxy",
advertisement=generate_advertisement_data(
manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\x07\x03S\x11\x04\x01d"},
),
device=generate_ble_device("AA:BB:CC:DD:EE:FF", None),
time=0,
connectable=True,
tx_power=-127,
)
WOBLINDTILT_SERVICE_INFO = BluetoothServiceInfoBleak(
name="WoBlindTilt",
address="AA:BB:CC:DD:EE:FF",
+101 -1
View File
@@ -10,7 +10,10 @@ from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
from pytest_homeassistant.components.bluetooth import inject_bluetooth_service_info
import switchbot
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
from homeassistant.components.bluetooth import (
BluetoothScannerDevice,
BluetoothServiceInfoBleak,
)
from homeassistant.components.switchbot.const import (
CONF_CURTAIN_SPEED,
CONF_RETRY_COUNT,
@@ -36,6 +39,7 @@ from . import (
HUBMINI_MATTER_SERVICE_INFO,
LOCK_SERVICE_INFO,
STANDING_FAN_SERVICE_INFO,
WOCURTAIN3_PASSIVE_SERVICE_INFO,
WOCURTAIN3_SERVICE_INFO,
WOCURTAIN_SERVICE_INFO,
WOMETERTHPC_SERVICE_INFO,
@@ -473,6 +477,102 @@ async def test_migrate_deprecated_model_device_not_in_range(
assert entry.data == original_data
assert entry.options == {}
assert entry.unique_id == "aabbccddeeff"
assert entry.reason.startswith(
f"Could not determine the model of Switchbot {sensor_type} with address"
" aa:bb:cc:dd:ee:ff from its Bluetooth advertisements"
)
@pytest.mark.parametrize(
("sensor_type", "expected_state", "expected_version"),
[
pytest.param("curtain", ConfigEntryState.LOADED, 2, id="curtain"),
pytest.param("ceiling_light", ConfigEntryState.LOADED, 2, id="ceiling_light"),
pytest.param("hygrometer", ConfigEntryState.SETUP_RETRY, 1, id="hygrometer"),
pytest.param("plug", ConfigEntryState.SETUP_RETRY, 1, id="plug"),
],
)
async def test_migrate_deprecated_model_without_scan_response(
hass: HomeAssistant,
sensor_type: str,
expected_state: ConfigEntryState,
expected_version: int,
) -> None:
"""Test a device only heard by passive scanners keeps a supported legacy type."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_ADDRESS: "aa:bb:cc:dd:ee:ff", CONF_SENSOR_TYPE: sensor_type},
unique_id="aabbccddeeff",
version=1,
minor_version=2,
options={CONF_RETRY_COUNT: DEFAULT_RETRY_COUNT},
)
entry.add_to_hass(hass)
with (
patch(
"homeassistant.components.switchbot.bluetooth.async_last_service_info",
return_value=WOCURTAIN3_PASSIVE_SERVICE_INFO,
),
patch(
"homeassistant.components.switchbot.bluetooth.async_scanner_devices_by_address",
return_value=[
BluetoothScannerDevice(
MagicMock(),
WOCURTAIN3_PASSIVE_SERVICE_INFO.device,
WOCURTAIN3_PASSIVE_SERVICE_INFO.advertisement,
)
],
),
patch_async_setup_entry(),
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
assert entry.state is expected_state
assert entry.version == expected_version
assert entry.data[CONF_SENSOR_TYPE] == sensor_type
async def test_migrate_deprecated_model_from_other_scanner(
hass: HomeAssistant,
) -> None:
"""Test the model is resolved from a scanner that received the scan response."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_ADDRESS: "aa:bb:cc:dd:ee:ff", CONF_SENSOR_TYPE: "curtain"},
unique_id="aabbccddeeff",
version=1,
minor_version=2,
options={CONF_RETRY_COUNT: DEFAULT_RETRY_COUNT},
)
entry.add_to_hass(hass)
with (
patch(
"homeassistant.components.switchbot.bluetooth.async_last_service_info",
return_value=WOCURTAIN3_PASSIVE_SERVICE_INFO,
),
patch(
"homeassistant.components.switchbot.bluetooth.async_scanner_devices_by_address",
return_value=[
BluetoothScannerDevice(
MagicMock(), service_info.device, service_info.advertisement
)
for service_info in (
WOCURTAIN3_PASSIVE_SERVICE_INFO,
WOCURTAIN3_SERVICE_INFO,
)
],
),
patch_async_setup_entry(),
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
assert entry.version == 2
assert entry.minor_version == 1
assert entry.data[CONF_SENSOR_TYPE] == "curtain_3"
async def test_migration_retries_when_device_returns(