Add text sensor to BTHome (#98355)

This commit is contained in:
Ernst Klamer 2023-08-22 14:34:26 +02:00 committed by GitHub
parent 32d8d65add
commit 1369874348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 9 deletions

View File

@ -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"]
}

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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()