Skip to content

Commit

Permalink
add method to get correct device
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Nov 8, 2024
1 parent 20d48df commit 6be16ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pyacaia_async/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Exceptions for pyacaia_async."""

from bleak.exc import BleakDeviceNotFoundError, BleakError


Expand All @@ -12,3 +13,7 @@ class AcaiaDeviceNotFound(BleakDeviceNotFoundError):

class AcaiaError(BleakError):
"""Exception for general bleak errors."""


class AcaiaUnknownDevice(Exception):
"""Exception for unknown devices."""
35 changes: 33 additions & 2 deletions pyacaia_async/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"""Helper functions, taken from pyacaia."""

import logging

from bleak import BleakScanner
from bleak import BleakScanner, BLEDevice, BleakClient
from bleak.exc import BleakDeviceNotFoundError, BleakError

from .const import HEADER1, HEADER2, SCALE_START_NAMES
from .const import (
HEADER1,
HEADER2,
SCALE_START_NAMES,
OLD_STYLE_CHAR_ID,
DEFAULT_CHAR_ID,
)
from .exceptions import AcaiaDeviceNotFound, AcaiaError, AcaiaUnknownDevice

_LOOGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,6 +41,28 @@ async def scan(scanner: BleakScanner, timeout) -> list:
return addresses


async def is_new_scale(address_or_ble_device: str | BLEDevice) -> bool:
"""Check if the scale is a new style scale."""
async with BleakClient(address_or_ble_device) as client:
try:
await client.connect()
services = await client.get_services()
except BleakDeviceNotFoundError as ex:
raise AcaiaDeviceNotFound("Device not found") from ex
except (BleakError, Exception) as ex:
raise AcaiaError from ex

characteristics = []
for char in services.characteristics.values():
characteristics.append(char.uuid)

if OLD_STYLE_CHAR_ID in characteristics:
return False
if DEFAULT_CHAR_ID in characteristics:
return True
raise AcaiaUnknownDevice


def encode(msg_type: int, payload: bytearray | list[int]) -> bytearray:
"""Encode a message to the scale."""
byte_msg = bytearray(5 + len(payload))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyacaia_async",
version="0.1.0",
version="0.1.1",
description="An async implementation of PyAcaia",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 6be16ed

Please sign in to comment.