Add support for BTHome V2 to bthome (#81811)

* Add BTHome v2 support

* Add new sensor types

* Add new sensor types
This commit is contained in:
Ernst Klamer 2022-11-09 15:31:58 +01:00 committed by GitHub
parent 4b4bf54994
commit b72876d369
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 790 additions and 44 deletions

View file

@ -21,16 +21,20 @@ from homeassistant.components.sensor import (
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION,
DEGREE,
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
LIGHT_LUX,
MASS_KILOGRAMS,
MASS_POUNDS,
PERCENTAGE,
POWER_WATT,
PRESSURE_MBAR,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS,
TIME_SECONDS,
UnitOfEnergy,
UnitOfLength,
UnitOfMass,
UnitOfPower,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
@ -43,7 +47,7 @@ SENSOR_DESCRIPTIONS = {
(BTHomeSensorDeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
(BTHomeSensorDeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
@ -61,7 +65,7 @@ SENSOR_DESCRIPTIONS = {
(BTHomeSensorDeviceClass.PRESSURE, Units.PRESSURE_MBAR): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.PRESSURE}_{Units.PRESSURE_MBAR}",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=PRESSURE_MBAR,
native_unit_of_measurement=UnitOfPressure.MBAR,
state_class=SensorStateClass.MEASUREMENT,
),
(BTHomeSensorDeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
@ -86,13 +90,13 @@ SENSOR_DESCRIPTIONS = {
): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.ENERGY}_{Units.ENERGY_KILO_WATT_HOUR}",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(BTHomeSensorDeviceClass.POWER, Units.POWER_WATT): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.POWER}_{Units.POWER_WATT}",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=POWER_WATT,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
),
(
@ -146,14 +150,14 @@ SENSOR_DESCRIPTIONS = {
(BTHomeSensorDeviceClass.MASS, Units.MASS_KILOGRAMS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.MASS}_{Units.MASS_KILOGRAMS}",
device_class=SensorDeviceClass.WEIGHT,
native_unit_of_measurement=MASS_KILOGRAMS,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for mass sensor with lb unit
(BTHomeSensorDeviceClass.MASS, Units.MASS_POUNDS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.MASS}_{Units.MASS_POUNDS}",
device_class=SensorDeviceClass.WEIGHT,
native_unit_of_measurement=MASS_POUNDS,
native_unit_of_measurement=UnitOfMass.POUNDS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for moisture sensor
@ -167,7 +171,7 @@ SENSOR_DESCRIPTIONS = {
(BTHomeSensorDeviceClass.DEW_POINT, Units.TEMP_CELSIUS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.DEW_POINT}_{Units.TEMP_CELSIUS}",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for count sensor
@ -176,6 +180,64 @@ SENSOR_DESCRIPTIONS = {
device_class=None,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for rotation sensor
(BTHomeSensorDeviceClass.ROTATION, Units.DEGREE): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.ROTATION}_{Units.DEGREE}",
device_class=None,
native_unit_of_measurement=DEGREE,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for distance sensor in mm
(
BTHomeSensorDeviceClass.DISTANCE,
Units.LENGTH_MILLIMETERS,
): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.DISTANCE}_{Units.LENGTH_MILLIMETERS}",
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement=UnitOfLength.MILLIMETERS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for distance sensor in m
(BTHomeSensorDeviceClass.DISTANCE, Units.LENGTH_METERS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.DISTANCE}_{Units.LENGTH_METERS}",
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement=UnitOfLength.METERS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for duration sensor
(BTHomeSensorDeviceClass.DURATION, Units.TIME_SECONDS): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.DURATION}_{Units.TIME_SECONDS}",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=TIME_SECONDS,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for current sensor
(
BTHomeSensorDeviceClass.CURRENT,
Units.ELECTRIC_CURRENT_AMPERE,
): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.CURRENT}_{Units.ELECTRIC_CURRENT_AMPERE}",
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for speed sensor
(
BTHomeSensorDeviceClass.SPEED,
Units.SPEED_METERS_PER_SECOND,
): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.SPEED}_{Units.SPEED_METERS_PER_SECOND}",
device_class=SensorDeviceClass.SPEED,
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
),
# Used for UV index sensor
(BTHomeSensorDeviceClass.UV_INDEX, None,): SensorEntityDescription(
key=f"{BTHomeSensorDeviceClass.UV_INDEX}",
device_class=None,
native_unit_of_measurement=None,
state_class=SensorStateClass.MEASUREMENT,
),
}