Skip to content

Commit

Permalink
fix: verify adapter type before method execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Sep 23, 2023
1 parent 0a09ae9 commit 90d430c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 10 additions & 0 deletions ruuvitag_sensor/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def is_async_from_env():
return "bleak" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower()


def throw_if_not_sync_adapter(ble: object):
if is_async_adapter(ble):
raise RuntimeError("Sync BLE adapter required")


def throw_if_not_async_adapter(ble: object):
if not is_async_adapter(ble):
raise RuntimeError("Async BLE adapter required")


class BleCommunication:
"""Bluetooth LE communication"""

Expand Down
17 changes: 11 additions & 6 deletions ruuvitag_sensor/ruuvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import AsyncGenerator, Callable, Dict, Generator, List, Optional
from warnings import warn

from ruuvitag_sensor.adapters import get_ble_adapter, is_async_adapter
from ruuvitag_sensor.adapters import get_ble_adapter, throw_if_not_async_adapter, throw_if_not_sync_adapter
from ruuvitag_sensor.data_formats import DataFormats
from ruuvitag_sensor.decoder import get_decoder, parse_mac
from ruuvitag_sensor.ruuvi_types import DataFormatAndRawSensorData, Mac, MacAndRawData, MacAndSensorData, SensorData
Expand Down Expand Up @@ -42,6 +42,7 @@ def get_first_raw_data(mac: str, bt_device: str = "") -> DataFormatAndRawSensorD
Returns:
tuple (int, string): Data Format type and raw Sensor data
"""
throw_if_not_sync_adapter(ble)

raw = ble.get_first_data(mac, bt_device)
return DataFormats.convert_data(raw)
Expand All @@ -60,6 +61,8 @@ async def get_first_raw_data_async(mac: str, bt_device: str = "") -> DataFormatA
Returns:
tuple (int, string): Data Format type and raw Sensor data
"""
throw_if_not_async_adapter(ble)

raw = await ble.get_first_data(mac, bt_device)
return DataFormats.convert_data(raw)

Expand All @@ -74,6 +77,7 @@ def find_ruuvitags(bt_device: str = "") -> Dict[Mac, SensorData]:
Returns:
dict: MAC and state of found sensors
"""
throw_if_not_sync_adapter(ble)

log.info("Finding RuuviTags. Stop with Ctrl+C.")

Expand All @@ -99,9 +103,7 @@ async def find_ruuvitags_async(bt_device: str = "") -> Dict[Mac, MacAndSensorDat
Returns:
dict: MAC and state of found sensors
"""

if not is_async_adapter(ble):
raise Exception("Only Bleak BLE communication is supported")
throw_if_not_async_adapter(ble)

log.info("Finding RuuviTags. Stop with Ctrl+C.")

Expand Down Expand Up @@ -135,6 +137,7 @@ def get_data_for_sensors(
Returns:
dict: MAC and state of found sensors
"""
throw_if_not_sync_adapter(ble)

log.info("Get latest data for sensors. Stop with Ctrl+C.")
log.info("Stops automatically in %ss", search_duratio_sec)
Expand Down Expand Up @@ -162,6 +165,7 @@ async def get_data_for_sensors_async(
Returns:
dict: MAC and state of found sensors
"""
throw_if_not_async_adapter(ble)

log.info("Get latest data for sensors. Stop with Ctrl+C.")
log.info("Stops automatically in %ss", search_duratio_sec)
Expand All @@ -180,8 +184,7 @@ async def get_data_for_sensors_async(

@staticmethod
async def get_data_async(macs: List[str] = [], bt_device: str = "") -> AsyncGenerator[MacAndSensorData, None]:
if not is_async_adapter(ble):
raise Exception("Only Bleak BLE communication is supported")
throw_if_not_async_adapter(ble)

mac_blacklist = Manager().list()
data_iter = ble.get_data(mac_blacklist, bt_device)
Expand Down Expand Up @@ -213,6 +216,7 @@ def get_data(
run_flag (object): RunFlag object. Function executes while run_flag.running
bt_device (string): Bluetooth device id
"""
throw_if_not_sync_adapter(ble)

log.info("Get latest data for sensors. Stop with Ctrl+C.")
log.info("MACs: %s", macs)
Expand All @@ -233,6 +237,7 @@ def get_datas(
Use get_data-method instead.
"""
warn("This method will be removed in a future version, use get_data() instead", FutureWarning)
throw_if_not_sync_adapter(ble)
return RuuviTagSensor.get_data(callback, macs, run_flag, bt_device)

@staticmethod
Expand Down

0 comments on commit 90d430c

Please sign in to comment.