Skip to content

Commit

Permalink
Merge pull request #8 from Bluetooth-Devices/binarysensors
Browse files Browse the repository at this point in the history
feat: add binary sensors
  • Loading branch information
Ernst79 authored Sep 9, 2022
2 parents bf4dd36 + 586c4c0 commit 613648c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 109 deletions.
22 changes: 20 additions & 2 deletions src/bthome_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

from sensor_state_data import (
BinarySensorDeviceClass,
DeviceClass,
DeviceKey,
SensorDescription,
Expand All @@ -12,13 +11,32 @@
Units,
)

from .const import (
BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES,
BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES,
HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES,
HOME_ASSISTANT_SENSOR_DEVICE_CLASSES,
)
from .parser import BTHomeBluetoothDeviceData

BTHOME_SENSORS = (
HOME_ASSISTANT_SENSOR_DEVICE_CLASSES + BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES
)
BTHOME_BINARY_SENSORS = (
HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES
+ BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES
)

__version__ = "1.1.1"

__all__ = [
"BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES",
"BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES",
"BTHOME_BINARY_SENSORS",
"BTHOME_SENSORS",
"HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES",
"HOME_ASSISTANT_SENSOR_DEVICE_CLASSES",
"BTHomeBluetoothDeviceData",
"BinarySensorDeviceClass",
"DeviceClass",
"DeviceKey",
"SensorDescription",
Expand Down
150 changes: 82 additions & 68 deletions src/bthome_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,88 @@

from sensor_state_data import BinarySensorDeviceClass, SensorLibrary, description

# Sensors with device classes that are available in Home Assistant
HOME_ASSISTANT_SENSOR_DEVICE_CLASSES = [
"apparent_power",
"aqi",
"battery",
"carbon_monoxide",
"carbon_dioxide",
"current",
"date",
"duration",
"energy",
"frequency",
"gas",
"humidity",
"illuminance",
"moisture",
"monetary",
"nitrogen_dioxide",
"nitrogen_monoxide",
"nitrous_oxide",
"ozone",
"pm1",
"pm10",
"pm25",
"power_factor",
"power",
"pressure",
"reactive_power",
"signal_strength",
"sulphur_dioxide",
"temperature",
"timestamp",
"volatile_organic_compounds",
"voltage",
]

# Additional BTHome sensors with device classes that
# are not available in Home Assistant
BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES = [
"mass",
"dew_point",
"count",
]

# Binary sensors with device classes that are available in Home Assistant
HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES = [
"battery",
"battery_charging",
"carbon_monoxide",
"cold",
"connectivity",
"door",
"garage_door",
"gas",
"heat",
"light",
"lock",
"moisture",
"motion",
"moving",
"occupancy",
"opening",
"plug",
"power",
"presence",
"problem",
"running",
"safety",
"smoke",
"sound",
"tamper",
"update",
"vibration",
"window",
]

# Additional BTHome binary sensors with device classes that
# are not available in Home Assistant
BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES = [
"generic",
]


@dataclasses.dataclass
class MeasTypes:
Expand Down Expand Up @@ -230,71 +312,3 @@ class MeasTypes:
)
),
}


# Sensors with device classes that are available in Home Assistant
HA_SENSOR_DEVICE_CLASSES = [
"apparent_power",
"aqi",
"battery",
"carbon_monoxide",
"carbon_dioxide",
"current",
"date",
"duration",
"energy",
"frequency",
"gas",
"humidity",
"illuminance",
"monetary",
"nitrogen_dioxide",
"nitrogen_monoxide",
"nitrous_oxide",
"ozone",
"pm1",
"pm10",
"pm25",
"power_factor",
"power",
"pressure",
"reactive_power",
"signal_strength",
"sulphur_dioxide",
"temperature",
"timestamp",
"volatile_organic_compounds",
"voltage",
]

