From e2bb2d3ff3f909b9417040de4dd8ea876777a6ee Mon Sep 17 00:00:00 2001 From: Edi Feschiyan Date: Tue, 9 Jun 2020 09:21:31 +0300 Subject: Adding argparser to bluetooth --- audiomixer.py | 6 +++--- bluetooth.py | 42 ++++++++++++++++++++++++++++++++++-------- test_bluetooth.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ test_gps.py | 12 ++++++------ 4 files changed, 93 insertions(+), 17 deletions(-) diff --git a/audiomixer.py b/audiomixer.py index 3d61c10..e594471 100644 --- a/audiomixer.py +++ b/audiomixer.py @@ -28,11 +28,11 @@ class AudioMixerService(AGLBaseService): async def list_controls(self): return await self.request('list_controls') - async def volume(self, value=None): + async def volume(self, control='Master', value=None): if value is not None: - return await self.request('volume', {'control': 'Master', 'value': value}) + return await self.request('volume', {'control': control, 'value': value}) else: - return await self.request('volume', {'control': 'Master'}) + return await self.request('volume', {'control': control}) async def mute(self, value=None): return await self.request('mute', {'control': 'Master', 'value': value}) diff --git a/bluetooth.py b/bluetooth.py index a4bbac3..658d2e7 100644 --- a/bluetooth.py +++ b/bluetooth.py @@ -1,14 +1,29 @@ -from aglbaseservice import AGLBaseService +from aglbaseservice import AGLBaseService, AFBResponse import asyncio import os Verbs = ['subscribe', 'unsubscribe', 'managed_objects', 'adapter_state', 'default_adapter', 'avrcp_controls', 'connect', 'disconnect', 'pair', 'cancel_pairing', 'confirm_pairing', 'remove_device'] AdapterStateParams = ['discovery', 'discoverable', 'powered', ] + BTEventType = ['adapter_changes', 'device_changes', 'media', 'agent'] class BluetoothService(AGLBaseService): + service = 'agl-service-bluetooth' + parser = AGLBaseService.getparser() + parser.add_argument('--default_adapter', help='Get default bluetooth adapter', action='store_true') + parser.add_argument('--managed_objects', help='Get managed objects', action='store_true') + parser.add_argument('--adapter', help='Select remote adapter', required=False, default='hci0') + parser.add_argument('--adapter_state') + parser.add_argument('--connect', help='Connect to device', metavar='dev_88_0F_10_96_D3_20') + parser.add_argument('--disconnect', help='Disconnect from device', metavar='dev_88_0F_10_96_D3_20') + parser.add_argument('--pair', help='Pair with a device', metavar='dev_88_0F_10_96_D3_20') + parser.add_argument('--cancel_pairing', help='Cancel ongoing pairing') + parser.add_argument('--confirm_pairing', metavar='pincode') + parser.add_argument('--remove_device', metavar='dev_88_0F_10_96_D3_20', help='Remove paired device') + + def __init__(self, ip, port=None, service='agl-service-bluetooth'): super().__init__(api='Bluetooth-Manager', ip=ip, port=port, service=service) @@ -53,13 +68,24 @@ class BluetoothService(AGLBaseService): pass async def main(loop): - addr = os.environ.get('AGL_TGT_IP', 'localhost') - port = os.environ.get('AGL_TGT_PORT', None) - BTS = await BluetoothService(ip=addr, service='agl-service-bluetooth', port=port) - listener = loop.create_task(BTS.listener()) - await BTS.subscribe() - BTS.logger.debug(await BTS.adapter_state('hci0', {'uuids': ['0000110e-0000-1000-8000-00805f9b34fb']})) - await listener + args = BluetoothService.parser.parse_args() + bts = await BluetoothService(ip=args.ipaddr, port=args.port) + + if args.default_adapter: + id = await bts.default_adapter() + print(f'Requesting default adapter with id {id}') + r = AFBResponse(await bts.response()) + print(r) + + if args.adapter_state: + pass + + if args.listener: + for response in bts.listener(): + print(response) + + bts.logger.debug(await bts.adapter_state('hci0', {'uuids': ['0000110e-0000-1000-8000-00805f9b34fb']})) + if __name__ == '__main__': diff --git a/test_bluetooth.py b/test_bluetooth.py index e69de29..1ccf499 100644 --- a/test_bluetooth.py +++ b/test_bluetooth.py @@ -0,0 +1,50 @@ +import asyncio +import os +import pytest +from bluetooth import BluetoothService as BTS +import logging +from aglbaseservice import AFBResponse, AFBT + +logger = logging.getLogger(f'pytest-{BTS.service}') +logger.setLevel(logging.DEBUG) +pytestmark = pytest.mark.asyncio + + +@pytest.fixture(scope="module") +def event_loop(): + loop = asyncio.get_event_loop() + yield loop + loop.close() + + +@pytest.fixture(scope='module') +async def service(): + address = os.environ.get('AGL_TGT_IP', 'localhost') + port = os.environ.get('AGL_TGT_PORT', None) + + bts = await BTS(ip=address, port=port) + yield bts + await bts.websocket.close() + + +async def test_default_adapter(event_loop, service: BTS): + id = await service.default_adapter() + resp = AFBResponse(await service.response()) + assert resp.status == 'success', resp + assert 'adapter' in resp.data.keys() + assert resp.data['adapter'] == 'hci0' + + +async def test_managed_objects(event_loop, service: BTS): + id = await service.managed_objects() + resp = AFBResponse(await service.response()) + assert resp.status == 'success', str(resp) + + +async def test_has_single_adapter(event_loop, service: BTS): + id = await service.managed_objects() + resp = AFBResponse(await service.response()) + assert len(resp.data['adapters']) == 1, \ + f'Detected {len(resp.data["adapters"])} adapters. Multiple adapters may also break testing' + + diff --git a/test_gps.py b/test_gps.py index a3b59c7..e1ad15e 100644 --- a/test_gps.py +++ b/test_gps.py @@ -1,7 +1,7 @@ import asyncio import os import pytest -from gps import GPSService +from gps import GPSService as GPS import logging from aglbaseservice import AFBResponse, AFBT @@ -18,7 +18,7 @@ def event_loop(): @pytest.fixture(scope='module') async def service(): address = os.environ.get('AGL_TGT_IP', 'localhost') - gpss = await GPSService(ip=address) + gpss = await GPS(ip=address) yield gpss await gpss.websocket.close() @@ -28,24 +28,24 @@ async def service(): # yield _response @pytest.mark.xfail # expecting this to fail because of "No 3D GNSS fix" and GPS is unavailable -async def test_location(event_loop, service: GPSService): +async def test_location(event_loop, service: GPS): id = await service.location() resp = AFBResponse(await service.response()) assert resp.status == 'success' -async def test_subscribe_location(event_loop, service: GPSService): +async def test_subscribe_location(event_loop, service: GPS): id = await service.subscribe('location') resp = AFBResponse(await service.response()) assert resp.msgid == id assert resp.status == 'success' -async def test_unsubscribe(event_loop, service: GPSService): +async def test_unsubscribe(event_loop, service: GPS): id = await service.unsubscribe('location') resp = AFBResponse(await service.response()) assert resp.msgid == id assert resp.status == 'success' -async def test_location_events(event_loop, service: GPSService): +async def test_location_events(event_loop, service: GPS): id = await service.subscribe('location') resp = AFBResponse(await service.response()) assert resp.msgid == id -- cgit 1.2.3-korg