1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Bumble device Mobly controller."""
16
17import asyncio
18import avatar.aio
19
20from bumble.pandora.device import PandoraDevice as BumblePandoraDevice
21from typing import Any, Dict, List, Optional
22
23MOBLY_CONTROLLER_CONFIG_NAME = 'BumbleDevice'
24
25
26def create(configs: List[Dict[str, Any]]) -> List[BumblePandoraDevice]:
27    """Create a list of `BumbleDevice` from configs."""
28    return [BumblePandoraDevice(config) for config in configs]
29
30
31def destroy(devices: List[BumblePandoraDevice]) -> None:
32    """Destroy each `BumbleDevice`"""
33
34    async def close_devices() -> None:
35        await asyncio.gather(*(device.close() for device in devices))
36
37    avatar.aio.run_until_complete(close_devices())
38
39
40def get_info(devices: List[BumblePandoraDevice]) -> List[Optional[Dict[str, str]]]:
41    """Return the device info for each `BumblePandoraDevice`."""
42    return [device.info() for device in devices]
43