def identify(accessory_id): """ This call can be used to trigger the identification of an accessory, that was not yet paired. A successful call should cause the accessory to perform some specific action by which it can be distinguished from others (blink a LED for example). It uses the /identify url as described on page 88 of the spec. :param accessory_id: the accessory's pairing id (e.g. retrieved via discover) :raises AccessoryNotFoundError: if the accessory could not be looked up via Bonjour :raises AlreadyPairedError: if the accessory is already paired """ if not IP_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('IP') connection_data = find_device_ip_and_port(accessory_id) if connection_data is None: raise AccessoryNotFoundError( 'Cannot find accessory with id "{i}".'.format(i=accessory_id)) conn = HomeKitHTTPConnection(connection_data['ip'], port=connection_data['port']) conn.request('POST', '/identify') resp = conn.getresponse() # spec says status code 400 on any error (page 88). It also says status should be -70401 (which is "Request # denied due to insufficient privileges." table 5-12 page 80) but this sounds odd. if resp.code == 400: data = json.loads(resp.read().decode()) code = data['status'] conn.close() raise AlreadyPairedError( 'identify failed because: {reason} ({code}).'.format( reason=HapStatusCodes[code], code=code)) conn.close()
def start_pairing(self, alias, accessory_id): """ This starts a pairing attempt with the IP accessory identified by its id. It returns a callable (finish_pairing) which you must call with the pairing pin. Accessories can be found via the discover method. The id field is the accessory's id for the second parameter. The required pin is either printed on the accessory or displayed. Must be a string of the form 'XXX-YY-ZZZ'. If this format is not used, a MalformedPinError is raised. Important: no automatic saving of the pairing data is performed. If you don't do this, the information is lost and you have to reset the accessory! :param alias: the alias for the accessory in the controllers data :param accessory_id: the accessory's id :param pin: function to return the accessory's pin :raises AccessoryNotFoundError: if no accessory with the given id can be found :raises AlreadyPairedError: if the alias was already used :raises UnavailableError: if the device is already paired :raises MaxTriesError: if the device received more than 100 unsuccessful attempts :raises BusyError: if a parallel pairing is ongoing :raises AuthenticationError: if the verification of the device's SRP proof fails :raises MaxPeersError: if the device cannot accept an additional pairing :raises UnavailableError: on wrong pin """ if not IP_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('IP') if alias in self.pairings: raise AlreadyPairedError( 'Alias "{a}" is already paired.'.format(a=alias)) connection_data = find_device_ip_and_port(accessory_id) if connection_data is None: raise AccessoryNotFoundError( 'Cannot find accessory with id "{i}".'.format(i=accessory_id)) conn = HomeKitHTTPConnection(connection_data['ip'], port=connection_data['port']) try: write_fun = create_ip_pair_setup_write(conn) salt, pub_key = perform_pair_setup_part1(write_fun) except Exception: conn.close() raise def finish_pairing(pin): Controller.check_pin_format(pin) try: pairing = perform_pair_setup_part2(pin, str(uuid.uuid4()), write_fun, salt, pub_key) finally: conn.close() pairing['AccessoryIP'] = connection_data['ip'] pairing['AccessoryPort'] = connection_data['port'] pairing['Connection'] = 'IP' self.pairings[alias] = IpPairing(pairing) return finish_pairing
def identify_ble(accessory_mac, adapter='hci0'): """ This call can be used to trigger the identification of an accessory, that was not yet paired. A successful call should cause the accessory to perform some specific action by which it can be distinguished from others (blink a LED for example). It uses the /identify url as described on page 88 of the spec. :param accessory_mac: the accessory's mac address (e.g. retrieved via discover) :raises AccessoryNotFoundError: if the accessory could not be looked up via Bonjour :param adapter: the bluetooth adapter to be used (defaults to hci0) :raises AlreadyPairedError: if the accessory is already paired """ if not BLE_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('BLE') from .ble_impl.device import DeviceManager manager = DeviceManager(adapter) device = manager.make_device(accessory_mac) device.connect() disco_info = device.get_homekit_discovery_data() if disco_info.get('flags', 'unknown') == 'paired': raise AlreadyPairedError( 'identify of {mac_address} failed not allowed as device already paired'.format( mac_address=accessory_mac), ) identify, identify_iid = find_characteristic_by_uuid( device, ServicesTypes.ACCESSORY_INFORMATION_SERVICE, CharacteristicsTypes.IDENTIFY, ) if not identify: raise AccessoryNotFoundError( 'Device with address {mac_address} exists but did not find IDENTIFY characteristic'.format( mac_address=accessory_mac) ) value = TLV.encode_list([ (1, b'\x01') ]) body = len(value).to_bytes(length=2, byteorder='little') + value tid = random.randrange(0, 255) request = bytearray([0x00, HapBleOpCodes.CHAR_WRITE, tid]) request.extend(identify_iid.to_bytes(length=2, byteorder='little')) request.extend(body) identify.write_value(request) response = bytearray(identify.read_value()) if not response or not response[2] == 0x00: raise UnknownError('Unpaired identify failed') return True
def start_pairing_ble(self, alias, accessory_mac, adapter='hci0'): """ This starts a pairing attempt with the Bluetooth LE accessory identified by its mac address. It returns a callable (finish_pairing) which you must call with the pairing pin. Accessories can be found via the discover method. The mac field is the accessory's mac for the second parameter. The required pin is either printed on the accessory or displayed. Must be a string of the form 'XXX-YY-ZZZ'. Important: no automatic saving of the pairing data is performed. If you don't do this, the information is lost and you have to reset the accessory! :param alias: the alias for the accessory in the controllers data :param accessory_mac: the accessory's mac address :param adapter: the bluetooth adapter to be used (defaults to hci0) # TODO add raised exceptions """ if not BLE_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('BLE') if alias in self.pairings: raise AlreadyPairedError( 'Alias "{a}" is already paired.'.format(a=alias)) from .ble_impl.device import DeviceManager manager = DeviceManager(adapter) device = manager.make_device(mac_address=accessory_mac) logging.debug('connecting to device') device.connect() logging.debug('connected to device') pair_setup_char, pair_setup_char_id = find_characteristic_by_uuid( device, ServicesTypes.PAIRING_SERVICE, CharacteristicsTypes.PAIR_SETUP) logging.debug('setup char: %s %s', pair_setup_char, pair_setup_char.service.device) write_fun = create_ble_pair_setup_write(pair_setup_char, pair_setup_char_id) salt, pub_key = perform_pair_setup_part1(write_fun) def finish_pairing(pin): Controller.check_pin_format(pin) pairing = perform_pair_setup_part2(pin, str(uuid.uuid4()), write_fun, salt, pub_key) pairing['AccessoryMAC'] = accessory_mac pairing['Connection'] = 'BLE' self.pairings[alias] = BlePairing(pairing, adapter) return finish_pairing
def perform_pairing(self, alias, accessory_id, pin): """ This performs a pairing attempt with the accessory identified by its id. Accessories can be found via the discover method. The id field is the accessory's for the second parameter. The required pin is either printed on the accessory or displayed. Must be a string of the form 'XXX-YY-ZZZ'. Important: no automatic saving of the pairing data is performed. If you don't do this, the information is lost and you have to reset the accessory! :param alias: the alias for the accessory in the controllers data :param accessory_id: the accessory's id :param pin: the accessory's pin :raises AccessoryNotFoundError: if no accessory with the given id can be found :raises AlreadyPairedError: if the alias was already used :raises UnavailableError: if the device is already paired :raises MaxTriesError: if the device received more than 100 unsuccessful attempts :raises BusyError: if a parallel pairing is ongoing :raises AuthenticationError: if the verification of the device's SRP proof fails :raises MaxPeersError: if the device cannot accept an additional pairing :raises UnavailableError: on wrong pin """ if alias in self.pairings: raise AlreadyPairedError( 'Alias "{a}" is already paired.'.format(a=alias)) connection_data = find_device_ip_and_port(accessory_id) if connection_data is None: raise AccessoryNotFoundError( 'Cannot find accessory with id "{i}".'.format(i=accessory_id)) conn = HomeKitHTTPConnection(connection_data['ip'], port=connection_data['port']) try: pairing = perform_pair_setup(conn, pin, str(uuid.uuid4())) finally: conn.close() pairing['AccessoryIP'] = connection_data['ip'] pairing['AccessoryPort'] = connection_data['port'] self.pairings[alias] = Pairing(pairing)
try: pairings = controller.get_pairings() if args.alias in pairings: pairing_data = pairings[args.alias]._get_pairing_data() additional_controller_pairing_identifier = pairing_data[ 'iOSPairingId'] ios_device_ltpk = pairing_data['iOSDeviceLTPK'] text = 'Alias "{a}" is already in state add additional pairing.\n'\ 'Please add this to homekit.add_additional_pairing:\n'\ ' -i {id} -k {pk}'\ .format(a=args.alias, id=additional_controller_pairing_identifier, pk=ios_device_ltpk ) raise AlreadyPairedError(text) additional_controller_pairing_identifier = str(uuid.uuid4()) ios_device_ltsk, ios_device_ltpk = ed25519.create_keypair() public_key = hexlify(ios_device_ltpk.to_bytes()).decode() text = 'Please add this to homekit.add_additional_pairing:\n' \ ' -i {id} -k {pk}' \ .format(a=args.alias, id=additional_controller_pairing_identifier, pk=public_key ) print(text) a = {