Skip to content

Commit

Permalink
rework status flags for BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
jlusiardi committed Jan 29, 2019
1 parent 5022f17 commit 1d4851c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 2 additions & 6 deletions homekit/controller/ble_impl/manufacturer_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging

from homekit.model import Categories
from homekit.model.status_flags import BleStatusFlags


def parse_manufacturer_specific(input_data):
Expand Down Expand Up @@ -46,12 +47,7 @@ def parse_manufacturer_specific(input_data):
input_data = input_data[1:]

sf = input_data[0]
if sf == 0:
flags = 'paired'
elif sf == 1:
flags = 'unpaired'
else:
flags = 'error'
flags = BleStatusFlags[sf]
input_data = input_data[1:]

device_id = (':'.join(input_data[:6].hex()[0 + i:2 + i] for i in range(0, 12, 2))).upper()
Expand Down
20 changes: 20 additions & 0 deletions homekit/model/status_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@ def __getitem__(self, item):
raise KeyError('Item {item} not found'.format(item=item))


class _BleStatusFlags(object):
"""
Data taken form table 6-32 page 125
"""

def __getitem__(self, item):
i = int(item)
result = []
if i & 0x01:
result.append('The accessory has not been paired with any controllers.')
i = i - 0x01
else:
result.append('The accessory has been paired with a controllers.')
if i == 0:
return ' '.join(result)
else:
raise KeyError('Item {item} not found'.format(item=item))


IpStatusFlags = _IpStatusFlags()
BleStatusFlags = _BleStatusFlags()
7 changes: 4 additions & 3 deletions tests/ble_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from homekit.model import mixin as model_mixin
from homekit import exceptions
from homekit.controller.ble_impl.manufacturer_data import parse_manufacturer_specific
from homekit.model.status_flags import BleStatusFlags


class DeviceManager:
Expand Down Expand Up @@ -993,10 +994,10 @@ def test_1(self):
'cn': 2,
'cv': 2,
'device_id': '99:99:99:99:99:99',
'flags': 'paired',
'gsn': 3985,
'manufacturer': 'apple',
'sf': 0,
'flags': BleStatusFlags[0],
'type': 'HomeKit'
})

Expand All @@ -1008,10 +1009,10 @@ def test_2(self):
'cn': 2,
'cv': 2,
'device_id': '4A:4D:00:00:00:00',
'flags': 'paired',
'gsn': 11,
'manufacturer': 'apple',
'sf': 0,
'flags': BleStatusFlags[0],
'type': 'HomeKit'
})

Expand All @@ -1023,9 +1024,9 @@ def test_3(self):
'cn': 2,
'cv': 2,
'device_id': '7B:21:21:49:23:3C',
'flags': 'unpaired',
'gsn': 66,
'manufacturer': 'apple',
'sf': 1,
'flags': BleStatusFlags[1],
'type': 'HomeKit'
})

0 comments on commit 1d4851c

Please sign in to comment.