Example #1
0
    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
Example #2
0
    def test_pair_setup_doesnt_add_extra_headers(self):
        """
        The tado internet bridge will fail if a pairing request has
        extraneous headers like `Accept-Encoding`.

        https://github.com/home-assistant/home-assistant/issues/16971
        https://github.com/jlusiardi/homekit_python/pull/130
        """
        connection = HomeKitHTTPConnection('localhost')
        write_fun = create_ip_pair_setup_write(connection)

        with mock.patch.object(connection, 'response_class') as resp:
            resp.return_value.read.return_value = b'\x00\x01\x01\x06\x01\x01'
            with mock.patch.object(connection, 'send') as send:
                write_fun([], [])
                assert b'accept-encoding' not in send.call_args[0][0].lower()