Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Nov 7, 2024
1 parent 61d20c8 commit 0e4dd57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 47 deletions.
70 changes: 24 additions & 46 deletions pyacaia_async/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@ class AcaiaScale:

def __init__(
self,
mac: str,
address_or_ble_device: str | BLEDevice,
is_new_style_scale: bool = True,
notify_callback: Callable[[], None] | None = None,
) -> None:
"""Initialize the scale."""

self._is_new_style_scale = is_new_style_scale
self._client: BleakClient | None = None

self._client = BleakClient(
address_or_ble_device=mac,
disconnected_callback=self._device_disconnected_callback,
)
self.address_or_ble_device = address_or_ble_device

# tasks
self.heartbeat_task: asyncio.Task | None = None
Expand Down Expand Up @@ -98,7 +96,11 @@ def __init__(
@property
def mac(self) -> str:
"""Return the mac address of the scale in upper case."""
return self._client.address.upper()
return (
self.address_or_ble_device.upper()
if isinstance(self.address_or_ble_device, str)
else self.address_or_ble_device.address.upper()
)

@property
def device_state(self) -> AcaiaDeviceState | None:
Expand All @@ -110,33 +112,6 @@ def weight(self) -> float | None:
"""Return the weight of the scale."""
return self._weight

@classmethod
async def create(
cls,
mac: str | None = None,
ble_device: BLEDevice | None = None,
is_new_style_scale: bool = True,
callback: (
Callable[[BleakGATTCharacteristic, bytearray], Awaitable[None] | None]
| None
) = None,
) -> AcaiaScale:
"""Create a new scale."""

if ble_device:
self = cls("", is_new_style_scale)
self._client = BleakClient(
address_or_ble_device=ble_device,
disconnected_callback=self._device_disconnected_callback,
)
elif mac:
self = cls(mac, is_new_style_scale)
else:
raise ValueError("Either mac or bleDevice must be specified")

await self.connect(callback)
return self

@property
def timer(self) -> int:
"""Return the current timer value in seconds."""
Expand All @@ -149,31 +124,27 @@ def timer(self) -> int:

return int(self._timer_stop - self._timer_start)

def _device_disconnected_callback(self, client: BleakClient) -> None:
def device_disconnected_handler(
self, client: BleakClient, notify: bool = True
) -> None:
"""Callback for device disconnected."""

_LOGGER.debug(
"Scale with address %s disconnected through disconnect callback",
"Scale with address %s disconnected through disconnect handler",
client.address,
)
self.timer_running = False
self.connected = False
self.last_disconnect_time = time.time()
if self._notify_callback:
self.async_empty_queue_and_cancel_tasks()
if notify and self._notify_callback:
self._notify_callback()

def new_client_from_ble_device(self, ble_device: BLEDevice) -> None:
"""Create a new client from a BLEDevice, used for Home Assistant"""
self._client = BleakClient(
address_or_ble_device=ble_device,
disconnected_callback=self._device_disconnected_callback,
)

async def _write_msg(self, char_id: str, payload: bytearray) -> None:
"""wrapper for writing to the device."""
if self._client is None:
raise AcaiaError("Client not initialized")
try:
if not self.connected:
return

await self._client.write_gatt_char(char_id, payload)
self._timestamp_last_command = time.time()
except BleakDeviceNotFoundError as ex:
Expand Down Expand Up @@ -242,6 +213,11 @@ async def connect(
)
return

self._client = BleakClient(
address_or_ble_device=self.address_or_ble_device,
disconnected_callback=self.device_disconnected_handler,
)

try:
await self._client.connect()
except BleakError as ex:
Expand Down Expand Up @@ -344,6 +320,8 @@ async def disconnect(self) -> None:
_LOGGER.debug("Disconnecting from scale")
self.connected = False
await self._queue.join()
if not self._client:
return
try:
await self._client.disconnect()
except BleakError as ex:
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.0.12b4",
version="0.0.12b5",
description="An async implementation of PyAcaia",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 0e4dd57

Please sign in to comment.