# Binary sensors with device classes that are available in Home Assistant
HA_BINARY_SENSOR_DEVICE_CLASSES = [
"battery",
"battery_charging",
"carbon_monoxide",
"cold",
"connectivity",
"door",
"garage_door",
"gas",
"heat",
"light",
"lock",
"moisture",
"motion",
"moving",
"occupancy",
"opening",
"plug",
"power",
"presence",
"problem",
"running",
"safety",
"smoke",
"sound",
"tamper",
"update",
"vibration",
"window",
]
95 changes: 57 additions & 38 deletions src/bthome_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
BaseSensorDescription,
)

from .const import HA_BINARY_SENSOR_DEVICE_CLASSES, HA_SENSOR_DEVICE_CLASSES, MEAS_TYPES
from .const import (
BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES,
BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES,
HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES,
HOME_ASSISTANT_SENSOR_DEVICE_CLASSES,
MEAS_TYPES,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -271,45 +277,58 @@ def _parse_bthome(
if value is not None:
if type(meas_format) == BaseSensorDescription:
# Update sensors
if meas_format.device_class in HA_SENSOR_DEVICE_CLASSES:
# Update sensors with a supported HA device class
self.update_predefined_sensor(
base_description=meas_format, native_value=value
)
elif meas_format.device_class:
# Update sensors without a supported HA device class
self.update_sensor(
key=meas_format.device_class,
native_unit_of_measurement=meas_format.native_unit_of_measurement,
native_value=value,
device_class=None,
)
else:
_LOGGER.debug(
"Unknown data object in BTHome BLE sensor payload! Adv: %s",
data.hex(),
)
if meas_format.device_class:
if (
meas_format.device_class
in HOME_ASSISTANT_SENSOR_DEVICE_CLASSES
):
# Update sensors with a supported HA device class
self.update_predefined_sensor(
base_description=meas_format, native_value=value
)
elif (
meas_format.device_class
in BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES
):
# Update sensors without a supported HA device class
self.update_sensor(
key=meas_format.device_class.value,
native_unit_of_measurement=meas_format.native_unit_of_measurement,
native_value=value,
device_class=None,
)
else:
_LOGGER.debug(
"Unknown data object in BTHome BLE sensor payload! Adv: %s",
data.hex(),
)
elif type(meas_format) == BaseBinarySensorDescription:
# update binary sensors
if meas_format.device_class in HA_BINARY_SENSOR_DEVICE_CLASSES:
# Update binary sensors with a supported HA device class
self.update_predefined_binary_sensor(
device_class=meas_format.device_class,
native_value=bool(value),
)
elif meas_format.device_class:
# Update binary sensors without a supported HA device class
# or without a device class
self.update_binary_sensor(
key=meas_format.device_class,
native_value=bool(value),
device_class=None,
)
else:
_LOGGER.debug(
"Unknown data object in BTHome BLE binary sensor payload! Adv: %s",
data.hex(),
)
if meas_format.device_class:
if (
meas_format.device_class
in HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES
):
# Update binary sensors with a supported HA device class
self.update_predefined_binary_sensor(
device_class=meas_format.device_class,
native_value=bool(value),
)
elif (
meas_format.device_class
in BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES
):
# Update binary sensors without a supported HA device class
self.update_binary_sensor(
key=meas_format.device_class,
native_value=bool(value),
device_class=None,
)
else:
_LOGGER.debug(
"Unknown data object in BTHome BLE binary sensor payload! Adv: %s",
data.hex(),
)
result = True
elif meas_str is not None:
_LOGGER.debug(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ def test_bthome_moisture(caplog):
entity_descriptions={
KEY_MOISTURE: SensorDescription(
device_key=KEY_MOISTURE,
device_class=None,
device_class=DeviceClass.MOISTURE,
native_unit_of_measurement=Units.PERCENTAGE,
),
KEY_SIGNAL_STRENGTH: SensorDescription(
Expand Down

0 comments on commit 613648c

Please sign in to comment.