Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/source/contrib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Contrib-Packages
****************


This section describes all contrib packages.

For ``balderhub-battery``
=========================

.. autoclass:: balderhub.ble.contrib.battery.setup_features.BatteryLevelReader
:members:
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ out the `official documentation <https://docs.balder.dev>`_ first.
features.rst
examples.rst
utilities.rst
contrib.rst
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pycryptodome>=3.17
pylint==2.17.7
balderhub-unit
balderhub-heart
balderhub-battery
bleak
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ project_urls =
[options]
packages =
balderhub.ble
balderhub.ble.contrib
balderhub.ble.contrib.battery
balderhub.ble.contrib.battery.setup_features
balderhub.ble.lib
balderhub.ble.lib.scenario_features
balderhub.ble.lib.scenario_features.gatt
Expand All @@ -39,6 +42,7 @@ packages =
install_requires =
baldertest
balderhub-heart
balderhub-battery
bleak
python_requires = >=3.9
package_dir =
Expand Down
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions src/balderhub/ble/contrib/battery/setup_features/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .battery_level_reader import BatteryLevelReader

__all__ = [
'BatteryLevelReader'
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Union
import time

import balder

import balderhub.ble.lib.scenario_features
import balderhub.ble.lib.scenario_features.gatt
import balderhub.ble.lib.setup_features
import balderhub.battery.lib.scenario_features


class BatteryLevelReader(balderhub.battery.lib.scenario_features.BatteryLevelReader):
"""Setup Level implementation over BLE for reading the battery level"""
controller = balderhub.ble.lib.setup_features.BleakGattControllerFeature()

class DUT(balder.VDevice):
"""vdevice representing the device under test that implements the ``GattBatteryServiceFeature``"""
config = balderhub.ble.lib.scenario_features.BleDeviceConfig()
gatt = balderhub.ble.lib.scenario_features.gatt.GattBatteryServiceFeature()

def read_current_battery_level(self) -> Union[float, None]:

try:
self.controller.connect(self.DUT.config.mac_address)
time.sleep(10) # TODO
return self.controller.read(self.DUT.gatt.BatteryLevel).level_percent / 100
except ValueError as exc:
# TODO improve
if "unable to connect with device at address" in str(exc):
return None
raise exc
finally:
self.controller.unpair()
#self.controller.disconnect()