pyHomeKit’s documentation¶
PyHomeKit - HomeKit for Python¶
Author: | Henri Dwyer |
---|

PyHomeKit is a set of python libraries that let you control HomeKit compatible accessories, both BLE and HTTP.
Important
PyHomeKit is currently in pre-alpha. Many features are not yet implemented or broken.
Getting Started¶
Usage¶
Connect to a HAP characteristics and view its signature:
import pyhomekit
device_mac = "aa:aa:aa:aa:aa"
characteristic_uuid = "00000000-0000-0000-0000-000000000000"
accessory = pyhomekit.ble.HapAccessory(device_mac, 'random')
accessory.connect()
hap_characteristic = pyhomekit.ble.HapCharacteristic(accessory=accessory, uuid=characteristic_uuid)
print(hap_characteristic.signature)
View the debug logs in stdout:
import logging
logging.basicConfig()
logging.getLogger('pyhomekit').setLevel(logging.DEBUG)
Installation¶
pyHomeKit
is on Pypi, so you can pip
install it:
pip install pyhomekit
If you want to install from source, clone the repository:
git clone git://github.com/henridwyer/pyhomekit.git
cd pyhomekit
pip install -r requirements.txt
pip install -e .
Then you can build the documentation:
make doc
And run the tests:
make tests
Links¶
For more information about HomeKit, see the Apple Developper HomeKit page.
Bluetooth Low Energy device compatibility is provided by bluepy.
Encryption is provided by the libsodium library.
pyhomekit¶
pyhomekit package¶
Module contents¶
pyhomekit.ble module¶
Contains all of the HAP-BLE classes.
-
class
pyhomekit.ble.
HapAccessory
(address: str, address_type: str = 'static') → None[source]¶ Bases:
object
HAP Accesory.
Parameters: - address – MAC address of the accessory
- address_type – Type of the address: static or random
-
charateristic
(uuid: str) → sphinx.ext.autodoc.Characteristic[source]¶ Return the GATT characteristic for the given UUID.
-
class
pyhomekit.ble.
HapAccessoryLock
(address: str, address_type: str = 'static') → None[source]¶ Bases:
pyhomekit.ble.HapAccessory
-
class
pyhomekit.ble.
HapBlePdu
(header: pyhomekit.ble.HapBlePduHeader, TLVs: typing.Sequence[typing.Tuple[int, bytes]]) → None[source]¶ Bases:
object
HAP BLE PDU
-
fragmented
¶
-
max_len
= 512¶
-
raw_data
¶
-
-
class
pyhomekit.ble.
HapBlePduHeader
(response: bool, continuation: bool) → None[source]¶ Bases:
object
Interface for HAP-BLE Headers.
This class is not meant to be instantiated. Use the children HapBlePduRequestHeader and HapBlePduResponseHeader.
Parameters: - continuation – indicates the fragmentation status of the HAP-BLE PDU. False indicates a first fragment or no fragmentation.
- response – indicates whether the PDU is a response (versus a request)
-
control_field
¶ Get Control Field as int.
-
control_field_bits
¶ Get Control Field as string of bits.
-
data
¶
-
class
pyhomekit.ble.
HapBlePduRequestHeader
(cid_sid: bytes, op_code: int, response: bool = False, continuation: bool = False, transaction_id: int = None) → None[source]¶ Bases:
pyhomekit.ble.HapBlePduHeader
HAP-BLE PDU Request Header.
Parameters: - continuation – indicates the fragmentation status of the HAP-BLE PDU. False indicates a first fragment or no fragmentation.
- response – indicates whether the PDU is a response (versus a request)
- transation_id – Transaction Identifier
- op_code – HAP Opcode field, which indicates the opcode for the HAP Request PDU.
- cid_sid – Characteristic / Service Instance Identifier is the instance id of the characteristic / service for a particular request.
-
data
¶ Byte representation of the PDU Header.
Depends on whether it is a continuation header or not.
-
transaction_id
¶ Get the transaction identifier, or generate a new one if none exists.
The transation ID is an 8 bit number identifying the transaction number of this PDU. The TID is randomly generated by the originator of the request and is used to match a request/response pair.
-
class
pyhomekit.ble.
HapBlePduResponseHeader
(status_code: int, transaction_id: int, continuation: bool = False, response: bool = True) → None[source]¶ Bases:
pyhomekit.ble.HapBlePduHeader
HAP-BLE PDU Response Header.
Parameters: - continuation – indicates the fragmentation status of the HAP-BLE PDU. False indicates a first fragment or no fragmentation.
- response – indicates whether the PDU is a response (versus a request)
- transaction_id – Transaction Identifier
- status_code – HAP Status code for the request.
-
data
¶ Byte representation of the PDU Header.
-
class
pyhomekit.ble.
HapCharacteristic
(accessory: pyhomekit.ble.HapAccessory, uuid: str, retry: bool = False, retry_max_attempts: int = 1, retry_wait_time: int = 2) → None[source]¶ Bases:
object
Represents data or an associated behavior of a service.
The characteristic is defined by a universally unique type, and has additional properties that determine how the value of the characteristic can be accessed.
Parameters: - accessory – The accessory this characteristic belongs to.
- uuid – The UUID of the underlying GATT characteristic
- retry – Attempt to reconnect when error.
- retry_max_attempts – How many times to attempt reconnection.
- retry_wait_time – How long to wait in s between reconnection attempts.
-
cid
¶ Get the Characteristic ID, reading it from the device if required.
-
read
(request_header: pyhomekit.ble.HapBlePduRequestHeader) → typing.Dict[str, typing.Any][source]¶ Perform a HAP Characteristic read.
Fragmented read if required.
-
signature
¶ Returns the signature, and adds the attributes.
-
pyhomekit.ble.
fragment_tlvs
(header: pyhomekit.ble.HapBlePduRequestHeader, TLVs: typing.List[typing.Tuple[int, bytes]]) → typing.Iterator[bytes][source]¶ Returns the fragmented TLVs to write.
pyhomekit.constants module¶
HAP Constants
-
class
pyhomekit.constants.
HapBleOpCodes
[source]¶ Bases:
object
HAP Opcode Descriptions.
-
Characteristic_Execute_Write
= 5¶
-
Characteristic_Read
= 3¶
-
Characteristic_Signature_Read
= 1¶
-
Characteristic_Timed_Write
= 4¶
-
Characteristic_Write
= 2¶
-
Service_Signature_Read
= 6¶
-
-
class
pyhomekit.constants.
HapBleStatusCodes
[source]¶ Bases:
object
HAP Status code definitions and descriptions.
-
Insufficient_Authentication
= 5¶
-
Insufficient_Authorization
= 3¶
-
Invalid_Instance_ID
= 4¶
-
Invalid_Request
= 6¶
-
Max_Procedures
= 2¶
-
Success
= 0¶
-
Unsupported_PDU
= 1¶
-
-
class
pyhomekit.constants.
HapParamTypes
[source]¶ Bases:
object
-
Additional_Authorization_Data
= 2¶
-
Characteristic_Instance_ID
= 5¶
-
Characteristic_Type
= 4¶
-
GATT_Presentation_Format_Descriptor
= 12¶
-
GATT_User_Description_Descriptor
= 11¶
-
GATT_Valid_Range
= 13¶
-
HAP_Characteristic_Properties_Descriptor
= 10¶
-
HAP_Linked_Services
= 16¶
-
HAP_Service_Properties
= 15¶
-
HAP_Step_Value_Descriptor
= 14¶
-
HAP_Valid_Values_Descriptor
= 17¶
-
HAP_Valid_Values_Range_Descriptor
= 18¶
-
Origin_local_vs_remote
= 3¶
-
Return_Response
= 9¶
-
Service_Instance_ID
= 7¶
-
Service_Type
= 6¶
-
TTL
= 8¶
-
Value
= 1¶
-
-
class
pyhomekit.constants.
PairingKTLVErrorCodes
[source]¶ Bases:
object
-
kTLVError_Authenticatio
= 2¶
-
kTLVError_Backof
= 3¶
-
kTLVError_Bus
= 7¶
-
kTLVError_MaxPeer
= 4¶
-
kTLVError_MaxTrie
= 5¶
-
kTLVError_Unknow
= 1¶
-
-
class
pyhomekit.constants.
PairingKTLVMethodValues
[source]¶ Bases:
object
Pairing service kTLV method values
-
Add_Pairing
= 3¶
-
List_Pairings
= 5¶
-
Pair_Setup
= 1¶
-
Pair_Verify
= 2¶
-
Remove_Pairing
= 4¶
-
Reserved
= 0¶
-
-
class
pyhomekit.constants.
PairingKTlvValues
[source]¶ Bases:
object
Pairng service TLV Values.
-
kTLVType_Certificate
= 9¶
-
kTLVType_EncryptedData
= 5¶
-
kTLVType_Error
= 7¶
-
kTLVType_FragmentData
= 12¶
-
kTLVType_FragmentLast
= 13¶
-
kTLVType_Identifier
= 1¶
-
kTLVType_Method
= 0¶
-
kTLVType_Permissions
= 11¶
-
kTLVType_Proof
= 4¶
-
kTLVType_PublicKey
= 3¶
-
kTLVType_RetryDelay
= 8¶
-
kTLVType_Salt
= 2¶
-
kTLVType_Separator
= 255¶
-
kTLVType_Signature
= 10¶
-
kTLVType_State
= 6¶
-
-
pyhomekit.constants.
parse_format
(b: bytes) → typing.Tuple[int, int][source]¶ Parse the bluetooth characteristic presentation format to format and unit code
-
pyhomekit.constants.
to_int32
(b: bytes) → int[source]¶ Convert to bytes to 32 bit signed int (little endian).
-
pyhomekit.constants.
to_uint16
(b: bytes) → int[source]¶ Convert to bytes to 16 bit unsigned short (little endian).
-
pyhomekit.constants.
to_uint32
(b: bytes) → int[source]¶ Convert to bytes to 32 bit unsigned int (little endian).
-
pyhomekit.constants.
to_uint64
(b: bytes) → int[source]¶ Convert to bytes to 64 bit unsigned int (little endian).
pyhomekit.utils module¶
Utility functions for BLE
-
exception
pyhomekit.utils.
HapBleError
(status_code: int = None, name: str = None, message: str = None, *args: str) → None[source]¶ Bases:
Exception
HAP Error.