Skip to content

Commit

Permalink
feat: add water storage sensor (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 authored Jan 10, 2024
1 parent 13b987c commit 6eb80e0
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 453 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
labels:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: 3.12
- name: Install labels
run: pip install labels
- name: Sync config with Github
Expand Down
27 changes: 13 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
- id: commitizen
stages: [commit-msg]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: debug-statements
- id: check-builtin-literals
Expand All @@ -28,38 +28,37 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: debug-statements
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: ["--tab-width", "2"]
# - repo: https://github.com/pre-commit/mirrors-prettier
# rev: v3.1.0
# hooks:
# - id: prettier
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.3.0
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.12.1
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
rev: v2.2.6
hooks:
- id: codespell
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.8.0
hooks:
- id: mypy
- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
rev: 1.7.6
hooks:
- id: bandit
args: [-x, tests]
924 changes: 494 additions & 430 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ docs = [
]

[tool.poetry.dev-dependencies]
pytest = "^7.0"
pytest-cov = "^3.0"
pytest = "^7.4"
pytest-cov = "^4.1"

[tool.semantic_release]
branch = "main"
Expand Down
14 changes: 14 additions & 0 deletions src/bthome_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
BaseDeviceClass,
BinarySensorDeviceClass,
SensorLibrary,
Units,
description,
)

Expand Down Expand Up @@ -33,6 +34,9 @@ class ExtendedSensorDeviceClass(BaseDeviceClass):
# Text
TEXT = "text"

# Water storage
WATER_STORAGE = "water_storage"


class ExtendedSensorLibrary(SensorLibrary):
"""Sensor Library for additional sensors (compared to sensor-state-data)."""
Expand All @@ -47,6 +51,11 @@ class ExtendedSensorLibrary(SensorLibrary):
native_unit_of_measurement=None,
)

WATER_STORAGE__VOLUME_LITERS = description.BaseSensorDescription(
device_class=ExtendedSensorDeviceClass.WATER_STORAGE,
native_unit_of_measurement=Units.VOLUME_LITERS,
)


MEAS_TYPES: dict[int, MeasTypes] = {
0x00: MeasTypes(meas_format=SensorLibrary.PACKET_ID__NONE),
Expand Down Expand Up @@ -394,4 +403,9 @@ class ExtendedSensorLibrary(SensorLibrary):
meas_format=ExtendedSensorLibrary.RAW__NONE,
data_format="raw",
),
0x55: MeasTypes(
meas_format=ExtendedSensorLibrary.WATER_STORAGE__VOLUME_LITERS,
data_length=4,
factor=0.001,
),
}
6 changes: 3 additions & 3 deletions src/bthome_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def _parse_payload(self, payload: bytes, sw_version: int) -> bool:

if value is not None:
if (
type(meas_format) == BaseSensorDescription
isinstance(meas_format, BaseSensorDescription)
and meas_format.device_class
):
self.update_sensor(
Expand All @@ -502,15 +502,15 @@ def _parse_payload(self, payload: bytes, sw_version: int) -> bool:
device_class=meas_format.device_class,
)
elif (
type(meas_format) == BaseBinarySensorDescription
isinstance(meas_format, BaseBinarySensorDescription)
and meas_format.device_class
):
self.update_binary_sensor(
key=f"{str(meas_format.device_class)}{postfix}",
device_class=meas_format.device_class,
native_value=bool(value),
)
elif type(meas_format) == EventDeviceKeys:
elif isinstance(meas_format, EventDeviceKeys):
event_type = parse_event_type(
event_device=meas_format,
data_obj=meas["measurement data"][0],
Expand Down
46 changes: 46 additions & 0 deletions tests/test_parser_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
KEY_VOLUME = DeviceKey(key="volume", device_id=None)
KEY_VOLUME_FLOW_RATE = DeviceKey(key="volume_flow_rate", device_id=None)
KEY_WATER = DeviceKey(key="water", device_id=None)
KEY_WATER_STORAGE = DeviceKey(key="water_storage", device_id=None)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -2699,6 +2700,51 @@ def test_bthome_text_invalid(caplog):
)


def test_bthome_water_storage(caplog):
"""Test BTHome parser for water storage in Liters."""
data_string = b"\x40\x55\x87\x56\x2a\x01"
advertisement = bytes_to_service_info(
data_string, local_name="TEST DEVICE", address="A4:C1:38:8D:18:B2"
)

device = BTHomeBluetoothDeviceData()

assert device.update(advertisement) == SensorUpdate(
title="TEST DEVICE 18B2",
devices={
None: SensorDeviceInfo(
name="TEST DEVICE 18B2",
manufacturer=None,
model="BTHome sensor",
sw_version="BTHome BLE v2",
hw_version=None,
)
},
entity_descriptions={
KEY_WATER_STORAGE: SensorDescription(
device_key=KEY_WATER_STORAGE,
device_class=ExtendedSensorDeviceClass.WATER_STORAGE,
native_unit_of_measurement=Units.VOLUME_LITERS,
),
KEY_SIGNAL_STRENGTH: SensorDescription(
device_key=KEY_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
),
},
entity_values={
KEY_WATER_STORAGE: SensorValue(
device_key=KEY_WATER_STORAGE,
name="Water Storage",
native_value=19551.879,
),
KEY_SIGNAL_STRENGTH: SensorValue(
device_key=KEY_SIGNAL_STRENGTH, name="Signal Strength", native_value=-60
),
},
)


def test_bthome_double_temperature(caplog):
"""Test BTHome parser for double temperature reading without encryption."""
data_string = b"\x40\x02\xca\x09\x02\xcf\x09"
Expand Down

0 comments on commit 6eb80e0

Please sign in to comment.