From 1369874348ee277f2820712f5b5dedc4e42c3a01 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Tue, 22 Aug 2023 14:34:26 +0200 Subject: [PATCH] Add text sensor to BTHome (#98355) --- homeassistant/components/bthome/manifest.json | 2 +- homeassistant/components/bthome/sensor.py | 15 ++++++++++---- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/bthome/test_sensor.py | 20 +++++++++++++++++-- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/bthome/manifest.json b/homeassistant/components/bthome/manifest.json index 418c7b8e3e3..7f53a5b5f06 100644 --- a/homeassistant/components/bthome/manifest.json +++ b/homeassistant/components/bthome/manifest.json @@ -20,5 +20,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/bthome", "iot_class": "local_push", - "requirements": ["bthome-ble==3.0.0"] + "requirements": ["bthome-ble==3.1.0"] } diff --git a/homeassistant/components/bthome/sensor.py b/homeassistant/components/bthome/sensor.py index caa652715bf..06f205246c8 100644 --- a/homeassistant/components/bthome/sensor.py +++ b/homeassistant/components/bthome/sensor.py @@ -2,6 +2,9 @@ from __future__ import annotations from bthome_ble import SensorDeviceClass as BTHomeSensorDeviceClass, SensorUpdate, Units +from bthome_ble.const import ( + ExtendedSensorDeviceClass as BTHomeExtendedSensorDeviceClass, +) from homeassistant import config_entries from homeassistant.components.bluetooth.passive_update_processor import ( @@ -66,7 +69,7 @@ SENSOR_DESCRIPTIONS = { ), # Count (-) (BTHomeSensorDeviceClass.COUNT, None): SensorEntityDescription( - key=f"{BTHomeSensorDeviceClass.COUNT}", + key=str(BTHomeSensorDeviceClass.COUNT), state_class=SensorStateClass.MEASUREMENT, ), # CO2 (parts per million) @@ -186,7 +189,7 @@ SENSOR_DESCRIPTIONS = { ), # Packet Id (-) (BTHomeSensorDeviceClass.PACKET_ID, None): SensorEntityDescription( - key=f"{BTHomeSensorDeviceClass.PACKET_ID}", + key=str(BTHomeSensorDeviceClass.PACKET_ID), state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, @@ -260,12 +263,16 @@ SENSOR_DESCRIPTIONS = { native_unit_of_measurement=UnitOfTemperature.CELSIUS, state_class=SensorStateClass.MEASUREMENT, ), + # Text (-) + (BTHomeExtendedSensorDeviceClass.TEXT, None): SensorEntityDescription( + key=str(BTHomeExtendedSensorDeviceClass.TEXT), + ), # Timestamp (datetime object) ( BTHomeSensorDeviceClass.TIMESTAMP, None, ): SensorEntityDescription( - key=f"{BTHomeSensorDeviceClass.TIMESTAMP}", + key=str(BTHomeSensorDeviceClass.TIMESTAMP), device_class=SensorDeviceClass.TIMESTAMP, state_class=SensorStateClass.MEASUREMENT, ), @@ -274,7 +281,7 @@ SENSOR_DESCRIPTIONS = { BTHomeSensorDeviceClass.UV_INDEX, None, ): SensorEntityDescription( - key=f"{BTHomeSensorDeviceClass.UV_INDEX}", + key=str(BTHomeSensorDeviceClass.UV_INDEX), state_class=SensorStateClass.MEASUREMENT, ), # Volatile organic Compounds (VOC) (µg/m3) diff --git a/requirements_all.txt b/requirements_all.txt index d5771e354d4..5f975c78e0c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -571,7 +571,7 @@ brunt==1.2.0 bt-proximity==0.2.1 # homeassistant.components.bthome -bthome-ble==3.0.0 +bthome-ble==3.1.0 # homeassistant.components.bt_home_hub_5 bthomehub5-devicelist==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5d704df04bf..fd63156cdd9 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -475,7 +475,7 @@ brottsplatskartan==0.0.1 brunt==1.2.0 # homeassistant.components.bthome -bthome-ble==3.0.0 +bthome-ble==3.1.0 # homeassistant.components.buienradar buienradar==1.0.5 diff --git a/tests/components/bthome/test_sensor.py b/tests/components/bthome/test_sensor.py index 7474e3ba890..831f7811972 100644 --- a/tests/components/bthome/test_sensor.py +++ b/tests/components/bthome/test_sensor.py @@ -869,7 +869,6 @@ async def test_v1_sensors( { "sensor_entity": "sensor.test_device_18b2_timestamp", "friendly_name": "Test Device 18B2 Timestamp", - "unit_of_measurement": "s", "state_class": "measurement", "expected_state": "2023-05-14T19:41:17+00:00", }, @@ -943,6 +942,21 @@ async def test_v1_sensors( }, ], ), + ( + "A4:C1:38:8D:18:B2", + make_bthome_v2_adv( + "A4:C1:38:8D:18:B2", + b"\x44\x53\x0C\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x21", + ), + None, + [ + { + "sensor_entity": "sensor.test_device_18b2_text", + "friendly_name": "Test Device 18B2 Text", + "expected_state": "Hello World!", + }, + ], + ), ( "A4:C1:38:8D:18:B2", make_bthome_v2_adv( @@ -1080,7 +1094,9 @@ async def test_v2_sensors( if ATTR_UNIT_OF_MEASUREMENT in sensor_attr: # Some sensors don't have a unit of measurement assert sensor_attr[ATTR_UNIT_OF_MEASUREMENT] == meas["unit_of_measurement"] - assert sensor_attr[ATTR_STATE_CLASS] == meas["state_class"] + if ATTR_STATE_CLASS in sensor_attr: + # Some sensors have state class None + assert sensor_attr[ATTR_STATE_CLASS] == meas["state_class"] assert await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done()