-
Notifications
You must be signed in to change notification settings - Fork 41
HomeKit Controller API
This page will describe the stable API for acting as a Homekit controller with this python module.
The HomeKit Controller API will cover the following use cases:
- discover HomeKit enabled devices (see homekit.Controller.discover)
- identify unpaired and paired HomeKit devices (see homekit.Controller.identify and homekit.Pairing.identify)
- pair HomeKit devices (see homekit.Controller.perform_pairing)
- read out the HomeKit accessories of a device (see homekit.Pairing.list_accessories_and_characteristics)
- read out one or more characteristics of an accessory (see homekit.Pairing.get_characteristics)
- write back one or more characteristics of an accessory (see homekit.Pairing.put_characteristics)
- register for events send back by the accessory to the controller (see homekit.Pairing.get_events)
- remove the pairing with a device (see homekit.Controller.remove_pairing)
Important Only IP Accessories are currently supported. No Bluetooth LE yet.
The use cases are split into two main classes. These are shown in the class diagram below:
To make handling of paired accessories easier, pairings will have an alias as a shortcut in the controller objects.
This class replaces the iOS Device acting as Controller in typical scenarios.
This method uses Bonjour / Zeroconf technology to gather HomeKit Accessories from within the same IP network.
-
max_seconds
(default: 10): how many seconds is the inquiry running. After that time the result is prepared and returned.
The method returns a list of the discovered devices. The devices are represented by dicts that contain the following keys:
-
name
: the Bonjour name of the HomeKit accessory (i.e. Testsensor1._hap._tcp.local.) -
address
: the IP address of the accessory -
port
: the used IP port -
c#
: the configuration number -
ff
/flags
: the numerical and human readable version of the feature flags (supports pairing or not, see table 5-8 page 69 or homekit.model.FeatureFlags) -
id
: the accessory's pairing id -
md
: the model name of the accessory -
pv
: the protocol version -
s#
: the current state number -
sf
: the status flag (see table 5-9 page 70) -
ci
/category
: the category identifier in numerical and human readable form. For more information see table 12-3 page 254 or homekit.model.Categories
This method is used to trigger the identification of an unpaired HomeKit Accessory. It is depending on the device's implementation on how it reacts to this call. A light bulb may be flashing in a specific pattern or a switch plug might turn its status LED on and off.
-
accessory_id
: the HomeKit accessory's pairing id (fieldid
of the results ofdiscover
).
This method does not return anything but raises a AccessoryNotFoundError
if no device with the accessory_id
could be looked up via Bonjour.
This method loads previously stored data about the pairings known to this controller from a file. Should be performed at the beginning of a program that uses the controller.
-
filename
: The file that should be loaded.
This method does not return anything but raises ConfigLoadingError
on any error while loading the data.
This method saves data about the pairings known to this controller to a file. Should be performed after all methods that change pairing information (perform_pairing
and remove_pairing
).
-
filename
: The file that should be saved to.
This method does not return anything but raises ConfigSavingError
on any error while loading the data.
This method offers access to all known pairing of the controller.
No parameter is required.
This method returns a dict of aliases to pairing objects. The aliases are defined while performing a pairing operation.
This method performs a pairing operation with a previously unpaired device. After a successful pairing the method
save_data
should be called to persist the pairing data.
-
alias
: The alias under which the device will be known to the controller, e.g.KitchenLight
. -
accessory_id
: The HomeKit accessory's pairing id (fieldid
of the results ofdiscover
). -
pin
: The HomeKit accessory's pairing PIN as printed on the device.
No return value but there are some exceptions to be raised on errors :
-
AccessoryNotFoundError
: no device with theaccessory_id
could be found -
AlreadyPairedError
: the alias is already used for a pairing -
UnavailableError
: the device is already paired to some other controller. Unpair it first. -
MaxTriesError
: if the device received more than 100 unsuccessful pairing attempts -
BusyError
: if a parallel pairing is ongoing -
AuthenticationError
: if the verification of the device's SRP proof fails, e.g. on a wrong pin -
MaxPeersError
: if the device cannot accept an additional pairing -
IllegalDataError
: if the verification of the accessory's data fails
Removes a pairing between controller and accessory. Pairing data is cleared on both ends so the accessory can be paired by controller's again.
-
alias
: The alias under which the device will be known to the controller, e.g.KitchenLight
.
No return value but there are some exceptions to be raised on errors :
-
AuthenticationError
: no device with theaccessory_id
could be found -
AccessoryNotFoundError
: if the device can not be found via zeroconf e.g. if it is out of reach or turned off -
UnknownError
: on unknown errors
This class represents the pairing between the controller and a HomeKit accessory.
Retrieves the accessories and characteristics behind a pairing.
No parameters are required.
If the call is successful, it returns data as specified in chapter 5.6 page 71 ff in the specification. In addtion it may raise the following error:
-
AccessoryNotFoundError
: if the device can not be found via zeroconf e.g. if it is out of reach or turned off
The resulting data is structured like this:
[
{
'aid': 1,
'services': [
{
'iid': 2,
'type': '0000003E-0000-1000-8000-0026BB765291',
'characteristics': [
{
'iid': 3,
'perms': ['pw'],
'format': 'bool',
'description': 'Identify',
'type': '00000014-0000-1000-8000-0026BB765291'
}, ...
]
}, ...
]
}
]
Retrieves the current value of a list of characteristics and optional information if requested.
-
characteristics
: a list of 2 tupels of accessory id and instance id(aid, iid)
-
include_meta
: set toTrue
if the result should contain meta information about the characteristics. This includes details about the format, unit and limits of the characteristics. (optional, defaultFalse
) -
include_perms
: set toTrue
if the result should contain information about permissions for the characteristics. (optional, defaultFalse
) -
include_type
: set toTrue
if the result should contain information about the type for the characteristics. The type result refers to the UUID defined in the specification in chapter 8. The rules for abbreviations are defined in chapter 5.6.1 page 72. (optional, defaultFalse
) -
include_events
: set toTrue
if the result should contain information if events are subscribed for by the caller. (optional, defaultFalse
)
The result is a dict with tupels (aid, iid)
as keys and dicts as values. The dict contains either information why the call failed (status
and description
) for the particular tupel of (aid, iid)
, or it contains the requested information.
As example, this is the result for a request for 3 characteristics (1.50
, 1.2
and 1.3
) including meta data, permissions and type information:
{
(1, 50): {
'status': -70409,
'description': 'Resource does not exist.'
},
(1, 2): {
'type': '23',
'format': 'string',
'maxLen': 64,
'perms': ['pr'],
'value': 'Name Characteristic'
},
(1, 3): {
'type': '20',
'format': 'string',
'maxLen': 64,
'perms': ['pr'],
'value': 'Manufacturer Characteristic'
}
}
This function is used to register to events sent from an accessory. Om each event, the given call back function is called.
-
characteristics
: a list of 2-tupels of accessory id (aid) and instance id (iid) -
callback_fun
: the callback function -
max_events
: the maximum number of events to wait for.-1
means unlimitted (optional, default-1
). -
max_seconds
: the maximum number of seconds to wait for. This is no absolute hard limit, it may take a little longer, so don't use for timing critical stuff!-1
means unlimitted (optional, default-1
).
As main result, the callback function is called with a list of 3-tupels each time at least one event occurs. Thevalues in the 3-tupels are:
- accessory id (
aid
) - instance id (
iid
) - the value of the characteristic
If there is a problem while subscribing to the events, a dict is returned. This dict contains tupels (aid, iid)
as key for all input tupels for which no events can be subscribed. The mapped values are dicts with keys:
-
status
: the numerical value as defined in table 5-12 on page 80 in the specification -
description
: a textual representation of the status (the description column of table 5-12 on page 80)
For more information see homekit.protocol.statuscodes.HapStatusCodes
.
This method returns all pairings of a HomeKit accessory. This always includes the local controller and can only be done by an admin controller.
No paramters are required.
One dict per pairing is returned in a list. These dicts contain keys for:
- pairingId: the pairing id of the controller
- publicKey: the ED25519 long-term public key of the controller
- permissions: bit value for the permissions
- controllerType: either admin or regular
In addtion it may raise the following errors:
-
UnknownError
: if the function receives unexpected data -
UnpairedError
: if the polled accessory is not paired
This function is used to change on or more characteristics to a new value.
-
characteristics
: a list of characteristics (identified by accessory idaid
and characteristic idiid
) and the target value to set. E.g.[(1,2,True), (1,3,0.2)]
-
do_conversion
: set toTrue
if a conversion into the format of the characteristic should be performed. (optional, defaultFalse
)
Returns a dict with the same number of keys as the first parameter. For each key (aid, iid)
it contains a dict with two keys:
-
status
: the numerical value as defined in table 5-12 on page 80 in the specification -
description
: a textual representation of the status (the description column of table 5-12 on page 80)
For more information see homekit.protocol.statuscodes.HapStatusCodes
.
In addtion, this function may raise a FormatError
if the parameter do_conversion
is set to True
and the conversion into the characteristic's format cannot be performed, e.g. when boolean value is given but a numerical format is required).
This triggers the identify function for a paired accessory.
No paramters are required.
Returns True
if the identify call could be executed successfully, False
otherwise.