diff --git a/homeassistant/components/rapt_ble/manifest.json b/homeassistant/components/rapt_ble/manifest.json index 79a6269f8191..bc630ea83d23 100644 --- a/homeassistant/components/rapt_ble/manifest.json +++ b/homeassistant/components/rapt_ble/manifest.json @@ -17,5 +17,5 @@ "documentation": "https://www.home-assistant.io/integrations/rapt_ble", "integration_type": "device", "iot_class": "local_push", - "requirements": ["rapt-ble==0.1.2"] + "requirements": ["rapt-ble==2.0.0"] } diff --git a/homeassistant/components/rapt_ble/sensor.py b/homeassistant/components/rapt_ble/sensor.py index f2416a47dbef..675e0fe212db 100644 --- a/homeassistant/components/rapt_ble/sensor.py +++ b/homeassistant/components/rapt_ble/sensor.py @@ -28,7 +28,7 @@ from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info from . import RAPTBLEConfigEntry -SENSOR_DESCRIPTIONS = { +SENSOR_DESCRIPTIONS: dict[tuple[str | None, str | None], SensorEntityDescription] = { (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription( key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}", device_class=SensorDeviceClass.TEMPERATURE, @@ -81,7 +81,8 @@ def sensor_update_to_bluetooth_data_update( (description.device_class, description.native_unit_of_measurement) ] for device_key, description in sensor_update.entity_descriptions.items() - if description.device_class and description.native_unit_of_measurement + if (description.device_class, description.native_unit_of_measurement) + in SENSOR_DESCRIPTIONS }, entity_data={ _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value diff --git a/requirements_all.txt b/requirements_all.txt index c544f738f381..86878c7e727c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2947,7 +2947,7 @@ radiotherm==2.1.0 raincloudy==0.0.7 # homeassistant.components.rapt_ble -rapt-ble==0.1.2 +rapt-ble==2.0.0 # homeassistant.components.raspyrfm raspyrfm-client==1.2.9 diff --git a/tests/components/rapt_ble/__init__.py b/tests/components/rapt_ble/__init__.py index 4b3959fdba7c..84c4b6c3656e 100644 --- a/tests/components/rapt_ble/__init__.py +++ b/tests/components/rapt_ble/__init__.py @@ -26,3 +26,29 @@ COMPLETE_SERVICE_INFO = BluetoothServiceInfo( service_uuids=[], source="local", ) + +V2_SERVICE_INFO = BluetoothServiceInfo( + name="", + address=RAPT_MAC, + rssi=-70, + manufacturer_data={ + 16722: b"PT\x02\x00\x01\xc1m&\x14\x92kD\x7fR\xc91\xc9\x02-)\x97?F", + 17739: b"G20220612_050156_81c6d14", + }, + service_data={}, + service_uuids=[], + source="local", +) + +V2_NO_VELOCITY_SERVICE_INFO = BluetoothServiceInfo( + name="", + address=RAPT_MAC, + rssi=-70, + manufacturer_data={ + 16722: b"PT\x02\x00\x00\x00\x00\x00\x00\x94\x8bD|\xb9\xf64E\x02b&w*\xac", + 17739: b"G20220612_050156_81c6d14", + }, + service_data={}, + service_uuids=[], + source="local", +) diff --git a/tests/components/rapt_ble/test_sensor.py b/tests/components/rapt_ble/test_sensor.py index b166679a906e..d28b854533ff 100644 --- a/tests/components/rapt_ble/test_sensor.py +++ b/tests/components/rapt_ble/test_sensor.py @@ -1,5 +1,7 @@ """Test the RAPT Pill BLE sensors.""" +import pytest + from homeassistant.components.rapt_ble.const import DOMAIN from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass from homeassistant.const import ( @@ -9,8 +11,14 @@ from homeassistant.const import ( UnitOfTemperature, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo -from . import COMPLETE_SERVICE_INFO, RAPT_MAC +from . import ( + COMPLETE_SERVICE_INFO, + RAPT_MAC, + V2_NO_VELOCITY_SERVICE_INFO, + V2_SERVICE_INFO, +) from tests.common import MockConfigEntry from tests.components.bluetooth import inject_bluetooth_service_info @@ -62,3 +70,32 @@ async def test_sensors(hass: HomeAssistant) -> None: assert await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done() + + +@pytest.mark.parametrize( + "service_info", + [ + pytest.param(V2_SERVICE_INFO, id="valid_velocity"), + pytest.param(V2_NO_VELOCITY_SERVICE_INFO, id="invalid_velocity"), + ], +) +async def test_sensors_v2_payload( + hass: HomeAssistant, service_info: BluetoothServiceInfo +) -> None: + """Test a version 2 advertisement sets up the known sensors.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=RAPT_MAC, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + inject_bluetooth_service_info(hass, service_info) + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 3 + assert hass.states.get("sensor.rapt_pill_0666_specific_gravity") is not None + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done()