Skip to content

Commit

Permalink
0.5.0 Sort devices by RSSI before connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
nayaverdier committed Jan 26, 2022
1 parent 849d948 commit 6128100
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.0 2022-01-25

- Sort devices by RSSI before connecting to avoid unnecessary delay

## 0.4.0 2021-11-08

(BREAKING)
Expand Down
1 change: 0 additions & 1 deletion halohome/VERSION

This file was deleted.

31 changes: 22 additions & 9 deletions halohome/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from importlib import resources
import logging
from importlib import metadata
from typing import Iterable, List

import aiohttp
import csrmesh
from bleak import BleakClient
from bleak import BleakClient, BleakScanner
from bleak.exc import BleakError

VERSION = resources.read_text("halohome", "VERSION").strip()
VERSION = metadata.version("halohome")
HOST = "https://api.avi-on.com"
PRODUCT_IDS = (93,)
TIMEOUT = 5

_LOGGER = logging.getLogger(__name__)


class HaloHomeError(Exception):
pass


def _format_mac_address(mac_address: str) -> str:
iterator = iter(mac_address)
iterator = iter(mac_address.lower())
pairs = zip(iterator, iterator)
return ":".join(a + b for a, b in pairs)

Expand Down Expand Up @@ -69,8 +72,20 @@ def __init__(self, location_id: str, passphrase: str, devices: List[dict], timeo
device = Device(self, device_id, device_name, pid, mac_address)
self.devices.append(device)

async def _priority_devices(self):
scanned_devices = sorted(await BleakScanner.discover(), key=lambda d: d.rssi)
sorted_addresses = [d.address.lower() for d in scanned_devices]

def priority(device: Device):
try:
return sorted_addresses.index(device.mac_address)
except ValueError:
return -1

return sorted(self.devices, key=priority, reverse=True)

async def _connect(self):
for device in self.devices:
for device in await self._priority_devices():
try:
client = BleakClient(device.mac_address, timeout=self.timeout)
await client.connect()
Expand All @@ -84,10 +99,7 @@ async def _send_packet(self, packet: bytes) -> bool:
low = csrpacket[:20]
high = csrpacket[20:]

tries = 3
while tries > 0:
tries -= 1

for _ in range(3):
try:
if self.mesh_connection is None:
await self._connect()
Expand All @@ -97,6 +109,7 @@ async def _send_packet(self, packet: bytes) -> bool:
return True
except Exception:
self.mesh_connection = None
_LOGGER.exception("Caught exception connecting to device")

return False

Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
changelog = (ROOT_DIRECTORY / "CHANGELOG.md").read_text()
long_description = readme + "\n\n" + changelog

version = (ROOT_DIRECTORY / "halohome" / "VERSION").read_text().strip()


DEV_REQUIRES = [
"black==21.9b0",
"flake8==4.0.1",
Expand All @@ -24,7 +21,7 @@

setup(
name="halohome",
version=version,
version="0.5.0",
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 6128100

Please sign in to comment.