From 02d7ca1435e3aa98b7c46f7cf6bbbf9285330973 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 21 Jul 2022 08:52:43 -0500 Subject: [PATCH] feat: refactor for sensor-state-data 2 (#12) --- poetry.lock | 22 ++++++------- pyproject.toml | 4 +-- src/inkbird_ble/__init__.py | 19 ++--------- src/inkbird_ble/parser.py | 18 ++++++---- tests/test_parser.py | 66 ++++++++++++++++++++++++------------- 5 files changed, 71 insertions(+), 58 deletions(-) diff --git a/poetry.lock b/poetry.lock index bca1b38..7d10899 100644 --- a/poetry.lock +++ b/poetry.lock @@ -64,7 +64,7 @@ python-versions = "*" [[package]] name = "bluetooth-sensor-state-data" -version = "1.4.6" +version = "1.5.0" description = "Models for storing and converting Bluetooth Sensor State Data" category = "main" optional = false @@ -72,7 +72,7 @@ python-versions = ">=3.9,<4.0" [package.dependencies] home-assistant-bluetooth = ">=1.3.0" -sensor-state-data = ">=1.11.1" +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)"] @@ -136,7 +136,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" [[package]] name = "home-assistant-bluetooth" -version = "1.3.0" +version = "1.4.0" description = "Home Assistant Bluetooth Models and Helpers" category = "main" optional = false @@ -439,7 +439,7 @@ use_chardet_on_py3 = ["chardet (>=3.0.2,<6)"] [[package]] name = "sensor-state-data" -version = "1.11.1" +version = "2.0.1" description = "Models for storing and converting Sensor Data state" category = "main" optional = false @@ -621,7 +621,7 @@ docs = ["myst-parser", "Sphinx", "sphinx-rtd-theme"] [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "8875389d32e55e31aedc7c8467972bf375bb4c91c6e40d7ad087f7b8d69e2958" +content-hash = "ac8be02e020c2877fd72ced791b320b27540cb8b3b0ff8333a8b1ff3f71354c6" [metadata.files] alabaster = [ @@ -655,8 +655,8 @@ bleak-winrt = [ {file = "bleak_winrt-1.1.1-cp39-cp39-win_amd64.whl", hash = "sha256:7481aed7d17e8c2aa2bf9e3786206e382f710f516e00887d825df1b7a4c613d5"}, ] bluetooth-sensor-state-data = [ - {file = "bluetooth-sensor-state-data-1.4.6.tar.gz", hash = "sha256:2bcb9063d9f48cc21348305d94dcd5152fc3456da20481bf13763b0baf8f3f6c"}, - {file = "bluetooth_sensor_state_data-1.4.6-py3-none-any.whl", hash = "sha256:c6650b0824cecd675a017588a9f845d9d0613123f1432da74e7cebe315287a7f"}, + {file = "bluetooth-sensor-state-data-1.5.0.tar.gz", hash = "sha256:a0e662f46c0fd9d1a1397f260f3c4dab1d9bb2005eb95082a8876b443ca90baa"}, + {file = "bluetooth_sensor_state_data-1.5.0-py3-none-any.whl", hash = "sha256:951c3e08820ed47b9913820f1af9a4c637c72c2dce998f50f99bb547c3eba5f3"}, ] certifi = [ {file = "certifi-2022.6.15-py3-none-any.whl", hash = "sha256:fe86415d55e84719d75f8b69414f6438ac3547d2078ab91b67e779ef69378412"}, @@ -722,8 +722,8 @@ docutils = [ {file = "docutils-0.17.1.tar.gz", hash = "sha256:686577d2e4c32380bb50cbb22f575ed742d58168cee37e99117a854bcd88f125"}, ] home-assistant-bluetooth = [ - {file = "home-assistant-bluetooth-1.3.0.tar.gz", hash = "sha256:da9fd3ba1e2231f69540f45d85636c2250a8fbe1ff74de216f9917a6afbe6d8b"}, - {file = "home_assistant_bluetooth-1.3.0-py3-none-any.whl", hash = "sha256:00aaffe0e5c37213d2f25b65aada15dd6d02f1b9d2fe89e927ac69e59ee8f2f9"}, + {file = "home-assistant-bluetooth-1.4.0.tar.gz", hash = "sha256:ca0967ec4a62ca2d06a2286f37f73b8284fa9ae8d53420b129351babdb230338"}, + {file = "home_assistant_bluetooth-1.4.0-py3-none-any.whl", hash = "sha256:7c5cb1e931e1198a260d36109dc26c4d186bab54ee8a92a8ae11fe62b90999a9"}, ] idna = [ {file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"}, @@ -908,8 +908,8 @@ requests = [ {file = "requests-2.28.1.tar.gz", hash = "sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983"}, ] sensor-state-data = [ - {file = "sensor-state-data-1.11.1.tar.gz", hash = "sha256:a008769ba324e77ae1b316458b2eb658fc0719bacba53bfdabfa6db25368f29a"}, - {file = "sensor_state_data-1.11.1-py3-none-any.whl", hash = "sha256:64355defc16fb11f38c767ceae2eb064f59b229164e17cd0fa818175cdca85da"}, + {file = "sensor-state-data-2.0.1.tar.gz", hash = "sha256:c18d22ef3c6b1440a3e96db0d9d691174e36a748c9f9c59283107a4e55aa2f09"}, + {file = "sensor_state_data-2.0.1-py3-none-any.whl", hash = "sha256:ee3b2c4081431faf6879da89035966edbdc557ab99bba6d15bd2693f591a8437"}, ] snowballstemmer = [ {file = "snowballstemmer-2.2.0-py2.py3-none-any.whl", hash = "sha256:c8e1716e83cc398ae16824e5572ae04e0d9fc2c6b985fb0f900f5f0c96ecba1a"}, diff --git a/pyproject.toml b/pyproject.toml index 17ba29f..a945ad2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,8 +29,8 @@ python = "^3.9" Sphinx = {version = "^5.0", optional = true} sphinx-rtd-theme = {version = "^1.0", optional = true} myst-parser = {version = "^0.18", optional = true} -bluetooth-sensor-state-data = ">=1.4.6" -sensor-state-data = ">=1.11.1" +bluetooth-sensor-state-data = ">=1.5.0" +sensor-state-data = ">=2.0.1" [tool.poetry.extras] docs = [ diff --git a/src/inkbird_ble/__init__.py b/src/inkbird_ble/__init__.py index 710628a..709d5f2 100644 --- a/src/inkbird_ble/__init__.py +++ b/src/inkbird_ble/__init__.py @@ -1,21 +1,14 @@ """Parser for Inkbird BLE advertisements.""" from __future__ import annotations -from bluetooth_sensor_state_data import SIGNAL_STRENGTH_KEY from sensor_state_data import ( DeviceClass, DeviceKey, SensorDescription, + SensorDeviceInfo, SensorUpdate, SensorValue, -) -from sensor_state_data.data import ( - ATTR_HW_VERSION, - ATTR_MANUFACTURER, - ATTR_MODEL, - ATTR_NAME, - ATTR_SW_VERSION, - SensorDeviceInfo, + Units, ) from .parser import INKBIRDBluetoothDeviceData @@ -24,13 +17,6 @@ __all__ = [ "INKBIRDBluetoothDeviceData", - "SIGNAL_STRENGTH_KEY", - "ATTR_HW_VERSION", - "ATTR_MANUFACTURER", - "ATTR_MODEL", - "ATTR_NAME", - "ATTR_SW_VERSION", - "SIGNAL_STRENGTH_KEY", "SensorDescription", "SensorDeviceInfo", "DeviceClass", @@ -38,4 +24,5 @@ "SensorUpdate", "SensorDeviceInfo", "SensorValue", + "Units", ] diff --git a/src/inkbird_ble/parser.py b/src/inkbird_ble/parser.py index 9ac58ff..6ae2ed0 100644 --- a/src/inkbird_ble/parser.py +++ b/src/inkbird_ble/parser.py @@ -69,12 +69,18 @@ def _process_update(self, local_name: str, address: str, data: bytes) -> None: (temp, hum) = unpack(" None: for idx, temp in enumerate(unpack(unpack_str, xvalue)): num = idx + 1 self.update_predefined_sensor( - SensorLibrary.TEMPERATURE, + SensorLibrary.TEMPERATURE__CELSIUS, convert_temperature(temp), key=f"temperature_probe_{num}", name=f"Temperature Probe {num}", diff --git a/tests/test_parser.py b/tests/test_parser.py index eb6f803..bc9b4cb 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,5 +1,11 @@ from bluetooth_sensor_state_data import BluetoothServiceInfo, DeviceClass, SensorUpdate -from sensor_state_data import DeviceKey, SensorDescription, SensorValue +from sensor_state_data import ( + DeviceKey, + SensorDescription, + SensorDeviceInfo, + SensorValue, + Units, +) from inkbird_ble.parser import INKBIRDBluetoothDeviceData @@ -23,48 +29,56 @@ def test_sps(): assert result == SensorUpdate( title=None, devices={ - None: {"manufacturer": "INKBIRD", "model": "IBS-TH", "name": "IBS-TH EEFF"} + None: SensorDeviceInfo( + name="IBS-TH EEFF", + model="IBS-TH", + manufacturer="INKBIRD", + sw_version=None, + hw_version=None, + ) }, entity_descriptions={ DeviceKey(key="battery", device_id=None): SensorDescription( device_key=DeviceKey(key="battery", device_id=None), - name="Battery", device_class=DeviceClass.BATTERY, - native_unit_of_measurement="%", + native_unit_of_measurement=Units.PERCENTAGE, ), DeviceKey(key="signal_strength", device_id=None): SensorDescription( device_key=DeviceKey(key="signal_strength", device_id=None), - name="Signal Strength", device_class=DeviceClass.SIGNAL_STRENGTH, - native_unit_of_measurement="dBm", + native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, ), DeviceKey(key="temperature", device_id=None): SensorDescription( device_key=DeviceKey(key="temperature", device_id=None), - name="Temperature", device_class=DeviceClass.TEMPERATURE, - native_unit_of_measurement="°C", + native_unit_of_measurement=Units.TEMP_CELSIUS, ), DeviceKey(key="humidity", device_id=None): SensorDescription( device_key=DeviceKey(key="humidity", device_id=None), - name="Humidity", device_class=DeviceClass.HUMIDITY, - native_unit_of_measurement="%", + native_unit_of_measurement=Units.PERCENTAGE, ), }, entity_values={ DeviceKey(key="battery", device_id=None): SensorValue( - device_key=DeviceKey(key="battery", device_id=None), native_value=86 + device_key=DeviceKey(key="battery", device_id=None), + native_value=86, + name="Battery", ), DeviceKey(key="signal_strength", device_id=None): SensorValue( device_key=DeviceKey(key="signal_strength", device_id=None), + name="Signal Strength", native_value=-60, ), DeviceKey(key="temperature", device_id=None): SensorValue( device_key=DeviceKey(key="temperature", device_id=None), + name="Temperature", native_value=20.44, ), DeviceKey(key="humidity", device_id=None): SensorValue( - device_key=DeviceKey(key="humidity", device_id=None), native_value=48.07 + device_key=DeviceKey(key="humidity", device_id=None), + native_value=48.07, + name="Humidity", ), }, ) @@ -87,59 +101,65 @@ def test_ibbq_4(): assert result == SensorUpdate( title=None, devices={ - None: {"manufacturer": "INKBIRD", "model": "iBBQ-4", "name": "iBBQ EEFF"} + None: SensorDeviceInfo( + name="iBBQ EEFF", + model="iBBQ-4", + manufacturer="INKBIRD", + sw_version=None, + hw_version=None, + ) }, entity_descriptions={ DeviceKey(key="signal_strength", device_id=None): SensorDescription( device_key=DeviceKey(key="signal_strength", device_id=None), - name="Signal Strength", device_class=DeviceClass.SIGNAL_STRENGTH, - native_unit_of_measurement="dBm", + native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, ), DeviceKey(key="temperature_probe_1", device_id=None): SensorDescription( device_key=DeviceKey(key="temperature_probe_1", device_id=None), - name="Temperature " "Probe " "1", device_class=DeviceClass.TEMPERATURE, - native_unit_of_measurement="°C", + native_unit_of_measurement=Units.TEMP_CELSIUS, ), DeviceKey(key="temperature_probe_2", device_id=None): SensorDescription( device_key=DeviceKey(key="temperature_probe_2", device_id=None), - name="Temperature " "Probe " "2", device_class=DeviceClass.TEMPERATURE, - native_unit_of_measurement="°C", + native_unit_of_measurement=Units.TEMP_CELSIUS, ), DeviceKey(key="temperature_probe_3", device_id=None): SensorDescription( device_key=DeviceKey(key="temperature_probe_3", device_id=None), - name="Temperature " "Probe " "3", device_class=DeviceClass.TEMPERATURE, - native_unit_of_measurement="°C", + native_unit_of_measurement=Units.TEMP_CELSIUS, ), DeviceKey(key="temperature_probe_4", device_id=None): SensorDescription( device_key=DeviceKey(key="temperature_probe_4", device_id=None), - name="Temperature " "Probe " "4", device_class=DeviceClass.TEMPERATURE, - native_unit_of_measurement="°C", + native_unit_of_measurement=Units.TEMP_CELSIUS, ), }, entity_values={ DeviceKey(key="signal_strength", device_id=None): SensorValue( device_key=DeviceKey(key="signal_strength", device_id=None), + name="Signal Strength", native_value=-60, ), DeviceKey(key="temperature_probe_1", device_id=None): SensorValue( device_key=DeviceKey(key="temperature_probe_1", device_id=None), + name="Temperature " "Probe " "1", native_value=26.0, ), DeviceKey(key="temperature_probe_2", device_id=None): SensorValue( device_key=DeviceKey(key="temperature_probe_2", device_id=None), + name="Temperature " "Probe " "2", native_value=25.0, ), DeviceKey(key="temperature_probe_3", device_id=None): SensorValue( device_key=DeviceKey(key="temperature_probe_3", device_id=None), + name="Temperature " "Probe " "3", native_value=26.0, ), DeviceKey(key="temperature_probe_4", device_id=None): SensorValue( device_key=DeviceKey(key="temperature_probe_4", device_id=None), + name="Temperature " "Probe " "4", native_value=25.0, ), },