From ce4e51078fda0353b7147ec9cbaf2fa8c6b361c5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 May 2025 03:00:32 -0500 Subject: [PATCH] Add test coverage for inkbird IBS-P02B (#144433) Was reported not working in https://github.com/Bluetooth-Devices/inkbird-ble/issues/95#issuecomment-2860473798 but cannot reproduce the issue. The tests are still useful --- tests/components/inkbird/__init__.py | 10 +++++++ tests/components/inkbird/test_sensor.py | 38 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/tests/components/inkbird/__init__.py b/tests/components/inkbird/__init__.py index f798fee292c..7228f64448b 100644 --- a/tests/components/inkbird/__init__.py +++ b/tests/components/inkbird/__init__.py @@ -103,3 +103,13 @@ IAM_T1_SERVICE_INFO = _make_bluetooth_service_info( service_data={}, source="local", ) + +IBS_P02B_SERVICE_INFO = _make_bluetooth_service_info( + name="IBS-P02B", + manufacturer_data={9289: bytes.fromhex("111800656e0100005f00000100000000")}, + service_uuids=["0000fff0-0000-1000-8000-00805f9b34fb"], + address="49:24:11:18:00:65", + rssi=-60, + service_data={}, + source="local", +) diff --git a/tests/components/inkbird/test_sensor.py b/tests/components/inkbird/test_sensor.py index 1feb5f5b02c..2a95714df4b 100644 --- a/tests/components/inkbird/test_sensor.py +++ b/tests/components/inkbird/test_sensor.py @@ -30,6 +30,7 @@ from homeassistant.util import dt as dt_util from . import ( IAM_T1_SERVICE_INFO, + IBS_P02B_SERVICE_INFO, SPS_PASSIVE_SERVICE_INFO, SPS_SERVICE_INFO, SPS_WITH_CORRUPT_NAME_SERVICE_INFO, @@ -256,3 +257,40 @@ async def test_notify_sensor(hass: HomeAssistant) -> None: saved_device_data_changed_callback({"temp_unit": "C"}) assert entry.data[CONF_DEVICE_DATA] == {"temp_unit": "C"} + + +async def test_ibs_p02b_sensors(hass: HomeAssistant) -> None: + """Test setting up creates the sensors for an IBS-P02B.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="49:24:11:18:00:65", + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 0 + inject_bluetooth_service_info(hass, IBS_P02B_SERVICE_INFO) + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 2 + + temp_sensor = hass.states.get("sensor.ibs_p02b_0065_battery") + temp_sensor_attribtes = temp_sensor.attributes + assert temp_sensor.state == "95" + assert temp_sensor_attribtes[ATTR_FRIENDLY_NAME] == "IBS-P02B 0065 Battery" + assert temp_sensor_attribtes[ATTR_UNIT_OF_MEASUREMENT] == "%" + assert temp_sensor_attribtes[ATTR_STATE_CLASS] == "measurement" + + temp_sensor = hass.states.get("sensor.ibs_p02b_0065_temperature") + temp_sensor_attribtes = temp_sensor.attributes + assert temp_sensor.state == "36.6" + assert temp_sensor_attribtes[ATTR_FRIENDLY_NAME] == "IBS-P02B 0065 Temperature" + assert temp_sensor_attribtes[ATTR_UNIT_OF_MEASUREMENT] == "°C" + assert temp_sensor_attribtes[ATTR_STATE_CLASS] == "measurement" + + # Make sure we remember the device type + # in case the name is corrupted later + assert entry.data[CONF_DEVICE_TYPE] == "IBS-P02B" + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done()