Add coverage for GATT services from dict (#760)

This commit is contained in:
J. Nick Koston 2023-11-27 08:43:51 -06:00 committed by GitHub
parent 60e193e4df
commit f6eff1a205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,10 +3,14 @@ from __future__ import annotations
from dataclasses import dataclass, field
import pytest
from google.protobuf import message
from aioesphomeapi.api_pb2 import (
AlarmControlPanelStateResponse,
BinarySensorStateResponse,
BluetoothGATTCharacteristic,
BluetoothGATTDescriptor,
BluetoothGATTGetServicesResponse,
ClimateStateResponse,
CoverStateResponse,
DeviceInfoResponse,
@ -49,6 +53,14 @@ from aioesphomeapi.model import (
APIVersion,
BinarySensorInfo,
BinarySensorState,
)
from aioesphomeapi.model import (
BluetoothGATTCharacteristic as BluetoothGATTCharacteristicModel,
)
from aioesphomeapi.model import BluetoothGATTDescriptor as BluetoothGATTDescriptorModel
from aioesphomeapi.model import BluetoothGATTService as BluetoothGATTServiceModel
from aioesphomeapi.model import BluetoothGATTServices as BluetoothGATTServicesModel
from aioesphomeapi.model import (
BluetoothProxyFeature,
ButtonInfo,
CameraInfo,
@ -497,3 +509,88 @@ def test_supported_color_modes_compat(
)
assert info.supported_color_modes_compat(APIVersion(1, 5)) == capability
assert info.supported_color_modes_compat(APIVersion(1, 9)) == [42]
@pytest.mark.asyncio
async def test_bluetooth_gatt_services_from_dict() -> None:
"""Test bluetooth_gatt_get_services success case."""
services: message.Message = BluetoothGATTGetServicesResponse(
address=1234,
services=[
{
"uuid": [1, 1],
"handle": 1,
"characteristics": [
{
"uuid": [1, 2],
"handle": 2,
"properties": 1,
"descriptors": [
{"uuid": [1, 3], "handle": 3},
],
},
],
}
],
)
services = BluetoothGATTServicesModel.from_pb(services)
assert services.services[0] == BluetoothGATTServiceModel(
uuid=[1, 1],
handle=1,
characteristics=[
BluetoothGATTCharacteristic(
uuid=[1, 2],
handle=2,
properties=1,
descriptors=[BluetoothGATTDescriptor(uuid=[1, 3], handle=3)],
)
],
)
services == BluetoothGATTServicesModel.from_dict(
{
"services": [
{
"uuid": [1, 1],
"handle": 1,
"characteristics": [
{
"uuid": [1, 2],
"handle": 2,
"properties": 1,
"descriptors": [
{"uuid": [1, 3], "handle": 3},
],
},
],
}
]
}
)
assert services.services[0] == BluetoothGATTServiceModel(
uuid=[1, 1],
handle=1,
characteristics=[
BluetoothGATTCharacteristic(
uuid=[1, 2],
handle=2,
properties=1,
descriptors=[BluetoothGATTDescriptor(uuid=[1, 3], handle=3)],
)
],
)
assert BluetoothGATTCharacteristicModel.from_dict(
{
"uuid": [1, 2],
"handle": 2,
"properties": 1,
"descriptors": [],
}
) == BluetoothGATTCharacteristicModel(
uuid=[1, 2],
handle=2,
properties=1,
descriptors=[],
)
assert BluetoothGATTDescriptorModel.from_dict(
{"uuid": [1, 3], "handle": 3},
) == BluetoothGATTDescriptorModel(uuid=[1, 3], handle=3)