mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Pass the Bluetooth sensor device model as a string (#194045)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
960ce1d45d
commit
8eae4f8554
@@ -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(
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user