From c83d0dab1aa44730976c0185c73d928a72a01b75 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 9 Sep 2022 13:44:12 +0200 Subject: [PATCH 1/5] feat: add binary sensors --- poetry.lock | 2 +- src/bthome_ble/__init__.py | 5 +- src/bthome_ble/const.py | 279 ++++++++++++++++++++++++++++--------- src/bthome_ble/parser.py | 109 ++++++++++----- tests/test_parser.py | 2 +- 5 files changed, 287 insertions(+), 110 deletions(-) diff --git a/poetry.lock b/poetry.lock index fb02d29..dbe9103 100644 --- a/poetry.lock +++ b/poetry.lock @@ -85,7 +85,7 @@ python-versions = ">=3.9,<4.0" [package.dependencies] home-assistant-bluetooth = ">=1.3.0" -sensor-state-data = ">=2.0" +sensor-state-data = ">=2.7.0" [package.extras] docs = ["Sphinx (>=5.0,<6.0)", "myst-parser (>=0.18,<0.19)", "sphinx-rtd-theme (>=1.0,<2.0)"] diff --git a/src/bthome_ble/__init__.py b/src/bthome_ble/__init__.py index 3975951..165b627 100644 --- a/src/bthome_ble/__init__.py +++ b/src/bthome_ble/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations from sensor_state_data import ( - BinarySensorDeviceClass, DeviceClass, DeviceKey, SensorDescription, @@ -12,13 +11,13 @@ Units, ) -from .parser import BTHomeBluetoothDeviceData +from .parser import BTHomeBluetoothDeviceData, BTHOME_BINARY_SENSOR_DEVICE_CLASS __version__ = "1.1.0" __all__ = [ + "BTHOME_BINARY_SENSOR_DEVICE_CLASS", "BTHomeBluetoothDeviceData", - "BinarySensorDeviceClass", "DeviceClass", "DeviceKey", "SensorDescription", diff --git a/src/bthome_ble/const.py b/src/bthome_ble/const.py index 8e061a7..fc2994d 100644 --- a/src/bthome_ble/const.py +++ b/src/bthome_ble/const.py @@ -2,7 +2,12 @@ import dataclasses from typing import Union -from sensor_state_data import BinarySensorDeviceClass, SensorLibrary, description +from sensor_state_data import ( + BinarySensorDeviceClass, + BaseDeviceClass, + SensorLibrary, + description, +) @dataclasses.dataclass @@ -232,69 +237,209 @@ 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", -] +class HomeAssistantSensorDeviceClass(BaseDeviceClass): + """Device class for sensors supported in Home Assistant.""" + + # apparent power (VA) + APPARENT_POWER = "apparent_power" + + # Air Quality Index + AQI = "aqi" + + # % of battery that is left + BATTERY = "battery" + + # ppm (parts per million) Carbon Monoxide gas concentration + CO = "carbon_monoxide" + + # ppm (parts per million) Carbon Dioxide gas concentration + CO2 = "carbon_dioxide" + + # current (A) + CURRENT = "current" + + # date (ISO8601) + DATE = "date" + + # fixed duration (TIME_DAYS, TIME_HOURS, TIME_MINUTES, TIME_SECONDS) + DURATION = "duration" + + # energy (Wh, kWh, MWh) + ENERGY = "energy" + + # frequency (Hz, kHz, MHz, GHz) + FREQUENCY = "frequency" + + # gas (m³ or ft³) + GAS = "gas" + + # Relative humidity (%) + HUMIDITY = "humidity" + + # current light level (lx/lm) + ILLUMINANCE = "illuminance" + + # moisture (%) + MOISTURE = "moisture" + + # Amount of money (currency) + MONETARY = "monetary" + + # Amount of NO2 (µg/m³) + NITROGEN_DIOXIDE = "nitrogen_dioxide" + + # Amount of NO (µg/m³) + NITROGEN_MONOXIDE = "nitrogen_monoxide" + + # Amount of N2O (µg/m³) + NITROUS_OXIDE = "nitrous_oxide" + + # Amount of O3 (µg/m³) + OZONE = "ozone" + + # Particulate matter <= 0.1 μm (µg/m³) + PM1 = "pm1" + + # Particulate matter <= 10 μm (µg/m³) + PM10 = "pm10" + + # Particulate matter <= 2.5 μm (µg/m³) + PM25 = "pm25" + + # power factor (%) + POWER_FACTOR = "power_factor" + + # power (W/kW) + POWER = "power" + + # pressure (hPa/mbar) + PRESSURE = "pressure" + + # reactive power (var) + REACTIVE_POWER = "reactive_power" + + # signal strength (dB/dBm) + SIGNAL_STRENGTH = "signal_strength" + + # Amount of SO2 (µg/m³) + SULPHUR_DIOXIDE = "sulphur_dioxide" + + # temperature (C/F) + TEMPERATURE = "temperature" + + # timestamp (ISO8601) + TIMESTAMP = "timestamp" + + # Amount of VOC (µg/m³) + VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds" + + # voltage (V) + VOLTAGE = "voltage" + + +class BTHomeAdditionalSensorDeviceClass(BaseDeviceClass): + """Device class for sensors supported in BTHome but not available in Home Assistant.""" + + # Mass + MASS = "mass" + + # Dew Point + DEW_POINT = "dew_point" + + # Count + COUNT = "count" + + +class HomeAssistantBinarySensorDeviceClass(BaseDeviceClass): + """Device class for binary sensors supported in Home Assistant.""" + + # On means low, Off means normal + BATTERY = "battery" + + # On means charging, Off means not charging + BATTERY_CHARGING = "battery_charging" + + # On means carbon monoxide detected, Off means no carbon monoxide (clear) + CO = "carbon_monoxide" + + # On means cold, Off means normal + COLD = "cold" + + # On means connected, Off means disconnected + CONNECTIVITY = "connectivity" + + # On means open, Off means closed + DOOR = "door" + + # On means open, Off means closed + GARAGE_DOOR = "garage_door" + + # On means gas detected, Off means no gas (clear) + GAS = "gas" + + # On means hot, Off means normal + HEAT = "heat" + + # On means light detected, Off means no light + LIGHT = "light" + + # On means open (unlocked), Off means closed (locked) + LOCK = "lock" + + # On means wet, Off means dry + MOISTURE = "moisture" + + # On means motion detected, Off means no motion (clear) + MOTION = "motion" + + # On means moving, Off means not moving (stopped) + MOVING = "moving" + + # On means occupied, Off means not occupied (clear) + OCCUPANCY = "occupancy" + + # On means open, Off means closed + OPENING = "opening" + + # On means plugged in, Off means unplugged + PLUG = "plug" + + # On means power detected, Off means no power + POWER = "power" + + # On means home, Off means away + PRESENCE = "presence" + + # On means problem detected, Off means no problem (OK) + PROBLEM = "problem" + + # On means running, Off means not running + RUNNING = "running" + + # On means unsafe, Off means safe + SAFETY = "safety" + + # On means smoke detected, Off means no smoke (clear) + SMOKE = "smoke" + + # On means sound detected, Off means no sound (clear) + SOUND = "sound" + + # On means tampering detected, Off means no tampering (clear) + TAMPER = "tamper" + + # On means update available, Off means up-to-date + UPDATE = "update" + + # On means vibration detected, Off means no vibration + VIBRATION = "vibration" + + # On means open, Off means closed + WINDOW = "window" + + +class BTHomeAdditionalBinarySensorDeviceClass(BaseDeviceClass): + """Device class for binary sensors supported in BTHome but not available in Home Assistant.""" + + # On means On, Off means Off + GENERIC = "generic" diff --git a/src/bthome_ble/parser.py b/src/bthome_ble/parser.py index ac61f6f..6664e9e 100644 --- a/src/bthome_ble/parser.py +++ b/src/bthome_ble/parser.py @@ -25,7 +25,30 @@ BaseSensorDescription, ) -from .const import HA_BINARY_SENSOR_DEVICE_CLASSES, HA_SENSOR_DEVICE_CLASSES, MEAS_TYPES +from .const import ( + BTHomeAdditionalBinarySensorDeviceClass, + BTHomeAdditionalSensorDeviceClass, + HomeAssistantBinarySensorDeviceClass, + HomeAssistantSensorDeviceClass, + MEAS_TYPES, +) + +HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASS = [ + i for i in HomeAssistantBinarySensorDeviceClass +] +HOME_ASSISTANT_SENSOR_DEVICE_CLASS = [i for i in HomeAssistantSensorDeviceClass] +BTHOME_ADD_BINARY_SENSOR_DEVICE_CLASS = [ + i for i in BTHomeAdditionalBinarySensorDeviceClass +] +BTHOME_ADD_SENSOR_DEVICE_CLASS = [i for i in BTHomeAdditionalSensorDeviceClass] +BTHOME_BINARY_SENSOR_DEVICE_CLASS = [ + *HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASS, + *BTHOME_ADD_BINARY_SENSOR_DEVICE_CLASS, +] +BTHOME_SENSOR_DEVICE_CLASS = [ + *HOME_ASSISTANT_SENSOR_DEVICE_CLASS, + *BTHOME_ADD_SENSOR_DEVICE_CLASS, +] _LOGGER = logging.getLogger(__name__) @@ -271,45 +294,55 @@ 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_CLASS + ): + # 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(), + ) 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_CLASS + ): + # 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_ADD_BINARY_SENSOR_DEVICE_CLASS + ): + # 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( diff --git a/tests/test_parser.py b/tests/test_parser.py index 70a150b..942ae2f 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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( From 716f626729080c451c24dd7da242d97709d620a7 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 9 Sep 2022 15:42:18 +0200 Subject: [PATCH 2/5] fix: mypy error --- src/bthome_ble/__init__.py | 23 ++- src/bthome_ble/const.py | 292 +++++++++++-------------------------- src/bthome_ble/parser.py | 38 ++--- 3 files changed, 116 insertions(+), 237 deletions(-) diff --git a/src/bthome_ble/__init__.py b/src/bthome_ble/__init__.py index 165b627..7869633 100644 --- a/src/bthome_ble/__init__.py +++ b/src/bthome_ble/__init__.py @@ -11,12 +11,31 @@ Units, ) -from .parser import BTHomeBluetoothDeviceData, BTHOME_BINARY_SENSOR_DEVICE_CLASS +from .parser import BTHomeBluetoothDeviceData +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, +) + +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.0" __all__ = [ - "BTHOME_BINARY_SENSOR_DEVICE_CLASS", + "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", "DeviceClass", "DeviceKey", diff --git a/src/bthome_ble/const.py b/src/bthome_ble/const.py index fc2994d..35b0dc8 100644 --- a/src/bthome_ble/const.py +++ b/src/bthome_ble/const.py @@ -3,12 +3,94 @@ from typing import Union from sensor_state_data import ( - BinarySensorDeviceClass, BaseDeviceClass, + 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: @@ -235,211 +317,3 @@ class MeasTypes: ) ), } - - -class HomeAssistantSensorDeviceClass(BaseDeviceClass): - """Device class for sensors supported in Home Assistant.""" - - # apparent power (VA) - APPARENT_POWER = "apparent_power" - - # Air Quality Index - AQI = "aqi" - - # % of battery that is left - BATTERY = "battery" - - # ppm (parts per million) Carbon Monoxide gas concentration - CO = "carbon_monoxide" - - # ppm (parts per million) Carbon Dioxide gas concentration - CO2 = "carbon_dioxide" - - # current (A) - CURRENT = "current" - - # date (ISO8601) - DATE = "date" - - # fixed duration (TIME_DAYS, TIME_HOURS, TIME_MINUTES, TIME_SECONDS) - DURATION = "duration" - - # energy (Wh, kWh, MWh) - ENERGY = "energy" - - # frequency (Hz, kHz, MHz, GHz) - FREQUENCY = "frequency" - - # gas (m³ or ft³) - GAS = "gas" - - # Relative humidity (%) - HUMIDITY = "humidity" - - # current light level (lx/lm) - ILLUMINANCE = "illuminance" - - # moisture (%) - MOISTURE = "moisture" - - # Amount of money (currency) - MONETARY = "monetary" - - # Amount of NO2 (µg/m³) - NITROGEN_DIOXIDE = "nitrogen_dioxide" - - # Amount of NO (µg/m³) - NITROGEN_MONOXIDE = "nitrogen_monoxide" - - # Amount of N2O (µg/m³) - NITROUS_OXIDE = "nitrous_oxide" - - # Amount of O3 (µg/m³) - OZONE = "ozone" - - # Particulate matter <= 0.1 μm (µg/m³) - PM1 = "pm1" - - # Particulate matter <= 10 μm (µg/m³) - PM10 = "pm10" - - # Particulate matter <= 2.5 μm (µg/m³) - PM25 = "pm25" - - # power factor (%) - POWER_FACTOR = "power_factor" - - # power (W/kW) - POWER = "power" - - # pressure (hPa/mbar) - PRESSURE = "pressure" - - # reactive power (var) - REACTIVE_POWER = "reactive_power" - - # signal strength (dB/dBm) - SIGNAL_STRENGTH = "signal_strength" - - # Amount of SO2 (µg/m³) - SULPHUR_DIOXIDE = "sulphur_dioxide" - - # temperature (C/F) - TEMPERATURE = "temperature" - - # timestamp (ISO8601) - TIMESTAMP = "timestamp" - - # Amount of VOC (µg/m³) - VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds" - - # voltage (V) - VOLTAGE = "voltage" - - -class BTHomeAdditionalSensorDeviceClass(BaseDeviceClass): - """Device class for sensors supported in BTHome but not available in Home Assistant.""" - - # Mass - MASS = "mass" - - # Dew Point - DEW_POINT = "dew_point" - - # Count - COUNT = "count" - - -class HomeAssistantBinarySensorDeviceClass(BaseDeviceClass): - """Device class for binary sensors supported in Home Assistant.""" - - # On means low, Off means normal - BATTERY = "battery" - - # On means charging, Off means not charging - BATTERY_CHARGING = "battery_charging" - - # On means carbon monoxide detected, Off means no carbon monoxide (clear) - CO = "carbon_monoxide" - - # On means cold, Off means normal - COLD = "cold" - - # On means connected, Off means disconnected - CONNECTIVITY = "connectivity" - - # On means open, Off means closed - DOOR = "door" - - # On means open, Off means closed - GARAGE_DOOR = "garage_door" - - # On means gas detected, Off means no gas (clear) - GAS = "gas" - - # On means hot, Off means normal - HEAT = "heat" - - # On means light detected, Off means no light - LIGHT = "light" - - # On means open (unlocked), Off means closed (locked) - LOCK = "lock" - - # On means wet, Off means dry - MOISTURE = "moisture" - - # On means motion detected, Off means no motion (clear) - MOTION = "motion" - - # On means moving, Off means not moving (stopped) - MOVING = "moving" - - # On means occupied, Off means not occupied (clear) - OCCUPANCY = "occupancy" - - # On means open, Off means closed - OPENING = "opening" - - # On means plugged in, Off means unplugged - PLUG = "plug" - - # On means power detected, Off means no power - POWER = "power" - - # On means home, Off means away - PRESENCE = "presence" - - # On means problem detected, Off means no problem (OK) - PROBLEM = "problem" - - # On means running, Off means not running - RUNNING = "running" - - # On means unsafe, Off means safe - SAFETY = "safety" - - # On means smoke detected, Off means no smoke (clear) - SMOKE = "smoke" - - # On means sound detected, Off means no sound (clear) - SOUND = "sound" - - # On means tampering detected, Off means no tampering (clear) - TAMPER = "tamper" - - # On means update available, Off means up-to-date - UPDATE = "update" - - # On means vibration detected, Off means no vibration - VIBRATION = "vibration" - - # On means open, Off means closed - WINDOW = "window" - - -class BTHomeAdditionalBinarySensorDeviceClass(BaseDeviceClass): - """Device class for binary sensors supported in BTHome but not available in Home Assistant.""" - - # On means On, Off means Off - GENERIC = "generic" diff --git a/src/bthome_ble/parser.py b/src/bthome_ble/parser.py index 6664e9e..1ef497d 100644 --- a/src/bthome_ble/parser.py +++ b/src/bthome_ble/parser.py @@ -26,30 +26,13 @@ ) from .const import ( - BTHomeAdditionalBinarySensorDeviceClass, - BTHomeAdditionalSensorDeviceClass, - HomeAssistantBinarySensorDeviceClass, - HomeAssistantSensorDeviceClass, + BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES, + BTHOME_ADDITIONAL_SENSOR_DEVICE_CLASSES, + HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES, + HOME_ASSISTANT_SENSOR_DEVICE_CLASSES, MEAS_TYPES, ) -HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASS = [ - i for i in HomeAssistantBinarySensorDeviceClass -] -HOME_ASSISTANT_SENSOR_DEVICE_CLASS = [i for i in HomeAssistantSensorDeviceClass] -BTHOME_ADD_BINARY_SENSOR_DEVICE_CLASS = [ - i for i in BTHomeAdditionalBinarySensorDeviceClass -] -BTHOME_ADD_SENSOR_DEVICE_CLASS = [i for i in BTHomeAdditionalSensorDeviceClass] -BTHOME_BINARY_SENSOR_DEVICE_CLASS = [ - *HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASS, - *BTHOME_ADD_BINARY_SENSOR_DEVICE_CLASS, -] -BTHOME_SENSOR_DEVICE_CLASS = [ - *HOME_ASSISTANT_SENSOR_DEVICE_CLASS, - *BTHOME_ADD_SENSOR_DEVICE_CLASS, -] - _LOGGER = logging.getLogger(__name__) @@ -297,16 +280,19 @@ def _parse_bthome( if meas_format.device_class: if ( meas_format.device_class - in HOME_ASSISTANT_SENSOR_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: + 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, + key=meas_format.device_class.value, native_unit_of_measurement=meas_format.native_unit_of_measurement, native_value=value, device_class=None, @@ -321,7 +307,7 @@ def _parse_bthome( if meas_format.device_class: if ( meas_format.device_class - in HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASS + in HOME_ASSISTANT_BINARY_SENSOR_DEVICE_CLASSES ): # Update binary sensors with a supported HA device class self.update_predefined_binary_sensor( @@ -330,7 +316,7 @@ def _parse_bthome( ) elif ( meas_format.device_class - in BTHOME_ADD_BINARY_SENSOR_DEVICE_CLASS + in BTHOME_ADDITIONAL_BINARY_SENSOR_DEVICE_CLASSES ): # Update binary sensors without a supported HA device class self.update_binary_sensor( From 44f171aad9375877dfba4f55d6f6f2ede12ccdad Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 9 Sep 2022 15:45:25 +0200 Subject: [PATCH 3/5] fix: remove unused import --- src/bthome_ble/__init__.py | 2 +- src/bthome_ble/const.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bthome_ble/__init__.py b/src/bthome_ble/__init__.py index 7869633..1a71233 100644 --- a/src/bthome_ble/__init__.py +++ b/src/bthome_ble/__init__.py @@ -11,13 +11,13 @@ Units, ) -from .parser import BTHomeBluetoothDeviceData 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 diff --git a/src/bthome_ble/const.py b/src/bthome_ble/const.py index 35b0dc8..2c8a43d 100644 --- a/src/bthome_ble/const.py +++ b/src/bthome_ble/const.py @@ -3,7 +3,6 @@ from typing import Union from sensor_state_data import ( - BaseDeviceClass, BinarySensorDeviceClass, SensorLibrary, description, From 477f974aba5ef9d469975473902994d01ed00181 Mon Sep 17 00:00:00 2001 From: Ernst79 Date: Fri, 9 Sep 2022 14:55:05 +0100 Subject: [PATCH 4/5] fix: update poetry lock file --- poetry.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index dbe9103..fb02d29 100644 --- a/poetry.lock +++ b/poetry.lock @@ -85,7 +85,7 @@ python-versions = ">=3.9,<4.0" [package.dependencies] home-assistant-bluetooth = ">=1.3.0" -sensor-state-data = ">=2.7.0" +sensor-state-data = ">=2.0" [package.extras] docs = ["Sphinx (>=5.0,<6.0)", "myst-parser (>=0.18,<0.19)", "sphinx-rtd-theme (>=1.0,<2.0)"] From 4ed3312453c35d1911bb463a2c0b35310626e84f Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 9 Sep 2022 16:06:51 +0200 Subject: [PATCH 5/5] fix: flake8 error --- src/bthome_ble/const.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/bthome_ble/const.py b/src/bthome_ble/const.py index 2c8a43d..dc83579 100644 --- a/src/bthome_ble/const.py +++ b/src/bthome_ble/const.py @@ -2,11 +2,7 @@ import dataclasses from typing import Union -from sensor_state_data import ( - BinarySensorDeviceClass, - SensorLibrary, - description, -) +from sensor_state_data import BinarySensorDeviceClass, SensorLibrary, description # Sensors with device classes that are available in Home Assistant HOME_ASSISTANT_SENSOR_DEVICE_CLASSES = [