From 8eae4f855497d09ff4e19879ec965ef562a86f26 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 24 Nov 2026 07:32:52 +0100 Subject: [PATCH] Pass the Bluetooth sensor device model as a string (#194045) Co-authored-by: Claude Opus 5.5 --- .../bluetooth/passive_update_processor.py | 18 +++++++++++------- homeassistant/helpers/sensor.py | 5 ++++- .../bluetooth/test_passive_update_processor.py | 13 +++++++++++++ tests/helpers/test_sensor.py | 13 +++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/bluetooth/passive_update_processor.py b/homeassistant/components/bluetooth/passive_update_processor.py index 1e2e2af4d3da..8f2a4922046f 100644 --- a/homeassistant/components/bluetooth/passive_update_processor.py +++ b/homeassistant/components/bluetooth/passive_update_processor.py @@ -203,14 +203,18 @@ class PassiveBluetoothDataUpdate[_T]: entity_description_class: type[EntityDescription], ) -> None: """Set the restored data from storage.""" - self.devices.update( - { - key or None: DeviceInfo( - **{k: v for k, v in device_info.items() if k in _DEVICE_INFO_FIELDS} - ) - for key, device_info in restore_data["devices"].items() + devices: dict[str | None, DeviceInfo] = {} + for key, device_info in restore_data["devices"].items(): + restored = { + k: v for k, v in device_info.items() if k in _DEVICE_INFO_FIELDS } - ) + # SensorPro and ThermoBeacon stored the model as an int. Remove the + # conversion once their parsers are fixed upstream and stored data has + # been replaced. + if isinstance(model := restored.get("model"), int): + restored["model"] = str(model) + devices[key or None] = DeviceInfo(**restored) + self.devices.update(devices) self.entity_descriptions.update( { PassiveBluetoothEntityKey.from_string( diff --git a/homeassistant/helpers/sensor.py b/homeassistant/helpers/sensor.py index 223371b00058..29587978a7b2 100644 --- a/homeassistant/helpers/sensor.py +++ b/homeassistant/helpers/sensor.py @@ -49,10 +49,13 @@ def sensor_device_info_to_hass_device_info( The same device info is returned for the same values, so it cannot be modified. """ + # SensorPro and ThermoBeacon set the model to an int. Remove the conversion + # once their parsers are fixed upstream. + model = sensor_device_info.model return _device_info( sensor_device_info.name, sensor_device_info.manufacturer, - sensor_device_info.model, + None if model is None else str(model), ) diff --git a/tests/components/bluetooth/test_passive_update_processor.py b/tests/components/bluetooth/test_passive_update_processor.py index 0094a6c6b96b..b0320e55bbe7 100644 --- a/tests/components/bluetooth/test_passive_update_processor.py +++ b/tests/components/bluetooth/test_passive_update_processor.py @@ -2010,6 +2010,19 @@ def test_restore_data_device_info_round_trip() -> None: assert restored.devices == devices +def test_restore_data_int_model() -> None: + """Test a stored int model, which some parsers set, is restored as a string.""" + stored = { + "devices": {"": {"name": "Main", "model": 17}}, + "entity_descriptions": {}, + "entity_names": {}, + "entity_data": {}, + } + restored = PassiveBluetoothDataUpdate() + restored.async_set_restore_data(stored, EntityDescription) + assert restored.devices == {None: DeviceInfo(name="Main", model="17")} + + @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters") async def test_plain_device_info_deprecated(hass: HomeAssistant) -> None: """Test a plain device info is converted, and reported once.""" diff --git a/tests/helpers/test_sensor.py b/tests/helpers/test_sensor.py index addfdcfe08d5..1eeeac4100d1 100644 --- a/tests/helpers/test_sensor.py +++ b/tests/helpers/test_sensor.py @@ -52,6 +52,19 @@ def test_sensor_device_info_to_hass_device_info() -> None: ).as_dict() == {"name": "Thermometer"} +def test_sensor_device_info_to_hass_device_info_int_model() -> None: + """Test an int model, which some parsers set, is converted to a string.""" + assert sensor_device_info_to_hass_device_info( + SensorDeviceInfo( + name="Thermometer", + manufacturer="Acme", + model=17, # type: ignore[arg-type] + sw_version=None, + hw_version=None, + ) + ) == DeviceInfo(name="Thermometer", manufacturer="Acme", model="17") + + def test_sensor_device_info_to_hass_device_info_read_only() -> None: """Test the shared device info cannot be modified, but can be copied.""" device_info = sensor_device_info_to_hass_device_info(