From 139de5c5fd1d2b143023be658d0641d49368e0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cerm=C3=A1k?= Date: Mon, 7 Sep 2026 11:47:16 +0200 Subject: [PATCH] Add specific gravity velocity sensor to rapt_ble (#181518) --- homeassistant/components/rapt_ble/icons.json | 9 +++++ homeassistant/components/rapt_ble/sensor.py | 10 ++++++ tests/components/rapt_ble/test_sensor.py | 38 +++++++++++++++----- 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 homeassistant/components/rapt_ble/icons.json diff --git a/homeassistant/components/rapt_ble/icons.json b/homeassistant/components/rapt_ble/icons.json new file mode 100644 index 000000000000..dcb6c6066eab --- /dev/null +++ b/homeassistant/components/rapt_ble/icons.json @@ -0,0 +1,9 @@ +{ + "entity": { + "sensor": { + "specific_gravity_velocity": { + "default": "mdi:trending-down" + } + } + } +} diff --git a/homeassistant/components/rapt_ble/sensor.py b/homeassistant/components/rapt_ble/sensor.py index 675e0fe212db..dd3a62fdd239 100644 --- a/homeassistant/components/rapt_ble/sensor.py +++ b/homeassistant/components/rapt_ble/sensor.py @@ -39,6 +39,16 @@ SENSOR_DESCRIPTIONS: dict[tuple[str | None, str | None], SensorEntityDescription key=f"{DeviceClass.SPECIFIC_GRAVITY}_{Units.SPECIFIC_GRAVITY}", state_class=SensorStateClass.MEASUREMENT, ), + ( + DeviceClass.SPECIFIC_GRAVITY_VELOCITY, + Units.SPECIFIC_GRAVITY_POINTS_PER_DAY, + ): SensorEntityDescription( + key=f"{DeviceClass.SPECIFIC_GRAVITY_VELOCITY}_{Units.SPECIFIC_GRAVITY_POINTS_PER_DAY}", + translation_key="specific_gravity_velocity", + native_unit_of_measurement=Units.SPECIFIC_GRAVITY_POINTS_PER_DAY, + state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, + ), (DeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription( key=f"{DeviceClass.BATTERY}_{Units.PERCENTAGE}", device_class=SensorDeviceClass.BATTERY, diff --git a/tests/components/rapt_ble/test_sensor.py b/tests/components/rapt_ble/test_sensor.py index d28b854533ff..40069052aab6 100644 --- a/tests/components/rapt_ble/test_sensor.py +++ b/tests/components/rapt_ble/test_sensor.py @@ -8,9 +8,11 @@ from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, + STATE_UNKNOWN, UnitOfTemperature, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo from . import ( @@ -23,6 +25,8 @@ from . import ( from tests.common import MockConfigEntry from tests.components.bluetooth import inject_bluetooth_service_info +VELOCITY_ENTITY_ID = "sensor.rapt_pill_0666_specific_gravity_velocity" + async def test_sensors(hass: HomeAssistant) -> None: """Test setting up creates the sensors.""" @@ -39,6 +43,7 @@ async def test_sensors(hass: HomeAssistant) -> None: inject_bluetooth_service_info(hass, COMPLETE_SERVICE_INFO) await hass.async_block_till_done() assert len(hass.states.async_all()) == 3 + assert hass.states.get(VELOCITY_ENTITY_ID) is None temp_sensor = hass.states.get("sensor.rapt_pill_0666_battery") assert temp_sensor is not None @@ -73,16 +78,19 @@ async def test_sensors(hass: HomeAssistant) -> None: @pytest.mark.parametrize( - "service_info", + ("service_info", "expected_state"), [ - pytest.param(V2_SERVICE_INFO, id="valid_velocity"), - pytest.param(V2_NO_VELOCITY_SERVICE_INFO, id="invalid_velocity"), + pytest.param(V2_SERVICE_INFO, "-14.8217964172363", id="valid_velocity"), + pytest.param(V2_NO_VELOCITY_SERVICE_INFO, STATE_UNKNOWN, id="invalid_velocity"), ], ) -async def test_sensors_v2_payload( - hass: HomeAssistant, service_info: BluetoothServiceInfo +async def test_specific_gravity_velocity_sensor( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + service_info: BluetoothServiceInfo, + expected_state: str, ) -> None: - """Test a version 2 advertisement sets up the known sensors.""" + """Test the specific gravity velocity sensor from a v2 payload.""" entry = MockConfigEntry( domain=DOMAIN, unique_id=RAPT_MAC, @@ -94,8 +102,22 @@ async def test_sensors_v2_payload( 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 len(hass.states.async_all()) == 4 + + velocity_sensor = hass.states.get(VELOCITY_ENTITY_ID) + assert velocity_sensor is not None + assert velocity_sensor.state == expected_state + assert ( + velocity_sensor.attributes[ATTR_FRIENDLY_NAME] + == "RAPT Pill 0666 Specific Gravity Velocity" + ) + assert velocity_sensor.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT + assert velocity_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == "SG points/day" + + entity_entry = entity_registry.async_get(VELOCITY_ENTITY_ID) + assert entity_entry is not None + assert entity_entry.translation_key == "specific_gravity_velocity" + assert entity_entry.options["sensor"]["suggested_display_precision"] == 1 assert await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done()